[gjs/native-registry: 71/71] Add native registry for GI modules.
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs/native-registry: 71/71] Add native registry for GI modules.
- Date: Sat, 8 Aug 2020 21:33:51 +0000 (UTC)
commit ab91a602a2db8dc9e25dcc6b436908ddbc4661b7
Author: Evan Welsh <noreply evanwelsh com>
Date: Tue Jun 16 16:25:28 2020 -0500
Add native registry for GI modules.
gi/repo.cpp | 50 ++++++++++++++++++++++++++++++++++----------------
gjs/global.cpp | 32 ++++++++++++++++++++++++++++----
gjs/global.h | 1 +
gjs/importer.cpp | 19 ++++++++++++++++++-
gjs/module.cpp | 13 ++++++++++++-
gjs/module.h | 45 +++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 138 insertions(+), 22 deletions(-)
---
diff --git a/gi/repo.cpp b/gi/repo.cpp
index 18f04568..f14e1266 100644
--- a/gi/repo.cpp
+++ b/gi/repo.cpp
@@ -31,6 +31,7 @@
#include <glib.h>
#include <js/Class.h>
+#include <js/GCHashTable.h> // for GCHashMap
#include <js/Id.h> // for JSID_IS_STRING, JSID_VOID
#include <js/PropertyDescriptor.h> // for JSPROP_PERMANENT, JSPROP_RESOLVING
#include <js/PropertySpec.h>
@@ -41,6 +42,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"
@@ -59,6 +62,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 {
@@ -637,37 +641,51 @@ 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) {
+ auto native_registry = gjs_get_native_module_registry(context);
+ 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 850df908..4679085e 100644
--- a/gjs/global.cpp
+++ b/gjs/global.cpp
@@ -50,6 +50,7 @@
#include "gjs/engine.h"
#include "gjs/global.h"
#include "gjs/jsapi-util.h"
+#include "gjs/module.h"
#include "gjs/native.h"
namespace mozilla {
@@ -158,13 +159,32 @@ class GjsBaseGlobal {
const JSClassOps defaultclassops = JS::DefaultGlobalClassOps;
class GjsGlobal : GjsBaseGlobal {
+ static void finalize(JSFreeOp* op G_GNUC_UNUSED, JSObject* obj) {
+ delete static_cast<GjsModuleRegistry*>(
+ gjs_get_global_slot(obj, GjsGlobalSlot::NATIVE_REGISTRY)
+ .toPrivate());
+ }
+
+ 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
@@ -202,6 +222,9 @@ class GjsGlobal : GjsBaseGlobal {
// const_cast is allowed here if we never free the realm data
JS::SetRealmPrivate(realm, const_cast<char*>(realm_name));
+ gjs_set_global_slot(global, GjsGlobalSlot::NATIVE_REGISTRY,
+ JS::PrivateValue(new GjsModuleRegistry()));
+
JS::Value v_importer =
gjs_get_global_slot(global, GjsGlobalSlot::IMPORTS);
g_assert(((void) "importer should be defined before passing null "
@@ -302,7 +325,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.");
@@ -378,6 +401,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 6f19023d..c6076a67 100644
--- a/gjs/global.h
+++ b/gjs/global.h
@@ -51,6 +51,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 a013315a..968243a4 100644
--- a/gjs/importer.cpp
+++ b/gjs/importer.cpp
@@ -31,6 +31,7 @@
#endif
#include <string>
+#include <utility> // for move
#include <vector> // for vector
#include <gio/gio.h>
@@ -40,6 +41,7 @@
#include <js/CallArgs.h>
#include <js/CharacterEncoding.h>
#include <js/Class.h>
+#include <js/GCHashTable.h>
#include <js/Id.h> // for PropertyKey, JSID_IS_STRING
#include <js/PropertyDescriptor.h>
#include <js/PropertySpec.h>
@@ -50,6 +52,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 <mozilla/Vector.h>
@@ -298,10 +301,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 dfb7d94d..7cf76bc9 100644
--- a/gjs/module.cpp
+++ b/gjs/module.cpp
@@ -1,6 +1,7 @@
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
/*
* Copyright (c) 2017 Philip Chimento <philip chimento gmail com>
+ * Copyright (c) 2020 Evan Welsh <contact evanwelsh com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
@@ -39,9 +40,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"
@@ -50,7 +53,7 @@
class GjsScriptModule {
char *m_name;
- GjsScriptModule(const char* name) {
+ explicit GjsScriptModule(const char* name) {
m_name = g_strdup(name);
GJS_INC_COUNTER(module);
}
@@ -263,3 +266,11 @@ gjs_module_import(JSContext *cx,
decltype(GjsScriptModule::klass) constexpr GjsScriptModule::klass;
decltype(GjsScriptModule::class_ops) constexpr GjsScriptModule::class_ops;
+
+GjsModuleRegistry* 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);
+
+ return static_cast<GjsModuleRegistry*>(native_registry.toPrivate());
+}
diff --git a/gjs/module.h b/gjs/module.h
index c63d6852..0919cfa4 100644
--- a/gjs/module.h
+++ b/gjs/module.h
@@ -1,6 +1,7 @@
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
/*
* Copyright (c) 2017 Philip Chimento <philip chimento gmail com>
+ * Copyright (c) 2020 Evan Welsh <contact evanwelsh com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
@@ -28,10 +29,51 @@
#include <gio/gio.h>
+#include <js/GCHashTable.h>
+#include <js/HashTable.h>
#include <js/TypeDecls.h>
+#include <string>
+
#include "gjs/macros.h"
+namespace JS {
+template <typename T>
+struct GCPolicy;
+template <typename T>
+class Heap;
+} // namespace JS
+
+namespace js {
+class SystemAllocPolicy;
+}
+
+class CppStringHashPolicy {
+ public:
+ typedef std::string Lookup;
+
+ static js::HashNumber hash(const Lookup& l) {
+ return std::hash<std::string>{}(std::string(l));
+ }
+
+ static bool match(const std::string& k, const Lookup& l) {
+ return k.compare(l) == 0;
+ }
+
+ static void rekey(std::string* k, const std::string& newKey) {
+ *k = newKey;
+ }
+};
+
+namespace JS {
+template <>
+struct GCPolicy<std::string> : public IgnoreGCPolicy<std::string> {};
+} // namespace JS
+
+using GjsModuleRegistry =
+ JS::GCHashMap<std::string, JS::Heap<JSObject*>, CppStringHashPolicy,
+ js::SystemAllocPolicy>;
+
GJS_JSAPI_RETURN_CONVENTION
JSObject *
gjs_module_import(JSContext *cx,
@@ -40,4 +82,7 @@ gjs_module_import(JSContext *cx,
const char *name,
GFile *file);
+GJS_JSAPI_RETURN_CONVENTION
+GjsModuleRegistry* gjs_get_native_module_registry(JSContext* js_context);
+
#endif // GJS_MODULE_H_
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]