JS_CompileScript

  • Revision slug: SpiderMonkey/JSAPI_Reference/JS_CompileScript
  • Revision title: JS_CompileScript
  • Revision id: 77475
  • Created:
  • Creator: Jorend
  • Is current revision? No
  • Comment add script lifetime stuff

Revision Content

{{ Jsapi_ref_header("JS_CompileScript") }}

Compiles a script for execution.

Syntax

JSScript * JS_CompileScript(JSContext *cx, JSObject *obj,
    const char *bytes, size_t length, const char *filename,
    uintN lineno);

JSScript * JS_CompileUCScript(JSContext *cx, JSObject *obj,
    const jschar *chars, size_t length, const char *filename,
    uintN lineno);
Name Type Description
cx JSContext * Pointer to a JS context from which to derive runtime information. {{ Jsapi-requires-request() }}
obj JSObject * Object with which the script is associated.
source const char * or const jschar * String containing the script to compile.
length size_t The length, in characters, of source.
filename const char * Name of file or URL containing the function. Used to report filename or URL in error messages.
lineno uintN Line number of the first line of source. Must be greater than zero. Used to report the offending line in the file or URL if an error occurs.

Description

JS_CompileScript compiles a script, source, for execution. JS_CompileUCScript is the Unicode version of the function.

The script is associated with a JS object. bytes is the string containing the text of the script. length indicates the size of the text version of the script in bytes.

filename is the name of the file (or URL) containing the script. This information is included in error messages if an error occurs during compilation. Similarly, lineno is used to report the line number of the script or file where an error occurred during compilation. If the script is not part of a larger document, lineno should be 1 (as the first line of a file is universally considered to be line 1, not line 0).

On success, JS_CompileScript and JS_CompileUCScript return a pointer to the newly compiled script. Otherwise, they report an error and return NULL.

{{ Jsapi_script_lifetime() }}

To compile a script from an external file source rather than passing the actual script as an argument, use JS_CompileFile instead of JS_CompileScript.

See Also

{{ Jsapi_compiled_script_example_pointer() }}

{{ LXRSearch("ident", "i", "JS_CompileScript") }}
{{ LXRSearch("ident", "i", "JS_CompileUCScript") }}

JS_DecompileScript, JS_EvaluateScript, JS_ExecuteScript

{{ languages( { "ja": "ja/JS_CompileScript" } ) }}

Revision Source

<p>{{ Jsapi_ref_header("JS_CompileScript") }}
</p><p>Compiles a script for execution.
</p>
<h2 name="Syntax"> Syntax </h2>
<pre class="eval"><a href="en/JSScript">JSScript</a> * <b>JS_CompileScript</b>(<a href="en/JSContext">JSContext</a> *cx, <a href="en/JSObject">JSObject</a> *obj,
    const char *bytes, size_t length, const char *filename,
    <a href="en/UintN">uintN</a> lineno);

<a href="en/JSScript">JSScript</a> * <b>JS_CompileUCScript</b>(<a href="en/JSContext">JSContext</a> *cx, <a href="en/JSObject">JSObject</a> *obj,
    const <a href="en/Jschar">jschar</a> *chars, size_t length, const char *filename,
    <a href="en/UintN">uintN</a> lineno);
</pre>
<table class="fullwidth-table">
<tbody><tr>
<th>Name</th>
<th>Type</th>
<th>Description</th>
</tr>
<tr>
<td><code>cx</code></td>
<td><code><a href="en/JSContext">JSContext</a> *</code></td>
<td>Pointer to a JS context from which to derive runtime information.
{{ Jsapi-requires-request() }}</td>
</tr>
<tr>
<td><code>obj</code></td>
<td><code><a href="en/JSObject">JSObject</a> *</code></td>
<td>Object with which the script is associated.</td>
</tr>
<tr>
<td><code>source</code></td>
<td><code>const char *</code> <i>or</i> <code>const <a href="en/Jschar">jschar</a> *</code></td>
<td>String containing the script to compile.</td>
</tr>
<tr>
<td><code>length</code></td>
<td><code>size_t</code></td>
<td>The length, in characters, of <code>source</code>.</td>
</tr>
<tr>
<td><code>filename</code></td>
<td><code>const char *</code></td>
<td>Name of file or URL containing the function. Used to report filename or URL in error messages.</td>
</tr>
<tr>
<td><code>lineno</code></td>
<td><code><a href="en/UintN">uintN</a></code></td>
<td>Line number of the first line of <code>source</code>. Must be greater than zero. Used to report the offending line in the file or URL if an error occurs.</td>
</tr>
</tbody></table>
<h2 name="Description"> Description </h2>
<p><code>JS_CompileScript</code> compiles a script, <code>source</code>, for execution. <code>JS_CompileUCScript</code> is the Unicode version of the function.
</p><p>The script is associated with a JS object. <code>bytes</code> is the string containing the text of the script. <code>length</code> indicates the size of the text version of the script in bytes.
</p><p><code>filename</code> is the name of the file (or URL) containing the script. This information is included in error messages if an error occurs during compilation. Similarly, <code>lineno</code> is used to report the line number of the script or file where an error occurred during compilation. If the script is not part of a larger document, <code>lineno</code> should be <code>1</code> (as the first line of a file is universally considered to be line <code>1</code>, not line <code>0</code>).
</p><p>On success, <code>JS_CompileScript</code> and <code>JS_CompileUCScript</code> return a pointer to the newly compiled script. Otherwise, they report an error and return <code>NULL</code>.
</p><p>{{ Jsapi_script_lifetime() }}
</p><p>To compile a script from an external file source rather than passing the actual script as an argument, use <code><a href="en/JS_CompileFile">JS_CompileFile</a></code> instead of <code>JS_CompileScript</code>.
</p>
<h2 name="See_Also"> See Also </h2>
<p>{{ Jsapi_compiled_script_example_pointer() }}
</p><p>{{ LXRSearch("ident", "i", "JS_CompileScript") }}<br>
{{ LXRSearch("ident", "i", "JS_CompileUCScript") }}
</p><p><a href="en/JS_DecompileScript">JS_DecompileScript</a>,
<a href="en/JS_EvaluateScript">JS_EvaluateScript</a>,
<a href="en/JS_ExecuteScript">JS_ExecuteScript</a>
</p>
<div class="noinclude">
</div>
{{ languages( { "ja": "ja/JS_CompileScript" } ) }}
Revert to this revision