diff --git a/Source/JavaScriptCore/runtime/JSModuleLoader.cpp b/Source/JavaScriptCore/runtime/JSModuleLoader.cpp index 5a050d840a722..ef4f008c26d6a 100644 --- a/Source/JavaScriptCore/runtime/JSModuleLoader.cpp +++ b/Source/JavaScriptCore/runtime/JSModuleLoader.cpp @@ -1060,7 +1060,17 @@ void JSModuleLoader::finishLoadingImportedModule(JSGlobalObject* globalObject, c auto scope = DECLARE_THROW_SCOPE(vm); // 1. If result is a normal completion, then +#if USE(BUN_JSC_ADDITIONS) + // hostLoadImportedModule() relies on the realm's [[LoadedModules]] holding only what the registry holds. removeEntry() + // and clearAll() take a module out of both, and a load of it that was in flight then must not put it back into one. + auto isRegistered = [&](AbstractModuleRecord* record) { + ModuleRegistryEntry* entry = getRegisteredMayBeNull(record->moduleKey(), moduleRequest.type()); + return entry && entry->record() == record; + }; + if (auto* resultRecord = std::get_if(&result); resultRecord && (!referrer.isRealm() || isRegistered(*resultRecord))) { +#else if (auto* resultRecord = std::get_if(&result)) { +#endif JSCell* owner = nullptr; auto& loadedModules = [&] -> ModuleMap & { diff --git a/Source/JavaScriptCore/runtime/JSModuleLoader.h b/Source/JavaScriptCore/runtime/JSModuleLoader.h index c7790bd4e3bbe..cd79f6a000baa 100644 --- a/Source/JavaScriptCore/runtime/JSModuleLoader.h +++ b/Source/JavaScriptCore/runtime/JSModuleLoader.h @@ -217,7 +217,8 @@ class JSModuleLoader final : public JSCell { auto* impl = key.impl(); Locker locker { cellLock() }; // visitChildren iterates these forgetPrelinkedRecordsWithKey(impl); - m_loadedModules.removeIf([&](auto& entry) { return entry.key.first == impl; }); + // Keyed by the specifier that was asked for, which resolve() may have turned into another key. + m_loadedModules.removeIf([&](auto& entry) { return entry.key.first == impl || entry.value.m_module->moduleKey().impl() == impl; }); m_resolutionFailures.removeIf([&](auto& entry) { return entry.key.first == impl || entry.key.second == impl; }); return m_moduleMap.removeIf([&](auto& entry) { if (entry.key.first != impl)