1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef js_loader_ImportMap_h
8 #define js_loader_ImportMap_h
13 #include "js/SourceText.h"
14 #include "mozilla/AlreadyAddRefed.h"
15 #include "mozilla/Logging.h"
16 #include "mozilla/RefPtr.h"
17 #include "mozilla/UniquePtr.h"
18 #include "nsStringFwd.h"
20 #include "ResolveResult.h"
23 class nsIScriptElement
;
26 namespace JS::loader
{
28 class ScriptLoaderInterface
;
29 class ScriptLoadRequest
;
32 * A helper class to report warning to ScriptLoaderInterface.
34 class ReportWarningHelper
{
36 ReportWarningHelper(ScriptLoaderInterface
* aLoader
,
37 ScriptLoadRequest
* aRequest
)
38 : mLoader(aLoader
), mRequest(aRequest
) {}
40 void Report(const char* aMessageName
,
41 const nsTArray
<nsString
>& aParams
= nsTArray
<nsString
>()) const;
44 RefPtr
<ScriptLoaderInterface
> mLoader
;
45 ScriptLoadRequest
* mRequest
;
48 // Specifier map from import maps.
49 // https://html.spec.whatwg.org/multipage/webappapis.html#module-specifier-map
51 std::map
<nsString
, nsCOMPtr
<nsIURI
>, std::greater
<nsString
>>;
53 // Scope map from import maps.
54 // https://html.spec.whatwg.org/multipage/webappapis.html#concept-import-map-scopes
55 using ScopeMap
= std::map
<nsCString
, mozilla::UniquePtr
<SpecifierMap
>,
56 std::greater
<nsCString
>>;
59 * Implementation of Import maps.
60 * https://html.spec.whatwg.org/multipage/webappapis.html#import-maps
64 ImportMap(mozilla::UniquePtr
<SpecifierMap
> aImports
,
65 mozilla::UniquePtr
<ScopeMap
> aScopes
)
66 : mImports(std::move(aImports
)), mScopes(std::move(aScopes
)) {}
69 * Parse the JSON string from the Import map script.
70 * This function will throw a TypeError if there's any invalid key or value in
71 * the JSON text according to the spec.
73 * https://html.spec.whatwg.org/multipage/webappapis.html#parse-an-import-map-string
75 static mozilla::UniquePtr
<ImportMap
> ParseString(
76 JSContext
* aCx
, JS::SourceText
<char16_t
>& aInput
, nsIURI
* aBaseURL
,
77 const ReportWarningHelper
& aWarning
);
80 * This implements "Resolve a module specifier" algorithm defined in the
84 * https://html.spec.whatwg.org/multipage/webappapis.html#resolve-a-module-specifier
86 * Impl note: According to the spec, if the specifier cannot be resolved, this
87 * method will throw a TypeError(Step 13). But the tricky part is when
88 * creating a module script,
90 * https://html.spec.whatwg.org/multipage/webappapis.html#validate-requested-module-specifiers
91 * If the resolving failed, it shall catch the exception and set to the
92 * script's parse error.
93 * For implementation we return a ResolveResult here, and the callers will
94 * need to convert the result to a TypeError if it fails.
96 static ResolveResult
ResolveModuleSpecifier(ImportMap
* aImportMap
,
97 ScriptLoaderInterface
* aLoader
,
98 LoadedScript
* aScript
,
99 const nsAString
& aSpecifier
);
102 static mozilla::LazyLogModule gImportMapLog
;
106 * https://html.spec.whatwg.org/multipage/webappapis.html#import-map-processing-model
108 * Formally, an import map is a struct with two items:
109 * 1. imports, a module specifier map, and
110 * 2. scopes, an ordered map of URLs to module specifier maps.
112 mozilla::UniquePtr
<SpecifierMap
> mImports
;
113 mozilla::UniquePtr
<ScopeMap
> mScopes
;
116 } // namespace JS::loader
118 #endif // js_loader_ImportMap_h