[gjs] jsapi-util: Debug value should not use JS_EncodeStringToBuffer()



commit 771d8ec72b28619ab27fff0a2209cea70fd2be3e
Author: Philip Chimento <philip chimento gmail com>
Date:   Sat Feb 25 14:40:25 2017 -0800

    jsapi-util: Debug value should not use JS_EncodeStringToBuffer()
    
    JS_EncodeStringToBuffer() does not encode to UTF-8, so if an object's
    toString() method returns any non-ASCII codepoints, they will get
    mangled. Instead use JS_EncodeStringToUTF8().
    
    https://bugzilla.gnome.org/show_bug.cgi?id=778729

 gjs/jsapi-util.cpp |   11 ++---------
 test/gjs-tests.cpp |   23 +++++++++++++++++++++++
 2 files changed, 25 insertions(+), 9 deletions(-)
---
diff --git a/gjs/jsapi-util.cpp b/gjs/jsapi-util.cpp
index 8a83071..f0bcd75 100644
--- a/gjs/jsapi-util.cpp
+++ b/gjs/jsapi-util.cpp
@@ -551,18 +551,11 @@ gjs_value_debug_string(JSContext      *context,
 
     g_assert(str != NULL);
 
-    size_t len = JS_GetStringEncodingLength(context, str);
-    if (len != (size_t)(-1)) {
-        bytes = (char*) g_malloc((len + 1) * sizeof(char));
-        JS_EncodeStringToBuffer(context, str, bytes, len);
-        bytes[len] = '\0';
-    } else {
-        bytes = g_strdup("[invalid string]");
-    }
+    bytes = JS_EncodeStringToUTF8(context, str);
     JS_EndRequest(context);
 
     debugstr = _gjs_g_utf8_make_valid(bytes);
-    g_free(bytes);
+    JS_free(context, bytes);
 
     return debugstr;
 }
diff --git a/test/gjs-tests.cpp b/test/gjs-tests.cpp
index 7131fb7..bd9c3e5 100644
--- a/test/gjs-tests.cpp
+++ b/test/gjs-tests.cpp
@@ -268,6 +268,27 @@ test_jsapi_util_debug_string_invalid_utf8(GjsUnitTestFixture *fx,
 }
 
 static void
+test_jsapi_util_debug_string_object_with_complicated_to_string(GjsUnitTestFixture *fx,
+                                                               gconstpointer       unused)
+{
+    const char16_t desserts[] = {
+        0xd83c, 0xdf6a,  /* cookie */
+        0xd83c, 0xdf69,  /* doughnut */
+    };
+    JS::AutoValueArray<2> contents(fx->cx);
+    contents[0].setString(JS_NewUCStringCopyN(fx->cx, desserts, 2));
+    contents[1].setString(JS_NewUCStringCopyN(fx->cx, desserts + 2, 2));
+    JS::RootedObject array(fx->cx, JS_NewArrayObject(fx->cx, contents));
+    JS::RootedValue v_array(fx->cx, JS::ObjectValue(*array));
+    char *debug_output = gjs_value_debug_string(fx->cx, v_array);
+
+    g_assert_nonnull(debug_output);
+    g_assert_cmpstr(u8"🍪,🍩", ==, debug_output);
+
+    g_free(debug_output);
+}
+
+static void
 gjstest_test_func_util_glib_strv_concat_null(void)
 {
     char **ret;
@@ -394,6 +415,8 @@ main(int    argc,
                         test_jsapi_util_debug_string_valid_utf8);
     ADD_JSAPI_UTIL_TEST("debug_string/invalid-utf8",
                         test_jsapi_util_debug_string_invalid_utf8);
+    ADD_JSAPI_UTIL_TEST("debug_string/object-with-complicated-to-string",
+                        test_jsapi_util_debug_string_object_with_complicated_to_string);
 
 #undef ADD_JSAPI_UTIL_TEST
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]