JS_ExecuteScript

  • Revision slug: SpiderMonkey/JSAPI_Reference/JS_ExecuteScript
  • Revision title: JS_ExecuteScript
  • Revision id: 77504
  • Created:
  • Creator: Jorend
  • Is current revision? No
  • Comment 14 words removed

Revision Content

Execute a compiled script.

Syntax

JSBool JS_ExecuteScript(JSContext *cx, JSObject *obj, JSScript *script,
    jsval *rval);
Name Type Description
cx JSContext * The context in which to execute the script. {{ Jsapi-requires-request() }}
obj JSObject *

The scope in which to execute the script.

In ECMAScript terms, the script is executed in a new execution context, but that context is not initialized quite as described in any of the three cases in {{ Es3_spec("10") }}. Instead:

  • The scope chain is initialized to contain obj, followed by its parent, then its parent's parent, etc. until NULL is reached.
  • Variable initialization is performed. If the JSOPTION_VAROBJFIX option is in effect (recommended), then the last object in the scope chain is used as the variable object. Otherwise obj is used.
  • The this value is obj.
script JSScript * The compiled script to execute.
rval jsval * Out parameter. On success, *rval receives the value from the last executed expression statement processed in the script.

Description

JS_ExecuteScript executes a previously-compiled script, script.

If the script executes successfully, *rval receives the value from the last executed expression statement processed in the script, and JS_ExecuteScript returns JS_TRUE. Otherwise it returns JS_FALSE, and the value left in *rval is unspecified.

{{ Jsapi_compiled_script_example_pointer() }}

To execute a script that has not been compiled, use JS_EvaluateScript instead.

{{ LXRSearch("ident", "i", "JS_ExecuteScript") }}

Revision Source

<p>Execute a compiled script.
</p>
<h2 name="Syntax">Syntax</h2>
<pre class="eval"><a href="/En/SpiderMonkey/JSAPI_Reference/JSBool" title="en/JSBool">JSBool</a> <strong>JS_ExecuteScript</strong>(<a href="/en/SpiderMonkey/JSAPI_Reference/JSRuntime" title="en/JSRuntime">JSContext</a> *cx, <a href="/en/SpiderMonkey/JSAPI_Reference/JSObject" title="en/JSObject">JSObject</a> *obj, <a href="/en/JSScript" title="en/JSScript">JSScript</a> *script,
    <a href="/En/SpiderMonkey/JSAPI_Reference/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/SpiderMonkey/JSAPI_Reference/JSRuntime" title="en/JSRuntime">JSContext</a> *</code></td> <td>The context in which to execute the script. {{ Jsapi-requires-request() }}</td> </tr> <tr> <td><code>obj</code></td> <td><code><a href="/en/SpiderMonkey/JSAPI_Reference/JSObject" title="en/JSObject">JSObject</a> *</code></td> <td> <p>The scope in which to execute the script.</p> <p>In ECMAScript terms, the script is executed in a new execution context, but that context is not initialized quite as described in any of the three cases in {{ Es3_spec("10") }}. Instead:</p> <ul> <li>The scope chain is initialized to contain <code>obj</code>, followed by its <a href="/en/SpiderMonkey/JSAPI_Reference/JS_GetParent" title="en/JS_GetParent">parent</a>, then its parent's parent, etc. until <code>NULL</code> is reached.</li> <li>Variable initialization is performed. If the <code><a href="/en/SpiderMonkey/JSAPI_Reference/JS_SetOptions" title="en/JS_SetOptions">JSOPTION_VAROBJFIX</a></code> option is in effect (recommended), then the last object in the scope chain is used as the variable object. Otherwise <code>obj</code> is used.</li> <li>The <code>this</code> value is <code>obj</code>.</li> </ul> </td> </tr> <tr> <td><code>script</code></td> <td><code><a href="/en/JSScript" title="en/JSScript">JSScript</a> *</code></td> <td>The compiled script to execute.</td> </tr> <tr> <td><code>rval</code></td> <td><code><a href="/En/SpiderMonkey/JSAPI_Reference/Jsval" title="en/jsval">jsval</a> *</code></td> <td>Out parameter. On success, <code>*rval</code> receives the value from the last executed expression statement processed in the script.</td> </tr> </tbody>
</table><h2 name="Description"> Description </h2>
<p><strong><code>JS_ExecuteScript</code></strong> executes a previously-compiled script, <code>script</code>.
</p><p>If the script executes successfully, <code>*rval</code> receives the value from the last executed expression statement processed in the script, and <code>JS_ExecuteScript</code> returns <code>JS_TRUE</code>. Otherwise it returns <code>JS_FALSE</code>, and the value left in <code>*rval</code> is unspecified.
</p><p>{{ Jsapi_compiled_script_example_pointer() }}
</p><p>To execute a script that has not been compiled, use <code><a href="/en/SpiderMonkey/JSAPI_Reference/JS_EvaluateScript" title="en/JS_EvaluateScript">JS_EvaluateScript</a></code> instead.
</p><p>{{ LXRSearch("ident", "i", "JS_ExecuteScript") }}
</p>
Revert to this revision