Bug 1466083 part 9 - Introduce JS::IterateRealmsInCompartment and use it in NukeAllWrappersForCompartment. r=jonco
authorJan de Mooij <jdemooij@mozilla.com>
Wed, 06 Jun 2018 12:55:50 +0200
changeset 421560 55c591df0a06acfb504020e63a5119da9f53ca45
parent 421559 bf5be9b21c3c33949a6a855a8a5a559cd888a496
child 421561 7bfcf62f68078e4854bfe41575521207f436a7aa
push id104059
push userjandemooij@gmail.com
push dateWed, 06 Jun 2018 10:56:17 +0000
treeherdermozilla-inbound@55c591df0a06 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjonco
bugs1466083
milestone62.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 1466083 part 9 - Introduce JS::IterateRealmsInCompartment and use it in NukeAllWrappersForCompartment. r=jonco
js/src/gc/PublicIterators.cpp
js/src/jsapi.h
js/xpconnect/src/XPCJSRuntime.cpp
--- a/js/src/gc/PublicIterators.cpp
+++ b/js/src/gc/PublicIterators.cpp
@@ -150,8 +150,21 @@ JS::IterateRealms(JSContext* cx, void* d
     AutoTraceSession session(cx->runtime());
 
     Rooted<Realm*> realm(cx);
     for (RealmsIter r(cx->runtime()); !r.done(); r.next()) {
         realm = r;
         (*realmCallback)(cx, data, realm);
     }
 }
+
+JS_PUBLIC_API(void)
+JS::IterateRealmsInCompartment(JSContext* cx, JSCompartment* compartment, void* data,
+                               JS::IterateRealmCallback realmCallback)
+{
+    AutoTraceSession session(cx->runtime());
+
+    Rooted<Realm*> realm(cx);
+    for (RealmsInCompartmentIter r(compartment); !r.done(); r.next()) {
+        realm = r;
+        (*realmCallback)(cx, data, realm);
+    }
+}
--- a/js/src/jsapi.h
+++ b/js/src/jsapi.h
@@ -1100,16 +1100,23 @@ using IterateRealmCallback = void (*)(JS
 /**
  * This function calls |realmCallback| on every realm. Beware that there is no
  * guarantee that the realm will survive after the callback returns. Also,
  * barriers are disabled via the TraceSession.
  */
 extern JS_PUBLIC_API(void)
 IterateRealms(JSContext* cx, void* data, IterateRealmCallback realmCallback);
 
+/**
+ * Like IterateRealms, but only iterates realms in |compartment|.
+ */
+extern JS_PUBLIC_API(void)
+IterateRealmsInCompartment(JSContext* cx, JSCompartment* compartment, void* data,
+                           IterateRealmCallback realmCallback);
+
 } // namespace JS
 
 typedef void (*JSIterateCompartmentCallback)(JSContext* cx, void* data, JSCompartment* compartment);
 
 /**
  * This function calls |compartmentCallback| on every compartment. Beware that
  * there is no guarantee that the compartment will survive after the callback
  * returns. Also, barriers are disabled via the TraceSession.
--- a/js/xpconnect/src/XPCJSRuntime.cpp
+++ b/js/xpconnect/src/XPCJSRuntime.cpp
@@ -572,19 +572,20 @@ NukeAllWrappersForCompartment(JSContext*
                                      js::NukeAllReferences);
 
     // At this point, we should cross-compartment wrappers for the nuked
     // compartment. Set the wasNuked bit so WrapperFactory will return a
     // DeadObjectProxy when asked to create a new wrapper for it, and mark as
     // unscriptable.
     xpc::CompartmentPrivate::Get(compartment)->wasNuked = true;
 
-    // TODO: Loop over all realms in the compartment instead.
-    Realm* realm = GetRealmForCompartment(compartment);
-    xpc::RealmPrivate::Get(realm)->scriptability.Block();
+    auto blockScriptability = [](JSContext*, void*, Handle<Realm*> realm) {
+        xpc::RealmPrivate::Get(realm)->scriptability.Block();
+    };
+    JS::IterateRealmsInCompartment(cx, compartment, nullptr, blockScriptability);
 }
 
 } // namespace xpc
 
 static void
 CompartmentDestroyedCallback(JSFreeOp* fop, JSCompartment* compartment)
 {
     // NB - This callback may be called in JS_DestroyContext, which happens