[gjs/refactor-globals: 3/5] global: Refactor to support multiple global types.
- From: Evan Welsh <ewlsh src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs/refactor-globals: 3/5] global: Refactor to support multiple global types.
- Date: Sat, 27 Jun 2020 02:05:23 +0000 (UTC)
commit 2d48f0d9be7952c86837914fe1e60d9dc82e82bd
Author: Evan Welsh <noreply evanwelsh com>
Date: Sat Jun 6 12:11:56 2020 -0500
global: Refactor to support multiple global types.
gi/repo.cpp | 12 ++-
gjs/context.cpp | 16 ++--
gjs/coverage.cpp | 5 +-
gjs/debugger.cpp | 6 +-
gjs/global.cpp | 222 +++++++++++++++++++++++++++++++++++++++++++-----------
gjs/global.h | 69 ++++++++++-------
gjs/importer.cpp | 3 +
gjs/jsapi-class.h | 15 +++-
gjs/module.cpp | 1 +
9 files changed, 260 insertions(+), 89 deletions(-)
---
diff --git a/gi/repo.cpp b/gi/repo.cpp
index 08df95e1..57458c69 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/RootingAPI.h>
@@ -40,6 +41,8 @@
#include <js/Warnings.h>
#include <jsapi.h> // for JS_DefinePropertyById, JS_GetProp...
+#include <mozilla/HashTable.h> // for HashTable<>::Ptr, HashMapEntry
+
#include "gi/arg.h"
#include "gi/boxed.h"
#include "gi/enumeration.h"
@@ -601,7 +604,9 @@ lookup_override_function(JSContext *cx,
{
JS::AutoSaveExceptionState saved_exc(cx);
- JS::RootedValue importer(cx, gjs_get_global_slot(cx, GJS_GLOBAL_SLOT_IMPORTS));
+ auto global = gjs_get_import_global(cx);
+ JS::RootedValue importer(
+ cx, gjs_get_global_slot(global, GjsGlobalSlot::IMPORTS));
g_assert(importer.isObject());
JS::RootedObject overridespkg(cx), module(cx);
@@ -644,8 +649,9 @@ JSObject*
gjs_lookup_namespace_object_by_name(JSContext *context,
JS::HandleId ns_name)
{
- JS::RootedValue importer(context,
- gjs_get_global_slot(context, GJS_GLOBAL_SLOT_IMPORTS));
+ auto global = 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());
diff --git a/gjs/context.cpp b/gjs/context.cpp
index 04f28f73..97f40cc2 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -494,7 +494,9 @@ GjsContextPrivate::GjsContextPrivate(JSContext* cx, GjsContext* public_context)
m_atoms = new GjsAtoms();
- JS::RootedObject global(m_cx, gjs_create_global_object(m_cx));
+ JS::RootedObject global(
+ m_cx, gjs_create_global_object(cx, GjsGlobalType::DEFAULT));
+
if (!global) {
gjs_log_exception(m_cx);
g_error("Failed to initialize global object");
@@ -509,24 +511,24 @@ GjsContextPrivate::GjsContextPrivate(JSContext* cx, GjsContext* public_context)
gjs_log_exception(m_cx);
g_error("Failed to initialize global strings");
}
-
std::vector<std::string> paths;
if (m_search_path)
paths = {m_search_path, m_search_path + g_strv_length(m_search_path)};
JS::RootedObject importer(m_cx, gjs_create_root_importer(m_cx, paths));
if (!importer) {
- gjs_log_exception(cx);
+ gjs_log_exception(m_cx);
g_error("Failed to create root importer");
}
- JS::Value v_importer = gjs_get_global_slot(m_cx, GJS_GLOBAL_SLOT_IMPORTS);
- g_assert(((void) "Someone else already created root importer",
+ JS::Value v_importer = gjs_get_global_slot(global, GjsGlobalSlot::IMPORTS);
+ g_assert(((void)"Someone else already created root importer",
v_importer.isUndefined()));
- gjs_set_global_slot(m_cx, GJS_GLOBAL_SLOT_IMPORTS,
+ gjs_set_global_slot(global, GjsGlobalSlot::IMPORTS,
JS::ObjectValue(*importer));
- if (!gjs_define_global_properties(m_cx, global, "GJS", "default")) {
+ if (!gjs_define_global_properties(m_cx, global, GjsGlobalType::DEFAULT,
+ "GJS", "default")) {
gjs_log_exception(m_cx);
g_error("Failed to define properties on global object");
}
diff --git a/gjs/coverage.cpp b/gjs/coverage.cpp
index cbeeafcb..12395078 100644
--- a/gjs/coverage.cpp
+++ b/gjs/coverage.cpp
@@ -350,8 +350,8 @@ bootstrap_coverage(GjsCoverage *coverage)
JSContext *context = (JSContext *) gjs_context_get_native_context(priv->context);
JSObject *debuggee = gjs_get_import_global(context);
- JS::RootedObject debugger_global(context,
- gjs_create_global_object(context));
+ JS::RootedObject debugger_global(
+ context, gjs_create_global_object(context, GjsGlobalType::DEBUGGER));
{
JSAutoRealm ar(context, debugger_global);
JS::RootedObject debuggeeWrapper(context, debuggee);
@@ -363,6 +363,7 @@ bootstrap_coverage(GjsCoverage *coverage)
if (!JS_SetPropertyById(context, debugger_global, atoms.debuggee(),
debuggeeWrapperValue) ||
!gjs_define_global_properties(context, debugger_global,
+ GjsGlobalType::DEBUGGER,
"GJS coverage", "coverage"))
return false;
diff --git a/gjs/debugger.cpp b/gjs/debugger.cpp
index 682ce73d..30b6f88b 100644
--- a/gjs/debugger.cpp
+++ b/gjs/debugger.cpp
@@ -135,7 +135,8 @@ void gjs_context_setup_debugger_console(GjsContext* gjs) {
auto cx = static_cast<JSContext*>(gjs_context_get_native_context(gjs));
JS::RootedObject debuggee(cx, gjs_get_import_global(cx));
- JS::RootedObject debugger_global(cx, gjs_create_global_object(cx));
+ JS::RootedObject debugger_global(
+ cx, gjs_create_global_object(cx, GjsGlobalType::DEBUGGER));
// Enter realm of the debugger and initialize it with the debuggee
JSAutoRealm ar(cx, debugger_global);
@@ -149,7 +150,8 @@ void gjs_context_setup_debugger_console(GjsContext* gjs) {
JS::RootedValue v_wrapper(cx, JS::ObjectValue(*debuggee_wrapper));
if (!JS_SetPropertyById(cx, debugger_global, atoms.debuggee(), v_wrapper) ||
!JS_DefineFunctions(cx, debugger_global, debugger_funcs) ||
- !gjs_define_global_properties(cx, debugger_global, "GJS debugger",
+ !gjs_define_global_properties(cx, debugger_global,
+ GjsGlobalType::DEBUGGER, "GJS debugger",
"debugger"))
gjs_log_exception(cx);
}
diff --git a/gjs/global.cpp b/gjs/global.cpp
index dd2ad2a9..b68a2461 100644
--- a/gjs/global.cpp
+++ b/gjs/global.cpp
@@ -3,6 +3,7 @@
* Copyright (c) 2008 litl, LLC
* Copyright (c) 2009 Red Hat, Inc.
* 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
@@ -45,16 +46,58 @@
#include <js/Utility.h> // for UniqueChars
#include <jsapi.h> // for AutoSaveExceptionState, ...
+
#include "gjs/atoms.h"
#include "gjs/context-private.h"
#include "gjs/engine.h"
#include "gjs/global.h"
#include "gjs/jsapi-util.h"
+#include "gjs/module.h"
namespace mozilla {
union Utf8Unit;
}
+inline JSObject* global_create(JSContext* cx, const JSClass* clasp,
+ JS::RealmCreationOptions options) {
+ options.setBigIntEnabled(true);
+ options.setFieldsEnabled(true);
+
+ JS::RealmBehaviors behaviors;
+ JS::RealmOptions compartment_options(options, behaviors);
+
+ JS::RootedObject global(
+ cx, JS_NewGlobalObject(cx, clasp, nullptr, JS::FireOnNewGlobalHook,
+ compartment_options));
+
+ if (!global)
+ return nullptr;
+
+ JSAutoRealm ac(cx, global);
+
+ if (!JS_InitReflectParse(cx, global) ||
+ !JS_DefineDebuggerObject(cx, global))
+ return nullptr;
+
+ return global;
+}
+
+inline JSObject* global_create_new(JSContext* cx, const JSClass* clasp) {
+ JS::RealmCreationOptions creation;
+ creation.setNewCompartmentAndZone();
+
+ return global_create(cx, clasp, creation);
+}
+
+inline JSObject* global_create_with_existing(JSContext* cx, JSObject* existing,
+ const JSClass* clasp) {
+ JS::RealmCreationOptions creation;
+ JS::RootedObject comp(cx, existing);
+ creation.setExistingCompartment(comp);
+
+ return global_create(cx, clasp, creation);
+}
+
GJS_JSAPI_RETURN_CONVENTION
static bool
run_bootstrap(JSContext *cx,
@@ -231,8 +274,11 @@ const JSClassOps defaultclassops = JS::DefaultGlobalClassOps;
class GjsGlobal {
static constexpr JSClass klass = {
+ // Keep this as "GjsGlobal" until Jasmine is upgraded to support
+ // globalThis
"GjsGlobal",
- JSCLASS_GLOBAL_FLAGS_WITH_SLOTS(GJS_GLOBAL_SLOT_LAST),
+ JSCLASS_HAS_PRIVATE | JSCLASS_GLOBAL_FLAGS_WITH_SLOTS(
+ static_cast<uint32_t>(GjsGlobalSlot::LAST)),
&defaultclassops,
};
@@ -245,28 +291,16 @@ class GjsGlobal {
public:
GJS_USE
- static JSObject *
- create(JSContext *cx)
- {
- JS::RealmBehaviors behaviors;
+ static JSObject* create(JSContext* cx) {
+ auto global = global_create_new(cx, &klass);
- JS::RealmCreationOptions creation;
- creation.setFieldsEnabled(true);
- creation.setBigIntEnabled(true);
-
- JS::RealmOptions options(creation, behaviors);
-
- JS::RootedObject global(
- cx, JS_NewGlobalObject(cx, &GjsGlobal::klass, nullptr,
- JS::FireOnNewGlobalHook, options));
- if (!global)
- return nullptr;
-
- JSAutoRealm ar(cx, global);
+ return global;
+ }
- if (!JS_InitReflectParse(cx, global) ||
- !JS_DefineDebuggerObject(cx, global))
- return nullptr;
+ GJS_USE
+ static JSObject* create_with_compartment(JSContext* cx,
+ JSObject* cmp_global) {
+ auto global = global_create_with_existing(cx, cmp_global, &klass);
return global;
}
@@ -286,9 +320,10 @@ class GjsGlobal {
// const_cast is allowed here if we never free the realm data
JS::SetRealmPrivate(realm, const_cast<char*>(realm_name));
- JS::Value v_importer = gjs_get_global_slot(cx, GJS_GLOBAL_SLOT_IMPORTS);
- g_assert(((void) "importer should be defined before passing null "
- "importer to GjsGlobal::define_properties",
+ JS::Value v_importer =
+ gjs_get_global_slot(global, GjsGlobalSlot::IMPORTS);
+ g_assert(((void)"importer should be defined before passing null "
+ "importer to GjsGlobal::define_properties",
v_importer.isObject()));
JS::RootedObject root_importer(cx, &v_importer.toObject());
@@ -307,6 +342,53 @@ class GjsGlobal {
}
};
+class GjsDebuggerGlobal {
+ static constexpr JSClass klass = {
+ "GjsDebuggerGlobal",
+ JSCLASS_GLOBAL_FLAGS_WITH_SLOTS(
+ static_cast<uint32_t>(GjsGlobalSlot::LAST)),
+ &defaultclassops,
+ };
+
+ static constexpr JSFunctionSpec static_funcs[] = {
+ JS_FN("logError", gjs_log_error, 2, GJS_MODULE_PROP_FLAGS),
+ JS_FN("print", gjs_print, 0, GJS_MODULE_PROP_FLAGS), JS_FS_END};
+
+ public:
+ GJS_USE
+ static JSObject* create(JSContext* cx) {
+ return global_create_new(cx, &klass);
+ }
+
+ GJS_USE
+ static JSObject* create_with_compartment(JSContext* cx,
+ JSObject* cmp_global) {
+ return global_create_with_existing(cx, cmp_global, &klass);
+ }
+
+ static bool define_properties(JSContext* cx, JS::HandleObject global,
+ const char* realm_name,
+ const char* bootstrap_script) {
+ const GjsAtoms& atoms = GjsContextPrivate::atoms(cx);
+ if (!JS_DefinePropertyById(cx, global, atoms.window(), global,
+ JSPROP_READONLY | JSPROP_PERMANENT) ||
+ !JS_DefineFunctions(cx, global, GjsDebuggerGlobal::static_funcs))
+ return false;
+
+ JS::Realm* realm = JS::GetObjectRealmOrNull(global);
+ g_assert(realm && "Global object must be associated with a realm");
+ // const_cast is allowed here if we never free the realm data
+ JS::SetRealmPrivate(realm, const_cast<char*>(realm_name));
+
+ if (bootstrap_script) {
+ if (!run_bootstrap(cx, bootstrap_script, global))
+ return false;
+ }
+
+ return true;
+ }
+};
+
/**
* gjs_create_global_object:
* @cx: a #JSContext
@@ -316,10 +398,50 @@ class GjsGlobal {
* Returns: the created global object on success, nullptr otherwise, in which
* case an exception is pending on @cx
*/
-JSObject *
-gjs_create_global_object(JSContext *cx)
-{
- return GjsGlobal::create(cx);
+JSObject* gjs_create_global_object(JSContext* cx, GjsGlobalType global_type,
+ JSObject* current_global) {
+ if (current_global) {
+ switch (global_type) {
+ case GjsGlobalType::DEFAULT:
+ return GjsGlobal::create_with_compartment(cx, current_global);
+ case GjsGlobalType::DEBUGGER:
+ return GjsDebuggerGlobal::create_with_compartment(
+ cx, current_global);
+ default:
+ return nullptr;
+ }
+ }
+
+ switch (global_type) {
+ case GjsGlobalType::DEFAULT:
+ return GjsGlobal::create(cx);
+ case GjsGlobalType::DEBUGGER:
+ return GjsDebuggerGlobal::create(cx);
+ default:
+ return nullptr;
+ }
+}
+
+GjsGlobalType gjs_global_get_type(JSContext* cx) {
+ auto global = JS::CurrentGlobalOrNull(cx);
+
+ g_assert(global && "gjs_global_get_type called when no global is present");
+
+ auto global_type = gjs_get_global_slot(global, GjsGlobalSlot::GLOBAL_TYPE);
+
+ g_assert(global_type.isInt32() &&
+ "Invalid type for GLOBAL_TYPE slot. Expected int32.");
+
+ return static_cast<GjsGlobalType>(global_type.toInt32());
+}
+
+GjsGlobalType gjs_global_get_type(JSObject* global) {
+ auto global_type = gjs_get_global_slot(global, GjsGlobalSlot::GLOBAL_TYPE);
+
+ g_assert(global_type.isInt32() &&
+ "Invalid type for GLOBAL_TYPE slot. Expected int32.");
+
+ return static_cast<GjsGlobalType>(global_type.toInt32());
}
/**
@@ -349,28 +471,42 @@ gjs_create_global_object(JSContext *cx)
* pending on @cx
*/
bool gjs_define_global_properties(JSContext* cx, JS::HandleObject global,
+ GjsGlobalType global_type,
const char* realm_name,
const char* bootstrap_script) {
- return GjsGlobal::define_properties(cx, global, realm_name,
- bootstrap_script);
+ gjs_set_global_slot(global.get(), GjsGlobalSlot::GLOBAL_TYPE,
+ JS::Int32Value(static_cast<uint32_t>(global_type)));
+
+ switch (global_type) {
+ case GjsGlobalType::DEFAULT:
+ return GjsGlobal::define_properties(cx, global, realm_name,
+ bootstrap_script);
+ case GjsGlobalType::DEBUGGER:
+ return GjsDebuggerGlobal::define_properties(cx, global, realm_name,
+ bootstrap_script);
+ default:
+ return true;
+ }
}
-void
-gjs_set_global_slot(JSContext *cx,
- GjsGlobalSlot slot,
- JS::Value value)
-{
- JSObject *global = gjs_get_import_global(cx);
- JS_SetReservedSlot(global, JSCLASS_GLOBAL_SLOT_COUNT + slot, value);
+template <typename GlobalSlot>
+void gjs_set_global_slot(JSObject* global, GlobalSlot slot, JS::Value value) {
+ JS_SetReservedSlot(
+ global, JSCLASS_GLOBAL_SLOT_COUNT + static_cast<uint32_t>(slot), value);
}
+template void gjs_set_global_slot(JSObject* global, GjsGlobalSlot slot,
+ JS::Value value);
-JS::Value
-gjs_get_global_slot(JSContext *cx,
- GjsGlobalSlot slot)
-{
- JSObject *global = gjs_get_import_global(cx);
- return JS_GetReservedSlot(global, JSCLASS_GLOBAL_SLOT_COUNT + slot);
+template <typename GlobalSlot>
+JS::Value gjs_get_global_slot(JSObject* global, GlobalSlot slot) {
+ return JS_GetReservedSlot(
+ global, JSCLASS_GLOBAL_SLOT_COUNT + static_cast<uint32_t>(slot));
}
+template JS::Value gjs_get_global_slot(JSObject* global, GjsGlobalSlot slot);
decltype(GjsGlobal::klass) constexpr GjsGlobal::klass;
decltype(GjsGlobal::static_funcs) constexpr GjsGlobal::static_funcs;
+
+decltype(GjsDebuggerGlobal::klass) constexpr GjsDebuggerGlobal::klass;
+decltype(
+ GjsDebuggerGlobal::static_funcs) constexpr GjsDebuggerGlobal::static_funcs;
diff --git a/gjs/global.h b/gjs/global.h
index 649ee3dc..5dca88ab 100644
--- a/gjs/global.h
+++ b/gjs/global.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
@@ -31,42 +32,54 @@
#include "gjs/macros.h"
-typedef enum {
- GJS_GLOBAL_SLOT_IMPORTS,
- GJS_GLOBAL_SLOT_PROTOTYPE_gtype,
- GJS_GLOBAL_SLOT_PROTOTYPE_function,
- GJS_GLOBAL_SLOT_PROTOTYPE_ns,
- GJS_GLOBAL_SLOT_PROTOTYPE_repo,
- GJS_GLOBAL_SLOT_PROTOTYPE_importer,
- GJS_GLOBAL_SLOT_PROTOTYPE_cairo_context,
- GJS_GLOBAL_SLOT_PROTOTYPE_cairo_gradient,
- GJS_GLOBAL_SLOT_PROTOTYPE_cairo_image_surface,
- GJS_GLOBAL_SLOT_PROTOTYPE_cairo_linear_gradient,
- GJS_GLOBAL_SLOT_PROTOTYPE_cairo_path,
- GJS_GLOBAL_SLOT_PROTOTYPE_cairo_pattern,
- GJS_GLOBAL_SLOT_PROTOTYPE_cairo_pdf_surface,
- GJS_GLOBAL_SLOT_PROTOTYPE_cairo_ps_surface,
- GJS_GLOBAL_SLOT_PROTOTYPE_cairo_radial_gradient,
- GJS_GLOBAL_SLOT_PROTOTYPE_cairo_region,
- GJS_GLOBAL_SLOT_PROTOTYPE_cairo_solid_pattern,
- GJS_GLOBAL_SLOT_PROTOTYPE_cairo_surface,
- GJS_GLOBAL_SLOT_PROTOTYPE_cairo_surface_pattern,
- GJS_GLOBAL_SLOT_PROTOTYPE_cairo_svg_surface,
- GJS_GLOBAL_SLOT_LAST,
-} GjsGlobalSlot;
+enum class GjsGlobalType {
+ DEFAULT,
+ DEBUGGER,
+};
+
+enum class GjsGlobalSlot : uint32_t {
+ GLOBAL_TYPE = 0,
+ IMPORTS,
+ PROTOTYPE_gtype,
+ PROTOTYPE_importer,
+ PROTOTYPE_function,
+ PROTOTYPE_ns,
+ PROTOTYPE_repo,
+ PROTOTYPE_byte_array,
+ PROTOTYPE_cairo_context,
+ PROTOTYPE_cairo_gradient,
+ PROTOTYPE_cairo_image_surface,
+ PROTOTYPE_cairo_linear_gradient,
+ PROTOTYPE_cairo_path,
+ PROTOTYPE_cairo_pattern,
+ PROTOTYPE_cairo_pdf_surface,
+ PROTOTYPE_cairo_ps_surface,
+ PROTOTYPE_cairo_radial_gradient,
+ PROTOTYPE_cairo_region,
+ PROTOTYPE_cairo_solid_pattern,
+ PROTOTYPE_cairo_surface,
+ PROTOTYPE_cairo_surface_pattern,
+ PROTOTYPE_cairo_svg_surface,
+ LAST,
+};
+
+GjsGlobalType gjs_global_get_type(JSContext* cx);
+GjsGlobalType gjs_global_get_type(JSObject* global);
GJS_JSAPI_RETURN_CONVENTION
-JSObject *gjs_create_global_object(JSContext *cx);
+JSObject* gjs_create_global_object(JSContext* cx, GjsGlobalType global_type,
+ JSObject* existing_global = nullptr);
GJS_JSAPI_RETURN_CONVENTION
bool gjs_define_global_properties(JSContext* cx, JS::HandleObject global,
+ GjsGlobalType global_type,
const char* realm_name,
const char* bootstrap_script);
-void gjs_set_global_slot(JSContext *context,
- GjsGlobalSlot slot,
- JS::Value value);
+template <typename GlobalSlot>
+void gjs_set_global_slot(JSObject* global, GlobalSlot slot, JS::Value value);
-JS::Value gjs_get_global_slot(JSContext* cx, GjsGlobalSlot slot);
+template <typename GlobalSlot>
+JS::Value gjs_get_global_slot(JSObject* global, GlobalSlot slot);
#endif // GJS_GLOBAL_H_
diff --git a/gjs/importer.cpp b/gjs/importer.cpp
index 628d3cad..71ec6683 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>
@@ -39,6 +40,7 @@
#include <js/CallArgs.h>
#include <js/CharacterEncoding.h>
#include <js/Class.h>
+#include <js/GCHashTable.h> // for GCHashMap
#include <js/Id.h> // for PropertyKey, JSID_IS_STRING
#include <js/PropertyDescriptor.h>
#include <js/PropertySpec.h>
@@ -49,6 +51,7 @@
#include <js/Value.h>
#include <jsapi.h> // for JS_DefinePropertyById, JS_DefineP...
#include <jspubtd.h> // for JSProto_Error
+#include <mozilla/HashTable.h> // for HashTable<>::AddPtr, HashMapEntry
#include <mozilla/UniquePtr.h>
#include <mozilla/Vector.h>
diff --git a/gjs/jsapi-class.h b/gjs/jsapi-class.h
index 17ab8fbb..3cdad3f1 100644
--- a/gjs/jsapi-class.h
+++ b/gjs/jsapi-class.h
@@ -198,8 +198,11 @@ GJS_DEFINE_PROTO_FUNCS_WITH_PARENT(cname, no_parent)
#define _GJS_DEFINE_GET_PROTO(cname) \
GJS_USE JSObject* gjs_##cname##_get_proto(JSContext* cx) { \
+ JSObject* global = JS::CurrentGlobalOrNull(cx); \
+ JSAutoRealm ar(cx, global); \
JS::RootedValue v_proto( \
- cx, gjs_get_global_slot(cx, GJS_GLOBAL_SLOT_PROTOTYPE_##cname)); \
+ cx, \
+ gjs_get_global_slot(global, GjsGlobalSlot::PROTOTYPE_##cname)); \
g_assert(((void)"gjs_" #cname "_define_proto() must be called before " \
"gjs_" #cname "_get_proto()", \
!v_proto.isUndefined())); \
@@ -213,8 +216,12 @@ GJS_DEFINE_PROTO_FUNCS_WITH_PARENT(cname, no_parent)
bool gjs_##cname##_define_proto(JSContext* cx, JS::HandleObject module, \
JS::MutableHandleObject proto) { \
/* If we've been here more than once, we already have the proto */ \
+ JSObject* global = JS::CurrentGlobalOrNull(cx); \
+ JSAutoRealm ar(cx, global); \
JS::RootedValue v_proto( \
- cx, gjs_get_global_slot(cx, GJS_GLOBAL_SLOT_PROTOTYPE_##cname)); \
+ cx, \
+ gjs_get_global_slot(global, GjsGlobalSlot::PROTOTYPE_##cname)); \
+ \
if (!v_proto.isUndefined()) { \
g_assert( \
((void)"Someone stored some weird value in a global slot", \
@@ -226,7 +233,7 @@ GJS_DEFINE_PROTO_FUNCS_WITH_PARENT(cname, no_parent)
/* If module is not given, we are defining a global class */ \
JS::RootedObject in_obj(cx, module); \
if (!in_obj) \
- in_obj = gjs_get_import_global(cx); \
+ in_obj = global; \
\
/* Create the class, prototype, and constructor */ \
JS::RootedObject parent_proto(cx, gjs_##parent_cname##_get_proto(cx)); \
@@ -236,7 +243,7 @@ GJS_DEFINE_PROTO_FUNCS_WITH_PARENT(cname, no_parent)
gjs_##cname##_static_funcs)); \
if (!proto) \
return false; \
- gjs_set_global_slot(cx, GJS_GLOBAL_SLOT_PROTOTYPE_##cname, \
+ gjs_set_global_slot(global, GjsGlobalSlot::PROTOTYPE_##cname, \
JS::ObjectValue(*proto)); \
\
/* Look up the constructor */ \
diff --git a/gjs/module.cpp b/gjs/module.cpp
index 6942ee46..a7d7b7ee 100644
--- a/gjs/module.cpp
+++ b/gjs/module.cpp
@@ -39,6 +39,7 @@
#include <js/RootingAPI.h>
#include <js/SourceText.h>
#include <js/TypeDecls.h>
+#include <js/Value.h>
#include <jsapi.h> // for JS_DefinePropertyById, ...
#include "gjs/context-private.h"
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]