Visit Mozilla.org

JS ExecuteScript

From MDC

Execute a compiled script.

[edit] 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.

Requires request. (In a JS_THREADSAFE build, the caller must be in a request on this JSContext.)

obj JSObject * The scope 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 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.
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.

[edit] 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.

The JSAPI User Guide contains example code using compiled scripts.

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

LXR ID Search for JS_ExecuteScript