Repository navigation
Expand file tree
/
Copy pathModuleRegistry.cpp
More file actions
170 lines (148 loc) · 5.98 KB
/
Copy pathModuleRegistry.cpp
File metadata and controls
170 lines (148 loc) · 5.98 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
#include "ns/loader/ModuleRegistry.h"
#include <unordered_set>
#include <utility>
#include "loader/LoaderStateInternal.h"
#include "ns/Trace.h"
#include "ns/loader/LoaderState.h"
namespace nativescript::common::loader {
namespace {
using platform::TraceCategory;
const http::CanonicalizationConfig* CanonicalizationFor(
const ModuleLoaderState& state) {
return state.vocabulary.canonicalizationConfigured
? &state.vocabulary.canonicalization
: nullptr;
}
// Removes the module registered under `key`, which is already
// canonical: `keyOrUrl` is only what the caller asked for, for the trace.
void EraseKey(v8::Isolate* isolate, ModuleLoaderState& state,
const std::string& keyOrUrl, const std::string& key) {
const char* kind = IsHttpRegistryKey(key) ? "http" : "path";
if (key != keyOrUrl) {
NS_TRACE(TraceCategory::kEsm,
"[resolver][remove:pre] raw=%s key=%s class=%s", keyOrUrl.c_str(),
key.c_str(), kind);
} else {
NS_TRACE(TraceCategory::kEsm, "[resolver][remove:pre] key=%s class=%s",
key.c_str(), kind);
}
SupersedeFileJob(state.graph, key);
ForgetPendingJson(isolate, state, key);
const size_t before = state.registry.Size();
if (state.registry.Erase(isolate, key)) {
if (!IsHttpRegistryKey(key)) {
NS_TRACE(TraceCategory::kEsm, "[resolver] removing stale module %s",
key.c_str());
}
} else {
NS_TRACE(TraceCategory::kEsm, "[resolver][remove:miss] key not found (%s)",
key.c_str());
}
NS_TRACE(TraceCategory::kEsm, "[resolver][remove:post] reg %zu->%zu", before,
state.registry.Size());
}
} // namespace
v8::Local<v8::Module> FindRegisteredModule(v8::Isolate* isolate,
const std::string& key) {
ModuleLoaderState* state = LoaderStateFor(isolate);
return state != nullptr ? state->registry.Find(isolate, key)
: v8::Local<v8::Module>();
}
bool IsModuleRegistered(v8::Isolate* isolate, const std::string& key) {
ModuleLoaderState* state = LoaderStateFor(isolate);
return state != nullptr && state->registry.Contains(key);
}
size_t RegisteredModuleCount(v8::Isolate* isolate) {
ModuleLoaderState* state = LoaderStateFor(isolate);
return state != nullptr ? state->registry.Size() : 0;
}
void SetRegisteredModule(v8::Isolate* isolate, const std::string& key,
v8::Local<v8::Module> module, std::string filePath) {
ModuleLoaderState* state = LoaderStateFor(isolate);
if (state != nullptr) {
state->registry.Set(isolate, key, module, std::move(filePath));
}
}
std::string RegistryKeyFor(v8::Isolate* isolate, const std::string& keyOrUrl) {
ModuleLoaderState* state = LoaderStateFor(isolate);
if (state == nullptr) {
return std::string();
}
return state->registry.CanonicalKey(keyOrUrl, LoaderConfigFor(isolate),
CanonicalizationFor(*state));
}
std::string RemoveModuleFromRegistry(v8::Isolate* isolate,
const std::string& keyOrUrl) {
ModuleLoaderState* state = LoaderStateFor(isolate);
if (state == nullptr) {
return std::string();
}
std::string key = state->registry.CanonicalKey(
keyOrUrl, LoaderConfigFor(isolate), CanonicalizationFor(*state));
EraseKey(isolate, *state, keyOrUrl, key);
return key;
}
std::string FindKeyForModule(v8::Isolate* isolate,
v8::Local<v8::Module> module) {
ModuleLoaderState* state = LoaderStateFor(isolate);
return state != nullptr ? state->registry.FindKey(isolate, module)
: std::string();
}
std::string ModuleFilePathForKey(v8::Isolate* isolate, const std::string& key) {
ModuleLoaderState* state = LoaderStateFor(isolate);
return std::string(state != nullptr ? state->registry.FilePathFor(key)
: UnrecordedKeyPath(key));
}
std::vector<std::string> GetLoadedModuleUrls(v8::Isolate* isolate) {
ModuleLoaderState* state = LoaderStateFor(isolate);
return state != nullptr ? state->registry.LoadedUrls()
: std::vector<std::string>();
}
void InvalidateModules(v8::Isolate* isolate, v8::Local<v8::Context> context,
const std::vector<std::string>& urls) {
ModuleLoaderState* state = LoaderStateFor(isolate);
if (state == nullptr || urls.empty()) {
return;
}
const LoaderConfig& config = LoaderConfigFor(isolate);
std::unordered_set<std::string> seen;
std::vector<std::string> keys;
keys.reserve(urls.size());
for (const std::string& url : urls) {
if (url.empty()) {
continue;
}
std::string key =
state->registry.CanonicalKey(url, config, CanonicalizationFor(*state));
if (!key.empty() && seen.insert(key).second) {
keys.push_back(std::move(key));
}
}
size_t hits = 0;
for (const std::string& key : keys) {
const bool present = state->registry.Contains(key);
hits += present ? 1 : 0;
NS_TRACE(TraceCategory::kRegistry, "invalidate %s key=%s",
present ? "HIT " : "MISS", key.c_str());
RejectInvalidatedImports(isolate, context, *state, key);
EraseKey(isolate, *state, key, key);
}
NS_TRACE(TraceCategory::kRegistry,
"invalidate summary unique=%zu hits=%zu misses=%zu (registry "
"now=%zu)",
keys.size(), hits, keys.size() - hits, state->registry.Size());
}
v8::Local<v8::Module> FindRequireFacade(v8::Isolate* isolate,
v8::Local<v8::Module> target) {
ModuleLoaderState* state = LoaderStateFor(isolate);
return state != nullptr ? state->registry.FindFacade(isolate, target)
: v8::Local<v8::Module>();
}
void AddRequireFacade(v8::Isolate* isolate, v8::Local<v8::Module> target,
v8::Local<v8::Module> facade) {
ModuleLoaderState* state = LoaderStateFor(isolate);
if (state != nullptr) {
state->registry.AddFacade(isolate, target, facade);
}
}
} // namespace nativescript::common::loader