[gjs: 4/5] global: Define debug names for compartments



commit e4b79a833ab687332647b2220f8b9a7d47489efb
Author: Philip Chimento <philip endlessm com>
Date:   Tue Nov 26 15:58:29 2019 -0800

    global: Define debug names for compartments
    
    We'll give each compartment a debug name which will be stuffed into its
    private pointer slot. This can be useful for debugging or for memory
    stats.

 gjs/context.cpp  |  2 +-
 gjs/coverage.cpp |  2 +-
 gjs/debugger.cpp |  3 ++-
 gjs/global.cpp   | 25 ++++++++++++++-----------
 gjs/global.h     |  6 +++---
 5 files changed, 21 insertions(+), 17 deletions(-)
---
diff --git a/gjs/context.cpp b/gjs/context.cpp
index f55d8a44..1accc773 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -517,7 +517,7 @@ GjsContextPrivate::GjsContextPrivate(JSContext* cx, GjsContext* public_context)
     gjs_set_global_slot(m_cx, GJS_GLOBAL_SLOT_IMPORTS,
                         JS::ObjectValue(*importer));
 
-    if (!gjs_define_global_properties(m_cx, global, "default")) {
+    if (!gjs_define_global_properties(m_cx, global, "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 ce7292c8..e02526b9 100644
--- a/gjs/coverage.cpp
+++ b/gjs/coverage.cpp
@@ -357,7 +357,7 @@ bootstrap_coverage(GjsCoverage *coverage)
         if (!JS_SetPropertyById(context, debugger_compartment, atoms.debuggee(),
                                 debuggeeWrapperValue) ||
             !gjs_define_global_properties(context, debugger_compartment,
-                                          "coverage"))
+                                          "GJS coverage", "coverage"))
             return false;
 
         /* Add a tracer, as suggested by jdm on #jsapi */
diff --git a/gjs/debugger.cpp b/gjs/debugger.cpp
index 217e3a28..1d04e449 100644
--- a/gjs/debugger.cpp
+++ b/gjs/debugger.cpp
@@ -146,6 +146,7 @@ void gjs_context_setup_debugger_console(GjsContext* gjs) {
     if (!JS_SetPropertyById(cx, debugger_compartment, atoms.debuggee(),
                             v_wrapper) ||
         !JS_DefineFunctions(cx, debugger_compartment, debugger_funcs) ||
-        !gjs_define_global_properties(cx, debugger_compartment, "debugger"))
+        !gjs_define_global_properties(cx, debugger_compartment, "GJS debugger",
+                                      "debugger"))
         gjs_log_exception(cx);
 }
diff --git a/gjs/global.cpp b/gjs/global.cpp
index 0214bf85..88cef746 100644
--- a/gjs/global.cpp
+++ b/gjs/global.cpp
@@ -260,17 +260,20 @@ class GjsGlobal {
     }
 
     GJS_JSAPI_RETURN_CONVENTION
-    static bool
-    define_properties(JSContext       *cx,
-                      JS::HandleObject global,
-                      const char      *bootstrap_script)
-    {
+    static bool define_properties(JSContext* cx, JS::HandleObject global,
+                                  const char* compartment_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, GjsGlobal::static_funcs))
             return false;
 
+        JSCompartment* compartment = js::GetObjectCompartment(global);
+        // const_cast is allowed here if we never free the compartment data
+        JS_SetCompartmentPrivate(compartment,
+                                 const_cast<char*>(compartment_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",
@@ -312,6 +315,7 @@ gjs_create_global_object(JSContext *cx)
  * gjs_define_global_properties:
  * @cx: a #JSContext
  * @global: a JS global object that has not yet been passed to this function
+ * @compartment_name: (nullable): name of the compartment, for debug output
  * @bootstrap_script: (nullable): name of a bootstrap script (found at
  * resource://org/gnome/gjs/modules/_bootstrap/@bootstrap_script) or %NULL for
  * none
@@ -333,12 +337,11 @@ gjs_create_global_object(JSContext *cx)
  * Returns: true on success, false otherwise, in which case an exception is
  * pending on @cx
  */
-bool
-gjs_define_global_properties(JSContext       *cx,
-                             JS::HandleObject global,
-                             const char      *bootstrap_script)
-{
-    return GjsGlobal::define_properties(cx, global, bootstrap_script);
+bool gjs_define_global_properties(JSContext* cx, JS::HandleObject global,
+                                  const char* compartment_name,
+                                  const char* bootstrap_script) {
+    return GjsGlobal::define_properties(cx, global, compartment_name,
+                                        bootstrap_script);
 }
 
 void
diff --git a/gjs/global.h b/gjs/global.h
index 9470be53..287d0290 100644
--- a/gjs/global.h
+++ b/gjs/global.h
@@ -57,9 +57,9 @@ GJS_JSAPI_RETURN_CONVENTION
 JSObject *gjs_create_global_object(JSContext *cx);
 
 GJS_JSAPI_RETURN_CONVENTION
-bool gjs_define_global_properties(JSContext       *cx,
-                                  JS::HandleObject global,
-                                  const char      *bootstrap_script);
+bool gjs_define_global_properties(JSContext* cx, JS::HandleObject global,
+                                  const char* compartment_name,
+                                  const char* bootstrap_script);
 
 void gjs_set_global_slot(JSContext    *context,
                          GjsGlobalSlot slot,


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