[gjs: 1/2] native: Convert to singleton class




commit 07d452ba0dcb1d9b63795ef561f830b21147a25c
Author: Nasah Kuma <nasahnash19 gmail com>
Date:   Thu Feb 24 23:37:14 2022 +0000

    native: Convert to singleton class
    
    Enhancement: Converts the code in gjs/native.cpp into a class with the singleton pattern.

 gjs/context.cpp     | 15 +++++++--------
 gjs/global.cpp      |  3 ++-
 gjs/importer.cpp    | 10 ++++++----
 gjs/module.cpp      |  2 +-
 gjs/native.cpp      | 35 +++++++++++++----------------------
 gjs/native.h        | 37 +++++++++++++++++++++++++------------
 modules/modules.cpp | 13 ++++++-------
 7 files changed, 60 insertions(+), 55 deletions(-)
---
diff --git a/gjs/context.cpp b/gjs/context.cpp
index 12eb0921c..b1cb934bc 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -126,6 +126,8 @@ struct _GjsContextClass {
 
 G_DEFINE_TYPE_WITH_PRIVATE(GjsContext, gjs_context, G_TYPE_OBJECT);
 
+Gjs::NativeModuleRegistry& registry = Gjs::NativeModuleRegistry::get();
+
 GjsContextPrivate* GjsContextPrivate::from_object(GObject* js_context) {
     g_return_val_if_fail(GJS_IS_CONTEXT(js_context), nullptr);
     return static_cast<GjsContextPrivate*>(
@@ -333,14 +335,11 @@ gjs_context_class_init(GjsContextClass *klass)
 #endif
         g_irepository_prepend_search_path(priv_typelib_dir);
     }
-
-    gjs_register_native_module("_promiseNative",
-                               gjs_define_native_promise_stuff);
-    gjs_register_native_module("_byteArrayNative", gjs_define_byte_array_stuff);
-    gjs_register_native_module("_encodingNative",
-                               gjs_define_text_encoding_stuff);
-    gjs_register_native_module("_gi", gjs_define_private_gi_stuff);
-    gjs_register_native_module("gi", gjs_define_repo);
+    registry.add("_promiseNative", gjs_define_native_promise_stuff);
+    registry.add("_byteArrayNative", gjs_define_byte_array_stuff);
+    registry.add("_encodingNative", gjs_define_text_encoding_stuff);
+    registry.add("_gi", gjs_define_private_gi_stuff);
+    registry.add("gi", gjs_define_repo);
 
     gjs_register_static_modules();
 }
diff --git a/gjs/global.cpp b/gjs/global.cpp
index 3ed6ac5f3..56fe8602b 100644
--- a/gjs/global.cpp
+++ b/gjs/global.cpp
@@ -132,7 +132,8 @@ class GjsBaseGlobal {
 
         JS::RootedObject native_obj(m_cx);
 
-        if (!gjs_load_native_module(m_cx, id.get(), &native_obj)) {
+        if (!Gjs::NativeModuleRegistry::get().load(m_cx, id.get(),
+                                                   &native_obj)) {
             gjs_throw(m_cx, "Failed to load native module: %s", id.get());
             return false;
         }
diff --git a/gjs/importer.cpp b/gjs/importer.cpp
index 273ab1bc2..c8bc553ef 100644
--- a/gjs/importer.cpp
+++ b/gjs/importer.cpp
@@ -264,7 +264,7 @@ cancel_import(JSContext       *context,
  * @cx: the #JSContext
  * @importer: the root importer
  * @parse_name: Name under which the module was registered with
- *  gjs_register_native_module(), should be in the format as returned by
+ *  add(), should be in the format as returned by
  *  g_file_get_parse_name()
  *
  * Imports a builtin native-code module so that it is available to JS code as
@@ -290,8 +290,9 @@ gjs_import_native_module(JSContext       *cx,
     if (!gjs_global_registry_get(cx, native_registry, id, &module))
         return false;
 
-    if (!module && (!gjs_load_native_module(cx, parse_name, &module) ||
-                    !gjs_global_registry_set(cx, native_registry, id, module)))
+    if (!module &&
+        (!Gjs::NativeModuleRegistry::get().load(cx, parse_name, &module) ||
+         !gjs_global_registry_set(cx, native_registry, id, module)))
         return false;
 
     return define_meta_properties(cx, module, nullptr, parse_name, importer) &&
@@ -493,7 +494,8 @@ static bool do_import(JSContext* context, JS::HandleObject obj,
         return false;
 
     /* First try importing an internal module like gi */
-    if (parent.isNull() && gjs_is_registered_native_module(name.get())) {
+    if (parent.isNull() &&
+        Gjs::NativeModuleRegistry::get().is_registered(name.get())) {
         if (!gjs_import_native_module(context, obj, name.get()))
             return false;
 
diff --git a/gjs/module.cpp b/gjs/module.cpp
index 115444830..d46a02c69 100644
--- a/gjs/module.cpp
+++ b/gjs/module.cpp
@@ -436,7 +436,7 @@ static bool import_native_module_sync(JSContext* cx, unsigned argc,
     }
 
     JS::RootedObject native_obj(cx);
-    if (!gjs_load_native_module(cx, id.get(), &native_obj)) {
+    if (!Gjs::NativeModuleRegistry::get().load(cx, id.get(), &native_obj)) {
         gjs_throw(cx, "Failed to load native module: %s", id.get());
         return false;
     }
diff --git a/gjs/native.cpp b/gjs/native.cpp
index 1bdea30ee..d62302321 100644
--- a/gjs/native.cpp
+++ b/gjs/native.cpp
@@ -18,14 +18,10 @@
 #include "gjs/native.h"
 #include "util/log.h"
 
-static std::unordered_map<std::string, GjsDefineModuleFunc> modules;
-
-void
-gjs_register_native_module (const char          *module_id,
-                            GjsDefineModuleFunc  func)
-{
+void Gjs::NativeModuleRegistry::add(const char* module_id,
+                                    GjsDefineModuleFunc func) {
     bool inserted;
-    std::tie(std::ignore, inserted) = modules.insert({module_id, func});
+    std::tie(std::ignore, inserted) = m_modules.insert({module_id, func});
     if (!inserted) {
         g_warning("A second native module tried to register the same id '%s'",
                   module_id);
@@ -38,22 +34,22 @@ gjs_register_native_module (const char          *module_id,
 }
 
 /**
- * gjs_is_registered_native_module:
+ * is_registered:
  * @name: name of the module
  *
  * Checks if a native module corresponding to @name has already
  * been registered. This is used to check to see if a name is a
  * builtin module without starting to try and load it.
  */
-bool gjs_is_registered_native_module(const char* name) {
-    return modules.count(name) > 0;
+bool Gjs::NativeModuleRegistry::is_registered(const char* name) const {
+    return m_modules.count(name) > 0;
 }
 
 /**
- * gjs_load_native_module:
+ * gjs_load:
  * @context: the #JSContext
  * @parse_name: Name under which the module was registered with
- *  gjs_register_native_module(), should be in the format as returned by
+ *  add(), should be in the format as returned by
  *  g_file_get_parse_name()
  * @module_out: Return location for a #JSObject
  *
@@ -61,18 +57,13 @@ bool gjs_is_registered_native_module(const char* name) {
  *
  * Returns: true on success, false if an exception was thrown.
  */
-bool
-gjs_load_native_module(JSContext              *context,
-                       const char             *parse_name,
-                       JS::MutableHandleObject module_out)
-{
-    gjs_debug(GJS_DEBUG_NATIVE,
-              "Defining native module '%s'",
-              parse_name);
+bool Gjs::NativeModuleRegistry::load(JSContext* context, const char* parse_name,
+                                     JS::MutableHandleObject module_out) {
+    gjs_debug(GJS_DEBUG_NATIVE, "Defining native module '%s'", parse_name);
 
-    const auto& iter = modules.find(parse_name);
+    const auto& iter = m_modules.find(parse_name);
 
-    if (iter == modules.end()) {
+    if (iter == m_modules.end()) {
         gjs_throw(context,
                   "No native module '%s' has registered itself",
                   parse_name);
diff --git a/gjs/native.h b/gjs/native.h
index 227c6abd0..8943547f2 100644
--- a/gjs/native.h
+++ b/gjs/native.h
@@ -6,26 +6,39 @@
 #define GJS_NATIVE_H_
 
 #include <config.h>
+#include <string>
+#include <unordered_map>
 
 #include <js/RootingAPI.h>
 #include <js/TypeDecls.h>
 
 #include "gjs/macros.h"
 
-typedef bool (* GjsDefineModuleFunc) (JSContext              *context,
-                                      JS::MutableHandleObject module_out);
+namespace Gjs {
+class NativeModuleRegistry {
+    NativeModuleRegistry() {}
+    typedef bool (*GjsDefineModuleFunc)(JSContext* context,
+                                        JS::MutableHandleObject module_out);
 
-/* called on context init */
-void   gjs_register_native_module (const char            *module_id,
-                                   GjsDefineModuleFunc  func);
+    std::unordered_map<std::string, GjsDefineModuleFunc> m_modules;
 
-/* called by importer.c to to check for already loaded modules */
-[[nodiscard]] bool gjs_is_registered_native_module(const char* name);
+ public:
+    static NativeModuleRegistry& get() {
+        static NativeModuleRegistry the_singleton;
+        return the_singleton;
+    }
 
-/* called by importer.cpp to load a statically linked native module */
-GJS_JSAPI_RETURN_CONVENTION
-bool gjs_load_native_module(JSContext              *cx,
-                            const char             *name,
-                            JS::MutableHandleObject module_out);
+    /* called on context init */
+    void add(const char* module_id, GjsDefineModuleFunc func);
+
+    /* called by importer.c to to check for already loaded modules */
+    [[nodiscard]] bool is_registered(const char* name) const;
+
+    /* called by importer.cpp to load a statically linked native module */
+    GJS_JSAPI_RETURN_CONVENTION
+    bool load(JSContext* cx, const char* name,
+              JS::MutableHandleObject module_out);
+};
+};  // namespace Gjs
 
 #endif  // GJS_NATIVE_H_
diff --git a/modules/modules.cpp b/modules/modules.cpp
index c9b7061d4..987aa51fb 100644
--- a/modules/modules.cpp
+++ b/modules/modules.cpp
@@ -14,13 +14,12 @@
 #    include "modules/cairo-module.h"
 #endif
 
-void
-gjs_register_static_modules (void)
-{
+void gjs_register_static_modules(void) {
+    Gjs::NativeModuleRegistry& registry = Gjs::NativeModuleRegistry::get();
 #ifdef ENABLE_CAIRO
-    gjs_register_native_module("cairoNative", gjs_js_define_cairo_stuff);
+    registry.add("cairoNative", gjs_js_define_cairo_stuff);
 #endif
-    gjs_register_native_module("system", gjs_js_define_system_stuff);
-    gjs_register_native_module("console", gjs_define_console_stuff);
-    gjs_register_native_module("_print", gjs_define_print_stuff);
+    registry.add("system", gjs_js_define_system_stuff);
+    registry.add("console", gjs_define_console_stuff);
+    registry.add("_print", gjs_define_print_stuff);
 }


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