[gjs/ewlsh/gobject-new] Add overrides for GObject.new and expose constructor lookups




commit f0e309a14d736c333a6418deb4939cbf977f0577
Author: Evan Welsh <contact evanwelsh com>
Date:   Wed Aug 25 09:12:48 2021 -0700

    Add overrides for GObject.new and expose constructor lookups
    
    Fixes #48

 gi/private.cpp                    | 23 +++++++++++++++++++++++
 modules/core/overrides/GObject.js | 26 ++++++++++++++++++++++++++
 2 files changed, 49 insertions(+)
---
diff --git a/gi/private.cpp b/gi/private.cpp
index a6ecc723..e0efceed 100644
--- a/gi/private.cpp
+++ b/gi/private.cpp
@@ -393,6 +393,28 @@ static bool gjs_signal_new(JSContext* cx, unsigned argc, JS::Value* vp) {
     return true;
 }
 
+GJS_JSAPI_RETURN_CONVENTION
+static bool gjs_lookup_constructor(JSContext* cx, unsigned argc,
+                                   JS::Value* vp) {
+    JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
+
+    JS::RootedObject gtype_obj(cx);
+    if (!gjs_parse_call_args(cx, "lookupConstructor", args, "o", "gtype",
+                             &gtype_obj))
+        return false;
+
+    GType gtype;
+    if (!gjs_gtype_get_actual_gtype(cx, gtype_obj, &gtype))
+        return false;
+
+    if (gtype == G_TYPE_NONE) {
+        gjs_throw(cx, "Invalid GType for constructor lookup");
+        return false;
+    }
+
+    return gjs_lookup_object_constructor(cx, gtype, args.rval());
+}
+
 template <GjsSymbolAtom GjsAtoms::*member>
 GJS_JSAPI_RETURN_CONVENTION static bool symbol_getter(JSContext* cx,
                                                       unsigned argc,
@@ -409,6 +431,7 @@ static JSFunctionSpec private_module_funcs[] = {
           GJS_MODULE_PROP_FLAGS),
     JS_FN("register_type", gjs_register_type, 4, GJS_MODULE_PROP_FLAGS),
     JS_FN("signal_new", gjs_signal_new, 6, GJS_MODULE_PROP_FLAGS),
+    JS_FN("lookupConstructor", gjs_lookup_constructor, 1, 0),
     JS_FS_END,
 };
 
diff --git a/modules/core/overrides/GObject.js b/modules/core/overrides/GObject.js
index 6bfaf144..ad0b222f 100644
--- a/modules/core/overrides/GObject.js
+++ b/modules/core/overrides/GObject.js
@@ -431,6 +431,32 @@ function _init() {
 
     GObject.registerClass = registerClass;
 
+    GObject.lookupConstructor = function (gtype) {
+        return Gi.lookupConstructor(gtype);
+    };
+
+    GObject.Object.new = function (gtype, ...args) {
+        const Constructor = Gi.lookupConstructor(gtype);
+
+        if (!Constructor)
+            throw new Error(`Constructor for gtype ${type_name} not found.`);
+        return new Constructor(...args);
+    };
+
+    GObject.Object.newv = function (gtype, args) {
+        if (!Array.isArray(args))
+            throw new Error('Arguments is not an array');
+        return GObject.Object.new(gtype, [...args]);
+    };
+
+    GObject.Object.new_from_type_name = function (type_name, ...args) {
+        const gtype = GObject.type_from_name(type_name);
+        if (!gtype)
+            throw new Error(`GType for name ${type_name} not found.`);
+
+        return GObject.Object.new(gtype, ...args);
+    };
+
     GObject.Object._classInit = function (klass) {
         let gtypename = _createGTypeName(klass);
         let gflags = klass.hasOwnProperty(GTypeFlags) ? klass[GTypeFlags] : 0;


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