JS::Add*Root 编辑

Obsolete since JSAPI 38
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 31

Register a variable as a member of the garbage collector's root set, to protect anything the root points at from garbage collection.

These functions are obsoleted, use JS::PersistentRooted instead.

Syntax

bool
JS::AddValueRoot(JSContext *cx, JS::Heap<JS::Value> *vp);

bool
JS::AddStringRoot(JSContext *cx, JS::Heap<JSString *> *rp);

bool
JS::AddObjectRoot(JSContext *cx, JS::Heap<JSObject *> *rp);

bool
JS::AddNamedValueRoot(JSContext *cx, JS::Heap<JS::Value> *vp,
                      const char *name);

bool
JS::AddNamedValueRootRT(JSRuntime *rt, JS::Heap<JS::Value> *vp,
                        const char *name);

bool
JS::AddNamedStringRoot(JSContext *cx, JS::Heap<JSString *> *rp,
                       const char *name);

bool
JS::AddNamedObjectRoot(JSContext *cx, JS::Heap<JSObject *> *rp,
                       const char *name);

bool
JS::AddNamedScriptRoot(JSContext *cx, JS::Heap<JSScript *> *rp,
                       const char *name);
NameTypeDescription
cxJSContext *The context in which to add the new root. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
rtJSRuntime *The runtime in which to add the new root.
vpJS::Heap&lt;JS::Value&gt;The address of the JS::Value variable to root.
rpJS::Heap&lt;JSString *&gt;The address of the JSString * variable to root.
rpJS::Heap&lt;JSObject *&gt;The address of the JSObject * variable to root.
rpJS::Heap&lt;JSScript *&gt;The address of the JSScript * variable to root.
nameconst char *The name of the new root, or NULL.

Description

The JS::Add*Root and functions add a C/C++ variable to the garbage collector's root set, the set of variables used as starting points each time the collector checks to see what memory is reachable. The garbage collector aggressively collects and recycles memory that it deems unreachable, so roots are often necessary to protect data from being prematurely collected.

vp/rp is the address of a C/C++ variable (or field, or array element) of type JS::Value, JSString *, JSObject *, or JSScript *. This variable must already be initialized. (For example, it must not be an uninitialized local variable. That could cause sporadic crashes during garbage collection, which can be hard to debug.) The variable must remain in memory until the balancing call to JS::Remove*Root. Note that this means that if the root is meant to live past the end of a function, the address of a local (stack-based) variable may not be used for vp/rp. If JS::Add*Root succeeds, then as long as this variable points to a JavaScript value or pointer to GC-thing, that value/GC-thing is protected from garbage collection. If the variable points to an object, then any memory reachable from its properties is automatically protected from garbage collection, too.

An entry for rp is added to the garbage collector's root set for the JSRuntime associated with cx (or, in JS::AddNamedValueRootRT, the runtime rt). The name parameter, if present and non-null, is stored in the JSRuntime's root table entry along with rp. The name string's lifetime must last at least until the balancing call to JS::Remove*Root. Typically name is a static string constant, identifying the source location of the call to JS::AddNamed*Root for debugging purposes.

Needs the Example for typical usage of the name parameter.

All the JS::Add*Root methods are idempotent: multiple calls for the same address will add only one root. Correspondingly, only a single JS::Remove*Root is required to unroot the location.

On success, these functions return true. Otherwise they report an out of memory error and return false.

See Also

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

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

发布评论

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

词条统计

浏览:106 次

字数:10276

最后编辑:7年前

编辑次数:0 次

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