[gjs: 2/5] jsapi-util-string: Print quotes around strings in gjs_debug_value()




commit 5e5db692717091519bec0e076da519d2ff05b1b5
Author: Philip Chimento <philip chimento gmail com>
Date:   Sat Apr 17 21:39:53 2021 -0700

    jsapi-util-string: Print quotes around strings in gjs_debug_value()
    
    In order to go along with the other source-like representations in
    gjs_debug_value(), such as Symbol("foo") and 42n, print quotes around
    strings.
    
    However, when printing strings as a property key in gjs_debug_id(), don't
    print quotes.

 gjs/jsapi-util-string.cpp | 25 ++++++++++++++++++++-----
 test/gjs-tests.cpp        | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 5 deletions(-)
---
diff --git a/gjs/jsapi-util-string.cpp b/gjs/jsapi-util-string.cpp
index 3f66340e..e4aa7ca9 100644
--- a/gjs/jsapi-util-string.cpp
+++ b/gjs/jsapi-util-string.cpp
@@ -400,16 +400,28 @@ gjs_intern_string_to_id(JSContext  *cx,
     return JS::PropertyKey::fromPinnedString(str);
 }
 
-[[nodiscard]] static std::string gjs_debug_linear_string(JSLinearString* str) {
+enum Quotes {
+    DoubleQuotes,
+    NoQuotes,
+};
+
+[[nodiscard]] static std::string gjs_debug_linear_string(JSLinearString* str,
+                                                         Quotes quotes) {
     size_t len = js::GetLinearStringLength(str);
 
+    std::ostringstream out;
+    if (quotes == DoubleQuotes)
+        out << '"';
+
     JS::AutoCheckCannotGC nogc;
     if (js::LinearStringHasLatin1Chars(str)) {
         const JS::Latin1Char *chars = js::GetLatin1LinearStringChars(nogc, str);
-        return std::string(reinterpret_cast<const char *>(chars), len);
+        out << std::string(reinterpret_cast<const char*>(chars), len);
+        if (quotes == DoubleQuotes)
+            out << '"';
+        return out.str();
     }
 
-    std::ostringstream out;
     const char16_t *chars = js::GetTwoByteLinearStringChars(nogc, str);
     for (size_t ix = 0; ix < len; ix++) {
         char16_t c = chars[ix];
@@ -424,6 +436,8 @@ gjs_intern_string_to_id(JSContext  *cx,
         else
             out << "\\x" << std::setfill('0') << std::setw(4) << unsigned(c);
     }
+    if (quotes == DoubleQuotes)
+        out << '"';
     return out.str();
 }
 
@@ -437,7 +451,8 @@ gjs_debug_string(JSString *str)
         out << JS_GetStringLength(str) << '>';
         return out.str();
     }
-    return gjs_debug_linear_string(JS_ASSERT_STRING_IS_LINEAR(str));
+    return gjs_debug_linear_string(JS_ASSERT_STRING_IS_LINEAR(str),
+                                   DoubleQuotes);
 }
 
 std::string
@@ -558,6 +573,6 @@ std::string
 gjs_debug_id(jsid id)
 {
     if (JSID_IS_STRING(id))
-        return gjs_debug_linear_string(JSID_TO_LINEAR_STRING(id));
+        return gjs_debug_linear_string(JSID_TO_LINEAR_STRING(id), NoQuotes);
     return gjs_debug_value(js::IdToValue(id));
 }
diff --git a/test/gjs-tests.cpp b/test/gjs-tests.cpp
index e63fc430..325c967c 100644
--- a/test/gjs-tests.cpp
+++ b/test/gjs-tests.cpp
@@ -19,6 +19,7 @@
 
 #include <js/Array.h>
 #include <js/CharacterEncoding.h>
+#include <js/Id.h>
 #include <js/RootingAPI.h>
 #include <js/TypeDecls.h>
 #include <js/Utility.h>  // for UniqueChars
@@ -574,6 +575,34 @@ static void test_jsapi_util_debug_string_object_with_complicated_to_string(
     g_assert_cmpstr(u8"🍪,🍩", ==, debug_output.c_str());
 }
 
+static void test_gjs_debug_id_string_no_quotes(GjsUnitTestFixture* fx,
+                                               const void*) {
+    jsid id = gjs_intern_string_to_id(fx->cx, "prop_key");
+    std::string debug_output = gjs_debug_id(id);
+
+    g_assert_cmpstr(debug_output.c_str(), ==, "prop_key");
+}
+
+static void test_gjs_debug_string_quotes(GjsUnitTestFixture* fx, const void*) {
+    JS::ConstUTF8CharsZ chars("a string", strlen("a string"));
+    JSString* str = JS_NewStringCopyUTF8Z(fx->cx, chars);
+    std::string debug_output = gjs_debug_string(str);
+
+    g_assert_cmpstr(debug_output.c_str(), ==, "\"a string\"");
+}
+
+static void test_gjs_debug_value_string_quotes(GjsUnitTestFixture* fx,
+                                               const void*) {
+    JS::RootedValue v(fx->cx);
+    bool ok = gjs_string_from_utf8(fx->cx, "a string", &v);
+
+    g_assert_true(ok);
+
+    std::string debug_output = gjs_debug_value(v);
+
+    g_assert_cmpstr(debug_output.c_str(), ==, "\"a string\"");
+}
+
 static void
 gjstest_test_func_util_misc_strv_concat_null(void)
 {
@@ -931,6 +960,13 @@ main(int    argc,
     ADD_JSAPI_UTIL_TEST("gi/args/safe-integer/min",
                         gjstest_test_safe_integer_min);
 
+    // Debug functions
+    ADD_JSAPI_UTIL_TEST("debug_id/string/no-quotes",
+                        test_gjs_debug_id_string_no_quotes);
+    ADD_JSAPI_UTIL_TEST("debug_string/quotes", test_gjs_debug_string_quotes);
+    ADD_JSAPI_UTIL_TEST("debug_value/string/quotes",
+                        test_gjs_debug_value_string_quotes);
+
 #undef ADD_JSAPI_UTIL_TEST
 
     gjs_test_add_tests_for_coverage ();


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