[gjs/ewlsh/fix-enumerate] gi: Only enumerate properties which GJS defines




commit 85ae49ee699055ee7f56e7d1f7e5d35bd066f8c5
Author: Evan Welsh <contact evanwelsh com>
Date:   Sat Jul 17 15:08:35 2021 -0700

    gi: Only enumerate properties which GJS defines

 gi/ns.cpp                               | 39 +++++++++++++++++++++++++++++++++
 installed-tests/js/testIntrospection.js |  9 ++++++++
 2 files changed, 48 insertions(+)
---
diff --git a/gi/ns.cpp b/gi/ns.cpp
index 5a30ca73..1d875476 100644
--- a/gi/ns.cpp
+++ b/gi/ns.cpp
@@ -29,6 +29,41 @@
 #include "gjs/mem-private.h"
 #include "util/log.h"
 
+[[nodiscard]] static bool type_is_enumerable(GIInfoType info_type) {
+    bool is_enumerable = false;
+    switch (info_type) {
+        case GI_INFO_TYPE_BOXED:
+        case GI_INFO_TYPE_STRUCT:
+        case GI_INFO_TYPE_UNION:
+        case GI_INFO_TYPE_OBJECT:
+        case GI_INFO_TYPE_VFUNC:
+        case GI_INFO_TYPE_ENUM:
+        case GI_INFO_TYPE_FLAGS:
+        case GI_INFO_TYPE_INTERFACE:
+        case GI_INFO_TYPE_FUNCTION:
+        case GI_INFO_TYPE_CONSTANT:
+        case GI_INFO_TYPE_VALUE:
+            is_enumerable = true;
+            break;
+        // GJS does not expose properties or fields on Namespaces
+        case GI_INFO_TYPE_PROPERTY:
+        case GI_INFO_TYPE_FIELD:
+        // GJS does not represent callbacks, signals, arguments, or types.
+        case GI_INFO_TYPE_CALLBACK:
+        case GI_INFO_TYPE_SIGNAL:
+        case GI_INFO_TYPE_ARG:
+        case GI_INFO_TYPE_TYPE:
+        // Don't attempt to enumerate any invalid or unresolved types.
+        case GI_INFO_TYPE_INVALID:
+        case GI_INFO_TYPE_UNRESOLVED:
+        default:
+            is_enumerable = false;
+            break;
+    }
+
+    return is_enumerable;
+}
+
 class Ns : private GjsAutoChar, public CWrapper<Ns> {
     friend CWrapperPointerOps<Ns>;
     friend CWrapper<Ns>;
@@ -106,6 +141,10 @@ class Ns : private GjsAutoChar, public CWrapper<Ns> {
 
         for (int k = 0; k < n; k++) {
             GjsAutoBaseInfo info = g_irepository_get_info(nullptr, get(), k);
+            GIInfoType info_type = g_base_info_get_type(info);
+            if (!type_is_enumerable(info_type))
+                continue;
+
             const char* name = info.name();
 
             jsid id = gjs_intern_string_to_id(cx, name);
diff --git a/installed-tests/js/testIntrospection.js b/installed-tests/js/testIntrospection.js
index 5e2ee7df..19b89584 100644
--- a/installed-tests/js/testIntrospection.js
+++ b/installed-tests/js/testIntrospection.js
@@ -166,4 +166,13 @@ describe('Complete enumeration of GIRepositoryNamespace (new_enumerate)', functi
         const expectAtLeast = ['KEY_ybelowdot', 'EventSequence', 'ByteOrder', 'Window'];
         expect(names).toEqual(jasmine.arrayContaining(expectAtLeast));
     });
+
+    it('all enumerated properties are defined', function () {
+        const names = Object.keys(Gdk);
+        
+        expect(() => {
+            // Access each enumerated property to check it can be defined.
+            names.forEach((name) => Gdk[name]);
+        }).not.toThrowError(/API of type .* not implemented, cannot define .*/);
+    });
 });


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