FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
node/src/node_builtins.cc at main · fetchapi/node · GitHub
fetchapi
/
node
Public
forked from
nodejs/node
Notifications
You must be signed in to change notification settings
Fork
1
Star
0
Code
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
node
/
src
/
node_builtins.cc
Copy path
More file actions
More file actions
Latest commit
History
History
History
751 lines (670 loc) · 26.5 KB
Breadcrumbs
node
/
src
/
node_builtins.cc
Copy path
File metadata and controls
751 lines (670 loc) · 26.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
#
include
"
node_builtins.h
"
#
include
"
debug_utils-inl.h
"
#
include
"
env-inl.h
"
#
include
"
node_external_reference.h
"
#
include
"
node_internals.h
"
#
include
"
node_threadsafe_cow-inl.h
"
#
include
"
simdutf.h
"
#
include
"
util-inl.h
"
namespace
node
{
namespace
builtins
{
using
v8::Context;
using
v8::
DEFAULT
;
using
v8::EscapableHandleScope;
using
v8::Function;
using
v8::FunctionCallbackInfo;
using
v8::IntegrityLevel;
using
v8::Isolate;
using
v8::Local;
using
v8::MaybeLocal;
using
v8::Name;
using
v8::None;
using
v8::Object;
using
v8::ObjectTemplate;
using
v8::PropertyCallbackInfo;
using
v8::ScriptCompiler;
using
v8::ScriptOrigin;
using
v8::Set;
using
v8::SideEffectType;
using
v8::String;
using
v8::Undefined;
using
v8::Value;
BuiltinLoader::BuiltinLoader
()
: config_(GetConfig()), code_cache_(std::make_shared<BuiltinCodeCache>()) {
LoadJavaScriptSource
();
#
ifdef
NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_LEXER_PATH
AddExternalizedBuiltin
(
"
internal/deps/cjs-module-lexer/lexer
"
,
STRINGIFY
(
NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_LEXER_PATH
));
#
endif
//
NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_LEXER_PATH
#
ifdef
NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_DIST_LEXER_PATH
AddExternalizedBuiltin
(
"
internal/deps/cjs-module-lexer/dist/lexer
"
,
STRINGIFY
(
NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_DIST_LEXER_PATH
));
#
endif
//
NODE_SHARED_BUILTIN_CJS_MODULE_LEXER_DIST_LEXER_PATH
#
ifdef
NODE_SHARED_BUILTIN_UNDICI_UNDICI_PATH
AddExternalizedBuiltin
(
"
internal/deps/undici/undici
"
,
STRINGIFY
(
NODE_SHARED_BUILTIN_UNDICI_UNDICI_PATH
));
#
endif
//
NODE_SHARED_BUILTIN_UNDICI_UNDICI_PATH
}
bool
BuiltinLoader::Exists
(
const
char
* id) {
auto
source = source_.
read
();
return
source->
find
(id) != source->
end
();
}
bool
BuiltinLoader::Add
(
const
char
* id,
const
UnionBytes& source) {
auto
result = source_.
write
()->
emplace
(id, source);
return
result.
second
;
}
void
BuiltinLoader::GetNatives
(Local<Name> property,
const
PropertyCallbackInfo<Value>& info) {
Environment* env =
Environment::GetCurrent
(info);
Isolate* isolate = env->
isolate
();
Local<Context> context = env->
context
();
Local<Object> out =
Object::New
(isolate);
auto
source = env->
builtin_loader
()->
source_
.
read
();
for
(
auto
const
& x : *source) {
Local<String> key =
OneByteString
(isolate, x.
first
.
c_str
(), x.
first
.
size
());
out->
Set
(context, key, x.
second
.
ToStringChecked
(isolate)).
FromJust
();
}
info.
GetReturnValue
().
Set
(out);
}
Local<String>
BuiltinLoader::GetConfigString
(Isolate* isolate) {
return
config_.
ToStringChecked
(isolate);
}
std::vector<std::string_view>
BuiltinLoader::GetBuiltinIds
()
const
{
std::vector<std::string_view> ids;
auto
source = source_.
read
();
ids.
reserve
(source->
size
());
for
(
auto
const
& x : *source) {
ids.
emplace_back
(x.
first
);
}
return
ids;
}
BuiltinLoader::BuiltinCategories
BuiltinLoader::GetBuiltinCategories
()
const
{
BuiltinCategories builtin_categories;
const
std::vector<std::string_view> prefixes = {
#
if
!HAVE_OPENSSL
"
internal/crypto/
"
,
"
internal/debugger/
"
,
#
endif
//
!HAVE_OPENSSL
"
internal/bootstrap/
"
,
"
internal/per_context/
"
,
"
internal/deps/
"
,
"
internal/main/
"
};
builtin_categories.
can_be_required
.
emplace
(
"
internal/deps/cjs-module-lexer/lexer
"
);
builtin_categories.
cannot_be_required
= std::set<std::string> {
#
if
!HAVE_INSPECTOR
"
inspector
"
,
"
inspector/promises
"
,
"
internal/util/inspector
"
,
#
endif
//
!HAVE_INSPECTOR
#
if
!NODE_USE_V8_PLATFORM || !defined(NODE_HAVE_I18N_SUPPORT)
"
trace_events
"
,
#
endif
//
!NODE_USE_V8_PLATFORM || !defined(NODE_HAVE_I18N_SUPPORT)
#
if
!HAVE_OPENSSL
"
crypto
"
,
"
crypto/promises
"
,
"
https
"
,
"
http2
"
,
"
tls
"
,
"
_tls_common
"
,
"
_tls_wrap
"
,
"
internal/tls/secure-pair
"
,
"
internal/tls/parse-cert-string
"
,
"
internal/tls/secure-context
"
,
"
internal/http2/core
"
,
"
internal/http2/compat
"
,
"
internal/policy/manifest
"
,
"
internal/process/policy
"
,
"
internal/streams/lazy_transform
"
,
#
endif
//
!HAVE_OPENSSL
"
sys
"
,
//
Deprecated.
"
wasi
"
,
//
Experimental.
"
internal/test/binding
"
,
"
internal/v8_prof_polyfill
"
,
"
internal/v8_prof_processor
"
,
};
auto
source = source_.
read
();
for
(
auto
const
& x : *source) {
const
std::string& id = x.
first
;
for
(
auto
const
& prefix : prefixes) {
if
(prefix.
length
() > id.
length
()) {
continue
;
}
if
(id.
find
(prefix) ==
0
&&
builtin_categories.
can_be_required
.
count
(id) ==
0
) {
builtin_categories.
cannot_be_required
.
emplace
(id);
}
}
}
for
(
auto
const
& x : *source) {
const
std::string& id = x.
first
;
if
(
0
== builtin_categories.
cannot_be_required
.
count
(id)) {
builtin_categories.
can_be_required
.
emplace
(id);
}
}
return
builtin_categories;
}
#
ifdef
NODE_BUILTIN_MODULES_PATH
static
std::string
OnDiskFileName
(
const
char
* id) {
std::string filename =
NODE_BUILTIN_MODULES_PATH
;
filename +=
"
/
"
;
if
(
strncmp
(id,
"
internal/deps
"
,
strlen
(
"
internal/deps
"
)) ==
0
) {
id +=
strlen
(
"
internal/
"
);
}
else
{
filename +=
"
lib/
"
;
}
filename += id;
filename +=
"
.js
"
;
return
filename;
}
#
endif
//
NODE_BUILTIN_MODULES_PATH
MaybeLocal<String>
BuiltinLoader::LoadBuiltinSource
(Isolate* isolate,
const
char
* id)
const
{
auto
source = source_.
read
();
#
ifndef
NODE_BUILTIN_MODULES_PATH
const
auto
source_it = source->
find
(id);
if
(
UNLIKELY
(source_it == source->
end
())) {
fprintf
(stderr,
"
Cannot find native builtin:
\"
%s
\"
.
\n
"
, id);
ABORT
();
}
return
source_it->
second
.
ToStringChecked
(isolate);
#
else
//
!NODE_BUILTIN_MODULES_PATH
std::string filename =
OnDiskFileName
(id);
std::string contents;
int
r =
ReadFileSync
(&contents, filename.
c_str
());
if
(r !=
0
) {
const
std::string buf =
SPrintF
(
"
Cannot read local builtin. %s: %s
\"
%s
\"
"
,
uv_err_name
(r),
uv_strerror
(r),
filename);
Local<String> message =
OneByteString
(isolate, buf.
c_str
());
isolate->
ThrowException
(
v8::Exception::Error
(message));
return
MaybeLocal<String>();
}
return
String::NewFromUtf8
(
isolate, contents.
c_str
(), v8::NewStringType::
kNormal
, contents.
length
());
#
endif
//
NODE_BUILTIN_MODULES_PATH
}
namespace
{
static
Mutex externalized_builtins_mutex;
std::unordered_map<std::string, std::unique_ptr<StaticExternalTwoByteResource>>
externalized_builtin_sources;
}
//
namespace
void
BuiltinLoader::AddExternalizedBuiltin
(
const
char
* id,
const
char
* filename) {
StaticExternalTwoByteResource* resource;
{
Mutex::ScopedLock
lock
(externalized_builtins_mutex);
auto
it = externalized_builtin_sources.
find
(id);
if
(it == externalized_builtin_sources.
end
()) {
std::string source;
int
r =
ReadFileSync
(&source, filename);
if
(r !=
0
) {
fprintf
(stderr,
"
Cannot load externalized builtin:
\"
%s:%s
\"
.
\n
"
,
id,
filename);
ABORT
();
}
size_t
expected_u16_length =
simdutf::utf16_length_from_utf8
(source.
data
(), source.
length
());
auto
out = std::make_shared<std::vector<
uint16_t
>>(expected_u16_length);
size_t
u16_length =
simdutf::convert_utf8_to_utf16
(
source.
data
(),
source.
length
(),
reinterpret_cast
<
char16_t
*>(out->
data
()));
out->
resize
(u16_length);
auto
result = externalized_builtin_sources.
emplace
(
id,
std::make_unique<StaticExternalTwoByteResource>(
out->
data
(), out->
size
(), out));
CHECK
(result.
second
);
it = result.
first
;
}
//
OK to get the raw pointer, since externalized_builtin_sources owns
//
the resource, resources are never removed from the map, and
//
externalized_builtin_sources has static lifetime.
resource = it->
second
.
get
();
}
Add
(id,
UnionBytes
(resource));
}
MaybeLocal<Function>
BuiltinLoader::LookupAndCompileInternal
(
Local<Context> context,
const
char
* id,
std::vector<Local<String>>* parameters,
Realm* optional_realm) {
Isolate* isolate = context->
GetIsolate
();
EscapableHandleScope
scope
(isolate);
Local<String> source;
if
(!
LoadBuiltinSource
(isolate, id).
ToLocal
(&source)) {
return
{};
}
std::string filename_s =
std::string
(
"
node:
"
) + id;
Local<String> filename =
OneByteString
(isolate, filename_s.
c_str
(), filename_s.
size
());
ScriptOrigin
origin
(isolate, filename,
0
,
0
,
true
);
BuiltinCodeCacheData cached_data{};
{
//
Note: The lock here should not extend into the
//
`CompileFunction()` call below, because this function may recurse if
//
there is a syntax error during bootstrap (because the fatal exception
//
handler is invoked, which may load built-in modules).
RwLock::ScopedLock
lock
(code_cache_->
mutex
);
auto
cache_it = code_cache_->
map
.
find
(id);
if
(cache_it != code_cache_->
map
.
end
()) {
//
Transfer ownership to ScriptCompiler::Source later.
cached_data = cache_it->
second
;
}
}
const
bool
has_cache = cached_data.
data
!=
nullptr
;
ScriptCompiler::CompileOptions options =
has_cache ? ScriptCompiler::
kConsumeCodeCache
: ScriptCompiler::
kNoCompileOptions
;
ScriptCompiler::Source
script_source
(
source,
origin,
has_cache ? cached_data.
AsCachedData
().
release
() :
nullptr
);
per_process::Debug
(DebugCategory::
CODE_CACHE
,
"
Compiling %s %s code cache
\n
"
,
id,
has_cache ?
"
with
"
:
"
without
"
);
MaybeLocal<Function> maybe_fun =
ScriptCompiler::CompileFunction
(context,
&script_source,
parameters->
size
(),
parameters->
data
(),
0
,
nullptr
,
options);
//
This could fail when there are early errors in the built-in modules,
//
e.g. the syntax errors
Local<Function> fun;
if
(!maybe_fun.
ToLocal
(&fun)) {
//
In the case of early errors, v8 is already capable of
//
decorating the stack for us - note that we use CompileFunction
//
so there is no need to worry about wrappers.
return
MaybeLocal<Function>();
}
//
XXX(joyeecheung): this bookkeeping is not exactly accurate because
//
it only starts after the Environment is created, so the per_context.js
//
will never be in any of these two sets, but the two sets are only for
//
testing anyway.
Result result = (has_cache && !script_source.
GetCachedData
()->
rejected
)
? Result::
kWithCache
: Result::
kWithoutCache
;
if
(optional_realm !=
nullptr
) {
DCHECK_EQ
(
this
, optional_realm->
env
()->
builtin_loader
());
RecordResult
(id, result, optional_realm);
}
if
(has_cache) {
per_process::Debug
(DebugCategory::
CODE_CACHE
,
"
Code cache of %s (%s) %s
\n
"
,
id,
script_source.
GetCachedData
()->
buffer_policy
==
ScriptCompiler::CachedData::BufferNotOwned
?
"
BufferNotOwned
"
:
"
BufferOwned
"
,
script_source.
GetCachedData
()->
rejected
?
"
is rejected
"
:
"
is accepted
"
);
}
if
(result == Result::
kWithoutCache
&& optional_realm !=
nullptr
&&
!optional_realm->
env
()->
isolate_data
()->
is_building_snapshot
()) {
//
We failed to accept this cache, maybe because it was rejected, maybe
//
because it wasn't present. Either way, we'll attempt to replace this
//
code cache info with a new one.
//
This is only done when the isolate is not being serialized because
//
V8 does not support serializing code cache with an unfinalized read-only
//
space (which is what isolates pending to be serialized have).
SaveCodeCache
(id, fun);
}
return
scope.
Escape
(fun);
}
void
BuiltinLoader::SaveCodeCache
(
const
char
* id, Local<Function> fun) {
std::shared_ptr<ScriptCompiler::CachedData>
new_cached_data
(
ScriptCompiler::CreateCodeCacheForFunction
(fun));
CHECK_NOT_NULL
(new_cached_data);
{
RwLock::ScopedLock
lock
(code_cache_->
mutex
);
code_cache_->
map
.
insert_or_assign
(
id,
BuiltinCodeCacheData
(
std::move
(new_cached_data)));
}
}
MaybeLocal<Function>
BuiltinLoader::LookupAndCompile
(Local<Context> context,
const
char
* id,
Realm* optional_realm) {
std::vector<Local<String>> parameters;
Isolate* isolate = context->
GetIsolate
();
//
Detects parameters of the scripts based on module ids.
//
internal/bootstrap/realm: process, getLinkedBinding,
//
getInternalBinding, primordials
if
(
strcmp
(id,
"
internal/bootstrap/realm
"
) ==
0
) {
parameters = {
FIXED_ONE_BYTE_STRING
(isolate,
"
process
"
),
FIXED_ONE_BYTE_STRING
(isolate,
"
getLinkedBinding
"
),
FIXED_ONE_BYTE_STRING
(isolate,
"
getInternalBinding
"
),
FIXED_ONE_BYTE_STRING
(isolate,
"
primordials
"
),
};
}
else
if
(
strncmp
(id,
"
internal/per_context/
"
,
strlen
(
"
internal/per_context/
"
)) ==
0
) {
//
internal/per_context/*: global, exports, primordials
parameters = {
FIXED_ONE_BYTE_STRING
(isolate,
"
exports
"
),
FIXED_ONE_BYTE_STRING
(isolate,
"
primordials
"
),
};
}
else
if
(
strncmp
(id,
"
internal/main/
"
,
strlen
(
"
internal/main/
"
)) ==
0
||
strncmp
(id,
"
internal/bootstrap/
"
,
strlen
(
"
internal/bootstrap/
"
)) ==
0
) {
//
internal/main/*, internal/bootstrap/*: process, require,
//
internalBinding, primordials
parameters = {
FIXED_ONE_BYTE_STRING
(isolate,
"
process
"
),
FIXED_ONE_BYTE_STRING
(isolate,
"
require
"
),
FIXED_ONE_BYTE_STRING
(isolate,
"
internalBinding
"
),
FIXED_ONE_BYTE_STRING
(isolate,
"
primordials
"
),
};
}
else
{
//
others: exports, require, module, process, internalBinding, primordials
parameters = {
FIXED_ONE_BYTE_STRING
(isolate,
"
exports
"
),
FIXED_ONE_BYTE_STRING
(isolate,
"
require
"
),
FIXED_ONE_BYTE_STRING
(isolate,
"
module
"
),
FIXED_ONE_BYTE_STRING
(isolate,
"
process
"
),
FIXED_ONE_BYTE_STRING
(isolate,
"
internalBinding
"
),
FIXED_ONE_BYTE_STRING
(isolate,
"
primordials
"
),
};
}
MaybeLocal<Function> maybe =
LookupAndCompileInternal
(context, id, ¶meters, optional_realm);
return
maybe;
}
MaybeLocal<Value>
BuiltinLoader::CompileAndCall
(Local<Context> context,
const
char
* id,
Realm* realm) {
Isolate* isolate = context->
GetIsolate
();
//
Arguments must match the parameters specified in
//
BuiltinLoader::LookupAndCompile().
std::vector<Local<Value>> arguments;
//
Detects parameters of the scripts based on module ids.
//
internal/bootstrap/realm: process, getLinkedBinding,
//
getInternalBinding, primordials
if
(
strcmp
(id,
"
internal/bootstrap/realm
"
) ==
0
) {
Local<Value> get_linked_binding;
Local<Value> get_internal_binding;
if
(!
NewFunctionTemplate
(isolate, binding::GetLinkedBinding)
->
GetFunction
(context)
.
ToLocal
(&get_linked_binding) ||
!
NewFunctionTemplate
(isolate, binding::GetInternalBinding)
->
GetFunction
(context)
.
ToLocal
(&get_internal_binding)) {
return
MaybeLocal<Value>();
}
arguments = {realm->
process_object
(),
get_linked_binding,
get_internal_binding,
realm->
primordials
()};
}
else
if
(
strncmp
(id,
"
internal/main/
"
,
strlen
(
"
internal/main/
"
)) ==
0
||
strncmp
(id,
"
internal/bootstrap/
"
,
strlen
(
"
internal/bootstrap/
"
)) ==
0
) {
//
internal/main/*, internal/bootstrap/*: process, require,
//
internalBinding, primordials
arguments = {realm->
process_object
(),
realm->
builtin_module_require
(),
realm->
internal_binding_loader
(),
realm->
primordials
()};
}
else
{
//
This should be invoked with the other CompileAndCall() methods, as
//
we are unable to generate the arguments.
//
Currently there are two cases:
//
internal/per_context/*: the arguments are generated in
//
InitializePrimordials()
//
all the other cases: the arguments are generated in the JS-land loader.
UNREACHABLE
();
}
return
CompileAndCall
(context, id, arguments.
size
(), arguments.
data
(), realm);
}
MaybeLocal<Value>
BuiltinLoader::CompileAndCall
(Local<Context> context,
const
char
* id,
int
argc,
Local<Value> argv[],
Realm* optional_realm) {
//
Arguments must match the parameters specified in
//
BuiltinLoader::LookupAndCompile().
MaybeLocal<Function> maybe_fn =
LookupAndCompile
(context, id, optional_realm);
Local<Function> fn;
if
(!maybe_fn.
ToLocal
(&fn)) {
return
MaybeLocal<Value>();
}
Local<Value> undefined =
Undefined
(context->
GetIsolate
());
return
fn->
Call
(context, undefined, argc, argv);
}
bool
BuiltinLoader::CompileAllBuiltins
(Local<Context> context) {
std::vector<std::string_view> ids =
GetBuiltinIds
();
bool
all_succeeded =
true
;
std::string v8_tools_prefix =
"
internal/deps/v8/tools/
"
;
for
(
const
auto
& id : ids) {
if
(id.
compare
(
0
, v8_tools_prefix.
size
(), v8_tools_prefix) ==
0
) {
continue
;
}
v8::TryCatch
bootstrapCatch
(context->
GetIsolate
());
auto
fn =
LookupAndCompile
(context, id.
data
(),
nullptr
);
if
(bootstrapCatch.
HasCaught
()) {
per_process::Debug
(DebugCategory::
CODE_CACHE
,
"
Failed to compile code cache for %s
\n
"
,
id.
data
());
all_succeeded =
false
;
PrintCaughtException
(context->
GetIsolate
(), context, bootstrapCatch);
}
else
{
//
This is used by the snapshot builder, so save the code cache
//
unconditionally.
SaveCodeCache
(id.
data
(), fn.
ToLocalChecked
());
}
}
return
all_succeeded;
}
void
BuiltinLoader::CopyCodeCache
(std::vector<CodeCacheInfo>* out)
const
{
RwLock::ScopedReadLock
lock
(code_cache_->
mutex
);
for
(
auto
const
& item : code_cache_->
map
) {
out->
push_back
({item.
first
, item.
second
});
}
}
void
BuiltinLoader::RefreshCodeCache
(
const
std::vector<CodeCacheInfo>& in) {
RwLock::ScopedLock
lock
(code_cache_->
mutex
);
code_cache_->
map
.
reserve
(in.
size
());
DCHECK
(code_cache_->
map
.
empty
());
for
(
auto
const
& item : in) {
auto
result = code_cache_->
map
.
emplace
(item.
id
, item.
data
);
USE
(result.
second
);
DCHECK
(result.
second
);
}
code_cache_->
has_code_cache
=
true
;
}
void
BuiltinLoader::GetBuiltinCategories
(
Local<Name> property,
const
PropertyCallbackInfo<Value>& info) {
Environment* env =
Environment::GetCurrent
(info);
Isolate* isolate = env->
isolate
();
Local<Context> context = env->
context
();
Local<Object> result =
Object::New
(isolate);
BuiltinCategories builtin_categories =
env->
builtin_loader
()->
GetBuiltinCategories
();
if
(!env->
owns_process_state
()) {
builtin_categories.
can_be_required
.
erase
(
"
trace_events
"
);
builtin_categories.
cannot_be_required
.
insert
(
"
trace_events
"
);
}
Local<Value> cannot_be_required_js;
Local<Value> can_be_required_js;
if
(!
ToV8Value
(context, builtin_categories.
cannot_be_required
)
.
ToLocal
(&cannot_be_required_js))
return
;
if
(result
->
Set
(context,
OneByteString
(isolate,
"
cannotBeRequired
"
),
cannot_be_required_js)
.
IsNothing
())
return
;
if
(!
ToV8Value
(context, builtin_categories.
can_be_required
)
.
ToLocal
(&can_be_required_js))
return
;
if
(result
->
Set
(context,
OneByteString
(isolate,
"
canBeRequired
"
),
can_be_required_js)
.
IsNothing
()) {
return
;
}
info.
GetReturnValue
().
Set
(result);
}
void
BuiltinLoader::GetCacheUsage
(
const
FunctionCallbackInfo<Value>& args) {
Realm* realm =
Realm::GetCurrent
(args);
Isolate* isolate = realm->
isolate
();
Local<Context> context = realm->
context
();
Local<Object> result =
Object::New
(isolate);
Local<Value> builtins_with_cache_js;
Local<Value> builtins_without_cache_js;
Local<Value> builtins_in_snapshot_js;
if
(!
ToV8Value
(context, realm->
builtins_with_cache
)
.
ToLocal
(&builtins_with_cache_js)) {
return
;
}
if
(result
->
Set
(context,
OneByteString
(isolate,
"
compiledWithCache
"
),
builtins_with_cache_js)
.
IsNothing
()) {
return
;
}
if
(!
ToV8Value
(context, realm->
builtins_without_cache
)
.
ToLocal
(&builtins_without_cache_js)) {
return
;
}
if
(result
->
Set
(context,
OneByteString
(isolate,
"
compiledWithoutCache
"
),
builtins_without_cache_js)
.
IsNothing
()) {
return
;
}
if
(!
ToV8Value
(context, realm->
builtins_in_snapshot
)
.
ToLocal
(&builtins_in_snapshot_js)) {
return
;
}
if
(result
->
Set
(context,
OneByteString
(isolate,
"
compiledInSnapshot
"
),
builtins_in_snapshot_js)
.
IsNothing
()) {
return
;
}
args.
GetReturnValue
().
Set
(result);
}
void
BuiltinLoader::BuiltinIdsGetter
(Local<Name> property,
const
PropertyCallbackInfo<Value>& info) {
Environment* env =
Environment::GetCurrent
(info);
Isolate* isolate = env->
isolate
();
std::vector<std::string_view> ids = env->
builtin_loader
()->
GetBuiltinIds
();
info.
GetReturnValue
().
Set
(
ToV8Value
(isolate->
GetCurrentContext
(), ids).
ToLocalChecked
());
}
void
BuiltinLoader::ConfigStringGetter
(
Local<Name> property,
const
PropertyCallbackInfo<Value>& info) {
Environment* env =
Environment::GetCurrent
(info);
info.
GetReturnValue
().
Set
(
env->
builtin_loader
()->
GetConfigString
(info.
GetIsolate
()));
}
void
BuiltinLoader::RecordResult
(
const
char
* id,
BuiltinLoader::Result result,
Realm* realm) {
if
(result == BuiltinLoader::Result::
kWithCache
) {
realm->
builtins_with_cache
.
insert
(id);
}
else
{
realm->
builtins_without_cache
.
insert
(id);
}
}
void
BuiltinLoader::CompileFunction
(
const
FunctionCallbackInfo<Value>& args) {
Realm* realm =
Realm::GetCurrent
(args);
CHECK
(args[
0
]->
IsString
());
node::Utf8Value
id_v
(realm->
isolate
(), args[
0
].
As
<String>());
const
char
* id = *id_v;
MaybeLocal<Function> maybe = realm->
env
()->
builtin_loader
()->
LookupAndCompile
(
realm->
context
(), id, realm);
Local<Function> fn;
if
(maybe.
ToLocal
(&fn)) {
args.
GetReturnValue
().
Set
(fn);
}
}
void
BuiltinLoader::HasCachedBuiltins
(
const
FunctionCallbackInfo<Value>& args) {
auto
instance =
Environment::GetCurrent
(args)->
builtin_loader
();
RwLock::ScopedReadLock
lock
(instance->
code_cache_
->
mutex
);
args.
GetReturnValue
().
Set
(
v8::Boolean::New
(
args.
GetIsolate
(), instance->
code_cache_
->
has_code_cache
));
}
void
SetInternalLoaders
(
const
FunctionCallbackInfo<Value>& args) {
Realm* realm =
Realm::GetCurrent
(args);
CHECK
(args[
0
]->
IsFunction
());
CHECK
(args[
1
]->
IsFunction
());
DCHECK
(realm->
internal_binding_loader
().
IsEmpty
());
DCHECK
(realm->
builtin_module_require
().
IsEmpty
());
realm->
set_internal_binding_loader
(args[
0
].
As
<Function>());
realm->
set_builtin_module_require
(args[
1
].
As
<Function>());
}
void
BuiltinLoader::CopySourceAndCodeCacheReferenceFrom
(
const
BuiltinLoader* other) {
code_cache_ = other->
code_cache_
;
source_ = other->
source_
;
}
void
BuiltinLoader::CreatePerIsolateProperties
(IsolateData* isolate_data,
Local<ObjectTemplate> target) {
Isolate* isolate = isolate_data->
isolate
();
target->
SetAccessor
(isolate_data->
config_string
(),
ConfigStringGetter,
nullptr
,
Local<Value>(),
DEFAULT
,
None,
SideEffectType::
kHasNoSideEffect
);
target->
SetAccessor
(
FIXED_ONE_BYTE_STRING
(isolate,
"
builtinIds
"
),
BuiltinIdsGetter,
nullptr
,
Local<Value>(),
DEFAULT
,
None,
SideEffectType::
kHasNoSideEffect
);
target->
SetAccessor
(
FIXED_ONE_BYTE_STRING
(isolate,
"
builtinCategories
"
),
GetBuiltinCategories,
nullptr
,
Local<Value>(),
DEFAULT
,
None,
SideEffectType::
kHasNoSideEffect
);
target->
SetAccessor
(
FIXED_ONE_BYTE_STRING
(isolate,
"
natives
"
),
GetNatives,
nullptr
,
Local<Value>(),
DEFAULT
,
None,
SideEffectType::
kHasNoSideEffect
);
SetMethod
(isolate, target,
"
getCacheUsage
"
, BuiltinLoader::GetCacheUsage);
SetMethod
(isolate, target,
"
compileFunction
"
, BuiltinLoader::CompileFunction);
SetMethod
(isolate, target,
"
hasCachedBuiltins
"
, HasCachedBuiltins);
SetMethod
(isolate, target,
"
setInternalLoaders
"
, SetInternalLoaders);
}
void
BuiltinLoader::CreatePerContextProperties
(Local<Object> target,
Local<Value> unused,
Local<Context> context,
void
* priv) {
//
internalBinding('builtins') should be frozen
target->
SetIntegrityLevel
(context, IntegrityLevel::
kFrozen
).
FromJust
();
}
void
BuiltinLoader::RegisterExternalReferences
(
ExternalReferenceRegistry* registry) {
registry->
Register
(ConfigStringGetter);
registry->
Register
(BuiltinIdsGetter);
registry->
Register
(GetBuiltinCategories);
registry->
Register
(GetCacheUsage);
registry->
Register
(CompileFunction);
registry->
Register
(HasCachedBuiltins);
registry->
Register
(SetInternalLoaders);
registry->
Register
(GetNatives);
RegisterExternalReferencesForInternalizedBuiltinCode
(registry);
}
}
//
namespace builtins
}
//
namespace node
NODE_BINDING_PER_ISOLATE_INIT
(
builtins, node::builtins::BuiltinLoader::CreatePerIsolateProperties)
NODE_BINDING_CONTEXT_AWARE_INTERNAL
(
builtins, node::builtins::BuiltinLoader::CreatePerContextProperties)
NODE_BINDING_EXTERNAL_REFERENCE
(
builtins, node::builtins::BuiltinLoader::RegisterExternalReferences)
Back
|
FazBrowse Home
|
New Git URL