forked from mirrors/gecko-dev
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:
parent
61dcf81cd6
commit
e490f40be2
2 changed files with 43 additions and 16 deletions
|
|
@ -655,6 +655,13 @@ static mozJSModuleLoader* GetModuleLoaderForCurrentGlobal(
|
||||||
}
|
}
|
||||||
|
|
||||||
if (targetModuleLoader->HasFetchingModules()) {
|
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(
|
if (!mozilla::SpinEventLoopUntil(
|
||||||
"importESModule for current global"_ns, [&]() -> bool {
|
"importESModule for current global"_ns, [&]() -> bool {
|
||||||
return !targetModuleLoader->HasFetchingModules();
|
return !targetModuleLoader->HasFetchingModules();
|
||||||
|
|
@ -685,6 +692,11 @@ static mozJSModuleLoader* GetModuleLoaderForOptions(
|
||||||
return mozJSModuleLoader::GetOrCreateDevToolsLoader();
|
return mozJSModuleLoader::GetOrCreateDevToolsLoader();
|
||||||
|
|
||||||
case ImportESModuleTargetGlobal::Contextual: {
|
case ImportESModuleTargetGlobal::Contextual: {
|
||||||
|
if (!NS_IsMainThread()) {
|
||||||
|
return GetModuleLoaderForCurrentGlobal(aCx, aGlobal,
|
||||||
|
aMaybeSyncLoaderScope);
|
||||||
|
}
|
||||||
|
|
||||||
RefPtr devToolsModuleloader = mozJSModuleLoader::GetDevToolsLoader();
|
RefPtr devToolsModuleloader = mozJSModuleLoader::GetDevToolsLoader();
|
||||||
if (devToolsModuleloader &&
|
if (devToolsModuleloader &&
|
||||||
devToolsModuleloader->IsLoaderGlobal(aGlobal.Get())) {
|
devToolsModuleloader->IsLoaderGlobal(aGlobal.Get())) {
|
||||||
|
|
@ -704,6 +716,17 @@ static mozJSModuleLoader* GetModuleLoaderForOptions(
|
||||||
|
|
||||||
static bool ValidateImportOptions(
|
static bool ValidateImportOptions(
|
||||||
JSContext* aCx, const ImportESModuleOptionsDictionary& aOptions) {
|
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() &&
|
if (aOptions.mGlobal.WasPassed() &&
|
||||||
aOptions.mLoadInDevToolsLoader.WasPassed()) {
|
aOptions.mLoadInDevToolsLoader.WasPassed()) {
|
||||||
JS_ReportErrorASCII(aCx,
|
JS_ReportErrorASCII(aCx,
|
||||||
|
|
|
||||||
|
|
@ -313,6 +313,25 @@ namespace ChromeUtils {
|
||||||
[NewObject]
|
[NewObject]
|
||||||
Promise<sequence<CDMInformation>> getGMPContentDecryptionModuleInformation();
|
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.
|
* IF YOU ADD NEW METHODS HERE, MAKE SURE THEY ARE THREAD-SAFE.
|
||||||
*/
|
*/
|
||||||
|
|
@ -508,22 +527,6 @@ partial namespace ChromeUtils {
|
||||||
[Throws]
|
[Throws]
|
||||||
object import(UTF8String aResourceURI, optional object aTargetObj);
|
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
|
* Defines a property on the given target which lazily imports a JavaScript
|
||||||
* module when accessed.
|
* module when accessed.
|
||||||
|
|
@ -1004,6 +1007,7 @@ enum ImportESModuleTargetGlobal {
|
||||||
/**
|
/**
|
||||||
* If the current global is DevTools' distinct system global, load into the
|
* If the current global is DevTools' distinct system global, load into the
|
||||||
* DevTools' distinct system global.
|
* DevTools' distinct system global.
|
||||||
|
* If the current thread is worker thread, load into the current global.
|
||||||
* Otherwise load into the shared system global.
|
* Otherwise load into the shared system global.
|
||||||
*
|
*
|
||||||
* This is a temporary workaround until DevTools modules are ESMified.
|
* This is a temporary workaround until DevTools modules are ESMified.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue