Repository navigation
Expand file tree
/
Copy pathLoaderState.cpp
More file actions
177 lines (155 loc) · 5.78 KB
/
Copy pathLoaderState.cpp
File metadata and controls
177 lines (155 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "ns/loader/LoaderState.h"
#include <cstdlib>
#include <utility>
#include "loader/LoaderStateInternal.h"
#include "ns/Trace.h"
namespace {
using nativescript::common::platform::TraceCategory;
} // namespace
namespace nativescript::common::loader {
void InstallLoader(v8::Isolate* isolate,
std::shared_ptr<const LoaderConfig> config) {
if (config == nullptr) [[unlikely]] {
Logf("NativeScript: InstallLoader was given no loader configuration");
std::abort();
}
ModuleLoaderState* state = LoaderStateFor(isolate);
if (state == nullptr) {
return;
}
// Callers hold LoaderConfigFor's reference for as long as the isolate
// lives; replacing the snapshot would free it under them.
if (state->config != nullptr) [[unlikely]] {
Logf("NativeScript: InstallLoader ran twice for one isolate");
std::abort();
}
state->config = std::move(config);
}
std::shared_ptr<const LoaderConfig> CaptureLoaderConfig(v8::Isolate* isolate) {
ModuleLoaderState* state = LoaderStateFor(isolate);
return state != nullptr ? state->config : nullptr;
}
const LoaderConfig& LoaderConfigFor(v8::Isolate* isolate) {
ModuleLoaderState* state = LoaderStateFor(isolate);
if (state == nullptr) {
static const LoaderConfig kClosed;
return kClosed;
}
if (state->config == nullptr) [[unlikely]] {
Logf("NativeScript: the module loader was used before InstallLoader");
std::abort();
}
return *state->config;
}
#ifndef NDEBUG
ModuleLoaderState::~ModuleLoaderState() {
if (config != nullptr && !closing) [[unlikely]] {
Logf(
"NativeScript: the runtime closed its state bag before "
"loader::Quiesce");
std::abort();
}
}
#endif
void Quiesce(v8::Isolate* isolate) {
ModuleLoaderState* state = LoaderStateFor(isolate);
if (state != nullptr && !state->closing) {
state->closing = true;
QuiesceGraphLoads(isolate, *state);
state->imports = ImportState();
}
}
bool IsClosing(v8::Isolate* isolate) {
ModuleLoaderState* state = LoaderStateFor(isolate);
return state == nullptr || state->closing;
}
const LoaderVocabulary* VocabularyFor(v8::Isolate* isolate) {
ModuleLoaderState* state = LoaderStateFor(isolate);
return state != nullptr ? &state->vocabulary : nullptr;
}
LoaderVocabulary CaptureLoaderVocabulary(v8::Isolate* isolate) {
ModuleLoaderState* state = LoaderStateFor(isolate);
return state != nullptr ? state->vocabulary : LoaderVocabulary();
}
void InstallLoaderVocabulary(v8::Isolate* isolate,
LoaderVocabulary vocabulary) {
ModuleLoaderState* state = LoaderStateFor(isolate);
if (state == nullptr) {
return;
}
state->vocabulary = std::move(vocabulary);
NS_TRACE(TraceCategory::kEsm,
"[import-map] inherited %zu entries, %zu scopes, %zu volatile "
"patterns",
state->vocabulary.importMap.imports.entries.size(),
state->vocabulary.importMap.scopes.size(),
state->vocabulary.volatilePatterns.size());
}
bool InstallImportMap(v8::Isolate* isolate, ParsedImportMap map,
std::string* error) {
ModuleLoaderState* state = LoaderStateFor(isolate);
if (state == nullptr) {
if (error != nullptr) {
*error = "the isolate is shutting down";
}
return false;
}
state->vocabulary.importMap = std::move(map);
NS_TRACE(TraceCategory::kEsm, "[import-map] loaded %zu entries, %zu scopes",
state->vocabulary.importMap.imports.entries.size(),
state->vocabulary.importMap.scopes.size());
return true;
}
void SetVolatilePatterns(v8::Isolate* isolate,
std::vector<std::string> patterns) {
ModuleLoaderState* state = LoaderStateFor(isolate);
if (state == nullptr) {
return;
}
state->vocabulary.volatilePatterns = std::move(patterns);
NS_TRACE(TraceCategory::kEsm, "[import-map] volatile patterns: %zu",
state->vocabulary.volatilePatterns.size());
}
void SetCanonicalizationConfig(v8::Isolate* isolate,
http::CanonicalizationConfig config) {
ModuleLoaderState* state = LoaderStateFor(isolate);
if (state == nullptr) {
return;
}
state->vocabulary.canonicalization = std::move(config);
state->vocabulary.canonicalizationConfigured = true;
NS_TRACE(TraceCategory::kEsm,
"[ns:module configureLoader] canonicalization set (strip=%zu "
"devPrefixes=%zu preserve=%zu)",
state->vocabulary.canonicalization.stripParams.size(),
state->vocabulary.canonicalization.devPathPrefixes.size(),
state->vocabulary.canonicalization.preserveQueryPrefixes.size());
}
const http::CanonicalizationConfig* CanonicalizationConfigFor(
v8::Isolate* isolate) {
ModuleLoaderState* state = LoaderStateFor(isolate);
if (state == nullptr || !state->vocabulary.canonicalizationConfigured) {
return nullptr;
}
return &state->vocabulary.canonicalization;
}
bool IsVolatileModuleUrl(v8::Isolate* isolate, std::string_view url) {
ModuleLoaderState* state = LoaderStateFor(isolate);
return state != nullptr && IsVolatileUrl(state->vocabulary, url);
}
ModuleResolution ResolveModuleSpecifier(v8::Isolate* isolate,
std::string_view specifier,
std::string_view referrerKey,
ImportMapLookup lookup) {
ModuleLoaderState* state = LoaderStateFor(isolate);
if (state == nullptr) {
ModuleResolution unresolved;
unresolved.specifier = std::string(specifier);
return unresolved;
}
const ResolveReferrer referrer{referrerKey,
state->registry.FilePathFor(referrerKey)};
return ResolveSpecifier(specifier, referrer, state->vocabulary,
LoaderConfigFor(isolate), lookup);
}
} // namespace nativescript::common::loader