[gjs: 2/4] jsapi-util-root: Add JSContext parameter to switch_to_unrooted



commit 5134c79686b4e501fe6d8e572ef7e454ee16c4bb
Author: Marco Trevisan (TreviƱo) <mail 3v1n0 net>
Date:   Tue Sep 24 01:29:29 2019 +0200

    jsapi-util-root: Add JSContext parameter to switch_to_unrooted
    
    switch_to_rooted required already a JSContext parameter to apply,
    how to unroot we used the current context by default.
    
    However this implies that a generic api as GjsMaybeOwned will
    depend on GjsContextPrivate that is instead unrelated to this.
    
    So, just take a context parameter, and reflect this in the calls

 gi/object.cpp             | 4 ++--
 gi/object.h               | 2 +-
 gjs/jsapi-util-root.h     | 3 +--
 test/gjs-test-rooting.cpp | 4 ++--
 4 files changed, 6 insertions(+), 7 deletions(-)
---
diff --git a/gi/object.cpp b/gi/object.cpp
index 6012e5b2..802e27f3 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -1093,7 +1093,8 @@ ObjectInstance::toggle_down(void)
      */
     if (wrapper_is_rooted()) {
         debug_lifecycle("Unrooting wrapper");
-        switch_to_unrooted();
+        GjsContextPrivate* gjs = GjsContextPrivate::from_current_context();
+        switch_to_unrooted(gjs->context());
 
         /* During a GC, the collector asks each object which other
          * objects that it wants to hold on to so if there's an entire
@@ -1111,7 +1112,6 @@ ObjectInstance::toggle_down(void)
          * always queue a garbage collection when a toggle reference goes
          * down.
          */
-        GjsContextPrivate* gjs = GjsContextPrivate::from_current_context();
         if (!gjs->destroying())
             gjs->schedule_gc();
     }
diff --git a/gi/object.h b/gi/object.h
index 24f89acc..10c383c9 100644
--- a/gi/object.h
+++ b/gi/object.h
@@ -335,7 +335,7 @@ class ObjectInstance : public GIWrapperInstance<ObjectBase, ObjectPrototype,
  private:
     void discard_wrapper(void) { m_wrapper.reset(); }
     void switch_to_rooted(JSContext* cx) { m_wrapper.switch_to_rooted(cx); }
-    void switch_to_unrooted(void) { m_wrapper.switch_to_unrooted(); }
+    void switch_to_unrooted(JSContext* cx) { m_wrapper.switch_to_unrooted(cx); }
     GJS_USE
     bool update_after_gc(void) { return m_wrapper.update_after_gc(); }
     GJS_USE
diff --git a/gjs/jsapi-util-root.h b/gjs/jsapi-util-root.h
index 024bd5b5..2aae8443 100644
--- a/gjs/jsapi-util-root.h
+++ b/gjs/jsapi-util-root.h
@@ -300,13 +300,12 @@ class GjsMaybeOwned {
         g_assert(m_root);
     }
 
-    void switch_to_unrooted() {
+    void switch_to_unrooted(JSContext* cx) {
         debug("switch to unrooted");
         g_assert(m_root);
 
         /* Prevent the thing from being garbage collected while it is in neither
          * m_heap nor m_root */
-        JSContext *cx = GjsContextPrivate::from_current_context()->context();
         JSAutoRequest ar(cx);
         JS::Rooted<T> thing(cx, *m_root);
 
diff --git a/test/gjs-test-rooting.cpp b/test/gjs-test-rooting.cpp
index d50b23b6..d37f8820 100644
--- a/test/gjs-test-rooting.cpp
+++ b/test/gjs-test-rooting.cpp
@@ -163,7 +163,7 @@ static void test_maybe_owned_switching_mode_keeps_same_value(
     g_assert_true(obj->rooted());
     g_assert_true(*obj == test_obj);
 
-    obj->switch_to_unrooted();
+    obj->switch_to_unrooted(PARENT(fx)->cx);
     g_assert_false(obj->rooted());
     g_assert_true(*obj == test_obj);
 
@@ -187,7 +187,7 @@ static void test_maybe_owned_switch_to_unrooted_allows_collection(
     auto obj = new GjsMaybeOwned<JSObject *>();
     obj->root(PARENT(fx)->cx, test_obj_new(fx));
 
-    obj->switch_to_unrooted();
+    obj->switch_to_unrooted(PARENT(fx)->cx);
     wait_for_gc(fx);
     g_assert_true(fx->finalized);
 


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