JSFastNative 编辑

Obsolete since JavaScript 1.8.5
This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.

This article covers features introduced in SpiderMonkey 1.8

JSFastNative is the type of fast native functions in the JSAPI. APIs such as JS_InitClass and JS_DefineFunctions can create custom methods that are implemented as C/C++ functions of this type, instead of JSNative.

The term "native" here refers to C/C++ code as opposed to JavaScript code.

Added in SpiderMonkey 1.8.1 JSFastNative has been renamed to JSNative.

Syntax

typedef JSBool
(*JSFastNative)(JSContext *cx, unsigned int argc, jsval *vp);
NameTypeDescription
cxJSContext *The context in which the fast native is being called. Provides request. In JS_THREADSAFE builds, the JavaScript engine calls this callback only from within an active request on cx. The callback does not need to call JS_BeginRequest()).
argcunsigned intThe number of arguments supplied to the function by the caller (as opposed to, say, the number of arguments the function is specified to take in its JSFunctionSpec).
vpjsval *The arguments, including the this argument, the return-value slot, and the callee Function object are accessible through this pointer using macros described below.

Description

The callback should use the following macros to access the fields of vp:

Macro nameDescription
JS_CALLEE(cx, vp)Returns the Function object that was called, as a jsval.

Native methods must not call JS_CALLEE after calling JS_SET_RVAL. (The return value and the callee are stored in the same stack slot.)

JS_THIS(cx, vp)Returns the this argument as a jsval, or JSVAL_NULL on error.

Native methods must not call JS_THIS after calling JS_SET_RVAL. (JS_THIS may call JS_CALLEE.)

JS_THIS_OBJECT(cx, vp)Returns the this argument as a JSObject *, or NULL on error.

Native methods must not call JS_THIS_OBJECT after calling JS_SET_RVAL. (JS_THIS_OBJECT may call JS_CALLEE.)

JS_ARGV(cx, vp)Returns the argv pointer. This points to element 0 of an array of argc jsvals, the arguments supplied by the caller.
JS_RVAL(cx, vp)Returns the jsval that is currently in the return-value slot.
JS_SET_RVAL(cx, vp, value)Sets the return value of the native function. It is safe to call this multiple times within a single call to a JSFastNative. If the JSFastNative returns JS_TRUE, the last value passed to this macro will be returned to the caller.

On success, the callback must call JS_SET_RVAL (at least once) and return JS_TRUE. On error or exception, it must return JS_FALSE.

To create a JSFunctionSpec that points to a JSFastNative, use JS_FN.

When a JSFastNative is called, no JSStackFrame is generated. This is what makes a fast native fast. However, it causes the function not to appear on the stack in JavaScript debuggers. It also means that applications that use SpiderMonkey's security features, particularly those that implement JSCheckAccessOp or JSCheckAccessIdOp in terms of APIs such as JS_FrameIterator and JS_StackFramePrincipals, must take extra care, as the native function's principals will be missing from the stack.

See Also

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

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

发布评论

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

词条统计

浏览:104 次

字数:7707

最后编辑:7年前

编辑次数:0 次

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