JSAutoByteString 编辑

This article covers features introduced in SpiderMonkey 17

Take ownership of a string and free it later.

Syntax

JSAutoByteString str;

JSAutoByteString(JSContext *cx, JSString *str);
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.
strJSString *A pointer to JSString to get initial content by calling JS_EncodeString(cx, str).

Methods

MethodDescription
void initBytes(char *bytes)Take ownership of the given byte array.
char *encodeLatin1(JSContext *cx, JSString *str)Call JS_EncodeString and take ownership of the returned string, and return the string.
char *encodeLatin1(js::ExclusiveContext *cx, JSString *str)Call JS::LossyTwoByteCharsToNewLatin1CharsZ, or allocate string and copy the content of JSLinearString::latin1Chars, and take the ownership of the string. This may fail if str is not linear.
char *encodeUtf8(JSContext *cx, JS::HandleString str)Call JS_EncodeStringToUTF8 and take ownership of the returned string, and return the string.
void clear()Free the owned string. You should call this before calling encode* methods or initBytes method if a string is already owned, otherwise the string will never be freed.
char *ptr() constReturn a pointer to the owned string. This could be NULL if no string is owned.
bool operator!() constReturn true if no string is owned.
size_t length() constReturn the length of the owned string. Return 0 if no string is owned.

Description

JSAutoByteString takes the ownership of string and frees it in destructor.

If JSAutoByteString instance is initialized with JSAutoByteString bytes(cx, str); style, it calls JS_EncodeString(cx, str) to get the string to take ownership.

If JSAutoByteString instance is initialized with JSAutoByteString bytes; style, it does not own any string. You can call some methods to take ownership of other string.

Note that the JS_EncodeString call in constructor and some encode* methods may fail and get NULL. So ensure the string is returned by operator!() before using ptr() method.

Examples

Use constructor arguments

{
  JSString *str = JS::ToString(cx, strVal);
  if (!str)
    return false;

  JSAutoByteString bytes(cx, str); /* calls JS_EncodeString internally */
  if (!bytes)
    return false;

  /* ...do something with bytes... */

  /* when leaving this scope, the string returned by JS_EncodeString is freed. */
}

Use method to encode string

{
  JS::RootedString str(cx, JS::ToString(cx, strVal));
  if (!str)
    return false;

  JSAutoByteString bytes;
  if (!bytes.encodeUtf8(cx, str)) /* calls JS_EncodeStringToUTF8 internally */
    return false;

  /* ...do something with bytes... */

  /* when leaving this scope, the string returned by JS_EncodeStringToUTF8 is freed. */
}

Take ownership of other buffer

{
  char *buff = cx->pod_malloc<char>(length + 1);
  if (!buff)
    return false;
  memcpy(buff, other_buff, length + 1)

  JSAutoByteString bytes;
  bytes.initBytes(buff);

  /* ...do something with bytes... */

  /* when leaving this scope, buff is freed. */
}

See Also

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

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

发布评论

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

词条统计

浏览:82 次

字数:7124

最后编辑:8年前

编辑次数:0 次

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