Bug 1769029 - Support ESM loaded by shim in Cu.loadedModules. r=yulia

Differential Revision: https://phabricator.services.mozilla.com/D146169
This commit is contained in:
Tooru Fujisawa 2022-05-13 22:02:41 +00:00
Родитель 980d04aebc
Коммит bb1d312b22
6 изменённых файлов: 70 добавлений и 2 удалений

Просмотреть файл

@ -355,6 +355,20 @@ bool ModuleLoaderBase::IsModuleFetched(nsIURI* aURL) const {
return mFetchedModules.Contains(aURL);
}
nsresult ModuleLoaderBase::GetFetchedModuleURLs(nsTArray<nsCString>& aURLs) {
for (const auto& entry : mFetchedModules) {
nsIURI* uri = entry.GetData()->BaseURL();
nsAutoCString spec;
nsresult rv = uri->GetSpec(spec);
NS_ENSURE_SUCCESS(rv, rv);
aURLs.AppendElement(spec);
}
return NS_OK;
}
void ModuleLoaderBase::SetModuleFetchStarted(ModuleLoadRequest* aRequest) {
// Update the module map to indicate that a module is currently being fetched.

Просмотреть файл

@ -277,6 +277,8 @@ class ModuleLoaderBase : public nsISupports {
// Returns true if the module for given URL is already fetched.
bool IsModuleFetched(nsIURI* aURL) const;
nsresult GetFetchedModuleURLs(nsTArray<nsCString>& aURLs);
// Internal methods.
private:

Просмотреть файл

@ -114,6 +114,19 @@ static bool IsJSM(const nsACString& aLocation) {
return ext == ".jsm";
}
static bool IsMJS(const nsACString& aLocation) {
if (aLocation.Length() < 4) {
return false;
}
const auto ext = Substring(aLocation, aLocation.Length() - 4);
return ext == ".mjs";
}
static void ToJSM(const nsACString& aLocation, nsAutoCString& aOut) {
aOut = Substring(aLocation, 0, aLocation.Length() - 4);
aOut += ".jsm";
}
static void ToMJS(const nsACString& aLocation, nsAutoCString& aOut) {
aOut = Substring(aLocation, 0, aLocation.Length() - 4);
aOut += ".mjs";
@ -1132,6 +1145,30 @@ void mozJSComponentLoader::GetLoadedModules(
}
}
nsresult mozJSComponentLoader::GetLoadedESModules(
nsTArray<nsCString>& aLoadedModules) {
return mModuleLoader->GetFetchedModuleURLs(aLoadedModules);
}
nsresult mozJSComponentLoader::GetLoadedJSAndESModules(
nsTArray<nsCString>& aLoadedModules) {
GetLoadedModules(aLoadedModules);
nsTArray<nsCString> modules;
nsresult rv = GetLoadedESModules(modules);
NS_ENSURE_SUCCESS(rv, rv);
for (const auto& location : modules) {
if (IsMJS(location)) {
nsAutoCString jsmLocation;
ToJSM(location, jsmLocation);
aLoadedModules.AppendElement(jsmLocation);
}
}
return NS_OK;
}
void mozJSComponentLoader::GetLoadedComponents(
nsTArray<nsCString>& aLoadedComponents) {
aLoadedComponents.SetCapacity(mModules.Count());

Просмотреть файл

@ -41,7 +41,15 @@ class mozJSComponentLoader final : public nsIMemoryReporter {
NS_DECL_ISUPPORTS
NS_DECL_NSIMEMORYREPORTER
// Returns the list of all JSMs.
void GetLoadedModules(nsTArray<nsCString>& aLoadedModules);
// Returns the list of all ESMs.
nsresult GetLoadedESModules(nsTArray<nsCString>& aLoadedModules);
// Returns the list of all JSMs and ESMs.
nsresult GetLoadedJSAndESModules(nsTArray<nsCString>& aLoadedModules);
void GetLoadedComponents(nsTArray<nsCString>& aLoadedComponents);
nsresult GetModuleImportStack(const nsACString& aLocation,
nsACString& aRetval);

Просмотреть файл

@ -2522,8 +2522,7 @@ nsXPCComponents_Utils::CreateHTMLCopyEncoder(
NS_IMETHODIMP
nsXPCComponents_Utils::GetLoadedModules(nsTArray<nsCString>& aLoadedModules) {
mozJSComponentLoader::Get()->GetLoadedModules(aLoadedModules);
return NS_OK;
return mozJSComponentLoader::Get()->GetLoadedJSAndESModules(aLoadedModules);
}
NS_IMETHODIMP

Просмотреть файл

@ -303,20 +303,26 @@ add_task(function test_Cu_import_not_exported_shim() {
add_task(function test_Cu_isModuleLoaded_shim() {
Assert.equal(Cu.isModuleLoaded("resource://test/esmified-5.jsm"), false);
Assert.equal(Cu.loadedModules.includes("resource://test/esmified-5.jsm"), false);
Assert.equal(Cu.isModuleLoaded("resource://test/esmified-5.mjs"), false);
Assert.equal(Cu.loadedModules.includes("resource://test/esmified-5.mjs"), false);
Cu.import("resource://test/esmified-5.jsm", {});
Assert.equal(Cu.isModuleLoaded("resource://test/esmified-5.jsm"), true);
Assert.equal(Cu.loadedModules.includes("resource://test/esmified-5.jsm"), true);
// This is false because Cu.isModuleLoaded does not support ESM directly
// (bug 1768819)
Assert.equal(Cu.isModuleLoaded("resource://test/esmified-5.mjs"), false);
Assert.equal(Cu.loadedModules.includes("resource://test/esmified-5.mjs"), false);
});
add_task(function test_Cu_isModuleLoaded_no_shim() {
Assert.equal(Cu.isModuleLoaded("resource://test/esmified-6.jsm"), false);
Assert.equal(Cu.loadedModules.includes("resource://test/esmified-6.jsm"), false);
Assert.equal(Cu.isModuleLoaded("resource://test/esmified-6.mjs"), false);
Assert.equal(Cu.loadedModules.includes("resource://test/esmified-6.mjs"), false);
ChromeUtils.importModule("resource://test/esmified-6.mjs");
@ -324,8 +330,10 @@ add_task(function test_Cu_isModuleLoaded_no_shim() {
// query that accesses the ESM-ified module returns the existence of
// ESM.
Assert.equal(Cu.isModuleLoaded("resource://test/esmified-6.jsm"), true);
Assert.equal(Cu.loadedModules.includes("resource://test/esmified-6.jsm"), true);
// This is false because Cu.isModuleLoaded does not support ESM directly
// (bug 1768819)
Assert.equal(Cu.isModuleLoaded("resource://test/esmified-6.mjs"), false);
Assert.equal(Cu.loadedModules.includes("resource://test/esmified-6.mjs"), false);
});