[gjs: 1/7] context: Move garbage collection handling callback into context




commit c9692635daf5322d8533b4c768efdd999aaa2eed
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Tue May 18 01:53:29 2021 +0200

    context: Move garbage collection handling callback into context
    
    This is its natural scope, so let's handle it has part of the context
    instead of during engine initialization, as we don't need it at that
    level anyway

 gjs/context-private.h |  1 +
 gjs/context.cpp       | 24 ++++++++++++++++++++++++
 gjs/engine.cpp        | 18 ------------------
 3 files changed, 25 insertions(+), 18 deletions(-)
---
diff --git a/gjs/context-private.h b/gjs/context-private.h
index 93d87909..8e4ee20b 100644
--- a/gjs/context-private.h
+++ b/gjs/context-private.h
@@ -115,6 +115,7 @@ class GjsContextPrivate : public JS::JobQueue {
 
     void schedule_gc_internal(bool force_gc);
     static gboolean trigger_gc_if_needed(void* data);
+    void on_garbage_collection(JSGCStatus);
 
     class SavedQueue;
     void start_draining_job_queue(void);
diff --git a/gjs/context.cpp b/gjs/context.cpp
index 41e3297e..f9f01539 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -58,6 +58,7 @@
 #include <jsfriendapi.h>  // for DumpHeap, IgnoreNurseryObjects
 #include <mozilla/UniquePtr.h>
 
+#include "gi/function.h"
 #include "gi/object.h"
 #include "gi/private.h"
 #include "gi/repo.h"
@@ -493,6 +494,15 @@ GjsContextPrivate::GjsContextPrivate(JSContext* cx, GjsContext* public_context)
       m_cx(cx),
       m_owner_thread(std::this_thread::get_id()),
       m_environment_preparer(cx) {
+
+    JS_SetGCCallback(
+        cx,
+        [](JSContext*, JSGCStatus status, JS::GCReason, void* data) {
+            static_cast<GjsContextPrivate*>(data)->on_garbage_collection(
+                status);
+        },
+        this);
+
     const char *env_profiler = g_getenv("GJS_ENABLE_PROFILER");
     if (env_profiler || m_should_listen_sigusr2)
         m_should_profile = true;
@@ -736,6 +746,20 @@ void GjsContextPrivate::schedule_gc_if_needed(void) {
     schedule_gc_internal(false);
 }
 
+void GjsContextPrivate::on_garbage_collection(JSGCStatus status) {
+    // We finalize any pending toggle refs before doing any garbage collection,
+    // so that we can collect the JS wrapper objects, and in order to minimize
+    // the chances of objects having a pending toggle up queued when they are
+    // garbage collected. */
+    if (status == JSGC_BEGIN) {
+        gjs_debug_lifecycle(GJS_DEBUG_CONTEXT, "Begin garbage collection");
+        gjs_object_clear_toggles();
+        gjs_function_clear_async_closures();
+    } else if (status == JSGC_END) {
+        gjs_debug_lifecycle(GJS_DEBUG_CONTEXT, "End garbage collection");
+    }
+}
+
 void GjsContextPrivate::set_sweeping(bool value) {
     // If we have a profiler enabled, record the duration of GC sweep
     if (this->m_profiler != nullptr) {
diff --git a/gjs/engine.cpp b/gjs/engine.cpp
index 4908c775..04e67415 100644
--- a/gjs/engine.cpp
+++ b/gjs/engine.cpp
@@ -27,8 +27,6 @@
 #include <jsapi.h>  // for InitSelfHostedCode, JS_Destr...
 #include <mozilla/UniquePtr.h>
 
-#include "gi/function.h"
-#include "gi/object.h"
 #include "gjs/context-private.h"
 #include "gjs/engine.h"
 #include "gjs/jsapi-util.h"
@@ -84,21 +82,6 @@ static void gjs_finalize_callback(JSFreeOp*, JSFinalizeStatus status,
         gjs->set_sweeping(false);
 }
 
-static void on_garbage_collect(JSContext*, JSGCStatus status, JS::GCReason,
-                               void*) {
-    /* We finalize any pending toggle refs before doing any garbage collection,
-     * so that we can collect the JS wrapper objects, and in order to minimize
-     * the chances of objects having a pending toggle up queued when they are
-     * garbage collected. */
-    if (status == JSGC_BEGIN) {
-        gjs_debug_lifecycle(GJS_DEBUG_CONTEXT, "Begin garbage collection");
-        gjs_object_clear_toggles();
-        gjs_function_clear_async_closures();
-    } else if (status == JSGC_END) {
-        gjs_debug_lifecycle(GJS_DEBUG_CONTEXT, "End garbage collection");
-    }
-}
-
 static void on_promise_unhandled_rejection(
     JSContext* cx, bool mutedErrors [[maybe_unused]], JS::HandleObject promise,
     JS::PromiseRejectionHandlingState state, void* data) {
@@ -213,7 +196,6 @@ JSContext* gjs_create_js_context(GjsContextPrivate* uninitialized_gjs) {
     JS_SetContextPrivate(cx, uninitialized_gjs);
 
     JS_AddFinalizeCallback(cx, gjs_finalize_callback, uninitialized_gjs);
-    JS_SetGCCallback(cx, on_garbage_collect, uninitialized_gjs);
     JS::SetWarningReporter(cx, gjs_warning_reporter);
     JS::SetJobQueue(cx, dynamic_cast<JS::JobQueue*>(uninitialized_gjs));
     JS::SetPromiseRejectionTrackerCallback(cx, on_promise_unhandled_rejection,


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