[gjs: 1/2] ns: Implement newEnumerate hook for namespaces



commit 89c8a8236d2d19aecae33eddf305b8ab18757171
Author: Ole Jørgen Brønner <olejorgenb yahoo no>
Date:   Sun Mar 8 19:30:08 2020 +0100

    ns: Implement newEnumerate hook for namespaces
    
    Before: Object.getOwnPropertyNames(import.gi.Gdk) did not returned names of
            members not yet accessed.

 gi/ns.cpp                               | 33 ++++++++++++++++++++++++++++++++-
 installed-tests/js/testIntrospection.js |  9 +++++++++
 2 files changed, 41 insertions(+), 1 deletion(-)
---
diff --git a/gi/ns.cpp b/gi/ns.cpp
index 7b692a57..1ceb610f 100644
--- a/gi/ns.cpp
+++ b/gi/ns.cpp
@@ -117,6 +117,37 @@ ns_resolve(JSContext       *context,
     return true;
 }
 
+GJS_JSAPI_RETURN_CONVENTION
+static bool
+ns_new_enumerate(JSContext* cx, JS::HandleObject obj,
+                 JS::MutableHandleIdVector properties,
+                 bool only_enumerable G_GNUC_UNUSED) {
+    Ns *priv = priv_from_js(cx, obj);
+
+    if (!priv) {
+        return true;
+    }
+
+    int n = g_irepository_get_n_infos(nullptr, priv->gi_namespace);
+    if (!properties.reserve(properties.length() + n)) {
+        JS_ReportOutOfMemory(cx);
+        return false;
+    }
+
+    for (int k = 0; k < n; k++) {
+        GjsAutoBaseInfo info =
+            g_irepository_get_info(nullptr, priv->gi_namespace, k);
+        const char *name = info.name();
+
+        jsid id = gjs_intern_string_to_id(cx, name);
+        if (id == JSID_VOID)
+            return false;
+        properties.infallibleAppend(id);
+    }
+
+    return true;
+}
+
 GJS_JSAPI_RETURN_CONVENTION
 static bool
 get_name (JSContext *context,
@@ -157,7 +188,7 @@ static const struct JSClassOps gjs_ns_class_ops = {
     nullptr,  // addProperty
     nullptr,  // deleteProperty
     nullptr,  // enumerate
-    nullptr,  // newEnumerate
+    ns_new_enumerate,
     ns_resolve,
     nullptr,  // mayResolve
     ns_finalize};
diff --git a/installed-tests/js/testIntrospection.js b/installed-tests/js/testIntrospection.js
index e4fba950..d50f1566 100644
--- a/installed-tests/js/testIntrospection.js
+++ b/installed-tests/js/testIntrospection.js
@@ -132,3 +132,12 @@ describe('Complete enumeration (boxed types)', function () {
         expect(names).toEqual(jasmine.arrayContaining(expectAtLeast));
     });
 });
+
+describe('Complete enumeration of GIRepositoryNamespace (new_enumerate)', function () {
+    it('enumerates all properties (sampled)', function () {
+        const names = Object.getOwnPropertyNames(Gdk);
+        // Note: properties which has been accessed are listed without new_enumerate hook
+        const expectAtLeast = ['KEY_ybelowdot', 'EventSequence', 'ByteOrder', 'Window'];
+        expect(names).toEqual(jasmine.arrayContaining(expectAtLeast));
+    });
+});


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