JS_SetBranchCallback 编辑

Obsolete since JavaScript 1.9.1
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.

Specifies a callback function that is automatically called when a script branches backward during execution, when a function returns, and at the end of the script.

Replaced with JS_SetOperationCallback.

Syntax

JSBranchCallback
JS_SetBranchCallback(JSContext *cx, JSBranchCallback cb);
NameTypeDescription
cxJSContext *The context to hook.
cbJSBranchCallbackPointer to the callback function.

Callback syntax

JSBool
(*JSBranchCallback)(JSContext *cx, JSScript *script);
NameTypeDescription
cxJSContext *Pointer to a JSContext which the callback may use to call into JSAPI functions. For example, the callback my call JS_MaybeGC(cx). This is the context that is currently executing the code that triggered the callback. 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()).
scriptJSScriptThe script that is running. This is guaranteed to be non-null, unless the JSOPTION_NATIVE_BRANCH_CALLBACK option has been enabled for the context cx using JS_SetOptions().

If the callback returns JS_TRUE, the JS engine continues to execute the script.

If the callback raises an exception using JS_SetPendingException() and returns JS_FALSE, then the JavaScript engine propagates the exception to the script that was executing at the time.

If the callback returns JS_FALSE without raising an exception, then the JavaScript engine immediately stops running the script with an uncatchable error. The engine does not execute finally blocks in this case; this is the same behavior as any native method or callback.

Description

JS_SetBranchCallback specifies a callback function that is automatically called when a script branches backward during execution, when a function returns, and at the end of the script. One typical use for a callback is in a client application to enable a user to abort a script that runs for a long time. Another use is to call JS_MaybeGC periodically.

The callback is called very frequently, so applications should ensure that the callback runs very quickly most of the time. Typically an application only wants to do something every 5,000 or so branches. A common technique is to use a counter. The callback simply increments the counter and does nothing further (returning JS_TRUE immediately) unless the counter has reached the threshold value.

See Also

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

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

发布评论

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

词条统计

浏览:93 次

字数:5466

最后编辑:7年前

编辑次数:0 次

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