Bug 1188445 - Allow PersistentRooted to store StaticTraceable; r=sfink
☠☠ backed out by 5738b6629d14 ☠ ☠
authorTerrence Cole <terrence@mozilla.com>
Tue, 28 Jul 2015 09:21:32 -0700
changeset 287002 a5c748f78e97f11a3354ce7142bad4a11c02fafc
parent 287001 54a082b0117453c81bb53c118576546a26178e4d
child 287003 6eebf397a8c29941862a164d3c3209707ae1faec
push id5067
push userraliiev@mozilla.com
push dateMon, 21 Sep 2015 14:04:52 +0000
treeherdermozilla-beta@14221ffe5b2f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssfink
bugs1188445
milestone42.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1188445 - Allow PersistentRooted to store StaticTraceable; r=sfink
js/public/RootingAPI.h
js/src/gc/RootMarking.cpp
js/src/jsapi-tests/testGCExactRooting.cpp
js/src/jsgc.cpp
--- a/js/public/RootingAPI.h
+++ b/js/public/RootingAPI.h
@@ -1016,16 +1016,17 @@ class PersistentRooted : public js::Pers
         roots.heapRoots_[kind].insertBack(reinterpret_cast<JS::PersistentRooted<void*>*>(this));
         // Until marking and destruction support the full set, we assert that
         // we don't try to add any unsupported types.
         MOZ_ASSERT(kind == js::THING_ROOT_OBJECT ||
                    kind == js::THING_ROOT_SCRIPT ||
                    kind == js::THING_ROOT_STRING ||
                    kind == js::THING_ROOT_ID ||
                    kind == js::THING_ROOT_VALUE ||
+                   kind == js::THING_ROOT_STATIC_TRACEABLE ||
                    kind == js::THING_ROOT_DYNAMIC_TRACEABLE);
     }
 
   public:
     PersistentRooted() : ptr(js::GCMethods<T>::initial()) {}
 
     template <typename RootingContext>
     explicit PersistentRooted(const RootingContext& cx)
@@ -1096,17 +1097,23 @@ class PersistentRooted : public js::Pers
     }
 
   private:
     void set(T value) {
         MOZ_ASSERT(initialized());
         ptr = value;
     }
 
