diff --git a/js/loader/ModuleLoaderBase.cpp b/js/loader/ModuleLoaderBase.cpp index 53717944d23b..cc5284702979 100644 --- a/js/loader/ModuleLoaderBase.cpp +++ b/js/loader/ModuleLoaderBase.cpp @@ -1385,6 +1385,42 @@ void ModuleLoaderBase::RegisterImportMap(UniquePtr aImportMap) { mImportMap = std::move(aImportMap); } +void ModuleLoaderBase::CopyModulesTo(ModuleLoaderBase* aDest) { + MOZ_ASSERT(aDest->mFetchingModules.IsEmpty()); + MOZ_ASSERT(aDest->mFetchedModules.IsEmpty()); + MOZ_ASSERT(mFetchingModules.IsEmpty()); + + for (const auto& entry : mFetchedModules) { + RefPtr moduleScript = entry.GetData(); + if (!moduleScript) { + continue; + } + aDest->mFetchedModules.InsertOrUpdate(entry.GetKey(), moduleScript); + } +} + +void ModuleLoaderBase::MoveModulesTo(ModuleLoaderBase* aDest) { + MOZ_ASSERT(mFetchingModules.IsEmpty()); + MOZ_ASSERT(aDest->mFetchingModules.IsEmpty()); + + for (const auto& entry : mFetchedModules) { + RefPtr moduleScript = entry.GetData(); + if (!moduleScript) { + continue; + } + +#ifdef DEBUG + if (auto existingEntry = aDest->mFetchedModules.Lookup(entry.GetKey())) { + MOZ_ASSERT(moduleScript == existingEntry.Data()); + } +#endif + + aDest->mFetchedModules.InsertOrUpdate(entry.GetKey(), moduleScript); + } + + mFetchedModules.Clear(); +} + #undef LOG #undef LOG_ENABLED diff --git a/js/loader/ModuleLoaderBase.h b/js/loader/ModuleLoaderBase.h index 1b66fed3a348..86879db84bb9 100644 --- a/js/loader/ModuleLoaderBase.h +++ b/js/loader/ModuleLoaderBase.h @@ -351,6 +351,21 @@ class ModuleLoaderBase : public nsISupports { void ResetOverride(); + // Copy fetched modules to `aDest`. + // `this` shouldn't have any fetching. + // `aDest` shouldn't have any fetching or fetched modules. + // + // This is used when starting sync module load, to replicate the module cache + // in the sync module loader pointed by `aDest`. + void CopyModulesTo(ModuleLoaderBase* aDest); + + // Move all fetched modules to `aDest`. + // Both `this` and `aDest` shouldn't have any fetching. + // + // This is used when finishing sync module load, to reflect the loaded modules + // to the async module loader pointed by `aDest`. + void MoveModulesTo(ModuleLoaderBase* aDest); + // Internal methods. private: