JS::CompileOffThread 编辑

This article covers features introduced in SpiderMonkey 31

Compile a script off thread for execution.

Syntax

bool
JS::CanCompileOffThread(JSContext *cx, const JS::ReadOnlyCompileOptions &options,
                        size_t length);

bool
JS::CompileOffThread(JSContext *cx, const JS::ReadOnlyCompileOptions &options,
                     const char16_t *chars, size_t length,
                     JS::OffThreadCompileCallback callback, void *callbackData);

JSScript *
JS::FinishOffThreadScript(JSContext *maybecx, JSRuntime *rt, void *token);

typedef void
(*JS::OffThreadCompileCallback)(void *token, void *callbackData);
NameTypeDescription
cx / maybeJSContext *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.
rtJSRuntime *Pointer to a JS runtime.
optionsJS::ReadOnlyCompileOptions &Compile options.
charsconst char16_t *String containing the script to compile.
lengthsize_tThe length of chars or bytes, in characters.
callbackJS::OffThreadCompileCallbackA callback function invoked when the compilation.
callbackData / tokenvoid *Pointer to application-defined data.

Description

JS::CompileOffThread compiles a script, chars for execution.

The script is associated with a JS object. chars is the string containing the text of the script. length indicates the size of the text version of the script in characters.

After successfully triggering an off thread compile of a script, the callback will eventually be invoked with the specified data and a token for the compilation. The callback will be invoked while off the main thread, so its operations must be thread safe. Afterwards, JS::FinishOffThreadScript must be invoked on the main thread to get the result script or nullptr. If maybecx is not specified, the resources will be freed, but no script will be returned.

The characters passed in to JS::CompileOffThread must remain live until the callback is invoked, and the resulting script will be rooted until the call to FinishOffThreadScript.

See Also

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

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

发布评论

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

词条统计

浏览:135 次

字数:4683

最后编辑:7年前

编辑次数:0 次

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