[gjs/wip/ptomato/mozjs31: 3/3] WIP - Destroy runtime



commit d8d383158377260a976ba2e30cdece1d86218567
Author: Philip Chimento <philip endlessm com>
Date:   Tue Nov 8 16:35:37 2016 -0800

    WIP - Destroy runtime
    
    If you call JS_ShutDown() with a runtime still in existence, then
    SpiderMonkey will shout at you. No kidding, the error message really says
    "FIX THIS!"
    
    This patch is for informational purposes only, I am going to rework it to
    do some reference counting so that the runtime is destroyed when the last
    GjsContext is destroyed. Or possibly make GjsContext a singleton.
    
    Note that in later SpiderMonkey versions there is only going to be one
    JSContext per JSRuntime allowed, and possibly the two are to be merged
    altogether.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=751252

 gjs/context.cpp              |    2 ++
 gjs/runtime.cpp              |    8 +++++++-
 gjs/runtime.h                |    2 ++
 installed-tests/gjs-unit.cpp |    2 ++
 test/gjs-tests.cpp           |    3 +++
 5 files changed, 16 insertions(+), 1 deletions(-)
---
diff --git a/gjs/context.cpp b/gjs/context.cpp
index 76a95af..afd5680 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -830,5 +830,7 @@ gjs_init(void)
 void
 gjs_shutdown(void)
 {
+    /* No-op if the runtime was already destroyed */
+    gjs_destroy_runtime_for_current_thread();
     JS_ShutDown();
 }
diff --git a/gjs/runtime.cpp b/gjs/runtime.cpp
index 6fd9467..05e51db 100644
--- a/gjs/runtime.cpp
+++ b/gjs/runtime.cpp
@@ -151,8 +151,8 @@ destroy_runtime(gpointer data)
     JSRuntime *runtime = (JSRuntime *) data;
     RuntimeData *rtdata = (RuntimeData *) JS_GetRuntimePrivate(runtime);
 
-    g_free(rtdata);
     JS_DestroyRuntime(runtime);
+    g_free(rtdata);
 }
 
 static GPrivate thread_runtime = G_PRIVATE_INIT(destroy_runtime);
@@ -246,3 +246,9 @@ gjs_runtime_for_current_thread(void)
 
     return runtime;
 }
+
+void
+gjs_destroy_runtime_for_current_thread(void)
+{
+    g_private_replace(&thread_runtime, NULL);
+}
diff --git a/gjs/runtime.h b/gjs/runtime.h
index 0900e28..914fdee 100644
--- a/gjs/runtime.h
+++ b/gjs/runtime.h
@@ -28,6 +28,8 @@
 
 JSRuntime * gjs_runtime_for_current_thread (void);
 
+void gjs_destroy_runtime_for_current_thread(void);
+
 bool        gjs_runtime_is_sweeping        (JSRuntime *runtime);
 
 #endif /* __GJS_RUNTIME_H__ */
diff --git a/installed-tests/gjs-unit.cpp b/installed-tests/gjs-unit.cpp
index 29b41ae..49f2fdb 100644
--- a/installed-tests/gjs-unit.cpp
+++ b/installed-tests/gjs-unit.cpp
@@ -34,6 +34,7 @@
 
 #include "gjs/coverage.h"
 #include "gjs/mem.h"
+#include "gjs/runtime.h"
 
 typedef struct {
     const char *coverage_prefix;
@@ -105,6 +106,7 @@ teardown(GjsTestJSFixture *fix,
 
     gjs_memory_report("before destroying context", false);
     g_object_unref(fix->context);
+    gjs_destroy_runtime_for_current_thread();
     gjs_memory_report("after destroying context", true);
 }
 
diff --git a/test/gjs-tests.cpp b/test/gjs-tests.cpp
index 0094f3c..d998f08 100644
--- a/test/gjs-tests.cpp
+++ b/test/gjs-tests.cpp
@@ -29,6 +29,7 @@
 #include <gjs/context.h>
 #include "gjs/jsapi-util.h"
 #include "gjs/jsapi-wrapper.h"
+#include "gjs/runtime.h"
 #include "gjs-test-utils.h"
 
 static void
@@ -44,6 +45,8 @@ gjstest_test_func_gjs_context_construct_destroy(void)
 
     context = gjs_context_new ();
     g_object_unref (context);
+
+    gjs_destroy_runtime_for_current_thread();
 }
 
 static void


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