Repository navigation
Expand file tree
/
Copy pathRegistryTable.h
More file actions
335 lines (305 loc) · 11 KB
/
Copy pathRegistryTable.h
File metadata and controls
335 lines (305 loc) · 11 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#ifndef NATIVESCRIPT_COMMON_LOADER_REGISTRY_TABLE_H_
#define NATIVESCRIPT_COMMON_LOADER_REGISTRY_TABLE_H_
#include <algorithm>
#include <cstddef>
#include <optional>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
#include <vector>
#include "ns/LoaderConfig.h"
#include "ns/http/ModuleFetch.h"
#include "ns/loader/ImportMap.h"
#include "ns/loader/ModuleIdentity.h"
namespace nativescript::common::loader {
// The path a file module's key names when the registry recorded none: the key
// up to its first '?' (after "file://" for a file: URL key). Empty for an
// empty key, an http(s) key and any other key with a URL scheme.
inline std::string_view UnrecordedKeyPath(std::string_view key) {
constexpr std::string_view kFile = "file://";
if (key.substr(0, kFile.size()) == kFile) {
return KeyPath(key.substr(kFile.size()));
}
if (key.empty() || HasUrlScheme(key) || IsHttpRegistryKey(key)) {
return std::string_view();
}
return KeyPath(key);
}
// The registry key a legacy origin names when read back verbatim: the path
// OriginToPath gives and the query exactly as written. Empty when OriginToPath
// rejects `origin`.
inline std::string LegacyOriginKey(std::string_view origin,
const LoaderConfig& config) {
std::optional<std::string> path = OriginToPath(origin, config);
if (!path) {
return std::string();
}
const size_t query = origin.find('?');
if (query != std::string_view::npos) {
path->append(origin.substr(query));
}
return std::move(*path);
}
// Whether GetLoadedModuleUrls lists `key`: a blob: key, or a key spelled as
// a URL with an authority, that does not name a file.
inline bool IsLoadedModuleUrl(std::string_view key) {
if (key.empty() || !UnrecordedKeyPath(key).empty()) {
return false;
}
return key.substr(0, 5) == "blob:" ||
key.find("://") != std::string_view::npos;
}
// One isolate's module registry: canonical key -> module, the file a file
// module was read from, the module -> key index and the require(esm) facades
// of registered modules. V8-free so the host tests can run it; the core
// instantiates it with V8 handles.
//
// Traits:
// Env what Get, Store and StoredHash need (the isolate)
// Ref a module as callers hold it; default-constructs empty
// Stored a module as the table holds it; releases it when
// destroyed
// IsEmpty(ref), Hash(ref) Hash is an identity hash, and hashes collide
// Get(env, stored) a Ref to return to the caller; the only call that
// makes one (a v8::Local, in the caller's HandleScope)
// Store(env, stored&, ref)
// IsStoredEmpty(stored), StoredHash(env, stored)
// Holds(stored, ref), SameStored(stored, stored) the same module
//
// Only Find and FindFacade, which return a module, call Get. Every other
// operation compares and hashes the handles it holds without making a Ref,
// so a caller that registers or invalidates many modules in one
// HandleScope does not grow it.
//
// Not thread-safe: the isolate's thread only.
template <typename Traits>
class RegistryTable {
public:
using Env = typename Traits::Env;
using Ref = typename Traits::Ref;
using Stored = typename Traits::Stored;
// The module registered under `key`; empty when there is none.
Ref Find(Env env, const std::string& key) const {
auto it = entries_.find(key);
return it == entries_.end() ? Ref() : Traits::Get(env, it->second.module);
}
bool Contains(const std::string& key) const {
return entries_.find(key) != entries_.end();
}
size_t Size() const { return entries_.size(); }
// The only write. The module that held `key` before leaves the index under
// `key`, and loses its facades unless another key still holds it, even
// when it is `module` itself. `filePath` is the file a file module was
// read from; empty for any other module.
void Set(Env env, const std::string& key, const Ref& module,
std::string filePath) {
auto it = entries_.find(key);
if (it != entries_.end()) {
Unindex(env, key, it->second);
} else {
it = entries_.try_emplace(key).first;
}
Traits::Store(env, it->second.module, module);
it->second.filePath = std::move(filePath);
Index(key, module);
}
// Whether `key` was registered.
bool Erase(Env env, const std::string& key) {
auto it = entries_.find(key);
if (it == entries_.end()) {
return false;
}
Unindex(env, key, it->second);
entries_.erase(it);
return true;
}
// The key `module` is registered under; empty when it is not registered.
// Drops the index candidates the registry no longer backs.
std::string FindKey(Env, const Ref& module) {
if (Traits::IsEmpty(module)) {
return std::string();
}
auto bucket = keysByHash_.find(Traits::Hash(module));
if (bucket == keysByHash_.end()) {
return std::string();
}
std::vector<std::string>& keys = bucket->second;
for (auto it = keys.begin(); it != keys.end();) {
auto entry = entries_.find(*it);
if (entry == entries_.end() ||
Traits::IsStoredEmpty(entry->second.module)) {
it = keys.erase(it);
continue;
}
if (Traits::Holds(entry->second.module, module)) {
return *it;
}
++it;
}
if (keys.empty()) {
keysByHash_.erase(bucket);
}
return std::string();
}
// The file recorded for `key`, or, when there is none, UnrecordedKeyPath.
// A view into the record or into `key`.
std::string_view FilePathFor(std::string_view key) const {
auto it = entries_.find(key);
if (it != entries_.end() && !it->second.filePath.empty()) {
return it->second.filePath;
}
return UnrecordedKeyPath(key);
}
bool HasRecordedPath(std::string_view key) const {
auto it = entries_.find(key);
return it != entries_.end() && !it->second.filePath.empty();
}
// CanonicalizeModuleKey, except for two spellings the URL parser would
// misread:
// - A registered file module's key is already canonical: its path may
// contain '?', which the key alone cannot tell from a query.
// - Under legacyOrigins, a file: URL that reads back verbatim (Q1) as a
// registered key names that module: the runtime wrote it with the path
// unencoded, so a '#' or '%XX' in a file name is not URL syntax there.
std::string CanonicalKey(
const std::string& key, const LoaderConfig& config,
const http::CanonicalizationConfig* vocabulary) const {
if (!key.empty() && key.front() == '/' && HasRecordedPath(key)) {
return key;
}
if (config.legacyOrigins && key.compare(0, 5, "file:") == 0) {
std::string verbatim = LegacyOriginKey(key, config);
if (!verbatim.empty() && entries_.count(verbatim) != 0) {
return verbatim;
}
}
return CanonicalizeModuleKey(key, config, vocabulary);
}
// The keys IsLoadedModuleUrl accepts, sorted.
std::vector<std::string> LoadedUrls() const {
std::vector<std::string> urls;
for (const auto& [key, entry] : entries_) {
if (IsLoadedModuleUrl(key)) {
urls.push_back(key);
}
}
std::sort(urls.begin(), urls.end());
return urls;
}
// The facade cached for `target`; empty when there is none.
Ref FindFacade(Env env, const Ref& target) const {
if (Traits::IsEmpty(target)) {
return Ref();
}
auto bucket = facadesByTargetHash_.find(Traits::Hash(target));
if (bucket == facadesByTargetHash_.end()) {
return Ref();
}
for (const Facade& facade : bucket->second) {
if (Traits::Holds(facade.target, target)) {
return Traits::Get(env, facade.facade);
}
}
return Ref();
}
// Caches `facade` for `target` until a key that holds `target` is removed
// or registered again (even with `target`) while no other key holds it.
void AddFacade(Env env, const Ref& target, const Ref& facade) {
if (Traits::IsEmpty(target)) {
return;
}
Facade entry;
Traits::Store(env, entry.target, target);
Traits::Store(env, entry.facade, facade);
facadesByTargetHash_[Traits::Hash(target)].push_back(std::move(entry));
}
size_t FacadeCount() const {
size_t count = 0;
for (const auto& [hash, facades] : facadesByTargetHash_) {
count += facades.size();
}
return count;
}
size_t IndexedKeyCount() const {
size_t count = 0;
for (const auto& [hash, keys] : keysByHash_) {
count += keys.size();
}
return count;
}
private:
struct Entry {
Stored module;
std::string filePath;
};
struct Facade {
Stored target;
Stored facade;
};
void Index(const std::string& key, const Ref& module) {
if (Traits::IsEmpty(module)) {
return;
}
std::vector<std::string>& keys = keysByHash_[Traits::Hash(module)];
if (std::find(keys.begin(), keys.end(), key) == keys.end()) {
keys.push_back(key);
}
}
// Must run while `entry` still holds the outgoing module: its hash is
// unrecoverable once the handle is gone. The module keeps its facades
// while another key still holds it.
void Unindex(Env env, const std::string& key, const Entry& entry) {
if (Traits::IsStoredEmpty(entry.module)) {
return;
}
const int hash = Traits::StoredHash(env, entry.module);
auto bucket = keysByHash_.find(hash);
if (bucket != keysByHash_.end()) {
std::vector<std::string>& keys = bucket->second;
keys.erase(std::remove(keys.begin(), keys.end(), key), keys.end());
if (HeldUnderAnyOf(keys, entry.module)) {
return;
}
if (keys.empty()) {
keysByHash_.erase(bucket);
}
}
DropFacades(entry.module, hash);
}
// Whether one of `keys` (an index bucket, whose hashes collide) holds
// `module`.
bool HeldUnderAnyOf(const std::vector<std::string>& keys,
const Stored& module) const {
for (const std::string& key : keys) {
auto entry = entries_.find(key);
if (entry != entries_.end() &&
Traits::SameStored(entry->second.module, module)) {
return true;
}
}
return false;
}
void DropFacades(const Stored& target, int hash) {
auto bucket = facadesByTargetHash_.find(hash);
if (bucket == facadesByTargetHash_.end()) {
return;
}
std::vector<Facade>& facades = bucket->second;
facades.erase(std::remove_if(facades.begin(), facades.end(),
[&](const Facade& facade) {
return Traits::SameStored(facade.target,
target);
}),
facades.end());
if (facades.empty()) {
facadesByTargetHash_.erase(bucket);
}
}
std::unordered_map<std::string, Entry, StringViewHash, std::equal_to<>>
entries_;
std::unordered_map<int, std::vector<std::string>> keysByHash_;
std::unordered_map<int, std::vector<Facade>> facadesByTargetHash_;
};
} // namespace nativescript::common::loader
#endif // NATIVESCRIPT_COMMON_LOADER_REGISTRY_TABLE_H_