[gjs/ewlsh/register-type: 68/69] gi: Use internal symbol for GType instead of




commit 698784f44c0ee9960b97fcfede6f4a4bae076fec
Author: Evan Welsh <contact evanwelsh com>
Date:   Sat Sep 11 15:21:16 2021 -0700

    gi: Use internal symbol for GType instead of

 gi/cwrapper.cpp |  8 ++++++++
 gi/cwrapper.h   |  6 ++++--
 gi/gtype.cpp    | 11 ++++++++---
 gi/object.cpp   |  4 ++--
 gjs/atoms.h     |  1 +
 5 files changed, 23 insertions(+), 7 deletions(-)
---
diff --git a/gi/cwrapper.cpp b/gi/cwrapper.cpp
index f5fbff8a..4fbe1954 100644
--- a/gi/cwrapper.cpp
+++ b/gi/cwrapper.cpp
@@ -23,6 +23,14 @@ bool gjs_wrapper_define_gtype_prop(JSContext* cx, JS::HandleObject constructor,
         return false;
 
     const GjsAtoms& atoms = GjsContextPrivate::atoms(cx);
+
+    // Define the GType on the constructor using an internal
+    // symbol.
+    if (!JS_DefinePropertyById(cx, constructor, atoms.gobject_type(), gtype_obj,
+                               JSPROP_PERMANENT))
+        return false;
+
+    // Define the public-facing $gtype property.
     return JS_DefinePropertyById(cx, constructor, atoms.gtype(), gtype_obj,
                                  JSPROP_PERMANENT);
 }
diff --git a/gi/cwrapper.h b/gi/cwrapper.h
index 436eefd2..d5fbd6f9 100644
--- a/gi/cwrapper.h
+++ b/gi/cwrapper.h
@@ -290,8 +290,10 @@ class CWrapper : public CWrapperPointerOps<Base, Wrapped> {
      * CWrapper::define_gtype_prop:
      *
      * This function can be used as the finishInit member of class_ops. It
-     * defines a '$gtype' property on the constructor. If you use it, you must
-     * implement a gtype() static method that returns the GType to define.
+     * defines two properties on the constructor. One using the internal
+     * gtype symbol and a public-facing property, '$gtype'. If you use it,
+     * you must implement a gtype() static method that returns the GType to
+     * define.
      */
     GJS_JSAPI_RETURN_CONVENTION
     static bool define_gtype_prop(JSContext* cx, JS::HandleObject ctor,
diff --git a/gi/gtype.cpp b/gi/gtype.cpp
index 7ccb8099..055b245b 100644
--- a/gi/gtype.cpp
+++ b/gi/gtype.cpp
@@ -125,9 +125,14 @@ class GTypeObj : public CWrapper<GTypeObj, void> {
 
         JS::RootedValue v_gtype(cx);
 
-        // OK, we don't have a GType wrapper object -- grab the "$gtype"
-        // property on that and hope it's a GType wrapper object
-        if (!JS_GetPropertyById(cx, object, atoms.gtype(), &v_gtype))
+        // If the object is not a GType wrapper object, grab the gtype
+        // symbol property, this symbol should always point to a GType
+        // wrapper object.
+        if (!JS_GetPropertyById(cx, object, atoms.gobject_type(), &v_gtype))
+            return false;
+        // Fallback to the public-facing $gtype...
+        if (!v_gtype.isObject() &&
+            !JS_GetPropertyById(cx, object, atoms.gtype(), &v_gtype))
             return false;
         if (!v_gtype.isObject()) {
             // OK, so we're not a class. But maybe we're an instance. Check for
diff --git a/gi/object.cpp b/gi/object.cpp
index 0effe95f..1d91df5f 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -1833,8 +1833,8 @@ bool ObjectInstance::constructor_impl(JSContext* context,
 
     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))
+    if (!JS_HasOwnPropertyById(context, rooted_target,
+                               gjs->atoms().gobject_type(), &has_gtype))
         return false;
 
     if (!has_gtype) {
diff --git a/gjs/atoms.h b/gjs/atoms.h
index 2c6e7c6b..0d4cdc0f 100644
--- a/gjs/atoms.h
+++ b/gjs/atoms.h
@@ -75,6 +75,7 @@ class JSTracer;
     macro(zone, "zone")
 
 #define FOR_EACH_SYMBOL_ATOM(macro) \
+    macro(gobject_type, "__GObject__type") \
     macro(hook_up_vfunc, "__GObject__hook_up_vfunc") \
     macro(private_ns_marker, "__gjsPrivateNS") \
     macro(signal_find, "__GObject__signal_find") \


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