[gjs: 23/25] tests: Add weak pointer callback in tests with untraced weak pointer



commit 03b463ec590d3578526ec597f9f6b485353d4c3c
Author: Philip Chimento <philip chimento gmail com>
Date:   Sat Jan 25 19:26:17 2020 -0800

    tests: Add weak pointer callback in tests with untraced weak pointer
    
    SpiderMonkey's pre-verify mode has gotten stricter and now detects if
    you have a JS::Heap holding a weak pointer that you didn't call
    JS_UpdateWeakPointerAfterGC on. We do this in two tests, so we have to
    add a weak pointer update callback or else these tests will crash in
    pre-verify mode.

 gjs/jsapi-util-root.h     |  2 +-
 test/gjs-test-rooting.cpp | 14 ++++++++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)
---
diff --git a/gjs/jsapi-util-root.h b/gjs/jsapi-util-root.h
index fb08d012..5534919a 100644
--- a/gjs/jsapi-util-root.h
+++ b/gjs/jsapi-util-root.h
@@ -329,7 +329,7 @@ class GjsMaybeOwned {
     /* If not tracing, then you must call this method during GC in order to
      * update the object's location if it was moved, or null it out if it was
      * finalized. If the object was finalized, returns true. */
-    GJS_USE bool update_after_gc() {
+    bool update_after_gc() {
         debug("update_after_gc()");
         g_assert(!m_root);
         return GjsHeapOperation<T>::update_after_gc(&m_heap);
diff --git a/test/gjs-test-rooting.cpp b/test/gjs-test-rooting.cpp
index 3241a314..3856efa5 100644
--- a/test/gjs-test-rooting.cpp
+++ b/test/gjs-test-rooting.cpp
@@ -135,13 +135,23 @@ static void test_maybe_owned_rooted_is_collected_after_reset(
     delete obj;
 }
 
+static void update_weak_pointer(JSContext*, JS::Compartment*, void* data) {
+    auto* obj = static_cast<GjsMaybeOwned<JSObject*>*>(data);
+    if (*obj)
+        obj->update_after_gc();
+}
+
 static void test_maybe_owned_weak_pointer_is_collected_by_gc(
     GjsRootingFixture* fx, const void*) {
     auto obj = new GjsMaybeOwned<JSObject *>();
     *obj = test_obj_new(fx);
 
+    JS_AddWeakPointerCompartmentCallback(PARENT(fx)->cx, &update_weak_pointer,
+                                         obj);
     wait_for_gc(fx);
     g_assert_true(fx->finalized);
+    JS_RemoveWeakPointerCompartmentCallback(PARENT(fx)->cx,
+                                            &update_weak_pointer);
     delete obj;
 }
 
@@ -195,8 +205,12 @@ static void test_maybe_owned_switch_to_unrooted_allows_collection(
     obj->root(PARENT(fx)->cx, test_obj_new(fx));
 
     obj->switch_to_unrooted(PARENT(fx)->cx);
+    JS_AddWeakPointerCompartmentCallback(PARENT(fx)->cx, &update_weak_pointer,
+                                         obj);
     wait_for_gc(fx);
     g_assert_true(fx->finalized);
+    JS_RemoveWeakPointerCompartmentCallback(PARENT(fx)->cx,
+                                            &update_weak_pointer);
 
     delete obj;
 }


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