Bug 1803810 - Part 9: Make ChromeUtils.importESModule compatible with workers. r=jonco

Expose importESModule to worker as well, while only supporting
{ global: "current" } and { global: "contextual" } options.
"contextual" in worker is an alias to "current".

Differential Revision: https://phabricator.services.mozilla.com/D199461
This commit is contained in:
Tooru Fujisawa 2024-02-13 14:34:24 +00:00
parent 61dcf81cd6
commit e490f40be2
2 changed files with 43 additions and 16 deletions

View file

@ -655,6 +655,13 @@ static mozJSModuleLoader* GetModuleLoaderForCurrentGlobal(
}
if (targetModuleLoader->HasFetchingModules()) {
if (!NS_IsMainThread()) {
JS_ReportErrorASCII(aCx,
"ChromeUtils.importESModule cannot be used in worker "
"when there is ongoing dynamic import");
return nullptr;
}
if (!mozilla::SpinEventLoopUntil(
"importESModule for current global"_ns, [&]() -> bool {
return !targetModuleLoader->HasFetchingModules();
@ -685,6 +692,11 @@ static mozJSModuleLoader* GetModuleLoaderForOptions(
return mozJSModuleLoader::GetOrCreateDevToolsLoader();
case ImportESModuleTargetGlobal::Contextual: {
if (!NS_IsMainThread()) {
return GetModuleLoaderForCurrentGlobal(aCx, aGlobal,
aMaybeSyncLoaderScope);
}
RefPtr devToolsModuleloader = mozJSModuleLoader::GetDevToolsLoader();
if (devToolsModuleloader &&
devToolsModuleloader->IsLoaderGlobal(aGlobal.Get())) {
@ -704,6 +716,17 @@ static mozJSModuleLoader* GetModuleLoaderForOptions(
static bool ValidateImportOptions(
JSContext* aCx, const ImportESModuleOptionsDictionary& aOptions) {
if (!NS_IsMainThread() &&
(!aOptions.mGlobal.WasPassed() ||
(aOptions.mGlobal.Value() != ImportESModuleTargetGlobal::Current &&
aOptions.mGlobal.Value() != ImportESModuleTargetGlobal::Contextual))) {
JS_ReportErrorASCII(aCx,
"ChromeUtils.importESModule: Only { global: "
"\"current\" } and { global: \"contextual\" } options "
"are supported on worker");
return false;
}
if (aOptions.mGlobal.WasPassed() &&
aOptions.mLoadInDevToolsLoader.WasPassed()) {
JS_ReportErrorASCII(aCx,

View file

@ -313,6 +313,25 @@ namespace ChromeUtils {
[NewObject]
Promise<sequence<CDMInformation>> getGMPContentDecryptionModuleInformation();
/**
* Synchronously loads and evaluates the JS module source located at
* 'aResourceURI'.
*
* @param aResourceURI A resource:// URI string to load the module from.
* @param aOption An option to specify where to load the module into.
* @returns the module's namespace object.
*
* The implementation maintains a hash of aResourceURI->global obj.
* Subsequent invocations of import with 'aResourceURI' pointing to
* the same file will not cause the module to be re-evaluated.
*
* In worker threads, aOption is required and only { global: "current" } and
* { global: "contextual" } are supported.
*/
[Throws]
object importESModule(DOMString aResourceURI,
optional ImportESModuleOptionsDictionary aOptions = {});
/**
* IF YOU ADD NEW METHODS HERE, MAKE SURE THEY ARE THREAD-SAFE.
*/
@ -508,22 +527,6 @@ partial namespace ChromeUtils {
[Throws]
object import(UTF8String aResourceURI, optional object aTargetObj);
/**
* Synchronously loads and evaluates the JS module source located at
* 'aResourceURI'.
*
* @param aResourceURI A resource:// URI string to load the module from.
* @param aOption An option to specify where to load the module into.
* @returns the module's namespace object.
*
* The implementation maintains a hash of aResourceURI->global obj.
* Subsequent invocations of import with 'aResourceURI' pointing to
* the same file will not cause the module to be re-evaluated.
*/
[Throws]
object importESModule(DOMString aResourceURI,
optional ImportESModuleOptionsDictionary aOptions = {});
/**
* Defines a property on the given target which lazily imports a JavaScript
* module when accessed.
@ -1004,6 +1007,7 @@ enum ImportESModuleTargetGlobal {
/**
* If the current global is DevTools' distinct system global, load into the
* DevTools' distinct system global.
* If the current thread is worker thread, load into the current global.
* Otherwise load into the shared system global.
*
* This is a temporary workaround until DevTools modules are ESMified.