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_THREADSAFEbuild, the caller must be in a request on thisJSContext. | 
| 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 
 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: 
 | 
| scopeChain | JS::AutoObjectVector & | An explicit scope chain. scopeChainmust 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. Objects in the vector should be ordered from inner to the outer scope. | 
| script | JS::HandleScript | The compiled script to execute. | 
| rval | JS::MutableHandleValue | Out parameter. On success, rvalreceives 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
- MXR ID Search for JS_ExecuteScript
- MXR ID Search for JS::CloneAndExecuteScript
- JS::Compile
- JS::Evaluate
- bug 993438
- bug 1145294 -- removed objfromJS::CloneAndExecuteScript.
- bug 1097987 -- removed objfromJS_ExecuteScript