JS::Compile

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_THREADSAFE build, the caller must be in a request on this JSContext.
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 chars or bytes, 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, script receives 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