JS::Handle 编辑

This article covers features introduced in SpiderMonkey 17

Reference to a T that has been rooted elsewhere. This is most useful as a parameter type, which guarantees that the T value is properly rooted.

Syntax

bool
someFunction(JSContext *cx, JS::Handle<T> var) {
...
}

Methods

Here, ptr represents the private member of JS::Handle<T>, typed with T.

MethodDescription
const T *address() constReturns a pointer to ptr.
const T &get() constReturns ptr.
operator const T&() const
T operator->() const
bool operator!=(const T &other) constCompares ptr and other.
bool operator==(const T &other) const

Description

JS::Handle<T> is a const reference to a JS::Rooted&lt;T&gt;. Functions which take GC things or values as arguments and need to root those arguments should generally use handles for those arguments and avoid any explicit rooting. This has two benefits. First, when several such functions call each other then redundant rooting of multiple copies of the GC thing can be avoided. Second, if the caller does not pass a rooted value a compile error will be generated, which is quicker and easier to fix than when relying on a separate rooting analysis.

If you want to add additional methods to JS::Handle for a specific specialization, define a js::HandleBase<T> specialization containing them.

There are typedefs available for the main types:

namespace JS {
typedef Handle<JSFunction*> HandleFunction;
typedef Handle<jsid>        HandleId;
typedef Handle<JSObject*>   HandleObject;
typedef Handle<JSScript*>   HandleScript;
typedef Handle<JSString*>   HandleString;
typedef Handle<JS::Symbol*> HandleSymbol; // Added in SpiderMonkey 38
typedef Handle<Value>       HandleValue;
}

See Also

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

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

发布评论

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

词条统计

浏览:147 次

字数:4798

最后编辑:6年前

编辑次数:0 次

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