diff --git a/dom/workers/RuntimeService.cpp b/dom/workers/RuntimeService.cpp index a1cdb5b1aef8..ce909ca1ac49 100644 --- a/dom/workers/RuntimeService.cpp +++ b/dom/workers/RuntimeService.cpp @@ -1059,7 +1059,7 @@ public: MOZ_ASSERT(aWorkerPrivate); } - void Shutdown() override + void Shutdown(JSContext* cx) override { // The CC is shut down, and the superclass destructor will GC, so make sure // we don't try to CC again. diff --git a/js/xpconnect/src/XPCJSContext.cpp b/js/xpconnect/src/XPCJSContext.cpp index 34a23a4080b2..1c18452cb861 100644 --- a/js/xpconnect/src/XPCJSContext.cpp +++ b/js/xpconnect/src/XPCJSContext.cpp @@ -790,7 +790,7 @@ XPCJSContext::Initialize() if (!xpc_LocalizeContext(cx)) NS_RUNTIMEABORT("xpc_LocalizeContext failed."); - Runtime()->Initialize(); + Runtime()->Initialize(cx); return NS_OK; } diff --git a/js/xpconnect/src/XPCJSRuntime.cpp b/js/xpconnect/src/XPCJSRuntime.cpp index 253a8673e1e2..916706ad3f75 100644 --- a/js/xpconnect/src/XPCJSRuntime.cpp +++ b/js/xpconnect/src/XPCJSRuntime.cpp @@ -673,7 +673,8 @@ void XPCJSRuntime::TraceNativeBlackRoots(JSTracer* trc) for (e = mObjectHolderRoots; e; e = e->GetNextRoot()) static_cast(e)->TraceJS(trc); - dom::TraceBlackJS(trc, JS_GetGCParameter(MainContext(), JSGC_NUMBER), + JSContext* cx = XPCJSContext::Get()->Context(); + dom::TraceBlackJS(trc, JS_GetGCParameter(cx, JSGC_NUMBER), nsXPConnect::XPConnect()->IsShuttingDown()); } @@ -1075,8 +1076,7 @@ CompartmentPrivate::SizeOfIncludingThis(MallocSizeOf mallocSizeOf) static void ReloadPrefsCallback(const char* pref, void* data) { - XPCJSRuntime* xpcrt = reinterpret_cast(data); - JSContext* cx = xpcrt->MainContext(); + JSContext* cx = XPCJSContext::Get()->Context(); bool safeMode = false; nsCOMPtr xr = do_GetService("@mozilla.org/xre/runtime;1"); @@ -1168,21 +1168,17 @@ ReloadPrefsCallback(const char* pref, void* data) } void -XPCJSRuntime::Shutdown() +XPCJSRuntime::Shutdown(JSContext* cx) { - // Elsewhere we abort immediately if XPCJSRuntime initialization fails. - // Therefore the context must be non-null. - MOZ_ASSERT(MainContext()); - // This destructor runs before ~CycleCollectedJSContext, which does the // actual JS_DestroyContext() call. But destroying the context triggers // one final GC, which can call back into the context with various // callbacks if we aren't careful. Null out the relevant callbacks. - JS_RemoveFinalizeCallback(MainContext(), FinalizeCallback); - JS_RemoveWeakPointerZonesCallback(MainContext(), WeakPointerZonesCallback); - JS_RemoveWeakPointerCompartmentCallback(MainContext(), WeakPointerCompartmentCallback); + JS_RemoveFinalizeCallback(cx, FinalizeCallback); + JS_RemoveWeakPointerZonesCallback(cx, WeakPointerZonesCallback); + JS_RemoveWeakPointerCompartmentCallback(cx, WeakPointerCompartmentCallback); - JS::SetGCSliceCallback(MainContext(), mPrevGCSliceCallback); + JS::SetGCSliceCallback(cx, mPrevGCSliceCallback); // We don't want to track wrapped JS roots after this point since we're // making them !IsValid anyway through SystemIsBeingShutDown. @@ -1212,10 +1208,10 @@ XPCJSRuntime::Shutdown() mDyingWrappedNativeProtoMap = nullptr; Preferences::UnregisterPrefixCallback(ReloadPrefsCallback, - JS_OPTIONS_DOT_STR, this); + JS_OPTIONS_DOT_STR); #ifdef FUZZING - Preferences::UnregisterCallback(ReloadPrefsCallback, "fuzzing.enabled", this); + Preferences::UnregisterCallback(ReloadPrefsCallback, "fuzzing.enabled"); #endif } @@ -2472,7 +2468,8 @@ JSReporter::CollectReports(WindowPaths* windowPaths, XPCJSRuntimeStats rtStats(windowPaths, topWindowPaths, getLocations, anonymize); OrphanReporter orphanReporter(XPCConvert::GetISupportsFromJSObject); - if (!JS::CollectRuntimeStats(xpcrt->MainContext(), &rtStats, &orphanReporter, + JSContext* cx = XPCJSContext::Get()->Context(); + if (!JS::CollectRuntimeStats(cx, &rtStats, &orphanReporter, anonymize)) { return; @@ -2961,11 +2958,8 @@ XPCJSRuntime::Get() } void -XPCJSRuntime::Initialize() +XPCJSRuntime::Initialize(JSContext* cx) { - MOZ_ASSERT(MainContext()); - JSContext* cx = MainContext(); - mUnprivilegedJunkScope.init(cx, nullptr); mPrivilegedJunkScope.init(cx, nullptr); mCompilationScope.init(cx, nullptr); @@ -3025,12 +3019,12 @@ XPCJSRuntime::Initialize() mozilla::RegisterJSSizeOfTab(JSSizeOfTab); // Watch for the JS boolean options. - ReloadPrefsCallback(nullptr, this); + ReloadPrefsCallback(nullptr, nullptr); Preferences::RegisterPrefixCallback(ReloadPrefsCallback, - JS_OPTIONS_DOT_STR, this); + JS_OPTIONS_DOT_STR); #ifdef FUZZING - Preferences::RegisterCallback(ReloadPrefsCallback, "fuzzing.enabled", this); + Preferences::RegisterCallback(ReloadPrefsCallback, "fuzzing.enabled"); #endif } @@ -3108,7 +3102,6 @@ XPCJSRuntime::DebugDump(int16_t depth) depth--; XPC_LOG_ALWAYS(("XPCJSRuntime @ %p", this)); XPC_LOG_INDENT(); - XPC_LOG_ALWAYS(("mJSContext @ %p", MainContext())); XPC_LOG_ALWAYS(("mWrappedJSClassMap @ %p with %d wrapperclasses(s)", mWrappedJSClassMap, mWrappedJSClassMap->Count())); @@ -3213,7 +3206,7 @@ void XPCJSRuntime::InitSingletonScopes() { // This all happens very early, so we don't bother with cx pushing. - JSContext* cx = MainContext(); + JSContext* cx = XPCJSContext::Get()->Context(); JSAutoRequest ar(cx); RootedValue v(cx); nsresult rv; diff --git a/js/xpconnect/src/XPCWrappedJS.cpp b/js/xpconnect/src/XPCWrappedJS.cpp index aace475c875f..abd31697891c 100644 --- a/js/xpconnect/src/XPCWrappedJS.cpp +++ b/js/xpconnect/src/XPCWrappedJS.cpp @@ -470,8 +470,10 @@ XPCJSRuntime::AssertInvalidWrappedJSNotInTable(nsXPCWrappedJS* wrapper) const #ifdef DEBUG if (!wrapper->IsValid()) { MOZ_ASSERT(!GetMultiCompartmentWrappedJSMap()->HasWrapper(wrapper)); - if (!mGCIsRunning) - JS_IterateCompartments(MainContext(), wrapper, NotHasWrapperAssertionCallback); + if (!mGCIsRunning) { + JSContext* cx = XPCJSContext::Get()->Context(); + JS_IterateCompartments(cx, wrapper, NotHasWrapperAssertionCallback); + } } #endif } diff --git a/js/xpconnect/src/xpcprivate.h b/js/xpconnect/src/xpcprivate.h index 5875619bb1da..f6a0729332d9 100644 --- a/js/xpconnect/src/xpcprivate.h +++ b/js/xpconnect/src/xpcprivate.h @@ -642,8 +642,8 @@ private: explicit XPCJSRuntime(JSContext* aCx); MOZ_IS_CLASS_INIT - void Initialize(); - void Shutdown() override; + void Initialize(JSContext* cx); + void Shutdown(JSContext* cx) override; void ReleaseIncrementally(nsTArray& array); diff --git a/xpcom/base/CycleCollectedJSContext.cpp b/xpcom/base/CycleCollectedJSContext.cpp index 0cb36ee61743..7be959ad0d62 100644 --- a/xpcom/base/CycleCollectedJSContext.cpp +++ b/xpcom/base/CycleCollectedJSContext.cpp @@ -68,7 +68,7 @@ CycleCollectedJSContext::~CycleCollectedJSContext() return; } - mRuntime->Shutdown(); + mRuntime->Shutdown(mJSContext); // Last chance to process any events. ProcessMetastableStateQueue(mBaseRecursionDepth); diff --git a/xpcom/base/CycleCollectedJSRuntime.cpp b/xpcom/base/CycleCollectedJSRuntime.cpp index abf5a474aab7..f25583fc926b 100644 --- a/xpcom/base/CycleCollectedJSRuntime.cpp +++ b/xpcom/base/CycleCollectedJSRuntime.cpp @@ -550,7 +550,7 @@ CycleCollectedJSRuntime::CycleCollectedJSRuntime(JSContext* aMainContext) } void -CycleCollectedJSRuntime::Shutdown() +CycleCollectedJSRuntime::Shutdown(JSContext* cx) { } @@ -1248,7 +1248,7 @@ CycleCollectedJSRuntime::DeferredFinalize(nsISupports* aSupports) void CycleCollectedJSRuntime::DumpJSHeap(FILE* aFile) { - js::DumpHeap(MainContext(), aFile, js::CollectNurseryBeforeDump); + js::DumpHeap(mJSContext, aFile, js::CollectNurseryBeforeDump); } IncrementalFinalizeRunnable::IncrementalFinalizeRunnable(CycleCollectedJSRuntime* aRt, @@ -1466,7 +1466,7 @@ void CycleCollectedJSRuntime::PrepareWaitingZonesForGC() { if (mZonesWaitingForGC.Count() == 0) { - JS::PrepareForFullGC(MainContext()); + JS::PrepareForFullGC(mJSContext); } else { for (auto iter = mZonesWaitingForGC.Iter(); !iter.Done(); iter.Next()) { JS::PrepareZoneForGC(iter.Get()->GetKey()); diff --git a/xpcom/base/CycleCollectedJSRuntime.h b/xpcom/base/CycleCollectedJSRuntime.h index 0e794c79e54b..4b6d06ab7c51 100644 --- a/xpcom/base/CycleCollectedJSRuntime.h +++ b/xpcom/base/CycleCollectedJSRuntime.h @@ -110,7 +110,7 @@ protected: CycleCollectedJSRuntime(JSContext* aMainContext); virtual ~CycleCollectedJSRuntime(); - virtual void Shutdown(); + virtual void Shutdown(JSContext* cx); size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; void UnmarkSkippableJSHolders(); @@ -275,14 +275,6 @@ public: // full GC. void PrepareWaitingZonesForGC(); - // The main context is the first one created in the runtime and the last one - // destroyed. - JSContext* MainContext() const - { - MOZ_ASSERT(mJSContext); - return mJSContext; - } - // Get the current thread's CycleCollectedJSRuntime. Returns null if there // isn't one. static CycleCollectedJSRuntime* Get();