[gjs/gnome-3-34] object: Warn if constructing an unregistered object inheriting from GObject



commit 760690a08b3c23630a318f0fc4b71b138e0cec1d
Author: Philip Chimento <philip chimento gmail com>
Date:   Mon Nov 25 11:36:24 2019 -0800

    object: Warn if constructing an unregistered object inheriting from GObject
    
    This partially reverts commit 974560a251d2e1fb57bd8514879d6b61263f2167,
    which reverted commit 90da04977b3d121218de0cf051770c34685c0cd1.
    This brings back the check but as a warning instead of an exception.

 gi/object.cpp | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
---
diff --git a/gi/object.cpp b/gi/object.cpp
index 82d6e542..64d7f4c6 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -1528,6 +1528,22 @@ bool ObjectInstance::constructor_impl(JSContext* context,
                                       const JS::CallArgs& argv) {
     JS::RootedValue initer(context);
     GjsContextPrivate* gjs = GjsContextPrivate::from_cx(context);
+    const auto& new_target = argv.newTarget();
+    bool has_gtype;
+
+    g_assert(new_target.isObject() && "new.target needs to be an object");
+    JS::RootedObject rooted_target(context, &new_target.toObject());
+    if (!JS_HasOwnPropertyById(context, rooted_target, gjs->atoms().gtype(),
+                               &has_gtype))
+        return false;
+
+    if (!has_gtype) {
+        JS_ReportWarningASCII(
+            context,
+            "Tried to construct an object without a GType; are you using "
+            "GObject.registerClass() when inheriting from a GObject type?");
+    }
+
     return gjs_object_require_property(context, object, "GObject instance",
                                        gjs->atoms().init(), &initer) &&
            gjs->call_function(object, initer, argv, argv.rval());


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