[gjs/wip/ptomato/mozjs38: 16/17] WIP - Latin1 vs UTF-16 strings
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs/wip/ptomato/mozjs38: 16/17] WIP - Latin1 vs UTF-16 strings
- Date: Thu, 19 Jan 2017 01:55:16 +0000 (UTC)
commit 41ed408bc65d6f5b4fb2d64191cf6cfd33be1c93
Author: Philip Chimento <philip chimento gmail com>
Date: Tue Jan 17 23:36:07 2017 -0800
WIP - Latin1 vs UTF-16 strings
Strings can be stored in one of two formats now, so we need to deal with
both when getting their characters.
gjs/byteArray.cpp | 45 ++++++++++++++++++++++++++++++---------------
gjs/jsapi-util-string.cpp | 11 +++++++++--
gjs/jsapi-util.cpp | 3 ++-
3 files changed, 41 insertions(+), 18 deletions(-)
---
diff --git a/gjs/byteArray.cpp b/gjs/byteArray.cpp
index 2809abd..b441f2d 100644
--- a/gjs/byteArray.cpp
+++ b/gjs/byteArray.cpp
@@ -603,24 +603,39 @@ from_string_func(JSContext *context,
g_byte_array_append(priv->array, (guint8*) utf8, strlen(utf8));
g_free(utf8);
} else {
- char *encoded;
+ JSString *str = argv[0].toString(); /* Rooted by argv */
+ GError *error = NULL;
+ char *encoded = NULL;
gsize bytes_written;
- GError *error;
- const char16_t *u16_chars;
- gsize u16_len;
- u16_chars = JS_GetStringCharsAndLength(context, argv[0].toString(), &u16_len);
- if (u16_chars == NULL)
- return false;
+ {
+ size_t len;
+ JS::AutoCheckCannotGC nogc;
+ if (JS_StringHasLatin1Chars(str)) {
+ const JS::Latin1Char *chars =
+ JS_GetLatin1StringCharsAndLength(context, nogc, str, &len);
+ if (chars == NULL)
+ return false;
+
+ encoded = g_convert((char *) chars, len,
+ encoding, /* to_encoding */
+ "LATIN1", /* from_encoding */
+ NULL, /* bytes read */
+ &bytes_written, &error);
+ } else {
+ const char16_t *chars =
+ JS_GetTwoByteStringCharsAndLength(context, nogc, str, &len);
+ if (chars == NULL)
+ return false;
+
+ encoded = g_convert((char *) chars, len * 2,
+ encoding, /* to_encoding */
+ "UTF-16", /* from_encoding */
+ NULL, /* bytes read */
+ &bytes_written, &error);
+ }
+ }
- error = NULL;
- encoded = g_convert((char*) u16_chars,
- u16_len * 2,
- encoding, /* to_encoding */
- "UTF-16", /* from_encoding */
- NULL, /* bytes read */
- &bytes_written,
- &error);
g_free(encoding);
if (encoded == NULL) {
/* frees the GError */
diff --git a/gjs/jsapi-util-string.cpp b/gjs/jsapi-util-string.cpp
index 6143b47..509413f 100644
--- a/gjs/jsapi-util-string.cpp
+++ b/gjs/jsapi-util-string.cpp
@@ -191,7 +191,12 @@ gjs_string_get_char16_data(JSContext *context,
goto out;
}
- js_data = JS_GetStringCharsAndLength(context, value.toString(), len_p);
+ /* Scope for nogc */
+ {
+ JS::AutoCheckCannotGC nogc;
+ js_data = JS_GetTwoByteStringCharsAndLength(context, nogc,
+ value.toString(), len_p);
+ }
if (js_data == NULL)
goto out;
@@ -231,7 +236,9 @@ gjs_string_to_ucs4(JSContext *cx,
size_t utf16_len;
GError *error = NULL;
- const char16_t *utf16 = JS_GetStringCharsAndLength(cx, str, &utf16_len);
+ JS::AutoCheckCannotGC nogc;
+ const char16_t *utf16 = JS_GetTwoByteStringCharsAndLength(cx, nogc, str,
+ &utf16_len);
if (utf16 == NULL) {
gjs_throw(cx, "Failed to get UTF-16 string data");
return false;
diff --git a/gjs/jsapi-util.cpp b/gjs/jsapi-util.cpp
index 417e4af..f8989f8 100644
--- a/gjs/jsapi-util.cpp
+++ b/gjs/jsapi-util.cpp
@@ -378,7 +378,8 @@ gjs_string_readable (JSContext *context,
size_t i, len;
const char16_t *uchars;
- uchars = JS_GetStringCharsAndLength(context, string, &len);
+ JS::AutoCheckCannotGC nogc;
+ uchars = JS_GetTwoByteStringCharsAndLength(context, nogc, string, &len);
for (i = 0; i < len; i++) {
char16_t c = uchars[i];
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]