Visit Mozilla.org

JS NewFunction

From MDC

Create a new JS function that wraps a native C function.

[edit] Syntax

JSFunction * JS_NewFunction(JSContext *cx, JSNative call,
   uintN nargs, uintN flags, JSObject *parent,
   const char *name);
Name Type Description
cx JSContext * The context in which to create the new function.

Requires request. (In a JS_THREADSAFE build, the caller must be in a request on this JSContext.)

call JSNative Native C/C++ implementation of this function.
nargs uintN Number of arguments that are passed to the underlying C function.
flags uintN Function attributes.
parent JSObject * Pointer to the parent object for this function.
name const char * Name to assign to the new function. If you do not assign a name to the function, it is assigned the name "anonymous".

[edit] Description

JS_NewFunction creates a new JS function based on the parameters you pass. call is a native C function call that this function wraps. If you are not wrapping a native function, use JS_DefineFunction instead. nargs is the number of arguments passed to the underlying C function. JS uses this information to allocate space for each argument.

flags lists the attributes to apply to the function. Currently documented attributes, JSFUN_BOUND_METHOD and JSFUN_GLOBAL_PARENT, are deprecated and should no longer be used, so 0 should generally be passed for flags in new code. They continue to be supported only for existing applications that already depend on them.

parent is the parent object for this function. If a function has no parent, you can set parent to NULL. name is the name to assign to the function. If you pass an empty C string for name, JS sets the function's name to "anonymous".

On success, JS_NewFunction returns a pointer to the newly created function. Otherwise it returns NULL.

[edit] See Also

LXR ID Search for JS_NewFunction

JSFUN_BOUND_METHOD, JSFUN_GLOBAL_PARENT, JS_CallFunction, JS_CallFunctionName, JS_CallFunctionValue, JS_CompileFunction, JS_CompileUCFunction, JS_DefineFunction, JS_DefineFunctions, JS_GetFunctionName, JS_GetFunctionObject,