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




commit d6dbd43b791d42ef045d569a18f87167219a2cce
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, 18 insertions(+), 3 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..ee3fe72d 100644
--- a/installed-tests/js/testGIMarshalling.js
+++ b/installed-tests/js/testGIMarshalling.js
@@ -767,7 +767,16 @@ describe('GValue', function () {
     xit('enum can be passed into a function and packed', function () {
         expect(() => GIMarshallingTests.gvalue_in_enum(GIMarshallingTests.Enum.VALUE3))
             .not.toThrow();
-    }).pend("GJS doesn't support native enum types");
+    }).pend("we don't know to pack enums in a GValue as enum and not int");
+
+    it('enum can be passed into a function as a boxed type 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();
+    });
 
     xit('flags can be passed into a function and packed', function () {
         expect(() => GIMarshallingTests.gvalue_in_flags(GIMarshallingTests.Flags.VALUE3))


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