[gjs/wip/inheritance-warning] WIP - exception for bad inheritance



commit 9335c6f4e117ab3296e4c58d2138f1fc2e2cf771
Author: Philip Chimento <philip endlessm com>
Date:   Tue Aug 27 15:03:28 2019 +0300

    WIP - exception for bad inheritance

 gi/object.cpp | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
---
diff --git a/gi/object.cpp b/gi/object.cpp
index a28f4979..0d2e4a2e 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -1528,6 +1528,21 @@ bool ObjectInstance::constructor_impl(JSContext* context,
                                       const JS::CallArgs& argv) {
     JS::RootedValue initer(context);
     GjsContextPrivate* gjs = GjsContextPrivate::from_cx(context);
+
+    g_assert(argv.newTarget().isObject() && "new.target needs to be an object");
+    JS::RootedObject new_target(context, &argv.newTarget().toObject());
+    bool has_gtype;
+    if (!JS_HasOwnPropertyById(context, new_target, gjs->atoms().gtype(),
+                               &has_gtype))
+        return false;
+    if (!has_gtype) {
+        gjs_throw(context,
+                  "Tried to construct an object without a GType; did you "
+                  "forget to use GObject.registerClass() when inheriting from "
+                  "a GObject type?");
+        return false;
+    }
+
     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]