JS::CompileFunction

This article covers features introduced in SpiderMonkey 17

Create a JavaScript function from a text string.

Syntax

bool
JS::CompileFunction(JSContext *cx, JS::AutoObjectVector &scopeChain,
                    const JS::ReadOnlyCompileOptions &options,
                    const char *name, unsigned nargs, const char *const *argnames,
                    const char16_t *chars, size_t length, JS::MutableHandleFunction fun);

bool
JS::CompileFunction(JSContext *cx, JS::AutoObjectVector &scopeChain,
                    const JS::ReadOnlyCompileOptions &options,
                    const char *name, unsigned nargs, const char *const *argnames,
                    JS::SourceBufferHolder &srcBuf, JS::MutableHandleFunction fun);

bool
JS::CompileFunction(JSContext *cx, JS::AutoObjectVector &scopeChain,
                    const JS::ReadOnlyCompileOptions &options,
                    const char *name, unsigned nargs, const char *const *argnames,
                    const char *bytes, size_t length, JS::MutableHandleFunction fun);
Name Type Description
cx JSContext * The context in which to compile the function. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
scopeChain JS::AutoObjectVector & The scope in which to compile the function.
options JS::ReadOnlyCompileOptions & Compile options.
name const char * Name to assign the newly compiled function.
nargs unsigned Number of arguments to pass to the function.
argnames const char *const * Names to assign to the arguments passed to the function.
chars const char16_t * String containing the body of the function to compile.
bytes const char * String containing the body of the function to compile.
length size_t The length, in characters, of body.
srcBuf JS::SourceBufferHolder & Source buffer containing the script function compile.
fun JS::MutableHandleFunction Out parameter. On success, fun receives the result function.

Description

JS::CompileFunction compiles a function from a text string chars, bytes, or srcBuf.

name is the name to assign to the newly created function. nargs is the number of arguments the function takes, and argnames is a pointer to the first element of an array of names to assign each argument. The number of argument names should match the number of arguments specified in nargs.

chars, bytes, or srcBuf is a string containing the source code of the function. length is the length of the source code in characters.

On success, JS::CompileFunction stores an function object representing the newly compiled function into fun and and returns true. Otherwise, it returns false and the value left in fun is undefined.

See Also