-    T ptr;
+    // See the comment above Rooted::ptr.
+    using MaybeWrapped = typename mozilla::Conditional<
+        mozilla::IsBaseOf<StaticTraceable, T>::value,
+        js::DispatchWrapper<T>,
+        T>::Type;
+
+    MaybeWrapped ptr;
 };
 
 class JS_PUBLIC_API(ObjectPtr)
 {
     Heap<JSObject*> value;
 
   public:
     ObjectPtr() : value(nullptr) {}
--- a/js/src/gc/RootMarking.cpp
+++ b/js/src/gc/RootMarking.cpp
@@ -328,20 +328,25 @@ js::gc::MarkPersistentRootedChainsInList
     PersistentRootedMarker<JSString*>::markChain(trc, roots.getPersistentRootedList<JSString*>(),
                                                  "PersistentRooted<JSString*>");
 
     PersistentRootedMarker<jsid>::markChain(trc, roots.getPersistentRootedList<jsid>(),
                                             "PersistentRooted<jsid>");
     PersistentRootedMarker<Value>::markChain(trc, roots.getPersistentRootedList<Value>(),
                                              "PersistentRooted<Value>");
 
+    PersistentRootedMarker<JS::StaticTraceable>::markChain<
+        js::DispatchWrapper<JS::StaticTraceable>::TraceWrapped>(trc,
+            reinterpret_cast<mozilla::LinkedList<JS::PersistentRooted<JS::StaticTraceable>>&>(
+                roots.heapRoots_[THING_ROOT_STATIC_TRACEABLE]),
+            "PersistentRooted<StaticTraceable>");
     PersistentRootedMarker<ConcreteTraceable>::markChain<MarkDynamicTraceable>(trc,
         reinterpret_cast<mozilla::LinkedList<JS::PersistentRooted<ConcreteTraceable>>&>(
             roots.heapRoots_[THING_ROOT_DYNAMIC_TRACEABLE]),
-        "PersistentRooted<Value>");
+        "PersistentRooted<DynamicTraceable>");
 }
 
 void
 js::gc::MarkPersistentRootedChains(JSTracer* trc)
 {
     for (ContextIter cx(trc->runtime()); !cx.done(); cx.next())
         MarkPersistentRootedChainsInLists(cx->roots, trc);
     MarkPersistentRootedChainsInLists(trc->runtime()->mainThread.roots, trc);
--- a/js/src/jsapi-tests/testGCExactRooting.cpp
+++ b/js/src/jsapi-tests/testGCExactRooting.cpp
@@ -55,16 +55,25 @@ struct MyContainer : public JS::StaticTr
 };
 
 namespace js {
 template <>
 struct RootedBase<MyContainer> {
     RelocatablePtrObject& obj() { return static_cast<Rooted<MyContainer>*>(this)->get().obj; }
     RelocatablePtrString& str() { return static_cast<Rooted<MyContainer>*>(this)->get().str; }
 };
+template <>
+struct PersistentRootedBase<MyContainer> {
+    RelocatablePtrObject& obj() {
+        return static_cast<PersistentRooted<MyContainer>*>(this)->get().obj;
+    }
+    RelocatablePtrString& str() {
+        return static_cast<PersistentRooted<MyContainer>*>(this)->get().str;
+    }
+};
 } // namespace js
 
 BEGIN_TEST(testGCRootedStaticStructInternalStackStorage)
 {
     JS::Rooted<MyContainer> container(cx);
     container.get().obj = JS_NewObject(cx, nullptr);
     container.get().str = JS_NewStringCopyZ(cx, "Hello");
 
@@ -85,16 +94,50 @@ BEGIN_TEST(testGCRootedStaticStructInter
     container.str() = JS_NewStringCopyZ(cx, "Hello");
 
     JS_GC(cx->runtime());
     JS_GC(cx->runtime());
 
     JS::RootedObject obj(cx, container.obj());
     JS::RootedValue val(cx, StringValue(container.str()));
     CHECK(JS_SetProperty(cx, obj, "foo", val));
+    obj = nullptr;
+    val = UndefinedValue();
+
+    {
+        JS::RootedString actual(cx);
+        bool same;
+
+        // Automatic move from stack to heap.
+        JS::PersistentRooted<MyContainer> heap(cx, container);
+
+        // clear prior rooting.
+        container.obj() = nullptr;
+        container.str() = nullptr;
+
+        obj = heap.obj();
+        CHECK(JS_GetProperty(cx, obj, "foo", &val));
+        actual = val.toString();
+        CHECK(JS_StringEqualsAscii(cx, actual, "Hello", &same));
+        CHECK(same);
+        obj = nullptr;
+        actual = nullptr;
+
+        JS_GC(cx->runtime());
+        JS_GC(cx->runtime());
+
+        obj = heap.obj();
+        CHECK(JS_GetProperty(cx, obj, "foo", &val));
+        actual = val.toString();
+        CHECK(JS_StringEqualsAscii(cx, actual, "Hello", &same));
+        CHECK(same);
+        obj = nullptr;
+        actual = nullptr;
+    }
+
     return true;
 }
 END_TEST(testGCRootedStaticStructInternalStackStorageAugmented)
 
 struct DynamicBase : public JS::DynamicTraceable
 {
     RelocatablePtrObject obj;
     DynamicBase() : obj(nullptr) {}
--- a/js/src/jsgc.cpp
+++ b/js/src/jsgc.cpp
@@ -1366,16 +1366,17 @@ FinishPersistentRootedChain(mozilla::Lin
 void
 js::gc::FinishPersistentRootedChains(RootLists& roots)
 {
     FinishPersistentRootedChain(roots.getPersistentRootedList<JSObject*>());
     FinishPersistentRootedChain(roots.getPersistentRootedList<JSScript*>());
     FinishPersistentRootedChain(roots.getPersistentRootedList<JSString*>());
     FinishPersistentRootedChain(roots.getPersistentRootedList<jsid>());
     FinishPersistentRootedChain(roots.getPersistentRootedList<Value>());
+    FinishPersistentRootedChain(roots.heapRoots_[THING_ROOT_STATIC_TRACEABLE]);
     FinishPersistentRootedChain(roots.heapRoots_[THING_ROOT_DYNAMIC_TRACEABLE]);
 }
 
 void
 GCRuntime::finishRoots()
 {
     if (rootsHash.initialized())
         rootsHash.clear();