Bug 1688879 - Part 3: Parse and register an import map. r=jonco,yulia

Implement
https://wicg.github.io/import-maps/#parse-an-import-map-string,
and
https://wicg.github.io/import-maps/#register-an-import-map

Differential Revision: https://phabricator.services.mozilla.com/D142071
This commit is contained in:
Yoshi Cheng-Hao Huang
2022-05-04 21:02:50 +00:00
parent 9d0b0b7820
commit 641d407325
10 changed files with 654 additions and 1 deletions

View File

@@ -8,6 +8,7 @@
#include "LoadedScript.h"
#include "ModuleLoadRequest.h"
#include "ScriptLoadRequest.h"
#include "mozilla/dom/ScriptSettings.h" // AutoJSAPI
#include "mozilla/dom/ScriptTrace.h"
#include "js/Array.h" // JS::GetArrayLength
@@ -31,6 +32,7 @@
using mozilla::GetMainThreadSerialEventTarget;
using mozilla::Preferences;
using mozilla::UniquePtr;
using mozilla::dom::AutoJSAPI;
namespace JS::loader {
@@ -1099,6 +1101,50 @@ void ModuleLoaderBase::CancelAndClearDynamicImports() {
mDynamicImportRequests.CancelRequestsAndClear();
}
UniquePtr<ImportMap> ModuleLoaderBase::ParseImportMap(
ScriptLoadRequest* aRequest) {
AutoJSAPI jsapi;
if (!jsapi.Init(GetGlobalObject())) {
return nullptr;
}
MOZ_ASSERT(aRequest->IsTextSource());
MaybeSourceText maybeSource;
nsresult rv = aRequest->GetScriptSource(jsapi.cx(), &maybeSource);
if (NS_FAILED(rv)) {
return nullptr;
}
JS::SourceText<char16_t>& text = maybeSource.ref<SourceText<char16_t>>();
ReportWarningHelper warning{mLoader, aRequest};
// https://html.spec.whatwg.org/multipage/scripting.html#prepare-a-script
// https://wicg.github.io/import-maps/#integration-prepare-a-script
// Insert the following case to prepare a script step 25.2:
// (Impl Note: the latest html spec is step 27.2)
// Switch on the script's type:
// "importmap"
// Step 1. Let import map parse result be the result of create an import map
// parse result, given source text, base URL and settings object.
//
// Impl note: According to the spec, ImportMap::ParseString will throw a
// TypeError if there's any invalid key/value in the text. After the parsing
// is done, we should report the error if there's any, this is done in
// ~AutoJSAPI.
//
// See https://wicg.github.io/import-maps/#register-an-import-map, step 7.
return ImportMap::ParseString(jsapi.cx(), text, aRequest->mBaseURL, warning);
}
void ModuleLoaderBase::RegisterImportMap(UniquePtr<ImportMap> aImportMap) {
// Check for aImportMap is done in ScriptLoader.
MOZ_ASSERT(aImportMap);
// Step 8. Set elements node document's import map to import map parse
// results import map.
mImportMap = std::move(aImportMap);
}
#undef LOG
#undef LOG_ENABLED