Bug 1777694 - Cache the result of JSM to ESM fallback in the shim. r=jonco

Differential Revision: https://phabricator.services.mozilla.com/D150874
This commit is contained in:
Tooru Fujisawa committed 2022-07-04 10:33:42 +00:00
1 parent 5eefd5b14e
commit 1538283319
2 files changed
+81

No files matched your search

+50
View File
@@ -263,6 +263,7 @@ NS_IMPL_ISUPPORTS(mozJSModuleLoader, nsIMemoryReporter)
mozJSModuleLoader::mozJSModuleLoader()
: mImports(16),
mInProgressImports(16),
mFallbackImports(16),
mLocations(16),
mInitialized(false),
mLoaderGlobal(dom::RootingCx()) {
@@ -457,6 +458,7 @@ size_t mozJSModuleLoader::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) {
n += SizeOfTableExcludingThis(mImports, aMallocSizeOf);
n += mLocations.ShallowSizeOfExcludingThis(aMallocSizeOf);
n += SizeOfTableExcludingThis(mInProgressImports, aMallocSizeOf);
n += SizeOfTableExcludingThis(mFallbackImports, aMallocSizeOf);
return n;
}
@@ -949,6 +951,7 @@ void mozJSModuleLoader::UnloadModules() {
mLoaderGlobal = nullptr;
}
mFallbackImports.Clear();
mInProgressImports.Clear();
mImports.Clear();
mLocations.Clear();
@@ -1341,6 +1344,16 @@ nsresult mozJSModuleLoader::Import(JSContext* aCx, const nsACString& aLocation,
return NS_ERROR_ILLEGAL_DURING_SHUTDOWN;
}
// If we've hit file-not-found and fallback was successful,
// return the cached data.
bool aFound;
rv = TryCachedFallbackToImportESModule(
aCx, aLocation, aModuleGlobal, aModuleExports, aIgnoreExports, &aFound);
NS_ENSURE_SUCCESS(rv, rv);
if (aFound) {
return NS_OK;
}
newEntry = MakeUnique<ModuleEntry>(RootingContext::get(aCx));
// Note: This implies EnsureURI().
@@ -1469,7 +1482,15 @@ nsresult mozJSModuleLoader::TryFallbackToImportESModule(
if (!globalProxy) {
return NS_ERROR_FAILURE;
}
// Cache the redirect to use in subsequent imports.
ModuleLoaderInfo info(aLocation);
auto newEntry = MakeUnique<FallbackModuleEntry>(RootingContext::get(aCx));
newEntry->globalProxy = globalProxy;
newEntry->moduleNamespace = moduleNamespace;
mFallbackImports.InsertOrUpdate(info.Key(), std::move(newEntry));
}
if (!JS_WrapObject(aCx, &globalProxy)) {
return NS_ERROR_FAILURE;
}
@@ -1486,6 +1507,35 @@ nsresult mozJSModuleLoader::TryFallbackToImportESModule(
return NS_OK;
}
nsresult mozJSModuleLoader::TryCachedFallbackToImportESModule(
JSContext* aCx, const nsACString& aLocation,
JS::MutableHandleObject aModuleGlobal,
JS::MutableHandleObject aModuleExports, bool aIgnoreExports, bool* aFound) {
ModuleLoaderInfo info(aLocation);
FallbackModuleEntry* fallbackMod;
if (!mFallbackImports.Get(info.Key(), &fallbackMod)) {
*aFound = false;
return NS_OK;
}
JS::RootedObject globalProxy(aCx, fallbackMod->globalProxy);
if (!JS_WrapObject(aCx, &globalProxy)) {
return NS_ERROR_FAILURE;
}
aModuleGlobal.set(globalProxy);
if (!aIgnoreExports) {
JS::RootedObject exports(aCx, fallbackMod->moduleNamespace);
if (!JS_WrapObject(aCx, &exports)) {
return NS_ERROR_FAILURE;
}
aModuleExports.set(exports);
}
*aFound = true;
return NS_OK;
}
nsresult mozJSModuleLoader::ImportESModule(
JSContext* aCx, const nsACString& aLocation,
JS::MutableHandleObject aModuleNamespace) {
+31
View File
@@ -81,6 +81,16 @@ class mozJSModuleLoader final : public nsIMemoryReporter {
JS::MutableHandleObject aModuleExports,
bool aIgnoreExports);
// If the request was handled by fallback before, fills the output and
// sets *aFound to true and returns NS_OK.
// If the request wasn't yet handled by fallback, sets *Found to false
// and returns NS_OK.
nsresult TryCachedFallbackToImportESModule(
JSContext* aCx, const nsACString& aResourceURI,
JS::MutableHandleObject aModuleGlobal,
JS::MutableHandleObject aModuleExports, bool aIgnoreExports,
bool* aFound);
nsresult Unload(const nsACString& aResourceURI);
nsresult IsModuleLoaded(const nsACString& aResourceURI, bool* aRetval);
nsresult IsJSModuleLoaded(const nsACString& aResourceURI, bool* aRetval);
@@ -184,11 +194,32 @@ class mozJSModuleLoader final : public nsIMemoryReporter {
#endif
};
class FallbackModuleEntry {
public:
explicit FallbackModuleEntry(JS::RootingContext* aRootingCx)
: globalProxy(aRootingCx), moduleNamespace(aRootingCx) {}
~FallbackModuleEntry() { Clear(); }
void Clear() {
globalProxy = nullptr;
moduleNamespace = nullptr;
}
size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const {
return aMallocSizeOf(this);
}
JS::PersistentRootedObject globalProxy;
JS::PersistentRootedObject moduleNamespace;
};
nsresult ExtractExports(JSContext* aCx, ModuleLoaderInfo& aInfo,
ModuleEntry* aMod, JS::MutableHandleObject aExports);
nsClassHashtable<nsCStringHashKey, ModuleEntry> mImports;
nsTHashMap<nsCStringHashKey, ModuleEntry*> mInProgressImports;
nsClassHashtable<nsCStringHashKey, FallbackModuleEntry> mFallbackImports;
// A map of on-disk file locations which are loaded as modules to the
// pre-resolved URIs they were loaded from. Used to prevent the same file