Bug 1803810 - Part 3: Add methods to copy and move imported modules. r=jonco

In non-shared global, the sync load is performed by separate SyncModuleLoader,
with the following algorithm:
  1. copy all loaded modules from async module loader, so that they can be
     used in the module graph in SyncModuleLoader
  2. import a module graph
  3. move all modules to async module loader

ModuleLoaderBase::CopyModulesTo is for step 1, and
ModuleLoaderBase::MoveModulesTo is for step 3.

The consumer should assert that there's no fetching modules.

Differential Revision: https://phabricator.services.mozilla.com/D199455
This commit is contained in:
Tooru Fujisawa
2024-02-13 14:34:21 +00:00
parent cd29228129
commit b51d74f7cd
2 changed files with 51 additions and 0 deletions

View File

@@ -1385,6 +1385,42 @@ void ModuleLoaderBase::RegisterImportMap(UniquePtr<ImportMap> 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> 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> 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

View File

@@ -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: