JS_AddExternalStringFinalizer 编辑

Obsolete since JSAPI 13
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.

Register a custom string memory manager.

Syntax

int
JS_AddExternalStringFinalizer(JSStringFinalizeOp finalizer);
NameTypeDescription
finalizerJSStringFinalizeOpPointer to a callback function, described below. The JS engine will automatically call this function each time a string created by JS_NewExternalString is garbage-collected.

Callback syntax

typedef void
(*JSStringFinalizeOp)(JSContext *cx, JSString *str);
NameTypeDescription
cxJSContext *Pointer to a JSContext which the finalizer may use for certain very limited operations (not documented). Since the JSStringFinalizeOp callback is called during garbage collection, it must avoid most JSAPI functions. (Is cx guaranteed to be non-NULL?)
strJSString *The string being finalized. This is always a string that the application previously created by calling JS_NewExternalString. The callback may use JS_GetStringChars(str) to get a pointer to the character buffer, which is the pointer which the application passed to JS_NewExternalString() when creating the string. It is the callback's responsibility to free the memory. After this callback, the JS engine will not use that memory anymore and will not keep a pointer to it.

The callback may not keep a reference to this JSString, as its memory is about to be freed.

Description

Add a finalizer for external strings created by JS_NewExternalString using a type-code returned from this function, and that understands how to free or release the memory pointed at by JS_GetStringChars(str).

Returns a nonnegative type index if there is room for finalizer in the global GC finalizers table, else returns -1. In a JS_THREADSAFE build, this function must be invoked on the primordial thread only, at startup—or else the entire program must single-thread itself while loading a module that calls this function.

To remove an external string finalizer, use JS_RemoveExternalStringFinalizer.

See Also

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

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

发布评论

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

词条统计

浏览:26 次

字数:5182

最后编辑:8年前

编辑次数:0 次

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