JS_CallFunction

Calls a specified JS function.

Syntax

/* Added in SpiderMonkey 31 */
bool
JS_CallFunction(JSContext *cx, JS::HandleObject obj, JS::HandleFunction fun,
                const JS::HandleValueArray& args,
                JS::MutableHandleValue rval);
bool
JS_CallFunctionName(JSContext *cx, JS::HandleObject obj, const char *name,
                    const JS::HandleValueArray& args, JS::MutableHandleValue rval);
bool
JS_CallFunctionValue(JSContext *cx, JS::HandleObject obj, JS::HandleValue fval,
                     const JS::HandleValueArray& args, JS::MutableHandleValue rval);
/* Obsolete since JSAPI 30 */
bool
JS_CallFunction(JSContext *cx, JSObject *obj, JSFunction *fun, unsigned argc,
                jsval *argv, jsval *rval);
bool
JS_CallFunctionName(JSContext *cx, JSObject *obj, const char *name, unsigned argc,
                    jsval *argv, jsval *rval);
bool
JS_CallFunctionValue(JSContext *cx, JSObject *obj, jsval fval, unsigned argc,
                     jsval *argv, jsval *rval);
Name Type Description
cx JSContext * Pointer to a JS context from which to derive runtime information. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
obj JS::HandleObject The "current" object on which the function operates; the object specified here is "this" when the function executes.
fun JS::HandleFunction Pointer to the function to call. fun should be a native function or JSAPI-compiled function. See below.
name const char * Pointer to the name of function to call.
fval JS::HandleValue Pointer to the function value to call.
args const JS::HandleValueArray & Reference to the array of argument values to pass to the function. Added in SpiderMonkey 31
argc unsigned Number of arguments you are passing to the function. Obsolete since JSAPI 30
argv jsval * Pointer to the array of argument values to pass to the function. Obsolete since JSAPI 30
rval JS::MutableHandleValue Out parameter. On success, *rval receives the return value from the function call.

Description

JS_CallFunction calls a specified function, fun, on an object, obj. In terms of function execution, the object is treated as this.

JS_CallFunctionName calls a function with specified name, name on an object obj. If JS engine fails to get the function, it returns false.

JS_CallFunctionValue calls a specified function, fval on an object obj. JS_CallFunctionValue(cx, obj, fval, args, rval) is analogous to the JavaScript statement rval = fval.apply(obj, args);.

In args, pass a reference to the actual argument values to use. There should be one value for each argument you pass to the function; the number of arguments you pass may be different from the number of arguments defined for the function.

Obsolete since JSAPI 30.

In argc, indicate the number of arguments passed to the function. In argv, pass a pointer to the actual argument values to use.

rval is a pointer to a variable that will hold the function's return value, if any, on successful function execution.

If the called function executes successfully, JS_CallFunction returns true. Otherwise it returns false, and rval is undefined.

Warning: Calling JS_CallFunction is safe only if the fun argument could be passed to JS_GetFunctionObject safely: that is, it is a function implemented by a JSNative or JSFastNative or the result of a call to JS_CompileFunction, JS_CompileUCFunction, JS_CompileFunctionForPrincipals, or JS_CompileUCFunctionForPrincipals. Passing any other JSFunction pointer can lead to a crash or worse.

See also

Document Tags and Contributors

 Contributors to this page: arai, fscholz, ethertank, trevorh, Jorend, Piers, Dria, MMondor
 Last updated by: arai,