JS_CompileScript

Compiles a script for execution.

Syntax

// Added in SpiderMonkey 45

bool
JS_CompileScript(JSContext *cx,
                 const char *ascii, size_t length,
                 const JS::CompileOptions &options,
                 JS::MutableHandleScript script);

bool
JS_CompileUCScript(JSContext *cx,
                   const char16_t *chars, size_t length,
                   const JS::CompileOptions &options,
                   JS::MutableHandleScript script);

// Obsolete since JSAPI 39

bool
JS_CompileScript(JSContext *cx, JS::HandleObject obj,
                 const char *ascii, size_t length,
                 const JS::CompileOptions &options,
                 JS::MutableHandleScript script);

bool
JS_CompileUCScript(JSContext *cx, JS::HandleObject obj,
                   const char16_t *chars, size_t length,
                   const JS::CompileOptions &options,
                   JS::MutableHandleScript script);
Name Type Description
cx JSContext * Pointer to a JS context from which to derive runtime information. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
obj JS::HandleObject The global object, or NULL. If the JSOPTION_COMPILE_N_GO option is set in cx, the resulting script must be executed in this scope, and at most once. Obsolete since JSAPI 39
ascii or chars const char * or const char16_t * String containing the script to compile.
length size_t The length, in characters, of source.
options JS::CompileOptions & Compile options.
script JS::MutableHandleScript Out parameter. On success, script receives the result script.

Description

JS_CompileScript compiles a script, source, for execution. JS_CompileUCScript is the Unicode version of the function.

The script is associated with a JS object. ascii and chars are the string containing the text of the script. length indicates the size of the text version of the script in characters.

On success, JS_CompileScript and JS_CompileUCScript stores the newly compiled script to *script and returns true. Otherwise, they report an error, stores NULL to *script, and return false.

See Also