Join MDN and developers like you at Mozilla's View Source conference, 12-14 September in Berlin, Germany. Learn more at

JS CompileFunctionForPrincipals

Create a security-enabled JS function from a text string.


JSFunction * JS_CompileFunctionForPrincipals(JSContext *cx,
    JSObject *obj, JSPrincipals *principals, const char *name,
    uintN nargs, const char **argnames, const char *body,
    size_t length, const char *filename, uintN lineno);

JSFunction * JS_CompileUCFunctionForPrincipals(JSContext *cx,
    JSObject *obj, JSPrincipals *principals, const char *name,
    uintN nargs, const char **argnames, const jschar *body,
    size_t length, const char *filename, uintN lineno);
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.
obj JSObject * Object with which the function is to be associated.
principals JSPrincipals * Pointer to the structure holding the security information for this function.
name const char * Name to assign the newly compiled function.
nargs uintN Number of arguments to pass to the function.
argnames const char ** Names to assign to the arguments passed to the function.
body const char * or const jschar * String containing the source code of the function to compile.
length size_t The length, in characters, of body.
filename const char * Name of file or URL containing the function. Used to report filename or URL in error messages.
lineno uintN Line number. Used to report the offending line in the file or URL if an error occurs.


JS_CompileFunctionForPrincipals compiles a security-enabled function from a text string, bytes, and associates it with a JS object, obj. JS_CompileUCFunctionForPrincipals is the Unicode version of the function.

principals is a pointer to the JSPrincipals structure that contains the security information to associate with this function.

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.

body is the string containing the source code of the function. length is the length of the string in characters.

filename is the name of the file (or URL) containing the function. This information is passed in messages if an error occurs during compilation. Similarly, lineno is used to report the line number of the function or file where an error occurred during compilation.

On success, JS_CompileFunctionForPrincipals and JS_CompileUCFunctionForPrincipals return a pointer to the newly compiled function. On error or exception, they return NULL.

See Also

MXR ID Search for JS_CompileFunctionForPrincipals
MXR ID Search for JS_CompileUCFunctionForPrincipals

JSFUN_BOUND_METHOD, JSFUN_GLOBAL_PARENT, JS_CallFunction, JS_CallFunctionName, JS_CallFunctionValue, JS_CompileFunction, JS_DecompileFunction, JS_DecompileFunctionBody, JS_DefineFunction, JS_DefineFunctions, JS_GetFunctionObject, JS_NewFunction, JS_ValueToFunction

Document Tags and Contributors

 Last updated by: Jorend,