JS_EvaluateScript

  • Revision slug: SpiderMonkey/JSAPI_Reference/JS_EvaluateScript
  • Revision title: JS_EvaluateScript
  • Revision id: 93895
  • Created:
  • Creator: Jorend
  • Is current revision? No
  • Comment no wording changes; page display name changed to 'JS_EvaluateScript'

Revision Content

{{ Jsapi_ref_header("JS_EvaluateScript") }}

Compile and execute a script.

Syntax

JSBool JS_EvaluateScript(JSContext *cx, JSObject *obj,
    const char *src, uintN length, const char *filename,
    uintN lineno, jsval *rval);

JSBool JS_EvaluateUCScript(JSContext *cx, JSObject *obj,
    const jschar *src, uintN length, const char *filename,
    uintN lineno, jsval *rval);
Name Type Description
cx JSContext * The context in which to run the script. {{ Jsapi-requires-request() }}
obj JSObject * The scope in which to execute the script. This parameter is documented in detail at JS_ExecuteScript.
src const char * or const jschar * String containing the script to compile and execute.
length uintN The length of src, in characters.
filename const char * Name of file or URL containing the script. Used to report filename or URL in error messages.
lineno uintN Line number. Used to report the offending line in the file or URL if an error occurs.
rval jsval * Out parameter. On success, *rval receives the value of the last-executed expression statement processed in the script.

Description

JS_EvaluateScript compiles and executes a script in the specified scope, obj. On successful completion, rval is a pointer to a variable that holds the value from the last executed expression statement processed in the script. JS_EvaluateUCScript is the Unicode version of the function.

src is the string containing the text of the script. length indicates the size of the text version of the script in characters.

filename is the name of the file (or URL) containing the script. This information is used in 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 a script compiles and executes successfully, *rval receives the value from the last-executed expression statement in the script, and JS_EvaluateScript or JS_EvaluateUCScript returns JS_TRUE. Otherwise it returns JS_FALSE and the value left in *rval is undefined.

See Also

{{ LXRSearch("ident", "i", "JS_EvaluateScript") }}
{{ LXRSearch("ident", "i", "JS_EvaluateUCScript") }}

JS_CompileFile, JS_CompileScript, JS_DecompileScript, JS_DestroyScript, JS_EvaluateScriptForPrincipals

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

Revision Source

<p>{{ Jsapi_ref_header("JS_EvaluateScript") }}
</p><p>Compile and execute a script.
</p>
<h2 name="Syntax"> Syntax </h2>
<pre class="eval"><a href="/en/JSBool" title="en/JSBool">JSBool</a> <strong>JS_EvaluateScript</strong>(<a href="/en/JSRuntime" title="en/JSRuntime">JSContext</a> *cx, <a href="/en/JSObject" title="en/JSObject">JSObject</a> *obj,
    const char *src, <a href="/en/jsint" title="en/jsint">uintN</a> length, const char *filename,
    <a href="/en/jsint" title="en/jsint">uintN</a> lineno, <a href="/en/jsval" title="en/jsval">jsval</a> *rval);

<a href="/en/JSBool" title="en/JSBool">JSBool</a> <strong>JS_EvaluateUCScript</strong>(<a href="/en/JSRuntime" title="en/JSRuntime">JSContext</a> *cx, <a href="/en/JSObject" title="en/JSObject">JSObject</a> *obj,
    const <a href="/en/jschar" title="en/jschar">jschar</a> *src, <a href="/en/jsint" title="en/jsint">uintN</a> length, const char *filename,
    <a href="/en/jsint" title="en/jsint">uintN</a> lineno, <a href="/en/jsval" title="en/jsval">jsval</a> *rval);
</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/JSRuntime" title="en/JSRuntime">JSContext</a> *</code></td>
<td>The context in which to run the script.
{{ Jsapi-requires-request() }}</td>
</tr>
<tr>
<td><code>obj</code></td>
<td><code><a href="/en/JSObject" title="en/JSObject">JSObject</a> *</code></td>
<td>The scope in which to execute the script.
This parameter is documented in detail at <code><a href="/en/JS_ExecuteScript" title="en/JS_ExecuteScript">JS_ExecuteScript</a></code>.</td>
</tr>
<tr>
<td><code>src</code></td>
<td><code>const char *</code> <em>or</em> <code>const <a href="/en/jschar" title="en/jschar">jschar</a> *</code></td>
<td>String containing the script to compile and execute.</td>
</tr>
<tr>
<td><code>length</code></td>
<td><code><a href="/en/jsint" title="en/jsint">uintN</a></code></td>
<td>The length of <code>src</code>, in characters.</td>
</tr>
<tr>
<td><code>filename</code></td>
<td><code>const char *</code></td>
<td>Name of file or URL containing the script. Used to report filename or URL in error messages.</td>
</tr>
<tr>
<td><code>lineno</code></td>
<td><code><a href="/en/jsint" title="en/jsint">uintN</a></code></td>
<td>Line number. Used to report the offending line in the file or URL if an error occurs.</td>
</tr>
<tr>
<td><code>rval</code></td>
<td><code><a href="/en/jsval" title="en/jsval">jsval</a> *</code></td>
<td>Out parameter. On success, <code>*rval</code> receives the value of the last-executed expression statement processed in the script.</td>
</tr>
</tbody></table>
<h2 name="Description"> Description </h2>
<p><code>JS_EvaluateScript</code> compiles and executes a script in the specified scope, <code>obj</code>. On successful completion, <code>rval</code> is a pointer to a variable that holds the value from the last executed expression statement processed in the script. <code>JS_EvaluateUCScript</code> is the Unicode version of the function.
</p><p><code>src</code> is the string containing the text of the script. <code>length</code> indicates the size of the text version of the script in characters.
</p><p><code>filename</code> is the name of the file (or URL) containing the script. This information is used in 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.
</p><p>If a script compiles and executes successfully, <code>*rval</code> receives the value from the last-executed expression statement in the script, and <code>JS_EvaluateScript</code> or <code>JS_EvaluateUCScript</code> returns <code>JS_TRUE</code>. Otherwise it returns <code>JS_FALSE</code> and the value left in <code>*rval</code> is undefined.
</p>
<h2 name="See_Also"> See Also </h2>
<p>{{ LXRSearch("ident", "i", "JS_EvaluateScript") }}<br>
{{ LXRSearch("ident", "i", "JS_EvaluateUCScript") }}
</p><p><a href="/en/JS_CompileFile" title="en/JS_CompileFile">JS_CompileFile</a>,
<a href="/en/JS_CompileScript" title="en/JS_CompileScript">JS_CompileScript</a>,
<a href="/en/JS_DecompileScript" title="en/JS_DecompileScript">JS_DecompileScript</a>,
<a href="/en/JS_DestroyScript" title="en/JS_DestroyScript">JS_DestroyScript</a>,
<a href="/en/JS_EvaluateScriptForPrincipals" title="en/JS_EvaluateScriptForPrincipals">JS_EvaluateScriptForPrincipals</a>
</p>

{{ languages( { "ja": "ja/JS_EvaluateScript" } ) }}
Revert to this revision