Bug 1817727 - Disable test_filename_sanitize.js on mac because of permafailures....
[gecko.git] / js / loader / ImportMap.h
blobd304a5285674f2c7d0c6b043461e8991f8987fc8
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
10 #include <functional>
11 #include <map>
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"
19 #include "nsTArray.h"
20 #include "ResolveResult.h"
22 struct JSContext;
23 class nsIScriptElement;
24 class nsIURI;
26 namespace JS::loader {
27 class LoadedScript;
28 class ScriptLoaderInterface;
29 class ScriptLoadRequest;
31 /**
32 * A helper class to report warning to ScriptLoaderInterface.
34 class ReportWarningHelper {
35 public:
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;
43 private:
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
50 using SpecifierMap =
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>>;
58 /**
59 * Implementation of Import maps.
60 * https://html.spec.whatwg.org/multipage/webappapis.html#import-maps
62 class ImportMap {
63 public:
64 ImportMap(mozilla::UniquePtr<SpecifierMap> aImports,
65 mozilla::UniquePtr<ScopeMap> aScopes)
66 : mImports(std::move(aImports)), mScopes(std::move(aScopes)) {}
68 /**
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);
79 /**
80 * This implements "Resolve a module specifier" algorithm defined in the
81 * Import maps spec.
83 * See
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,
89 * see
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);
101 // Logging
102 static mozilla::LazyLogModule gImportMapLog;
104 private:
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