[gjs: 6/12] engine: Cleanup leftover async calls trampolines on gc




commit e81a7b509a7d74ea61b9faa837e042ebf8a6370a
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Thu Sep 3 17:53:55 2020 +0200

    engine: Cleanup leftover async calls trampolines on gc
    
    Instead of cleaning up the async trampolines we've left around at very
    next function call, let's do it when next garbage collection cycle is
    performed.

 gi/function.cpp | 10 +---------
 gi/function.h   |  2 ++
 gjs/engine.cpp  |  2 ++
 3 files changed, 5 insertions(+), 9 deletions(-)
---
diff --git a/gi/function.cpp b/gi/function.cpp
index daaf19da..1fcbb723 100644
--- a/gi/function.cpp
+++ b/gi/function.cpp
@@ -635,9 +635,7 @@ GjsCallbackTrampoline* gjs_callback_trampoline_new(
                            g_base_info_get_name(function->info));
 }
 
-static void
-complete_async_calls(void)
-{
+void gjs_function_clear_async_closures() {
     g_slist_free_full(
         static_cast<GSList*>(g_steal_pointer(&completed_trampolines)),
         (GDestroyNotify)gjs_callback_trampoline_unref);
@@ -712,12 +710,6 @@ static bool gjs_invoke_c_function(JSContext* context, Function* function,
     bool is_method;
     JS::RootedValueVector return_values(context);
 
-    /* Because we can't free a closure while we're in it, we defer
-     * freeing until the next time a C function is invoked.  What
-     * we should really do instead is queue it for a GC thread.
-     */
-    complete_async_calls();
-
     is_method = g_callable_info_is_method(function->info);
     can_throw_gerror = g_callable_info_can_throw_gerror(function->info);
 
diff --git a/gi/function.h b/gi/function.h
index b295154c..f50b747a 100644
--- a/gi/function.h
+++ b/gi/function.h
@@ -99,4 +99,6 @@ bool gjs_invoke_constructor_from_c(JSContext* cx, GIFunctionInfo* info,
                                    const JS::CallArgs& args,
                                    GIArgument* rvalue);
 
+void gjs_function_clear_async_closures();
+
 #endif  // GI_FUNCTION_H_
diff --git a/gjs/engine.cpp b/gjs/engine.cpp
index ec44a3e7..33a1dce8 100644
--- a/gjs/engine.cpp
+++ b/gjs/engine.cpp
@@ -46,6 +46,7 @@
 #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"
@@ -111,6 +112,7 @@ static void on_garbage_collect(JSContext*, JSGCStatus status, JS::GCReason,
     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");
     }


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