[gjs/ewlsh/chain-static: 6/6] Correctly chain constructor prototypes to enable static inheritance




commit f61a5613366fda1f9e04faf3e66501d60e4b91cf
Author: Evan Welsh <contact evanwelsh com>
Date:   Sun Sep 12 00:26:06 2021 -0700

    Correctly chain constructor prototypes to enable static inheritance
    
    In ES2015+ classes it is expected for constructors to inherit the
    static methods of their parent class, our implementation of
    classes previously did not do this as it was uncommon in ES5 era
    classes.

 gi/object.cpp | 29 +++++++++++++++++++++++++++--
 gi/object.h   |  3 +++
 2 files changed, 30 insertions(+), 2 deletions(-)
---
diff --git a/gi/object.cpp b/gi/object.cpp
index cc945d6e..49a1c60e 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -2383,6 +2383,20 @@ bool ObjectPrototype::get_parent_proto(JSContext* cx,
     return true;
 }
 
+bool ObjectPrototype::get_parent_constructor(
+    JSContext* cx, JS::MutableHandleObject constructor) const {
+    GType parent_type = g_type_parent(gtype());
+
+    if (parent_type != G_TYPE_INVALID) {
+        JS::RootedValue v_constructor(cx);
+        if (!gjs_lookup_object_constructor(cx, parent_type, &v_constructor))
+            return false;
+
+        constructor.set(&v_constructor.toObject());
+    }
+    return true;
+}
+
 /*
  * ObjectPrototype::define_class:
  * @in_object: Object where the constructor is stored, typically a repo object.
@@ -2401,9 +2415,20 @@ bool ObjectPrototype::define_class(JSContext* context,
                                    GIObjectInfo* info, GType gtype,
                                    JS::MutableHandleObject constructor,
                                    JS::MutableHandleObject prototype) {
-    if (!ObjectPrototype::create_class(context, in_object, info, gtype,
-                                       constructor, prototype))
+    ObjectPrototype* priv = ObjectPrototype::create_class(
+        context, in_object, info, gtype, constructor, prototype);
+    if (!priv)
+        return false;
+
+    JS::RootedObject parent_constructor(context);
+    if (!priv->get_parent_constructor(context, &parent_constructor))
         return false;
+    // If this is a fundamental constructor (e.g. GObject.Object) the
+    // parent constructor may be null.
+    if (parent_constructor) {
+        if (!JS_SetPrototype(context, constructor, parent_constructor))
+            return false;
+    }
 
     // hook_up_vfunc and the signal handler matcher functions can't be included
     // in gjs_object_instance_proto_funcs because they are custom symbols.
diff --git a/gi/object.h b/gi/object.h
index e988189c..044a3e23 100644
--- a/gi/object.h
+++ b/gi/object.h
@@ -211,6 +211,9 @@ class ObjectPrototype
  private:
     GJS_JSAPI_RETURN_CONVENTION
     bool get_parent_proto(JSContext* cx, JS::MutableHandleObject proto) const;
+    GJS_JSAPI_RETURN_CONVENTION
+    bool get_parent_constructor(JSContext* cx,
+                                JS::MutableHandleObject constructor) const;
 
     [[nodiscard]] bool is_vfunc_unchanged(GIVFuncInfo* info);
     static void vfunc_invalidated_notify(void* data, GClosure* closure);


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