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_THREADSAFEbuild, the caller must be in a request on thisJSContext. | 
| obj | JS::HandleObject | The global object, or NULL. If theJSOPTION_COMPILE_N_GOoption is set incx, the resulting script must be executed in this scope, and at most once. Obsolete since JSAPI 39 | 
| asciiorchars | const char *orconst 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, scriptreceives 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
- The JSAPI User Guide contains example code using compiled scripts.
- MXR ID Search for JS_CompileScript
- MXR ID Search for JS_CompileUCScript
- JS::Evaluate
- JS::CompileOffThread
- JS::CompileFunction
- JS_ExecuteScript
- JS_DecompileScript
- bug 1143793 -- removed objparameter