[gjs/wip/ptomato/mozjs38] WIP - early define props



commit 702303e590baa6f7afa2034434fd4c0458aa4cfe
Author: Philip Chimento <philip endlessm com>
Date:   Sat Jan 28 12:13:38 2017 -0800

    WIP - early define props

 gjs/context.cpp  |    1 -
 gjs/importer.cpp |   11 ++++++++++-
 gjs/jsapi-util.h |   54 +++++++++++++++++++++++++++++-------------------------
 3 files changed, 39 insertions(+), 27 deletions(-)
---
diff --git a/gjs/context.cpp b/gjs/context.cpp
index 1763dc8..eea35a3 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -372,7 +372,6 @@ gjs_context_class_init(GjsContextClass *klass)
 
     gjs_register_native_module("byteArray", gjs_define_byte_array_stuff);
     gjs_register_native_module("_gi", gjs_define_private_gi_stuff);
-    gjs_register_native_module("gi", gjs_define_repo);
 
     gjs_register_static_modules();
 }
diff --git a/gjs/importer.cpp b/gjs/importer.cpp
index ffef420..8f1515e 100644
--- a/gjs/importer.cpp
+++ b/gjs/importer.cpp
@@ -26,6 +26,7 @@
 #include <util/log.h>
 #include <util/glib.h>
 
+#include "gi/repo.h"
 #include "importer.h"
 #include "jsapi-wrapper.h"
 #include "mem.h"
@@ -1112,5 +1113,13 @@ gjs_define_root_importer(JSContext       *cx,
 {
     JS::Value importer = gjs_get_global_slot(cx, GJS_GLOBAL_SLOT_IMPORTS);
     JS::RootedObject rooted_importer(cx, &importer.toObject());
-    return gjs_define_root_importer_object(cx, in_object, rooted_importer);
+    if (!gjs_define_root_importer_object(cx, in_object, rooted_importer))
+        return false;
+
+    JS::RootedObject gi_repo(cx);
+    if (!gjs_define_repo(cx, &gi_repo))
+        return false;
+
+    return JS_DefineProperty(cx, rooted_importer, "gi", gi_repo,
+                             GJS_MODULE_PROP_FLAGS);
 }
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index e9e8d56..abce223 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -197,32 +197,36 @@ gjs_##cname##_create_proto(JSContext *context,                                 \
     JS::RootedObject global(context, gjs_get_import_global(context));          \
     JS::RootedId class_name(context,                                           \
         gjs_intern_string_to_id(context, gjs_##cname##_class.name));           \
-    if (!JS_GetPropertyById(context, global, class_name, &rval))               \
+    bool found = false;                                                        \
+    if (!JS_HasPropertyById(context, global, class_name, &found))              \
         return JS::NullValue(); \
-    if (rval.isUndefined()) { \
-        JS::RootedObject prototype(context,                                    \
-            JS_InitClass(context, global, parent, &gjs_##cname##_class, ctor,  \
-                         0, &gjs_##cname##_proto_props[0],                     \
-                         &gjs_##cname##_proto_funcs[0],                        \
-                         NULL, NULL));                                         \
-        if (prototype == NULL) { \
-            return JS::NullValue(); \
-        } \
-        if (!gjs_object_require_property( \
-                context, global, NULL, \
-                class_name, &rval)) { \
-            return JS::NullValue(); \
-        } \
-        if (!JS_DefineProperty(context, module, proto_name, \
-                               rval, GJS_MODULE_PROP_FLAGS))                   \
-            return JS::NullValue(); \
-        if (gtype != G_TYPE_NONE) { \
-            JS::RootedObject rval_obj(context, &rval.toObject());              \
-            JS::RootedObject gtype_obj(context,                                \
-                gjs_gtype_create_gtype_wrapper(context, gtype));               \
-            JS_DefineProperty(context, rval_obj, "$gtype", gtype_obj,          \
-                              JSPROP_PERMANENT);                               \
-        } \
+    if (found) {                                                               \
+        if (!JS_GetPropertyById(context, global, class_name, &rval))           \
+            return JS::NullValue();                                            \
+        return rval;                                                           \
+    }                                                                          \
+    JS::RootedObject prototype(context,                                        \
+        JS_InitClass(context, global, parent, &gjs_##cname##_class, ctor,      \
+                     0, &gjs_##cname##_proto_props[0],                         \
+                     &gjs_##cname##_proto_funcs[0],                            \
+                     NULL, NULL));                                             \
+    if (prototype == NULL) { \
+        return JS::NullValue(); \
+    } \
+    if (!gjs_object_require_property( \
+            context, global, NULL, \
+            class_name, &rval)) { \
+        return JS::NullValue(); \
+    } \
+    if (!JS_DefineProperty(context, module, proto_name, \
+                           rval, GJS_MODULE_PROP_FLAGS))                       \
+        return JS::NullValue(); \
+    if (gtype != G_TYPE_NONE) { \
+        JS::RootedObject rval_obj(context, &rval.toObject());                  \
+        JS::RootedObject gtype_obj(context,                                    \
+            gjs_gtype_create_gtype_wrapper(context, gtype));                   \
+        JS_DefineProperty(context, rval_obj, "$gtype", gtype_obj,              \
+                          JSPROP_PERMANENT);                                   \
     } \
     return rval; \
 }


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