JS::Rooted 编辑

This article covers features introduced in SpiderMonkey 17

Local variable of type T whose value is always rooted.

Syntax

JS::Rooted<T> var(cx);
JS::Rooted<T> var(cx, initial);
JS::Rooted<T> var(rt);
JS::Rooted<T> var(rt, initial);
NameTypeDescription
cxJSContext *The context in which to add the 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 root.
initialTAn initial value for the rooted variable.

Methods

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

MethodDescription
T &get()Returns ptr.
const T &get() const
operator const T&() const
T operator->() const
T *address()Returns a pointer to ptr.
const T *address() const
T &operator=(T value)Sets the value of ptr to value.
T &operator=(const Rooted &value)
void set(T value)
bool operator!=(const T &other) constCompares ptr and other.
bool operator==(const T &other) const

Description

JS::Rooted<T> declares a local variable of type T whose value is always rooted. This is typically used for local variables, or for non-rooted values being passed to a function that requires a handle, e.g. Foo(JS::Rooted<T>(cx, x)).

JS::Rooted<T> may be automatically coerced to a JS::Handle&lt;T&gt;JS::Rooted<T> should be used whenever a local variable's value may be held live across a call which can trigger a GC.

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

There are typedefs available for the main types:

namespace JS {
typedef Rooted<JSObject*>       RootedObject;
typedef Rooted<JSFunction*>     RootedFunction;
typedef Rooted<JSScript*>       RootedScript;
typedef Rooted<JSString*>       RootedString;
typedef Rooted<JS::Symbol*>     RootedSymbol; // Added in SpiderMonkey 38
typedef Rooted<jsid>            RootedId;
typedef Rooted<JS::Value>       RootedValue;
}

See Also

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

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

发布评论

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

词条统计

浏览:150 次

字数:6094

最后编辑:8年前

编辑次数:0 次

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