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);
NameTypeDescription
cxJSContext *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.
objJS::HandleObjectThe "current" object on which the function operates; the object specified here is "this" when the function executes.
funJS::HandleFunctionPointer to the function to call. fun should be a native function or JSAPI-compiled function. See below.
nameconst char *Pointer to the name of function to call.
fvalJS::HandleValuePointer to the function value to call.
argsconst JS::HandleValueArray &Reference to the array of argument values to pass to the function. Added in SpiderMonkey 31
argcunsignedNumber of arguments you are passing to the function. Obsolete since JSAPI 30
argvjsval *Pointer to the array of argument values to pass to the function. Obsolete since JSAPI 30
rvalJS::MutableHandleValueOut 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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:109 次

字数:7301

最后编辑:6年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文