JS_NewExternalString 编辑

Create a new JSString whose characters are stored in external memory.

Syntax

JSString *
JS_NewExternalString(JSContext *cx, const char16_t *chars, size_t length,
                     const JSStringFinalizer *fin);

JSString *
JS_NewExternalStringWithClosure(JSContext *cx, jschar *chars, size_t length,
                                int type, void *closure); // Obsolete since JSAPI 13
NameTypeDescription
cxJSContext *The context in which to create the new string. Requires request. In a JS_THREADSAFE build, the caller must be in a request on this JSContext.
charsconst char16_t * or const jschar *Pointer to the first element of an array of char16_ts. This array is used as the character buffer of the JSString to be created. The array must be populated with the desired character data before JS_NewExternalString is called, and the array must remain in memory, with its contents unchanged, for as long as the JavaScript engine needs to hold on to it. (Ultimately, the string will be garbage collected, and the JavaScript engine will call the string finalizer callback, allowing the application to free the array.) The array does not need to be zero-terminated.
lengthsize_tThe number of characters in the array chars.
finJSStringFinalizerA string finalizer the JavaScript engine should use (later) to free the string buffer chars. Added in SpiderMonkey 17
typeint(JS_NewExternalStringWithClosure only) Indicates which string finalizer callback the JavaScript engine should use (later) to free the string buffer chars. This must be an id number previously returned by a successful call to JS_AddExternalStringFinalizer. Obsolete since JSAPI 13
closurevoid *(JS_NewExternalStringWithClosure only) Arbitrary, application-defined data to include in the string object after it's created. If you use this as a pointer, you should clean it up in the external string finalizer that was previously set up using JS_AddExternalStringFinalizer. Obsolete since JSAPI 13

Description

JS_NewExternalString and JS_NewExternalStringWithClosure create a new JSString whose characters are stored in external memory, i.e., memory allocated by the application, not the JavaScript engine. Since the program allocated the memory, it will need to free it; this happens in an external string finalizer indicated by the type parameter.

JS_NewExternalStringWithClosure works similarly, except it accepts an additional parameter, closure, which is saved in the string's internal data structure. It can be retrieved later by calling JS_GetExternalStringClosure. This is application-defined data you can associate with the string.

To determine if a string was created as an external string, you can call JS_IsExternalString.

See Also

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

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

发布评论

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

词条统计

浏览:66 次

字数:6233

最后编辑:7年前

编辑次数:0 次

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