[gjs: 10/15] js: Factor out definition of '$gtype' property



commit ad8a6925226c3fd81b3aaf24829e6bb2f25ab43c
Author: Philip Chimento <philip chimento gmail com>
Date:   Sat Dec 15 23:23:58 2018 -0800

    js: Factor out definition of '$gtype' property
    
    This factors out all code that defines a '$gtype' property on an object,
    into a new API in wrapperutils. One advantage is that we now use
    consistent flags when defining these '$gtype' properties.
    
    It's necessary to separate the $gtype define code out of
    gjs_define_enum_values(), as that is also called for GError objects
    which subsequently define another $gtype property over it. Due to
    changing the flags to include JSPROP_PERMANENT, that will no longer
    work.

 gi/boxed.cpp        |  9 +--------
 gi/enumeration.cpp  | 18 ++++++------------
 gi/fundamental.cpp  | 14 +++-----------
 gi/interface.cpp    |  9 +--------
 gi/object.cpp       |  8 +-------
 gi/param.cpp        |  7 +------
 gi/union.cpp        |  9 +--------
 gi/wrapperutils.cpp | 12 ++++++++++++
 gi/wrapperutils.h   |  5 +++++
 gjs/jsapi-class.h   |  8 ++------
 10 files changed, 33 insertions(+), 66 deletions(-)
---
diff --git a/gi/boxed.cpp b/gi/boxed.cpp
index d3fd4ca0..8338c8e5 100644
--- a/gi/boxed.cpp
+++ b/gi/boxed.cpp
@@ -1177,14 +1177,7 @@ bool gjs_define_boxed_class(JSContext* context, JS::HandleObject in_object,
                            0, GJS_MODULE_PROP_FLAGS))
         return false;
 
