Create a native function and assign it as a property to a specified JS object.
Syntax
JSFunction * JS_DefineFunction(JSContext *cx, JS::Handle<JSObject*> obj, const char *name, JSNative call, unsigned nargs, unsigned attrs); JSFunction * JS_DefineUCFunction(JSContext *cx, JS::Handle<JSObject*> obj, const char16_t *name, size_t namelen, JSNative call, unsigned nargs, unsigned attrs); JSFunction * JS_DefineFunctionById(JSContext *cx, JS::Handle<JSObject*> obj, JS::Handle<jsid> id, JSNative call, unsigned nargs, unsigned attrs); // Added in SpiderMonkey 17
Name | Type | Description |
---|---|---|
cx |
JSContext * |
The context in which to define the function. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext . |
obj |
JS::Handle<JSObject*> |
Object for which to define a function as a property (method). |
name |
const char * or const char16_t * |
Name to assign to the function. |
id |
JS::Handle<jsid> |
A pointer to jsid for name to assign to the function. Added in SpiderMonkey 17 |
call |
JSNative |
The native C/C++ function to be wrapped by the new function. |
nargs |
unsigned |
Number of arguments that are passed to the function when it is called. |
flags |
unsigned |
The logical OR of zero or more property attributes. |
Description
JS_DefineFunction
exposes a C/C++ function to scripts by defining a new method on an existing JavaScript object. It creates the new Function object as though by calling JS_NewFunction
, then makes it a method of obj
as though by calling JS_DefineProperty
. JS_DefineUCFunction
is the Unicode version of the function.
name
is the name of the property to create in obj
. That is, it is the method name that scripts can use to call the new function. It also becomes the name
property of the new function object and can be accessed using JS_GetFunctionId
.
call
is a pointer to the C/C++ function that is to be exposed to JavaScript.
nargs
indicates the number of arguments the function expects to receive. This becomes the new function object's length
property and can be accessed using JS_GetFunctionArity
.
flags
is the logical OR of zero or more property attributes, which apply to the new property.
On success, JS_DefineFunction
and JS_DefineUCFunction
return a pointer to the new function. On error or exception, they return NULL
.
See Also
- MXR ID Search for
JS_DefineFunction
- MXR ID Search for
JS_DefineUCFunction
- MXR ID Search for
JS_DefineFunctionById
JS_CallFunctionName
JS_CallFunctionValue
JS_CompileFunction
JS_DefineFunctions
JS_DefineObject
JS_DefineProperties
JS_DefineProperty
JS_GetFunctionObject
JS_NewFunction
- bug 607695 - added
JS_DefineFunctionById