diff --git a/js/loader/ModuleLoaderBase.cpp b/js/loader/ModuleLoaderBase.cpp index bfcf7ab458ef..10f9ca6a0e59 100644 --- a/js/loader/ModuleLoaderBase.cpp +++ b/js/loader/ModuleLoaderBase.cpp @@ -27,11 +27,9 @@ #include "nsContentUtils.h" #include "nsICacheInfoChannel.h" // nsICacheInfoChannel #include "nsNetUtil.h" // NS_NewURI -#include "nsThreadUtils.h" // GetMainThreadSerialEventTarget #include "xpcpublic.h" using mozilla::Err; -using mozilla::GetMainThreadSerialEventTarget; using mozilla::Preferences; using mozilla::UniquePtr; using mozilla::WrapNotNull; @@ -57,7 +55,8 @@ NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ModuleLoaderBase) NS_INTERFACE_MAP_END NS_IMPL_CYCLE_COLLECTION(ModuleLoaderBase, mFetchedModules, - mDynamicImportRequests, mGlobalObject, mLoader) + mDynamicImportRequests, mGlobalObject, mEventTarget, + mLoader) NS_IMPL_CYCLE_COLLECTING_ADDREF(ModuleLoaderBase) NS_IMPL_CYCLE_COLLECTING_RELEASE(ModuleLoaderBase) @@ -321,7 +320,7 @@ nsresult ModuleLoaderBase::StartOrRestartModuleLoad(ModuleLoadRequest* aRequest, if (aRestart == RestartRequest::No && ModuleMapContainsURL(request->mURI)) { LOG(("ScriptLoadRequest (%p): Waiting for module fetch", aRequest)); WaitForModuleFetch(request->mURI) - ->Then(GetMainThreadSerialEventTarget(), __func__, request, + ->Then(mEventTarget, __func__, request, &ModuleLoadRequest::ModuleLoaded, &ModuleLoadRequest::LoadFailed); return NS_OK; @@ -747,9 +746,8 @@ void ModuleLoaderBase::StartFetchingModuleDependencies( // Wait for all imports to become ready. RefPtr allReady = - mozilla::GenericPromise::All(mozilla::GetMainThreadSerialEventTarget(), - importsReady); - allReady->Then(mozilla::GetMainThreadSerialEventTarget(), __func__, aRequest, + mozilla::GenericPromise::All(mEventTarget, importsReady); + allReady->Then(mEventTarget, __func__, aRequest, &ModuleLoadRequest::DependenciesLoaded, &ModuleLoadRequest::ModuleErrored); } @@ -852,9 +850,13 @@ void ModuleLoaderBase::FinishDynamicImport( } ModuleLoaderBase::ModuleLoaderBase(ScriptLoaderInterface* aLoader, - nsIGlobalObject* aGlobalObject) - : mGlobalObject(aGlobalObject), mLoader(aLoader) { + nsIGlobalObject* aGlobalObject, + nsISerialEventTarget* aEventTarget) + : mGlobalObject(aGlobalObject), + mEventTarget(aEventTarget), + mLoader(aLoader) { MOZ_ASSERT(mGlobalObject); + MOZ_ASSERT(mEventTarget); MOZ_ASSERT(mLoader); EnsureModuleHooksInitialized(); diff --git a/js/loader/ModuleLoaderBase.h b/js/loader/ModuleLoaderBase.h index 23d804fde5b2..bb074c906b61 100644 --- a/js/loader/ModuleLoaderBase.h +++ b/js/loader/ModuleLoaderBase.h @@ -18,6 +18,7 @@ #include "nsCOMPtr.h" #include "nsILoadInfo.h" // nsSecurityFlags #include "nsINode.h" // nsIURI +#include "nsThreadUtils.h" // GetMainThreadSerialEventTarget #include "nsURIHashKey.h" #include "mozilla/CORSMode.h" #include "mozilla/dom/JSExecutionContext.h" @@ -155,6 +156,10 @@ class ModuleLoaderBase : public nsISupports { nsCOMPtr mGlobalObject; + // Event handler used to process MozPromise actions, used internally to wait + // for fetches to finish and for imports to become avilable. + nsCOMPtr mEventTarget; + // https://wicg.github.io/import-maps/#document-acquiring-import-maps // // Each Document has an acquiring import maps boolean. It is initially true. @@ -171,7 +176,9 @@ class ModuleLoaderBase : public nsISupports { NS_DECL_CYCLE_COLLECTING_ISUPPORTS NS_DECL_CYCLE_COLLECTION_CLASS(ModuleLoaderBase) explicit ModuleLoaderBase(ScriptLoaderInterface* aLoader, - nsIGlobalObject* aGlobalObject); + nsIGlobalObject* aGlobalObject, + nsISerialEventTarget* aEventTarget = + mozilla::GetMainThreadSerialEventTarget()); using LoadedScript = JS::loader::LoadedScript; using ScriptFetchOptions = JS::loader::ScriptFetchOptions;