[gjs/esm/static-imports: 264/268] Add native registry for GI modules.




commit 850da10bc520e19d29ec0c71a28d9682aff6353b
Author: Evan Welsh <noreply evanwelsh com>
Date:   Fri Oct 16 18:25:00 2020 -0500

    Add native registry for GI modules.

 gi/repo.cpp      | 52 ++++++++++++++++++++++++++++++++++++----------------
 gjs/global.cpp   | 36 ++++++++++++++++++++++++++++++++----
 gjs/global.h     |  1 +
 gjs/importer.cpp | 18 +++++++++++++++++-
 gjs/module.cpp   | 14 ++++++++++++++
 gjs/module.h     |  4 ++++
 6 files changed, 104 insertions(+), 21 deletions(-)
---
diff --git a/gi/repo.cpp b/gi/repo.cpp
index ed725ca6..ab48ece8 100644
--- a/gi/repo.cpp
+++ b/gi/repo.cpp
@@ -24,6 +24,8 @@
 #include <js/Warnings.h>
 #include <jsapi.h>  // for JS_DefinePropertyById, JS_GetProp...
 
+#include <mozilla/HashTable.h>
+
 #include "gi/arg.h"
 #include "gi/boxed.h"
 #include "gi/enumeration.h"
@@ -42,6 +44,7 @@
 #include "gjs/jsapi-class.h"
 #include "gjs/jsapi-util.h"
 #include "gjs/mem-private.h"
+#include "gjs/module.h"
 #include "util/log.h"
 
 typedef struct {
@@ -620,37 +623,54 @@ lookup_override_function(JSContext             *cx,
     }
     return true;
 
- fail:
+fail:
     saved_exc.drop();
     return false;
 }
 
