JS_NewFunction

This article is in need of a technical review.

Create a new JavaScript function that is implemented in C/C++ as a JSNative.

Syntax

// Added in SpiderMonkey 45

JSFunction *
JS_NewFunction(JSContext *cx, JSNative call, unsigned nargs, unsigned flags,
               const char *name);

JSFunction *
JS_NewFunctionById(JSContext *cx, JSNative call, unsigned nargs, unsigned flags,
                   JS::Handle<jsid> id);

// Obsolete since JSAPI 39

JSFunction *
JS_NewFunction(JSContext *cx, JSNative call, unsigned nargs, unsigned flags,
               JS::Handle<JSObject*> parent, const char *name);

JSFunction *
JS_NewFunctionById(JSContext *cx, JSNative call, unsigned nargs, unsigned flags,
                   JS::Handle<JSObject*> parent, JS::Handle<jsid> id); // Added in SpiderMonkey 17
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 unsigned Number of arguments the function expects.
flags unsigned Pass JSFUN_CONSTRUCTOR to make a native that can be called as a constructor, otherwise 0.
parent JS::Handle<JSObject*> Pointer to the parent object for this function. If this is NULL, a default parent object is used. Obsolete since JSAPI 39
name const char * Name to assign to the new function. If this is NULL, the new function has no name.
id JS::Handle<jsid> A pointer to jsid of the name to assign to the new function. Added in SpiderMonkey 17

Description

JS_NewFunction creates a new JavaScript function implemented in C/C++. (To create a new function implemented in JavaScript, use JS_CompileFunction instead.)

call is a C/C++ function pointer that the new function wraps. nargs is the number of arguments the function expects. If flags is JSFUN_CONSTRUCTOR, a created function can be called as a constructor. parent may be used to specify the new function's parent; NULL is usually the right thing here. name is the name to assign to the function. If name is NULL, the new function has no name. (JS_GetFunctionId, passed the new function, will return NULL.)

On success, JS_NewFunction returns a pointer to the newly created function. Otherwise it reports an out-of-memory error and returns NULL.

See Also

Document Tags and Contributors

Contributors to this page: arai, kscarfone, fscholz, nikhilm, Jorend, Dria, MMondor
Last updated by: arai,
Hide Sidebar