Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

JS_ExecuteScript

Execute a compiled script.

Syntax

bool
JS_ExecuteScript(JSContext *cx, JS::HandleScript script,
                 JS::MutableHandleValue rval); // Added in SpiderMonkey 45

bool
JS_ExecuteScript(JSContext *cx, JS::HandleScript script); // Added in SpiderMonkey 45

bool
JS_ExecuteScript(JSContext *cx, JS::AutoObjectVector &scopeChain,
                 JS::HandleScript script, JS::MutableHandleValue rval); // Added in SpiderMonkey 36

bool
JS_ExecuteScript(JSContext *cx, JS::AutoObjectVector &scopeChain,
                 JS::HandleScript script); // Added in SpiderMonkey 36

bool
JS_ExecuteScript(JSContext *cx, JS::HandleObject obj, JS::HandleScript script,
                 JS::MutableHandleValue rval); // Obsolete since JSAPI 39

bool
JS_ExecuteScript(JSContext *cx, JS::HandleObject obj, JS::HandleScript script); // Obsolete since JSAPI 39

bool
JS::CloneAndExecuteScript(JSContext *cx, JS::Handle<JSScript*> script); // Added in SpiderMonkey 45

bool
JS::CloneAndExecuteScript(JSContext *cx, JS::Handle<JSObject*> obj,
                          JS::Handle<JSScript*> script); // Added in SpiderMonkey 31, obsoleted since JSAPI 39
Name Type Description
cx JSContext * The context in which to execute the script. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
obj JS::HandleObject

The scope in which to execute the script. In the simplest cases, this should just be the embedding's global object. Obsolete since JSAPI 39

obj must not be an array, an E4X XML object, a With object, or a proxy.

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 ECMA 262-3 §10.2. 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.
scopeChain JS::AutoObjectVector & An explicit scope chain. scopeChain must not include the global object on it; that's implicit. It needs to contain the other objects that should end up on the scripts's scope chain.
script JS::HandleScript The compiled script to execute.
rval JS::MutableHandleValue 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.

JS::CloneAndExecuteScript handles a cross-compartment script. If the script is cross-compartment, it is cloned into the current compartment before executing.

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

The JSAPI User Guide contains example code using compiled scripts.

To execute a script that has not been compiled, use JS::Evaluate instead.

See Also

Document Tags and Contributors

 Contributors to this page: arai, fscholz, Jorend, Dria, MMondor
 Last updated by: arai,