[gjs/ewlsh/handle-native-enums] gi: Assume native enums are signed, avoid asserting.




commit 40d6c2af62520daa8fcd5c21d56a3519c1de0c83
Author: Evan Welsh <contact evanwelsh com>
Date:   Tue Aug 24 22:03:14 2021 -0700

    gi: Assume native enums are signed, avoid asserting.

 gi/value.cpp                            | 10 ++++++++--
 installed-tests/js/testGIMarshalling.js | 11 +++++++----
 2 files changed, 15 insertions(+), 6 deletions(-)
---
diff --git a/gi/value.cpp b/gi/value.cpp
index ec64f0ac..b58388d8 100644
--- a/gi/value.cpp
+++ b/gi/value.cpp
@@ -756,9 +756,15 @@ gjs_value_to_g_value_no_copy(JSContext      *context,
     } else {
         /* Need to distinguish between negative integers and unsigned integers */
         GjsAutoEnumInfo info = g_irepository_find_by_gtype(nullptr, gtype);
-        g_assert (info);
 
-        v_double = _gjs_enum_from_int(info, v);
+        // Native enums don't have type info, assume
+        // they are signed to avoid crashing when
+        // they are exposed to JS.
+        if (!info) {
+            v_double = int64_t(v);
+        } else {
+            v_double = _gjs_enum_from_int(info, v);
+        }
     }
 
     return JS::NumberValue(v_double);
diff --git a/installed-tests/js/testGIMarshalling.js b/installed-tests/js/testGIMarshalling.js
index 27e05c8a..999a1720 100644
--- a/installed-tests/js/testGIMarshalling.js
+++ b/installed-tests/js/testGIMarshalling.js
@@ -764,11 +764,14 @@ describe('GValue', function () {
         // See https://gitlab.gnome.org/GNOME/gjs/issues/80
     });
 
-    xit('enum can be passed into a function and packed', function () {
-        expect(() => GIMarshallingTests.gvalue_in_enum(GIMarshallingTests.Enum.VALUE3))
+    it('enum can be passed into a function and packed', function () {
+        const value = new GObject.Value();
+        // GIMarshallingTests.Enum is a native enum.
+        value.init(GObject.TYPE_ENUM);
+        value.set_enum(GIMarshallingTests.Enum.VALUE3);
+        expect(() => GIMarshallingTests.gvalue_in_enum(value))
             .not.toThrow();
-    }).pend("GJS doesn't support native enum types");
-
+    });
     xit('flags can be passed into a function and packed', function () {
         expect(() => GIMarshallingTests.gvalue_in_flags(GIMarshallingTests.Flags.VALUE3))
             .not.toThrow();


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