Bug 1881564 - Initialize module loader also for ServiceWorker to allow ChromeUtils.importESModule in DevTools ServiceWorker debugger global. r=yulia

Dynamic import is prevented by IsDynamicImportSupported, and now it now throws
TypeError instead of Error, and related wpt passes with it.

Differential Revision: https://phabricator.services.mozilla.com/D202494
This commit is contained in:
Tooru Fujisawa 2024-03-20 11:22:45 +00:00
parent 763685f55f
commit 5f7c671736
7 changed files with 20 additions and 17 deletions

View File

@ -517,9 +517,7 @@ already_AddRefed<WorkerScriptLoader> WorkerScriptLoader::Create(
}
// Set up the module loader, if it has not been initialzied yet.
if (!aWorkerPrivate->IsServiceWorker()) {
self->InitModuleLoader();
}
self->InitModuleLoader();
return self.forget();
}

View File

@ -147,6 +147,15 @@ already_AddRefed<ModuleLoadRequest> WorkerModuleLoader::CreateDynamicImport(
return request.forget();
}
bool WorkerModuleLoader::IsDynamicImportSupported() {
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
if (workerPrivate->IsServiceWorker()) {
return false;
}
return true;
}
bool WorkerModuleLoader::CanStartLoad(ModuleLoadRequest* aRequest,
nsresult* aRvOut) {
return true;

View File

@ -63,6 +63,8 @@ class WorkerModuleLoader : public JS::loader::ModuleLoaderBase {
JS::Handle<JSString*> aSpecifier,
JS::Handle<JSObject*> aPromise) override;
bool IsDynamicImportSupported() override;
bool CanStartLoad(ModuleLoadRequest* aRequest, nsresult* aRvOut) override;
// StartFetch is special for worker modules, as we need to move back to the

View File

@ -308,6 +308,12 @@ bool ModuleLoaderBase::HostImportModuleDynamically(
return false;
}
if (!loader->IsDynamicImportSupported()) {
JS_ReportErrorNumberASCII(aCx, js::GetErrorMessage, nullptr,
JSMSG_DYNAMIC_IMPORT_NOT_SUPPORTED);
return false;
}
auto result = loader->ResolveModuleSpecifier(script, specifier);
if (result.isErr()) {
JS::Rooted<JS::Value> error(aCx);

View File

@ -251,6 +251,8 @@ class ModuleLoaderBase : public nsISupports {
JSContext* aCx, nsIURI* aURI, LoadedScript* aMaybeActiveScript,
JS::Handle<JSString*> aSpecifier, JS::Handle<JSObject*> aPromise) = 0;
virtual bool IsDynamicImportSupported() { return true; }
// Called when dynamic import started successfully.
virtual void OnDynamicImportStarted(ModuleLoadRequest* aRequest) {}

View File

@ -1,3 +0,0 @@
[serviceworker.any.serviceworker.html]
[import() should not drain the microtask queue if it fails because it's used in a ServiceWorker]
expected: FAIL

View File

@ -1,11 +0,0 @@
[no-dynamic-import.any.serviceworker.html]
expected:
if (os == "android") and fission: [OK, TIMEOUT]
[Module URL]
expected: FAIL
[Another module URL]
expected: FAIL
[Module data: URL]
expected: FAIL