Visit Mozilla.org

JS CallFunction

From MDC

Calls a specified JS function.

Contents

[edit] Syntax

JSBool JS_CallFunction(JSContext *cx, JSObject *obj,
    JSFunction *fun, uintN 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 JSObject * The "current" object on which the function operates; the object specified here is "this" when the function executes.
fun JSFunction * Pointer to the function to call.
argc uintN Number of arguments you are passing to the function.
argv jsval * Pointer to the array of argument values to pass to the function.
rval jsval * Out parameter. On success, *rval receives the return value from the function call.

[edit] Description

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

In argc, indicate the number of arguments passed to the function. In argv, pass a pointer 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.

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 JS_TRUE. Otherwise it returns JS_FALSE, and rval is undefined.

Warning sign
Warning: Prior to SpiderMonkey 1.8, calling JS_CallFunction is not the same as calling JS_CallFunctionValue on the corresponding Function object. In particular, if the function to be called is a closure, calling it via JS_CallFunction will definitely do the wrong thing, as the JSFunction does not include the necessary reference to the enclosing scope. Similarly, if the Function object has properties, JS_CallFunction might not see them.

New in SpiderMonkey 1.8 (not yet released) This is fixed in SpiderMonkey 1.8. See bug 424376.

[edit] Notes

To call a method of an object, use JS_CallFunctionName.

If you have a Function object, call it using JS_CallFunctionValue.

[edit] See Also

LXR ID Search for JS_CallFunction

JS_CompileFunction, JS_DefineFunction, JS_DefineFunctions, JS_GetFunctionObject, JS_NewFunction, JS_ValueToFunction