-    JS::RootedObject gtype_obj(context,
-        gjs_gtype_create_gtype_wrapper(context, priv->gtype));
-    if (!gtype_obj)
-        return false;
-
-    const GjsAtoms& atoms = GjsContextPrivate::atoms(context);
-    return JS_DefinePropertyById(context, constructor, atoms.gtype(), gtype_obj,
-                                 JSPROP_PERMANENT);
+    return gjs_wrapper_define_gtype_prop(context, constructor, priv->gtype);
 }
 
 JSObject* gjs_boxed_from_c_struct(JSContext* cx, GIStructInfo* info,
diff --git a/gi/enumeration.cpp b/gi/enumeration.cpp
index 4306bcfb..9b19b771 100644
--- a/gi/enumeration.cpp
+++ b/gi/enumeration.cpp
@@ -26,6 +26,7 @@
 #include <string.h>
 
 #include "function.h"
+#include "gi/wrapperutils.h"
 #include "gjs/context-private.h"
 #include "gjs/jsapi-wrapper.h"
 #include "gtype.h"
@@ -85,7 +86,6 @@ gjs_define_enum_values(JSContext       *context,
                        JS::HandleObject in_object,
                        GIEnumInfo      *info)
 {
-    GType gtype;
     int i, n_values;
 
     /* Fill in enum values first, so we don't define the enum itself until we're
@@ -104,16 +104,7 @@ gjs_define_enum_values(JSContext       *context,
             return false;
         }
     }
-
-    gtype = g_registered_type_info_get_g_type((GIRegisteredTypeInfo*)info);
-    JS::RootedObject gtype_obj(context,
-        gjs_gtype_create_gtype_wrapper(context, gtype));
-    if (!gtype_obj)
-        return false;
-
-    const GjsAtoms& atoms = GjsContextPrivate::atoms(context);
-    return JS_DefinePropertyById(context, in_object, atoms.gtype(), gtype_obj,
-                                 JSPROP_PERMANENT);
+    return true;
 }
 
 bool
@@ -174,8 +165,11 @@ gjs_define_enumeration(JSContext       *context,
         return false;
     }
 
+    GType gtype = g_registered_type_info_get_g_type(info);
+
     if (!gjs_define_enum_values(context, enum_obj, info) ||
-        !gjs_define_enum_static_methods(context, enum_obj, info))
+        !gjs_define_enum_static_methods(context, enum_obj, info) ||
+        !gjs_wrapper_define_gtype_prop(context, enum_obj, gtype))
         return false;
 
     gjs_debug(GJS_DEBUG_GENUM,
diff --git a/gi/fundamental.cpp b/gi/fundamental.cpp
index 49da7240..ee25a604 100644
--- a/gi/fundamental.cpp
+++ b/gi/fundamental.cpp
@@ -704,17 +704,9 @@ gjs_define_fundamental_class(JSContext              *context,
                   g_base_info_get_name ((GIBaseInfo *)priv->info));
     }
 
-    if (!gjs_object_define_static_methods(context, constructor, gtype, info))
-        return false;
-
-    JS::RootedObject gtype_obj(context,
-        gjs_gtype_create_gtype_wrapper(context, gtype));
-    if (!gtype_obj)
-        return false;
-
-    const GjsAtoms& atoms = GjsContextPrivate::atoms(context);
-    return JS_DefinePropertyById(context, constructor, atoms.gtype(), gtype_obj,
-                                 JSPROP_PERMANENT);
+    return gjs_object_define_static_methods(context, constructor, gtype,
+                                            info) &&
+           gjs_wrapper_define_gtype_prop(context, constructor, gtype);
 }
 
 JSObject*
diff --git a/gi/interface.cpp b/gi/interface.cpp
index a53d11d3..4618a7b1 100644
--- a/gi/interface.cpp
+++ b/gi/interface.cpp
@@ -257,14 +257,7 @@ gjs_define_interface_class(JSContext              *context,
             return false;
     }
 
-    JS::RootedObject gtype_obj(context,
-        gjs_gtype_create_gtype_wrapper(context, priv->gtype));
-    if (!gtype_obj)
-        return false;
-
-    const GjsAtoms& atoms = GjsContextPrivate::atoms(context);
-    return JS_DefinePropertyById(context, constructor, atoms.gtype(), gtype_obj,
-                                 JSPROP_PERMANENT);
+    return gjs_wrapper_define_gtype_prop(context, constructor, priv->gtype);
 }
 
 bool
diff --git a/gi/object.cpp b/gi/object.cpp
index f9d90f15..35a083fd 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -2266,13 +2266,7 @@ gjs_define_object_class(JSContext              *context,
         if (!gjs_object_define_static_methods(context, constructor, gtype, info))
             return false;
 
-    JS::RootedObject gtype_obj(context,
-        gjs_gtype_create_gtype_wrapper(context, gtype));
-    if (!gtype_obj)
-        return false;
-
-    return JS_DefinePropertyById(context, constructor, atoms.gtype(), gtype_obj,
-                                 JSPROP_PERMANENT);
+    return gjs_wrapper_define_gtype_prop(context, constructor, gtype);
 }
 
 JSObject*
diff --git a/gi/param.cpp b/gi/param.cpp
index 15b14f9c..7195d5a5 100644
--- a/gi/param.cpp
+++ b/gi/param.cpp
@@ -214,12 +214,7 @@ gjs_define_param_class(JSContext       *context,
                                 &constructor))
         return false;
 
-    JS::RootedObject gtype_obj(context,
-        gjs_gtype_create_gtype_wrapper(context, G_TYPE_PARAM));
-    const GjsAtoms& atoms = GjsContextPrivate::atoms(context);
-    if (!gtype_obj ||
-        !JS_DefinePropertyById(context, constructor, atoms.gtype(), gtype_obj,
-                               JSPROP_PERMANENT))
+    if (!gjs_wrapper_define_gtype_prop(context, constructor, G_TYPE_PARAM))
         return false;
 
     GjsAutoObjectInfo info = g_irepository_find_by_gtype(nullptr, G_TYPE_PARAM);
diff --git a/gi/union.cpp b/gi/union.cpp
index 6860d643..b8c1355d 100644
--- a/gi/union.cpp
+++ b/gi/union.cpp
@@ -359,14 +359,7 @@ gjs_define_union_class(JSContext       *context,
               constructor_name, prototype.get(), JS_GetClass(prototype),
               in_object.get());
 
-    const GjsAtoms& atoms = GjsContextPrivate::atoms(context);
-    JS::RootedObject gtype_obj(context,
-        gjs_gtype_create_gtype_wrapper(context, gtype));
-    if (!gtype_obj)
-        return false;
-
-    return JS_DefinePropertyById(context, constructor, atoms.gtype(), gtype_obj,
-                                 JSPROP_PERMANENT);
+    return gjs_wrapper_define_gtype_prop(context, constructor, gtype);
 }
 
 JSObject*
diff --git a/gi/wrapperutils.cpp b/gi/wrapperutils.cpp
index 4881a8e5..29e800de 100644
--- a/gi/wrapperutils.cpp
+++ b/gi/wrapperutils.cpp
@@ -26,6 +26,7 @@
 #include <string.h>
 
 #include "gi/wrapperutils.h"
+#include "gjs/context-private.h"
 
 /* Default spidermonkey toString is worthless.  Replace it
  * with something that gives us both the introspection name
@@ -82,3 +83,14 @@ bool gjs_wrapper_throw_readonly_field(JSContext* cx, GType gtype,
               field_name);
     return false;
 }
+
+bool gjs_wrapper_define_gtype_prop(JSContext* cx, JS::HandleObject constructor,
+                                   GType gtype) {
+    JS::RootedObject gtype_obj(cx, gjs_gtype_create_gtype_wrapper(cx, gtype));
+    if (!gtype_obj)
+        return false;
+
+    const GjsAtoms& atoms = GjsContextPrivate::atoms(cx);
+    return JS_DefinePropertyById(cx, constructor, atoms.gtype(), gtype_obj,
+                                 GJS_MODULE_PROP_FLAGS);
+}
diff --git a/gi/wrapperutils.h b/gi/wrapperutils.h
index b3ad5789..bddb8436 100644
--- a/gi/wrapperutils.h
+++ b/gi/wrapperutils.h
@@ -24,6 +24,7 @@
 #ifndef GI_WRAPPERUTILS_H_
 #define GI_WRAPPERUTILS_H_
 
+#include "gjs/context-private.h"
 #include "gjs/jsapi-util.h"
 #include "gjs/macros.h"
 
@@ -41,6 +42,10 @@ bool gjs_wrapper_throw_nonexistent_field(JSContext* cx, GType gtype,
 bool gjs_wrapper_throw_readonly_field(JSContext* cx, GType gtype,
                                       const char* field_name);
 
+GJS_JSAPI_RETURN_CONVENTION
+bool gjs_wrapper_define_gtype_prop(JSContext* cx, JS::HandleObject constructor,
+                                   GType gtype);
+
 G_END_DECLS
 
 #endif  // GI_WRAPPERUTILS_H_
diff --git a/gjs/jsapi-class.h b/gjs/jsapi-class.h
index bbd6ea03..d6fd812a 100644
--- a/gjs/jsapi-class.h
+++ b/gjs/jsapi-class.h
@@ -24,6 +24,7 @@
 #ifndef GJS_JSAPI_CLASS_H
 #define GJS_JSAPI_CLASS_H
 
+#include "gi/wrapperutils.h"
 #include "gjs/context-private.h"
 #include "global.h"
 #include "jsapi-util.h"
@@ -262,12 +263,7 @@ GJS_DEFINE_PROTO_FUNCS_WITH_PARENT(cname, no_parent)
                                                                                \
         /* Define the GType value as a "$gtype" property on the constructor */ \
         if (type != G_TYPE_NONE) {                                             \
-            const GjsAtoms& atoms = GjsContextPrivate::atoms(cx);              \
-            JS::RootedObject gtype_obj(                                        \
-                cx, gjs_gtype_create_gtype_wrapper(cx, type));                 \
-            if (!gtype_obj ||                                                  \
-                !JS_DefinePropertyById(cx, ctor_obj, atoms.gtype(), gtype_obj, \
-                                       JSPROP_PERMANENT))                      \
+            if (!gjs_wrapper_define_gtype_prop(cx, ctor_obj, type))            \
                 return false;                                                  \
         }                                                                      \
         gjs_debug(GJS_DEBUG_CONTEXT, "Initialized class %s prototype %p",      \


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