[gjs: 1/7] jsapi-util-string: Handle BigInt in gjs_debug_value()




commit ead52f32001cc46695d99cadcf14d5ad3a1c01d9
Author: Philip Chimento <philip chimento gmail com>
Date:   Sat Apr 17 21:36:59 2021 -0700

    jsapi-util-string: Handle BigInt in gjs_debug_value()
    
    We print out the value % INT64_MAX in this debug function because printing
    out the full value requires a JSContext and may be fallible.

 gjs/gjs_pch.hh                          |  1 +
 gjs/jsapi-util-string.cpp               |  8 ++++++--
 installed-tests/js/testGIMarshalling.js |  2 +-
 meson.build                             |  2 +-
 test/gjs-tests.cpp                      | 36 +++++++++++++++++++++++++++++++++
 5 files changed, 45 insertions(+), 4 deletions(-)
---
diff --git a/gjs/gjs_pch.hh b/gjs/gjs_pch.hh
index fe3bd60f..86d83153 100644
--- a/gjs/gjs_pch.hh
+++ b/gjs/gjs_pch.hh
@@ -55,6 +55,7 @@
 #include <js/AllocPolicy.h>
 #include <js/Array.h>
 #include <js/ArrayBuffer.h>
+#include <js/BigInt.h>
 #include <js/CallArgs.h>
 #include <js/CharacterEncoding.h>
 #include <js/Class.h>
diff --git a/gjs/jsapi-util-string.cpp b/gjs/jsapi-util-string.cpp
index 5b681005..b2ea9f3d 100644
--- a/gjs/jsapi-util-string.cpp
+++ b/gjs/jsapi-util-string.cpp
@@ -16,6 +16,7 @@
 
 #include <glib.h>
 
+#include <js/BigInt.h>
 #include <js/CharacterEncoding.h>
 #include <js/Class.h>
 #include <js/ComparisonOperators.h>
@@ -543,8 +544,11 @@ gjs_debug_value(JS::Value v)
         out << v.toDouble();
         return out.str();
     }
-    if (v.isBigInt())
-        return "<BigInt>";
+    if (v.isBigInt()) {
+        // technically this prints v % INT64_MAX
+        out << JS::ToBigInt64(v.toBigInt()) << 'n';
+        return out.str();
+    }
     if (v.isString()) {
         out << gjs_debug_string(v.toString());
         return out.str();
diff --git a/installed-tests/js/testGIMarshalling.js b/installed-tests/js/testGIMarshalling.js
index ba6c80a5..c0bce490 100644
--- a/installed-tests/js/testGIMarshalling.js
+++ b/installed-tests/js/testGIMarshalling.js
@@ -1410,7 +1410,7 @@ describe('Virtual function', function () {
         expect(() => tester.vfunc_meth_with_error(6)).toThrowError(/42/);
         expect(() => tester.vfunc_meth_with_error(7)).toThrowError(/true/);
         expect(() => tester.vfunc_meth_with_error(8)).toThrowError(/"a string"/);
-        expect(() => tester.vfunc_meth_with_error(9)).toThrow();  // TODO(ptomato): toThrowError(/42n/)
+        expect(() => tester.vfunc_meth_with_error(9)).toThrowError(/42n/);
         expect(() => tester.vfunc_meth_with_error(10)).toThrowError(/Symbol\("a symbol"\)/);
     });
 
diff --git a/meson.build b/meson.build
index bf767296..267ab9ad 100644
--- a/meson.build
+++ b/meson.build
@@ -122,7 +122,7 @@ gio = dependency('gio-2.0', version: glib_required_version,
 ffi = dependency('libffi', fallback: ['libffi', 'ffi_dep'])
 gi = dependency('gobject-introspection-1.0', version: '>= 1.66.0',
     fallback: ['gobject-introspection', 'girepo_dep'])
-spidermonkey = dependency('mozjs-78')
+spidermonkey = dependency('mozjs-78', version: '>= 78.2.0')
 
 # We might need to look for the headers and lib's for Cairo
 # manually on MSVC/clang-cl builds...
diff --git a/test/gjs-tests.cpp b/test/gjs-tests.cpp
index e49d5808..cdb24408 100644
--- a/test/gjs-tests.cpp
+++ b/test/gjs-tests.cpp
@@ -18,6 +18,7 @@
 #include <glib/gstdio.h>  // for g_unlink
 
 #include <js/Array.h>
+#include <js/BigInt.h>
 #include <js/CharacterEncoding.h>
 #include <js/Id.h>
 #include <js/RootingAPI.h>
@@ -27,6 +28,7 @@
 #include <js/ValueArray.h>
 #include <jsapi.h>
 #include <jspubtd.h>  // for JSProto_Number
+#include <mozilla/Span.h>  // for MakeStringSpan
 
 #include "gi/arg-inl.h"
 #include "gjs/context.h"
@@ -578,6 +580,35 @@ static void test_gjs_debug_string_quotes(GjsUnitTestFixture* fx, const void*) {
     g_assert_cmpstr(debug_output.c_str(), ==, "\"a string\"");
 }
 
+static void test_gjs_debug_value_bigint(GjsUnitTestFixture* fx, const void*) {
+    JS::BigInt* bi = JS::NumberToBigInt(fx->cx, 42);
+    JS::Value v = JS::BigIntValue(bi);
+    std::string debug_output = gjs_debug_value(v);
+
+    g_assert_cmpstr(debug_output.c_str(), ==, "42n");
+}
+
+static void test_gjs_debug_value_bigint_uint64(GjsUnitTestFixture* fx,
+                                               const void*) {
+    // gjs_debug_value(BigIntValue) prints whatever fits into int64_t, because
+    // more complicated operations might be fallible
+    JS::BigInt* bi = JS::NumberToBigInt(fx->cx, G_MAXUINT64);
+    JS::Value v = JS::BigIntValue(bi);
+    std::string debug_output = gjs_debug_value(v);
+
+    g_assert_cmpstr(debug_output.c_str(), ==, "-1n");
+}
+
+static void test_gjs_debug_value_bigint_huge(GjsUnitTestFixture* fx,
+                                             const void*) {
+    JS::BigInt* bi = JS::SimpleStringToBigInt(
+        fx->cx, mozilla::MakeStringSpan("10000000000000001"), 16);
+    JS::Value v = JS::BigIntValue(bi);
+    std::string debug_output = gjs_debug_value(v);
+
+    g_assert_cmpstr(debug_output.c_str(), ==, "1n");
+}
+
 static void test_gjs_debug_value_string_quotes(GjsUnitTestFixture* fx,
                                                const void*) {
     JS::RootedValue v(fx->cx);
@@ -956,6 +987,11 @@ main(int    argc,
     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/bigint", test_gjs_debug_value_bigint);
+    ADD_JSAPI_UTIL_TEST("debug_value/bigint/uint64",
+                        test_gjs_debug_value_bigint_uint64);
+    ADD_JSAPI_UTIL_TEST("debug_value/bigint/huge",
+                        test_gjs_debug_value_bigint_huge);
     ADD_JSAPI_UTIL_TEST("debug_value/string/quotes",
                         test_gjs_debug_value_string_quotes);
 


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