This article covers features introduced in SpiderMonkey 17
Compile a script for execution.
Syntax
// Added in SpiderMonkey 45
bool
JS::Compile(JSContext *cx,
            const JS::ReadOnlyCompileOptions &options,
            JS::SourceBufferHolder &srcBuf, JS::MutableHandleScript script);
bool
JS::Compile(JSContext *cx,
            const JS::ReadOnlyCompileOptions &options,
            const char *bytes, size_t length, JS::MutableHandleScript script);
bool
JS::Compile(JSContext *cx,
            const JS::ReadOnlyCompileOptions &options,
            const char16_t *chars, size_t length, JS::MutableHandleScript script);
bool
JS::Compile(JSContext *cx,
            const JS::ReadOnlyCompileOptions &options, FILE *file,
            JS::MutableHandleScript script);
bool
JS::Compile(JSContext *cx,
            const JS::ReadOnlyCompileOptions &options, const char *filename,
            JS::MutableHandleScript script);
// Obsolete since JSAPI 39
bool
JS::Compile(JSContext *cx, JS::HandleObject obj,
            const JS::ReadOnlyCompileOptions &options,
            JS::SourceBufferHolder &srcBuf, JS::MutableHandleScript script);
bool
JS::Compile(JSContext *cx, JS::HandleObject obj,
            const JS::ReadOnlyCompileOptions &options,
            const char *bytes, size_t length, JS::MutableHandleScript script);
bool
JS::Compile(JSContext *cx, JS::HandleObject obj,
            const JS::ReadOnlyCompileOptions &options,
            const char16_t *chars, size_t length, JS::MutableHandleScript script);
bool
JS::Compile(JSContext *cx, JS::HandleObject obj,
            const JS::ReadOnlyCompileOptions &options, FILE *file,
            JS::MutableHandleScript script);
bool
JS::Compile(JSContext *cx, JS::HandleObject obj,
            const JS::ReadOnlyCompileOptions &options, const char *filename,
            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. Obsolete since JSAPI 39 | 
| options | JS::ReadOnlyCompileOptions & | Compile options. | 
| srcBuf | JS::SourceBufferHolder & | Source buffer containing the script to compile. | 
| chars | const char16_t * | String containing the script to compile. | 
| bytes | const char * | String containing the script to compile. | 
| length | size_t | The length of charsorbytes, in characters. | 
| file | FILE * | File handle containing the function. | 
| filename | const char * | Name of file or URL containing the function. Used to report filename or URL in error messages. | 
| script | JS::MutableHandleScript | Out parameter. On success, scriptreceives the result script. | 
Description
JS::Compile compiles a script, srcBuf, chars, bytes, or file, for execution.
The script is associated with a JS object. srcBuf, chars, bytes is the string containing the text of the script. length indicates the size of the text version of the script in characters.
file is the filehandle containing the text of the script.
filename is the name of the file (or URL) containing the script. This information is included in error messages if an error occurs during compilation..
On success, JS::Compile stores an object representing the newly compiled script into script, and returns true. Otherwise, it returns false and the value left in script is undefined.
See Also
- The JSAPI User Guide contains example code using compiled scripts.
- MXR ID Search for JS::Compile
- JS::Evaluate
- JS::CompileOffThread
- JS::CompileFunction
- JS_DecompileScript
- bug 771705
- bug 1143793 -- removed objparameter