-JSObject*
-gjs_lookup_namespace_object_by_name(JSContext      *context,
-                                    JS::HandleId    ns_name)
-{
-    JS::RootedObject global(context, gjs_get_import_global(context));
-    JS::RootedValue importer(
-        context, gjs_get_global_slot(global, GjsGlobalSlot::IMPORTS));
-    g_assert(importer.isObject());
-
-    JS::RootedObject repo(context), importer_obj(context, &importer.toObject());
-    const GjsAtoms& atoms = GjsContextPrivate::atoms(context);
-    if (!gjs_object_require_property(context, importer_obj, "importer",
-                                     atoms.gi(), &repo)) {
+GJS_JSAPI_RETURN_CONVENTION
+static JSObject* lookup_namespace(JSContext* context, JS::HandleId ns_name) {
+    JS::RootedObject native_registry(context,
+                                     gjs_get_native_module_registry(context));
+    JS::RootedValue it(context);
+    JS::MapGet(context, native_registry, );
+    auto it = native_registry->lookup("gi");
+    if (!it.found()) {
         gjs_log_exception(context);
-        gjs_throw(context, "No gi property in importer");
+        gjs_throw(context, "No gi property in native registry");
         return NULL;
     }
 
+    JS::RootedObject gi(context, it->value());
     JS::RootedObject retval(context);
-    if (!gjs_object_require_property(context, repo, "GI repository object",
+    if (!gjs_object_require_property(context, gi, "GI repository object",
                                      ns_name, &retval))
         return NULL;
 
     return retval;
 }
 
+JSObject* gjs_lookup_namespace_object_by_name(JSContext* context,
+                                              JS::HandleId ns_name) {
+    JSObject* global = JS::CurrentGlobalOrNull(context);
+    JSObject* ns = nullptr;
+
+    switch (gjs_global_get_type(global)) {
+        case GjsGlobalType::DEFAULT:
+            ns = lookup_namespace(context, ns_name);
+            break;
+        case GjsGlobalType::DEBUGGER:
+            ns = nullptr;
+            break;
+    }
+
+    if (!ns) {
+        return nullptr;
+    }
+
+    return ns;
+}
+
 const char*
 gjs_info_type_name(GIInfoType type)
 {
diff --git a/gjs/global.cpp b/gjs/global.cpp
index 4779e4c4..cf1d73bb 100644
--- a/gjs/global.cpp
+++ b/gjs/global.cpp
@@ -31,6 +31,7 @@
 #include "gjs/engine.h"
 #include "gjs/global.h"
 #include "gjs/jsapi-util.h"
+#include "gjs/module.h"
 #include "gjs/native.h"
 
 namespace mozilla {
@@ -135,13 +136,30 @@ class GjsBaseGlobal {
 const JSClassOps defaultclassops = JS::DefaultGlobalClassOps;
 
 class GjsGlobal : GjsBaseGlobal {
+    static void finalize(JSFreeOp* op G_GNUC_UNUSED, JSObject* obj) {
+        gjs_get_global_slot(obj, GjsGlobalSlot::NATIVE_REGISTRY).toObject());
+    }
+
+    static constexpr JSClassOps classops = {nullptr,  // addProperty
+                                            nullptr,  // deleteProperty
+                                            nullptr,  // enumerate
+                                            JS_NewEnumerateStandardClasses,
+                                            JS_ResolveStandardClass,
+                                            JS_MayResolveStandardClass,
+                                            GjsGlobal::finalize,
+                                            nullptr,  // call
+                                            nullptr,  // hasInstance
+                                            nullptr,  // construct
+                                            JS_GlobalObjectTraceHook};
+
     static constexpr JSClass klass = {
         // Jasmine depends on the class name "GjsGlobal" to detect GJS' global
         // object.
         "GjsGlobal",
-        JSCLASS_GLOBAL_FLAGS_WITH_SLOTS(
-            static_cast<uint32_t>(GjsGlobalSlot::LAST)),
-        &defaultclassops,
+        JSCLASS_FOREGROUND_FINALIZE |
+            JSCLASS_GLOBAL_FLAGS_WITH_SLOTS(
+                static_cast<uint32_t>(GjsGlobalSlot::LAST)),
+        &classops,
     };
 
     // clang-format off
@@ -179,6 +197,15 @@ class GjsGlobal : GjsBaseGlobal {
         // const_cast is allowed here if we never free the realm data
         JS::SetRealmPrivate(realm, const_cast<char*>(realm_name));
 
+        JSObject* registry = JS::NewMapObject(cx);
+
+        if (!registry) {
+            return false;
+        }
+
+        gjs_set_global_slot(global, GjsGlobalSlot::NATIVE_REGISTRY,
+                            JS::ObjectValue(*registry));
+
         JS::Value v_importer =
             gjs_get_global_slot(global, GjsGlobalSlot::IMPORTS);
         g_assert(((void) "importer should be defined before passing null "
@@ -281,7 +308,7 @@ JSObject* gjs_create_global_object(JSContext* cx, GjsGlobalType global_type,
 }
 
 GjsGlobalType gjs_global_get_type(JSContext* cx) {
-    auto global = JS::CurrentGlobalOrNull(cx);
+    JS::RootedObject global(cx, JS::CurrentGlobalOrNull(cx));
 
     g_assert(global &&
              "gjs_global_get_type called before a realm was entered.");
@@ -357,6 +384,7 @@ JS::Value detail::get_global_slot(JSObject* global, uint32_t slot) {
 }
 
 decltype(GjsGlobal::klass) constexpr GjsGlobal::klass;
+decltype(GjsGlobal::classops) constexpr GjsGlobal::classops;
 decltype(GjsGlobal::static_funcs) constexpr GjsGlobal::static_funcs;
 decltype(GjsGlobal::static_props) constexpr GjsGlobal::static_props;
 
diff --git a/gjs/global.h b/gjs/global.h
index 0ac56c51..6be2fe9e 100644
--- a/gjs/global.h
+++ b/gjs/global.h
@@ -32,6 +32,7 @@ enum class GjsDebuggerGlobalSlot : uint32_t {
 
 enum class GjsGlobalSlot : uint32_t {
     IMPORTS = static_cast<uint32_t>(GjsBaseGlobalSlot::LAST),
+    NATIVE_REGISTRY,
     PROTOTYPE_gtype,
     PROTOTYPE_importer,
     PROTOTYPE_function,
diff --git a/gjs/importer.cpp b/gjs/importer.cpp
index efa55261..62da17da 100644
--- a/gjs/importer.cpp
+++ b/gjs/importer.cpp
@@ -12,6 +12,7 @@
 #endif
 
 #include <string>
+#include <utility>  // for move
 #include <vector>   // for vector
 
 #include <gio/gio.h>
@@ -33,6 +34,7 @@
 #include <js/Value.h>
 #include <jsapi.h>    // for JS_DefinePropertyById, JS_DefineP...
 #include <jspubtd.h>  // for JSProto_Error
+#include <mozilla/HashTable.h>
 #include <mozilla/UniquePtr.h>
 
 #include "gjs/atoms.h"
@@ -280,10 +282,24 @@ gjs_import_native_module(JSContext       *cx,
 {
     gjs_debug(GJS_DEBUG_IMPORTER, "Importing '%s'", parse_name);
 
+    auto native_registry = gjs_get_native_module_registry(cx);
+    auto it = native_registry->lookupForAdd(parse_name);
+
     JS::RootedObject module(cx);
+
+    if (it.found()) {
+        module.set(it->value().get());
+        return define_meta_properties(cx, module, nullptr, parse_name,
+                                      importer) &&
+               JS_DefineProperty(cx, importer, parse_name, module,
+                                 GJS_MODULE_PROP_FLAGS);
+    }
+
     return gjs_load_native_module(cx, parse_name, &module) &&
+           native_registry->put(parse_name, module) &&
            define_meta_properties(cx, module, nullptr, parse_name, importer) &&
-           JS_DefineProperty(cx, importer, parse_name, module, GJS_MODULE_PROP_FLAGS);
+           JS_DefineProperty(cx, importer, parse_name, module,
+                             GJS_MODULE_PROP_FLAGS);
 }
 
 GJS_JSAPI_RETURN_CONVENTION
diff --git a/gjs/module.cpp b/gjs/module.cpp
index 0d637fd0..d58ffb5a 100644
--- a/gjs/module.cpp
+++ b/gjs/module.cpp
@@ -1,6 +1,7 @@
 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
 // SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
 // SPDX-FileCopyrightText: 2017 Philip Chimento <philip chimento gmail com>
+// SPDX-FileCopyrightText: 2020 Evan Welsh <contact evanwelsh com>
 
 #include <config.h>
 
@@ -20,9 +21,11 @@
 #include <js/RootingAPI.h>
 #include <js/SourceText.h>
 #include <js/TypeDecls.h>
+#include <js/Value.h>  // for Value
 #include <jsapi.h>  // for JS_DefinePropertyById, ...
 
 #include "gjs/context-private.h"
+#include "gjs/global.h"
 #include "gjs/jsapi-util.h"
 #include "gjs/mem-private.h"
 #include "gjs/module.h"
@@ -244,3 +247,14 @@ gjs_module_import(JSContext       *cx,
 
 decltype(GjsScriptModule::klass) constexpr GjsScriptModule::klass;
 decltype(GjsScriptModule::class_ops) constexpr GjsScriptModule::class_ops;
+
+GJS_USE
+JSObject* gjs_get_native_module_registry(JSContext* cx) {
+    JS::RootedObject global(cx, gjs_get_import_global(cx));
+    auto native_registry =
+        gjs_get_global_slot(global, GjsGlobalSlot::NATIVE_REGISTRY);
+
+    g_assert(native_registry.isObject());
+
+    return &native_registry.toObject();
+}
diff --git a/gjs/module.h b/gjs/module.h
index e366eaf8..de86a2e1 100644
--- a/gjs/module.h
+++ b/gjs/module.h
@@ -1,6 +1,7 @@
 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
 // SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
 // SPDX-FileCopyrightText: 2017 Philip Chimento <philip chimento gmail com>
+// SPDX-FileCopyrightText: 2020 Evan Welsh <contact evanwelsh com>
 
 #ifndef GJS_MODULE_H_
 #define GJS_MODULE_H_
@@ -21,4 +22,7 @@ gjs_module_import(JSContext       *cx,
                   const char      *name,
                   GFile           *file);
 
+GJS_USE
+JSObject* gjs_get_native_module_registry(JSContext* cx);
+
 #endif  // GJS_MODULE_H_


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