[gjs: 6/10] arg: Fix returning false without throwing exception




commit d1ec79ea20669e131a5108e08dda1ed264e67080
Author: Philip Chimento <philip chimento gmail com>
Date:   Sat Feb 19 17:07:15 2022 -0800

    arg: Fix returning false without throwing exception
    
    This g_return_val_if_fail() was incorrect because if
    value_to_ghashtable_key() returns false, an exception must be pending.
    
    However, the condition never fails because object property keys can only
    be int32, string, or symbol; and JS_Enumerate() iterates over all
    enumerable non-symbol property keys. At this point it's only possible to
    have an int32 or string key, so change the check to an assertion, and add
    a marshalling test ensuring that symbol keys are ignored.

 gi/arg.cpp                              |  3 ++-
 installed-tests/js/testGIMarshalling.js | 11 +++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
---
diff --git a/gi/arg.cpp b/gi/arg.cpp
index 87eeebcea..4aa64927e 100644
--- a/gi/arg.cpp
+++ b/gi/arg.cpp
@@ -381,7 +381,8 @@ value_to_ghashtable_key(JSContext      *cx,
     GITypeTag type_tag = g_type_info_get_tag((GITypeInfo*) type_info);
     bool unsupported = false;
 
-    g_return_val_if_fail(value.isString() || value.isInt32(), false);
+    g_assert((value.isString() || value.isInt32()) &&
+             "keys from JS_Enumerate must be non-symbol property keys");
 
     gjs_debug_marshal(GJS_DEBUG_GFUNCTION,
                       "Converting JS::Value to GHashTable key %s",
diff --git a/installed-tests/js/testGIMarshalling.js b/installed-tests/js/testGIMarshalling.js
index 1f13b311f..797c05a0c 100644
--- a/installed-tests/js/testGIMarshalling.js
+++ b/installed-tests/js/testGIMarshalling.js
@@ -762,6 +762,17 @@ describe('GHashTable', function () {
         };
         testInParameter('ghashtable_uint64', uint64Dict);
     });
+
+    it('symbol keys are ignored', function () {
+        const symbolDict = {
+            [Symbol('foo')]: 2,
+            '-1': 1,
+            0: 0,
+            1: -1,
+            2: -2,
+        };
+        expect(() => GIMarshallingTests.ghashtable_int_none_in(symbolDict)).not.toThrow();
+    });
 });
 
 describe('GValue', function () {


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