[gjs/refactor-globals] global: Refactor to support multiple global types.



commit 5a649f4fdef11eea79b28b77bcf9c862182c847d
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       |   9 ++-
 gjs/context.cpp   |  13 +--
 gjs/coverage.cpp  |   5 +-
 gjs/debugger.cpp  |   6 +-
 gjs/global.cpp    | 231 +++++++++++++++++++++++++++++++++++++++++-------------
 gjs/global.h      |  71 ++++++++++-------
 gjs/jsapi-class.h |  19 ++++-
 7 files changed, 257 insertions(+), 97 deletions(-)
---
diff --git a/gi/repo.cpp b/gi/repo.cpp
index 08df95e1..25b4369d 100644
--- a/gi/repo.cpp
+++ b/gi/repo.cpp
@@ -601,7 +601,9 @@ lookup_override_function(JSContext             *cx,
 {
     JS::AutoSaveExceptionState saved_exc(cx);
 
-    JS::RootedValue importer(cx, gjs_get_global_slot(cx, GJS_GLOBAL_SLOT_IMPORTS));
+    JS::RootedObject global(cx, 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 +646,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));
+    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());
diff --git a/gjs/context.cpp b/gjs/context.cpp
index 04f28f73..892dfb0f 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");
@@ -519,14 +521,15 @@ GjsContextPrivate::GjsContextPrivate(JSContext* cx, GjsContext* public_context)
         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..989149ea 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,6 +46,7 @@
 #include <js/Utility.h>  // for UniqueChars
 #include <jsapi.h>       // for AutoSaveExceptionState, ...
 
+
 #include "gjs/atoms.h"
 #include "gjs/context-private.h"
 #include "gjs/engine.h"
@@ -55,12 +57,48 @@ namespace mozilla {
 union Utf8Unit;
 }
 
+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;
+}
+
+JSObject* global_create_new(JSContext* cx, const JSClass* clasp) {
+    JS::RealmCreationOptions creation;
+    creation.setNewCompartmentAndZone();
+
+    return global_create(cx, clasp, creation);
+}
+
+JSObject* global_create_with_existing(JSContext* cx, JS::HandleObject existing,
+                                      const JSClass* clasp) {
+    JS::RealmCreationOptions creation;
+    creation.setExistingCompartment(existing);
+
+    return global_create(cx, clasp, creation);
+}
+
 GJS_JSAPI_RETURN_CONVENTION
-static bool
-run_bootstrap(JSContext       *cx,
-              const char      *bootstrap_script,
-              JS::HandleObject global)
-{
+static bool run_bootstrap(JSContext* cx, const char* bootstrap_script,
+                          JS::HandleObject global) {
     GjsAutoChar uri = g_strdup_printf(
         "resource:///org/gnome/gjs/modules/script/_bootstrap/%s.js",
         bootstrap_script);
@@ -184,7 +222,6 @@ gjs_print_parse_args(JSContext              *cx,
                 *buffer = g_strdup("<invalid string>");
             return true;
         }
-
     }
     *buffer = g_string_free(str, false);
 
@@ -197,7 +234,7 @@ gjs_print(JSContext *context,
           unsigned   argc,
           JS::Value *vp)
 {
-    JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
+    JS::CallArgs argv = JS::CallArgsFromVp(argc, vp);
 
     GjsAutoChar buffer;
     if (!gjs_print_parse_args(context, argv, &buffer))
@@ -231,8 +268,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,30 +285,14 @@ class GjsGlobal {
 
  public:
     GJS_USE
-    static JSObject *
-    create(JSContext *cx)
-    {
-        JS::RealmBehaviors behaviors;
-
-        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);
-
-        if (!JS_InitReflectParse(cx, global) ||
-            !JS_DefineDebuggerObject(cx, global))
-            return nullptr;
+    static JSObject* create(JSContext* cx) {
+        return global_create_new(cx, &klass);
+    }
 
-        return global;
+    GJS_USE
+    static JSObject* create_with_compartment(JSContext* cx,
+                                             JS::HandleObject cmp_global) {
+        return global_create_with_existing(cx, cmp_global, &klass);
     }
 
     GJS_JSAPI_RETURN_CONVENTION
@@ -286,9 +310,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 +332,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,
+                                             JS::HandleObject 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 +388,49 @@ 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,
+                                   JS::HandleObject 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 before a realm was entered.");
+
+    auto global_type = gjs_get_global_slot(global, GjsGlobalSlot::GLOBAL_TYPE);
+
+    g_assert(global_type.isInt32());
+
+    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());
+
+    return static_cast<GjsGlobalType>(global_type.toInt32());
 }
 
 /**
@@ -349,28 +460,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..e25b442f 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
@@ -29,44 +30,58 @@
 #include <js/TypeDecls.h>
 #include <js/Value.h>
 
+#include <stdint.h>
+
 #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,
+                                   JS::HandleObject 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/jsapi-class.h b/gjs/jsapi-class.h
index 17ab8fbb..1957ed68 100644
--- a/gjs/jsapi-class.h
+++ b/gjs/jsapi-class.h
@@ -198,8 +198,13 @@ 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);                        \
+        g_assert(global);                                                      \
+                                                                               \
+        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 +218,14 @@ 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);                        \
+        g_assert(global);                                                      \
+                                                                               \
+        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 +237,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 +247,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 */                                          \


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