Bug 1768819 - Add Cu.{isJSModuleLoaded,isESModuleLoaded,loadedJSModules,loadedESModules}. r=kmag

Added 4 new functions, into Cu, for the following reasons:
  * Cu.isModuleLoaded and Cu.loadedModule is kept for backward compatibility
  * Cu.isModuleLoaded and Cu.loadedModule uses shim (bug 1769029 and
    bug 1768922), and loaded ESM (.sys.mjs) is visible as JSM (`.jsm`) there

Differential Revision: https://phabricator.services.mozilla.com/D146167
This commit is contained in:
Tooru Fujisawa 2022-06-17 04:59:14 +00:00
Родитель 088b6d97cd
Коммит 47f69a4984
12 изменённых файлов: 235 добавлений и 4 удалений

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

@ -213,8 +213,8 @@ interface nsIXPCComponents_Utils : nsISupports
jsval import(in AUTF8String aResourceURI, [optional] in jsval targetObj);
/**
* Returns true if the js file located at 'registryLocation' location has
* been loaded previously via the import method above. Returns false
* Returns true if the JSM is loaded into the system global previously via
* the import method above, or corresponding ESM is loaded. Returns false
* otherwise.
*
* @param resourceURI A resource:// URI string representing the location of
@ -224,6 +224,19 @@ interface nsIXPCComponents_Utils : nsISupports
*/
boolean isModuleLoaded(in AUTF8String aResourceURI);
/**
* Returns true if the JSM is loaded into the system global previously via
* the import method above. Returns false otherwise.
*/
boolean isJSModuleLoaded(in AUTF8String aResourceURI);
/**
* Returns true if the ESM is loaded into the system global previously via
* the ChromeUtils.importESModule method etc. Returns false otherwise.
*/
boolean isESModuleLoaded(in AUTF8String aResourceURI);
/*
* Unloads the JS module at 'registryLocation'. Existing references to the
* module will continue to work but any subsequent import of the module will
@ -707,9 +720,19 @@ interface nsIXPCComponents_Utils : nsISupports
// These attributes are for startup testing purposes. They are not expected
// to be used for production code.
// Array of the URI of JSM and ESM loaded, converting ESM URI into JSM URI.
readonly attribute Array<ACString> loadedModules;
readonly attribute Array<ACString> loadedComponents;
// Array of the URI of JSM loaded.
readonly attribute Array<ACString> loadedJSModules;
// Array of the URI of ESM loaded.
readonly attribute Array<ACString> loadedESModules;
// These 2 functions will only return useful values if the
// "browser.startup.record" preference was true at the time the JS file
// was loaded.

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

@ -1188,6 +1188,40 @@ nsresult mozJSComponentLoader::IsModuleLoaded(const nsACString& aLocation,
return NS_OK;
}
nsresult mozJSComponentLoader::IsJSModuleLoaded(const nsACString& aLocation,
bool* retval) {
MOZ_ASSERT(nsContentUtils::IsCallerChrome());
mInitialized = true;
ComponentLoaderInfo info(aLocation);
if (mImports.Get(info.Key())) {
*retval = true;
return NS_OK;
}
*retval = false;
return NS_OK;
}
nsresult mozJSComponentLoader::IsESModuleLoaded(const nsACString& aLocation,
bool* retval) {
MOZ_ASSERT(nsContentUtils::IsCallerChrome());
mInitialized = true;
ComponentLoaderInfo info(aLocation);
nsresult rv = info.EnsureURI();
NS_ENSURE_SUCCESS(rv, rv);
if (mModuleLoader->IsModuleFetched(info.URI())) {
*retval = true;
return NS_OK;
}
*retval = false;
return NS_OK;
}
void mozJSComponentLoader::GetLoadedModules(
nsTArray<nsCString>& aLoadedModules) {
aLoadedModules.SetCapacity(mImports.Count());

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

@ -92,6 +92,8 @@ class mozJSComponentLoader final : public nsIMemoryReporter {
nsresult Unload(const nsACString& aResourceURI);
nsresult IsModuleLoaded(const nsACString& aResourceURI, bool* aRetval);
nsresult IsJSModuleLoaded(const nsACString& aResourceURI, bool* aRetval);
nsresult IsESModuleLoaded(const nsACString& aResourceURI, bool* aRetval);
bool IsLoaderGlobal(JSObject* aObj) { return mLoaderGlobal == aObj; }
// Public methods for use from ComponentModuleLoader.

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

@ -1561,11 +1561,27 @@ nsXPCComponents_Utils::Import(const nsACString& registryLocation,
}
NS_IMETHODIMP
nsXPCComponents_Utils::IsModuleLoaded(const nsACString& registryLocation,
nsXPCComponents_Utils::IsModuleLoaded(const nsACString& aResourceURI,
bool* retval) {
RefPtr<mozJSComponentLoader> moduleloader = mozJSComponentLoader::Get();
MOZ_ASSERT(moduleloader);
return moduleloader->IsModuleLoaded(registryLocation, retval);
return moduleloader->IsModuleLoaded(aResourceURI, retval);
}
NS_IMETHODIMP
nsXPCComponents_Utils::IsJSModuleLoaded(const nsACString& aResourceURI,
bool* retval) {
RefPtr<mozJSComponentLoader> moduleloader = mozJSComponentLoader::Get();
MOZ_ASSERT(moduleloader);
return moduleloader->IsJSModuleLoaded(aResourceURI, retval);
}
NS_IMETHODIMP
nsXPCComponents_Utils::IsESModuleLoaded(const nsACString& aResourceURI,
bool* retval) {
RefPtr<mozJSComponentLoader> moduleloader = mozJSComponentLoader::Get();
MOZ_ASSERT(moduleloader);
return moduleloader->IsESModuleLoaded(aResourceURI, retval);
}
NS_IMETHODIMP
@ -2532,6 +2548,19 @@ nsXPCComponents_Utils::GetLoadedComponents(
return NS_OK;
}
NS_IMETHODIMP
nsXPCComponents_Utils::GetLoadedJSModules(
nsTArray<nsCString>& aLoadedJSModules) {
mozJSComponentLoader::Get()->GetLoadedModules(aLoadedJSModules);
return NS_OK;
}
NS_IMETHODIMP
nsXPCComponents_Utils::GetLoadedESModules(
nsTArray<nsCString>& aLoadedESModules) {
return mozJSComponentLoader::Get()->GetLoadedESModules(aLoadedESModules);
}
NS_IMETHODIMP
nsXPCComponents_Utils::GetModuleImportStack(const nsACString& aLocation,
nsACString& aRetval) {

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

@ -0,0 +1 @@
export function test() {}

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

@ -0,0 +1 @@
export function test() {}

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

@ -0,0 +1 @@
export function test() {}

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

@ -0,0 +1,2 @@
function test() {}
var EXPORTED_SYMBOLS = ["test"];

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

@ -0,0 +1,2 @@
function test() {}
var EXPORTED_SYMBOLS = ["test"];

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

@ -0,0 +1,2 @@
function test() {}
var EXPORTED_SYMBOLS = ["test"];

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

@ -0,0 +1,127 @@
add_task(function test_JSModule() {
const URL1 = "resource://test/jsm_loaded-1.jsm";
const URL2 = "resource://test/jsm_loaded-2.jsm";
const URL3 = "resource://test/jsm_loaded-3.jsm";
Assert.ok(!Cu.loadedJSModules.includes(URL1));
Assert.ok(!Cu.isJSModuleLoaded(URL1));
Assert.ok(!Cu.loadedJSModules.includes(URL2));
Assert.ok(!Cu.isJSModuleLoaded(URL2));
Assert.ok(!Cu.loadedJSModules.includes(URL3));
Assert.ok(!Cu.isJSModuleLoaded(URL3));
Assert.ok(!Cu.loadedESModules.includes(URL1));
Assert.ok(!Cu.isESModuleLoaded(URL1));
Assert.ok(!Cu.loadedESModules.includes(URL2));
Assert.ok(!Cu.isESModuleLoaded(URL2));
Assert.ok(!Cu.loadedESModules.includes(URL3));
Assert.ok(!Cu.isESModuleLoaded(URL3));
ChromeUtils.import(URL1);
Assert.ok(Cu.loadedJSModules.includes(URL1));
Assert.ok(Cu.isJSModuleLoaded(URL1));
Assert.ok(!Cu.loadedJSModules.includes(URL2));
Assert.ok(!Cu.isJSModuleLoaded(URL2));
Assert.ok(!Cu.loadedJSModules.includes(URL3));
Assert.ok(!Cu.isJSModuleLoaded(URL3));
Assert.ok(!Cu.loadedESModules.includes(URL1));
Assert.ok(!Cu.isESModuleLoaded(URL1));
Assert.ok(!Cu.loadedESModules.includes(URL2));
Assert.ok(!Cu.isESModuleLoaded(URL2));
Assert.ok(!Cu.loadedESModules.includes(URL3));
Assert.ok(!Cu.isESModuleLoaded(URL3));
ChromeUtils.import(URL2);
Assert.ok(Cu.loadedJSModules.includes(URL1));
Assert.ok(Cu.isJSModuleLoaded(URL1));
Assert.ok(Cu.loadedJSModules.includes(URL2));
Assert.ok(Cu.isJSModuleLoaded(URL2));
Assert.ok(!Cu.loadedJSModules.includes(URL3));
Assert.ok(!Cu.isJSModuleLoaded(URL3));
Assert.ok(!Cu.loadedESModules.includes(URL1));
Assert.ok(!Cu.isESModuleLoaded(URL1));
Assert.ok(!Cu.loadedESModules.includes(URL2));
Assert.ok(!Cu.isESModuleLoaded(URL2));
Assert.ok(!Cu.loadedESModules.includes(URL3));
Assert.ok(!Cu.isESModuleLoaded(URL3));
ChromeUtils.import(URL3);
Assert.ok(Cu.loadedJSModules.includes(URL1));
Assert.ok(Cu.isJSModuleLoaded(URL1));
Assert.ok(Cu.loadedJSModules.includes(URL2));
Assert.ok(Cu.isJSModuleLoaded(URL2));
Assert.ok(Cu.loadedJSModules.includes(URL3));
Assert.ok(Cu.isJSModuleLoaded(URL3));
Assert.ok(!Cu.loadedESModules.includes(URL1));
Assert.ok(!Cu.isESModuleLoaded(URL1));
Assert.ok(!Cu.loadedESModules.includes(URL2));
Assert.ok(!Cu.isESModuleLoaded(URL2));
Assert.ok(!Cu.loadedESModules.includes(URL3));
Assert.ok(!Cu.isESModuleLoaded(URL3));
});
add_task(function test_ESModule() {
const URL1 = "resource://test/es6module_loaded-1.sys.mjs";
const URL2 = "resource://test/es6module_loaded-2.sys.mjs";
const URL3 = "resource://test/es6module_loaded-3.sys.mjs";
Assert.ok(!Cu.loadedJSModules.includes(URL1));
Assert.ok(!Cu.isJSModuleLoaded(URL1));
Assert.ok(!Cu.loadedJSModules.includes(URL2));
Assert.ok(!Cu.isJSModuleLoaded(URL2));
Assert.ok(!Cu.loadedJSModules.includes(URL3));
Assert.ok(!Cu.isJSModuleLoaded(URL3));
Assert.ok(!Cu.loadedESModules.includes(URL1));
Assert.ok(!Cu.isESModuleLoaded(URL1));
Assert.ok(!Cu.loadedESModules.includes(URL2));
Assert.ok(!Cu.isESModuleLoaded(URL2));
Assert.ok(!Cu.loadedESModules.includes(URL3));
Assert.ok(!Cu.isESModuleLoaded(URL3));
ChromeUtils.importESModule(URL1);
Assert.ok(!Cu.loadedJSModules.includes(URL1));
Assert.ok(!Cu.isJSModuleLoaded(URL1));
Assert.ok(!Cu.loadedJSModules.includes(URL2));
Assert.ok(!Cu.isJSModuleLoaded(URL2));
Assert.ok(!Cu.loadedJSModules.includes(URL3));
Assert.ok(!Cu.isJSModuleLoaded(URL3));
Assert.ok(Cu.loadedESModules.includes(URL1));
Assert.ok(Cu.isESModuleLoaded(URL1));
Assert.ok(!Cu.loadedESModules.includes(URL2));
Assert.ok(!Cu.isESModuleLoaded(URL2));
Assert.ok(!Cu.loadedESModules.includes(URL3));
Assert.ok(!Cu.isESModuleLoaded(URL3));
ChromeUtils.importESModule(URL2);
Assert.ok(!Cu.loadedJSModules.includes(URL1));
Assert.ok(!Cu.isJSModuleLoaded(URL1));
Assert.ok(!Cu.loadedJSModules.includes(URL2));
Assert.ok(!Cu.isJSModuleLoaded(URL2));
Assert.ok(!Cu.loadedJSModules.includes(URL3));
Assert.ok(!Cu.isJSModuleLoaded(URL3));
Assert.ok(Cu.loadedESModules.includes(URL1));
Assert.ok(Cu.isESModuleLoaded(URL1));
Assert.ok(Cu.loadedESModules.includes(URL2));
Assert.ok(Cu.isESModuleLoaded(URL2));
Assert.ok(!Cu.loadedESModules.includes(URL3));
Assert.ok(!Cu.isESModuleLoaded(URL3));
ChromeUtils.importESModule(URL3);
Assert.ok(!Cu.loadedJSModules.includes(URL1));
Assert.ok(!Cu.isJSModuleLoaded(URL1));
Assert.ok(!Cu.loadedJSModules.includes(URL2));
Assert.ok(!Cu.isJSModuleLoaded(URL2));
Assert.ok(!Cu.loadedJSModules.includes(URL3));
Assert.ok(!Cu.isJSModuleLoaded(URL3));
Assert.ok(Cu.loadedESModules.includes(URL1));
Assert.ok(Cu.isESModuleLoaded(URL1));
Assert.ok(Cu.loadedESModules.includes(URL2));
Assert.ok(Cu.isESModuleLoaded(URL2));
Assert.ok(Cu.loadedESModules.includes(URL3));
Assert.ok(Cu.isESModuleLoaded(URL3));
});

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

@ -38,6 +38,12 @@ support-files =
not-esmified-not-exported.jsm
esm_lazy-1.sys.mjs
esm_lazy-2.sys.mjs
jsm_loaded-1.jsm
jsm_loaded-2.jsm
jsm_loaded-3.jsm
es6module_loaded-1.sys.mjs
es6module_loaded-2.sys.mjs
es6module_loaded-3.sys.mjs
[test_allowWaivers.js]
[test_bogus_files.js]
@ -183,3 +189,4 @@ head = head_watchdog.js
[test_import_es6_modules.js]
[test_import_shim.js]
[test_defineESModuleGetters.js]
[test_loadedESModules.js]