-
-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathNsBuiltinModules.cpp
More file actions
460 lines (409 loc) · 16.3 KB
/
Copy pathNsBuiltinModules.cpp
File metadata and controls
460 lines (409 loc) · 16.3 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#include "NsBuiltinModules.h"
#include <vector>
#include "BuiltinLoader.h"
#include "Caches.h"
#include "Console.h"
#include "Helpers.h"
#include "Messaging.h"
#include "ModuleInternalCallbacks.h"
#include "Runtime.h"
#include "StructuredSerialization.h"
#include "TextEncoding.h"
using namespace v8;
namespace tns {
namespace {
constexpr const char* kNsPrefix = "ns:";
constexpr const char* kNodePrefix = "node:";
// Defined below, each next to the natives it gathers.
MaybeLocal<Object> NsModuleBinding(Local<Context> context);
MaybeLocal<Object> NsRuntimeBinding(Local<Context> context);
MaybeLocal<Object> NsUtilBinding(Local<Context> context);
struct Registration {
const char* specifier;
BuiltinId builtin;
// Natives the file receives as `binding`, null when it needs none (every
// `node:` shim, which reaches its `ns:` module through require instead).
BuiltinLoader::BindingFactory binding;
// The internal tier (runtime/js/README.md): resolvable only through the
// require builtins receive, never through the module system, so the file's
// exports can carry capabilities app code must not reach.
bool internalOnly = false;
};
// The public registry (docs/ns-builtin-modules.md) plus the internal tier.
// One specifier, one source file: a `node:` shim is its own builtin that
// requires the `ns:` module it adapts, so the two module objects stay
// distinct and the standard module never carries compatibility code.
constexpr Registration kRegistry[] = {
{"ns:module", BuiltinId::kNsModule, NsModuleBinding},
{"ns:runtime", BuiltinId::kNsRuntime, NsRuntimeBinding},
{"ns:util", BuiltinId::kNsUtil, NsUtilBinding},
{"node:module", BuiltinId::kNodeModule, nullptr},
{"node:url", BuiltinId::kNodeUrl, nullptr},
{"node:util", BuiltinId::kNodeUtil, nullptr},
{"node:worker_threads", BuiltinId::kNodeWorkerThreads,
messaging::CreateBinding},
{"internal/broadcast-channel", BuiltinId::kBroadcastChannel,
messaging::CreateBinding, true},
{"internal/dom-exception", BuiltinId::kDomException,
serialization::DomExceptionBinding, true},
{"internal/events", BuiltinId::kEvents, nullptr, true},
{"internal/message-channel", BuiltinId::kMessageChannel,
messaging::CreateBinding, true},
{"internal/message-event", BuiltinId::kMessageEvent, nullptr, true},
};
// ns:runtime config keys. Each key defines its value domain and scope here;
// process-wide keys (see Helpers.h) affect every isolate, so writes are
// restricted to the main isolate. Remote-module security
// (`security.allowRemoteModules`, `security.remoteModuleAllowlist`) is
// deliberately not registered — it is boot-time nativescript.config only.
constexpr const char* kReleasedObjectPolicyKey = "releasedObjectPolicy";
constexpr const char* kDebugKey = "debug";
void ThrowTypeError(Isolate* isolate, const std::string& message) {
isolate->ThrowException(
Exception::TypeError(tns::ToV8String(isolate, message)));
}
bool EnsureMainIsolateWrite(Isolate* isolate, const std::string& key) {
if (Runtime::IsWorker()) {
ThrowTypeError(isolate, "'" + key +
"' is process-wide and can only be set from the main "
"isolate");
return false;
}
return true;
}
void SetConfigCallback(const FunctionCallbackInfo<Value>& info) {
Isolate* isolate = info.GetIsolate();
if (info.Length() < 2 || !info[0]->IsString()) {
ThrowTypeError(isolate, "setConfig expects (key: string, value)");
return;
}
std::string key = tns::ToString(isolate, info[0]);
if (key == kReleasedObjectPolicyKey) {
if (!EnsureMainIsolateWrite(isolate, key)) {
return;
}
std::string value =
info[1]->IsString() ? tns::ToString(isolate, info[1]) : std::string();
if (value == "report") {
tns::SetReleasedObjectPolicy(tns::ReleasedObjectPolicy::kReport);
} else if (value == "throw") {
tns::SetReleasedObjectPolicy(tns::ReleasedObjectPolicy::kThrow);
} else {
ThrowTypeError(isolate, "'" + key + "' must be 'report' or 'throw'");
}
return;
}
if (key == kDebugKey) {
if (!EnsureMainIsolateWrite(isolate, key)) {
return;
}
if (!info[1]->IsString()) {
ThrowTypeError(
isolate, "'" + key + "' must be a comma-separated category string (" +
tns::AllLogCategoryNames() +
"), or '' to disable tracing");
return;
}
// The list replaces the whole mask, so a caller never has to know what was
// already on to turn something off.
std::string value = tns::ToString(isolate, info[1]);
bool hadUnknown = false;
uint32_t mask = tns::ParseLogCategories(value, &hadUnknown);
tns::SetEnabledLogCategories(mask);
if (hadUnknown) {
Log("ns:runtime setConfig('debug', '%s'): ignoring unknown categories; "
"valid "
"categories are %s",
value.c_str(), tns::AllLogCategoryNames().c_str());
}
return;
}
ThrowTypeError(isolate, "Unknown runtime config key: '" + key + "'");
}
void GetConfigCallback(const FunctionCallbackInfo<Value>& info) {
Isolate* isolate = info.GetIsolate();
if (info.Length() < 1 || !info[0]->IsString()) {
ThrowTypeError(isolate, "getConfig expects (key: string)");
return;
}
std::string key = tns::ToString(isolate, info[0]);
if (key == kReleasedObjectPolicyKey) {
const char* value =
tns::GetReleasedObjectPolicy() == tns::ReleasedObjectPolicy::kThrow
? "throw"
: "report";
info.GetReturnValue().Set(tns::ToV8String(isolate, value));
return;
}
if (key == kDebugKey) {
info.GetReturnValue().Set(
tns::ToV8String(isolate, tns::EnabledLogCategoryNames()));
return;
}
ThrowTypeError(isolate, "Unknown runtime config key: '" + key + "'");
}
const Registration* Find(const std::string& specifier) {
for (const Registration& registration : kRegistry) {
if (specifier == registration.specifier) {
return ®istration;
}
}
return nullptr;
}
bool HasPrefix(const std::string& specifier, const char* prefix) {
return specifier.rfind(prefix, 0) == 0;
}
MaybeLocal<Object> NsModuleBinding(Local<Context> context) {
Isolate* isolate = v8::Isolate::GetCurrent();
Local<Object> binding = Object::New(isolate);
// The HTTP-loader control surface (HttpLoader.mm). The binding builder
// decides build-dependent membership; ns-module.js only shapes and freezes
// whatever arrives.
if (!BuildNsModuleBinding(context, binding)) {
return MaybeLocal<Object>();
}
return binding;
}
MaybeLocal<Object> NsRuntimeBinding(Local<Context> context) {
Isolate* isolate = v8::Isolate::GetCurrent();
Local<Object> binding = Object::New(isolate);
Local<v8::Function> setConfig, getConfig;
if (!v8::Function::New(context, SetConfigCallback).ToLocal(&setConfig) ||
!v8::Function::New(context, GetConfigCallback).ToLocal(&getConfig) ||
!binding->Set(context, tns::ToV8String(isolate, "setConfig"), setConfig)
.FromMaybe(false) ||
!binding->Set(context, tns::ToV8String(isolate, "getConfig"), getConfig)
.FromMaybe(false)) {
return MaybeLocal<Object>();
}
return binding;
}
// TextEncoder / TextDecoder for ns:util, read straight out of the
// text-encoding builtin's per-isolate run, so the module's classes are the
// objects the globals of the same name expose.
void TextEncodingClassGetter(Local<v8::Name> property,
const PropertyCallbackInfo<Value>& info) {
Local<Context> context = info.GetIsolate()->GetCurrentContext();
Local<Object> exports;
Local<Value> value;
if (TextEncoding::GetExports(context).ToLocal(&exports) &&
exports->Get(context, property).ToLocal(&value)) {
info.GetReturnValue().Set(value);
}
}
MaybeLocal<Object> NsUtilBinding(Local<Context> context) {
Isolate* isolate = v8::Isolate::GetCurrent();
Local<Object> binding = Object::New(isolate);
// The console formatter is built once per realm; ns:util re-exports that
// instance instead of creating a second one.
Console::InitInspect(context);
std::shared_ptr<Caches> cache = Caches::Get(isolate);
if (cache->InspectFunc == nullptr) {
return MaybeLocal<Object>();
}
if (!binding
->Set(context, tns::ToV8String(isolate, "inspect"),
cache->InspectFunc->Get(isolate))
.FromMaybe(false)) {
return MaybeLocal<Object>();
}
// Lazy so that requiring ns:util does not run the text-encoding builtin;
// ns-util.js keeps the read inside its own getters to preserve that.
for (const char* name : {"TextEncoder", "TextDecoder"}) {
if (binding
->SetLazyDataProperty(context, tns::ToV8String(isolate, name),
TextEncodingClassGetter)
.IsNothing()) {
return MaybeLocal<Object>();
}
}
return binding;
}
// The exports a synthetic module re-exports by name, in the order used both
// when declaring the export names and when populating them.
MaybeLocal<v8::Array> ExportKeys(Local<Context> context,
Local<Object> exports) {
return exports->GetOwnPropertyNames(context, PropertyFilter::ONLY_ENUMERABLE,
KeyConversionMode::kConvertToString);
}
MaybeLocal<Module> NoDependencies(Local<Context> context,
Local<v8::String> specifier,
Local<FixedArray> import_attributes,
Local<Module> referrer) {
// Synthetic modules never request anything.
return MaybeLocal<Module>();
}
MaybeLocal<Value> PopulateSyntheticModule(Local<Context> context,
Local<Module> module) {
Isolate* isolate = v8::Isolate::GetCurrent();
// The specifier was handed to CreateSyntheticModule as the module name,
// which is how these steps find the exports object again.
std::string specifier = tns::ToString(isolate, module->GetResourceName());
Local<Object> exports;
if (!NsBuiltinModules::GetExports(context, specifier).ToLocal(&exports)) {
return MaybeLocal<Value>();
}
Local<v8::String> defaultName = tns::ToV8String(isolate, "default");
if (module->SetSyntheticModuleExport(isolate, defaultName, exports)
.IsNothing()) {
return MaybeLocal<Value>();
}
Local<v8::Array> keys;
if (!ExportKeys(context, exports).ToLocal(&keys)) {
return MaybeLocal<Value>();
}
for (uint32_t i = 0; i < keys->Length(); i++) {
Local<Value> key;
Local<Value> value;
if (!keys->Get(context, i).ToLocal(&key) || !key->IsString()) {
return MaybeLocal<Value>();
}
if (key.As<v8::String>()->StringEquals(defaultName)) {
continue;
}
if (!exports->Get(context, key).ToLocal(&value) ||
module->SetSyntheticModuleExport(isolate, key.As<v8::String>(), value)
.IsNothing()) {
return MaybeLocal<Value>();
}
}
Local<Promise::Resolver> resolver;
if (!Promise::Resolver::New(context).ToLocal(&resolver) ||
!resolver->Resolve(context, v8::Undefined(isolate)).FromMaybe(false)) {
return MaybeLocal<Value>();
}
return resolver->GetPromise();
}
} // namespace
bool NsBuiltinModules::IsBuiltinScheme(const std::string& specifier) {
return HasPrefix(specifier, kNsPrefix) || HasPrefix(specifier, kNodePrefix);
}
bool NsBuiltinModules::IsNsScheme(const std::string& specifier) {
return HasPrefix(specifier, kNsPrefix);
}
bool NsBuiltinModules::IsRegistered(const std::string& specifier,
bool includeInternal) {
const Registration* registration = Find(specifier);
return registration != nullptr &&
(includeInternal || !registration->internalOnly);
}
std::string NsBuiltinModules::NotFoundMessage(const std::string& specifier) {
return "No such built-in module: " + specifier;
}
MaybeLocal<Object> NsBuiltinModules::GetExports(Local<Context> context,
const std::string& specifier) {
const Registration* registration = Find(specifier);
if (registration == nullptr) {
return MaybeLocal<Object>();
}
Isolate* isolate = v8::Isolate::GetCurrent();
std::shared_ptr<Caches> cache = Caches::Get(isolate);
// A shim reaches its ns: module through the builtin require, so the graph is
// walked while a module is still being built; a cycle would otherwise
// recurse until the stack runs out.
if (cache->BuiltinModulesInProgress.count(specifier) > 0) {
isolate->ThrowException(Exception::Error(tns::ToV8String(
isolate, "Circular require of built-in module: " + specifier)));
return MaybeLocal<Object>();
}
cache->BuiltinModulesInProgress.emplace(specifier);
TryCatch tc(isolate);
Local<Object> exports;
bool built = BuiltinLoader::GetExports(context, registration->builtin,
registration->binding)
.ToLocal(&exports);
cache->BuiltinModulesInProgress.erase(specifier);
if (built) {
return exports;
}
if (tc.HasCaught()) {
tc.ReThrow();
return MaybeLocal<Object>();
}
isolate->ThrowException(Exception::Error(tns::ToV8String(
isolate, "Failed to initialize built-in module '" + specifier + "'")));
return MaybeLocal<Object>();
}
MaybeLocal<Module> NsBuiltinModules::GetModule(Local<Context> context,
const std::string& specifier) {
// The module system is the app-facing entry point; the internal tier only
// exists behind the require builtins receive.
const Registration* registration = Find(specifier);
if (registration == nullptr || registration->internalOnly) {
return MaybeLocal<Module>();
}
Isolate* isolate = v8::Isolate::GetCurrent();
std::shared_ptr<Caches> cache = Caches::Get(isolate);
auto it = cache->BuiltinModules.find(specifier);
if (it != cache->BuiltinModules.end()) {
Local<Module> cached = it->second->Get(isolate);
if (!cached.IsEmpty() && cached->GetStatus() != Module::kErrored) {
return cached;
}
cache->BuiltinModules.erase(it);
}
Local<Object> exports;
if (!GetExports(context, specifier).ToLocal(&exports)) {
return MaybeLocal<Module>();
}
Local<v8::String> defaultName = tns::ToV8String(isolate, "default");
std::vector<Local<v8::String>> exportNames{defaultName};
Local<v8::Array> keys;
if (!ExportKeys(context, exports).ToLocal(&keys)) {
return MaybeLocal<Module>();
}
for (uint32_t i = 0; i < keys->Length(); i++) {
Local<Value> key;
if (!keys->Get(context, i).ToLocal(&key) || !key->IsString()) {
return MaybeLocal<Module>();
}
if (!key.As<v8::String>()->StringEquals(defaultName)) {
exportNames.push_back(key.As<v8::String>());
}
}
Local<Module> module = Module::CreateSyntheticModule(
isolate, tns::ToV8String(isolate, specifier),
MemorySpan<const Local<v8::String>>(exportNames.data(),
exportNames.size()),
PopulateSyntheticModule);
// No dependencies and no user code, so the module can be driven to its final
// state here; importers then only ever see an evaluated module.
if (!module->InstantiateModule(context, &NoDependencies).FromMaybe(false)) {
return MaybeLocal<Module>();
}
if (module->Evaluate(context).IsEmpty()) {
return MaybeLocal<Module>();
}
cache->BuiltinModules[specifier] =
std::make_unique<Persistent<Module>>(isolate, module);
return module;
}
Local<v8::Function> NsBuiltinModules::GetFormatFunc(Local<Context> context) {
Isolate* isolate = v8::Isolate::GetCurrent();
std::shared_ptr<Caches> cache = Caches::Get(isolate);
if (cache->FormatFunc != nullptr) {
return cache->FormatFunc->Get(isolate);
}
if (cache->FormatFuncUnavailable) {
return Local<v8::Function>();
}
TryCatch tc(isolate);
Local<Object> exports;
Local<Value> format;
if (!GetExports(context, "ns:util").ToLocal(&exports) ||
!exports->Get(context, tns::ToV8String(isolate, "format"))
.ToLocal(&format) ||
!format->IsFunction()) {
if (tc.HasCaught()) {
tns::LogError(isolate, tc);
}
// One attempt per realm: a broken builtin must not make every log call
// recompile it.
cache->FormatFuncUnavailable = true;
return Local<v8::Function>();
}
cache->FormatFunc = std::make_unique<Persistent<v8::Function>>(
isolate, format.As<v8::Function>());
return format.As<v8::Function>();
}
} // namespace tns