Skip to content

Commit d2e5529

Browse files
joyeecheungtargos
authored andcommittedFeb 28, 2021
bootstrap: include v8 module into the builtin snapshot
PR-URL: #36943 Fixes: #35930 Refs: #35711 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 59861ba commit d2e5529

13 files changed

+147
-29
lines changed
 

‎lib/internal/bootstrap/node.js

+1
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ process.emitWarning = emitWarning;
279279

280280
// Preload modules so that they are included in the builtin snapshot.
281281
require('fs');
282+
require('v8');
282283

283284
function setupPrepareStackTrace() {
284285
const {

‎lib/v8.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,13 @@ function getHeapSnapshot() {
7272
return new HeapSnapshotStream(handle);
7373
}
7474

75+
// We need to get the buffer from the binding at the callsite since
76+
// it's re-initialized after deserialization.
77+
const binding = internalBinding('v8');
78+
7579
const {
7680
cachedDataVersionTag,
7781
setFlagsFromString: _setFlagsFromString,
78-
heapStatisticsBuffer,
79-
heapSpaceStatisticsBuffer,
80-
heapCodeStatisticsBuffer,
8182
updateHeapStatisticsBuffer,
8283
updateHeapSpaceStatisticsBuffer,
8384
updateHeapCodeStatisticsBuffer,
@@ -106,7 +107,7 @@ const {
106107
kCodeAndMetadataSizeIndex,
107108
kBytecodeAndMetadataSizeIndex,
108109
kExternalScriptSourceSizeIndex
109-
} = internalBinding('v8');
110+
} = binding;
110111

111112
const kNumberOfHeapSpaces = kHeapSpaces.length;
112113

@@ -116,7 +117,7 @@ function setFlagsFromString(flags) {
116117
}
117118

118119
function getHeapStatistics() {
119-
const buffer = heapStatisticsBuffer;
120+
const buffer = binding.heapStatisticsBuffer;
120121

121122
updateHeapStatisticsBuffer();
122123

@@ -137,7 +138,7 @@ function getHeapStatistics() {
137138

138139
function getHeapSpaceStatistics() {
139140
const heapSpaceStatistics = new Array(kNumberOfHeapSpaces);
140-
const buffer = heapSpaceStatisticsBuffer;
141+
const buffer = binding.heapSpaceStatisticsBuffer;
141142

142143
for (let i = 0; i < kNumberOfHeapSpaces; i++) {
143144
updateHeapSpaceStatisticsBuffer(i);
@@ -154,7 +155,7 @@ function getHeapSpaceStatistics() {
154155
}
155156

156157
function getHeapCodeStatistics() {
157-
const buffer = heapCodeStatisticsBuffer;
158+
const buffer = binding.heapCodeStatisticsBuffer;
158159

159160
updateHeapCodeStatisticsBuffer();
160161
return {

‎src/heap_utils.cc

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "diagnosticfilename-inl.h"
22
#include "env-inl.h"
33
#include "memory_tracker-inl.h"
4+
#include "node_external_reference.h"
45
#include "stream_base-inl.h"
56
#include "util-inl.h"
67

@@ -399,7 +400,15 @@ void Initialize(Local<Object> target,
399400
env->SetMethod(target, "createHeapSnapshotStream", CreateHeapSnapshotStream);
400401
}
401402

403+
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
404+
registry->Register(BuildEmbedderGraph);
405+
registry->Register(TriggerHeapSnapshot);
406+
registry->Register(CreateHeapSnapshotStream);
407+
}
408+
402409
} // namespace heap
403410
} // namespace node
404411

405412
NODE_MODULE_CONTEXT_AWARE_INTERNAL(heap_utils, node::heap::Initialize)
413+
NODE_MODULE_EXTERNAL_REFERENCE(heap_utils,
414+
node::heap::RegisterExternalReferences)

‎src/inspector_profiler.cc

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
#include "debug_utils-inl.h"
44
#include "diagnosticfilename-inl.h"
55
#include "memory_tracker-inl.h"
6-
#include "node_file.h"
76
#include "node_errors.h"
7+
#include "node_external_reference.h"
8+
#include "node_file.h"
89
#include "node_internals.h"
910
#include "util-inl.h"
1011
#include "v8-inspector.h"
@@ -519,7 +520,16 @@ static void Initialize(Local<Object> target,
519520
env->SetMethod(target, "stopCoverage", StopCoverage);
520521
}
521522

523+
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
524+
registry->Register(SetCoverageDirectory);
525+
registry->Register(SetSourceMapCacheGetter);
526+
registry->Register(TakeCoverage);
527+
registry->Register(StopCoverage);
528+
}
529+
522530
} // namespace profiler
523531
} // namespace node
524532

525533
NODE_MODULE_CONTEXT_AWARE_INTERNAL(profiler, node::profiler::Initialize)
534+
NODE_MODULE_EXTERNAL_REFERENCE(profiler,
535+
node::profiler::RegisterExternalReferences)

‎src/node_external_reference.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,22 @@ class ExternalReferenceRegistry {
5656
V(fs) \
5757
V(fs_dir) \
5858
V(handle_wrap) \
59+
V(heap_utils) \
5960
V(messaging) \
6061
V(native_module) \
6162
V(process_methods) \
6263
V(process_object) \
6364
V(task_queue) \
6465
V(url) \
6566
V(util) \
67+
V(serdes) \
6668
V(string_decoder) \
69+
V(stream_wrap) \
6770
V(trace_events) \
6871
V(timers) \
6972
V(types) \
73+
V(uv) \
74+
V(v8) \
7075
V(worker)
7176

7277
#if NODE_HAVE_I18N_SUPPORT
@@ -76,7 +81,9 @@ class ExternalReferenceRegistry {
7681
#endif // NODE_HAVE_I18N_SUPPORT
7782

7883
#if HAVE_INSPECTOR
79-
#define EXTERNAL_REFERENCE_BINDING_LIST_INSPECTOR(V) V(inspector)
84+
#define EXTERNAL_REFERENCE_BINDING_LIST_INSPECTOR(V) \
85+
V(inspector) \
86+
V(profiler)
8087
#else
8188
#define EXTERNAL_REFERENCE_BINDING_LIST_INSPECTOR(V)
8289
#endif // HAVE_INSPECTOR

‎src/node_serdes.cc

+31-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
#include "node_internals.h"
1+
#include "base_object-inl.h"
22
#include "node_buffer.h"
33
#include "node_errors.h"
4+
#include "node_external_reference.h"
5+
#include "node_internals.h"
46
#include "util-inl.h"
5-
#include "base_object-inl.h"
67

78
namespace node {
89

@@ -26,7 +27,7 @@ using v8::Value;
2627
using v8::ValueDeserializer;
2728
using v8::ValueSerializer;
2829

29-
namespace {
30+
namespace serdes {
3031

3132
class SerializerContext : public BaseObject,
3233
public ValueSerializer::Delegate {
@@ -503,7 +504,32 @@ void Initialize(Local<Object> target,
503504
env->SetConstructorFunction(target, "Deserializer", des);
504505
}
505506

506-
} // anonymous namespace
507+
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
508+
registry->Register(SerializerContext::New);
509+
510+
registry->Register(SerializerContext::WriteHeader);
511+
registry->Register(SerializerContext::WriteValue);
512+
registry->Register(SerializerContext::ReleaseBuffer);
513+
registry->Register(SerializerContext::TransferArrayBuffer);
514+
registry->Register(SerializerContext::WriteUint32);
515+
registry->Register(SerializerContext::WriteUint64);
516+
registry->Register(SerializerContext::WriteDouble);
517+
registry->Register(SerializerContext::WriteRawBytes);
518+
registry->Register(SerializerContext::SetTreatArrayBufferViewsAsHostObjects);
519+
520+
registry->Register(DeserializerContext::New);
521+
registry->Register(DeserializerContext::ReadHeader);
522+
registry->Register(DeserializerContext::ReadValue);
523+
registry->Register(DeserializerContext::GetWireFormatVersion);
524+
registry->Register(DeserializerContext::TransferArrayBuffer);
525+
registry->Register(DeserializerContext::ReadUint32);
526+
registry->Register(DeserializerContext::ReadUint64);
527+
registry->Register(DeserializerContext::ReadDouble);
528+
registry->Register(DeserializerContext::ReadRawBytes);
529+
}
530+
531+
} // namespace serdes
507532
} // namespace node
508533

509-
NODE_MODULE_CONTEXT_AWARE_INTERNAL(serdes, node::Initialize)
534+
NODE_MODULE_CONTEXT_AWARE_INTERNAL(serdes, node::serdes::Initialize)
535+
NODE_MODULE_EXTERNAL_REFERENCE(serdes, node::serdes::RegisterExternalReferences)

‎src/node_snapshotable.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ class Environment;
1313
struct EnvSerializeInfo;
1414

1515
#define SERIALIZABLE_OBJECT_TYPES(V) \
16-
V(fs_binding_data, fs::BindingData)
16+
V(fs_binding_data, fs::BindingData) \
17+
V(v8_binding_data, v8_utils::BindingData)
1718

1819
enum class EmbedderObjectType : uint8_t {
1920
k_default = 0,

‎src/node_v8.cc

+39-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "env-inl.h"
2525
#include "memory_tracker-inl.h"
2626
#include "node.h"
27+
#include "node_external_reference.h"
2728
#include "util-inl.h"
2829
#include "v8.h"
2930

@@ -32,6 +33,7 @@ namespace v8_utils {
3233
using v8::Array;
3334
using v8::Context;
3435
using v8::FunctionCallbackInfo;
36+
using v8::HandleScope;
3537
using v8::HeapCodeStatistics;
3638
using v8::HeapSpaceStatistics;
3739
using v8::HeapStatistics;
@@ -45,6 +47,7 @@ using v8::Uint32;
4547
using v8::V8;
4648
using v8::Value;
4749

50+
4851
#define HEAP_STATISTICS_PROPERTIES(V) \
4952
V(0, total_heap_size, kTotalHeapSizeIndex) \
5053
V(1, total_heap_size_executable, kTotalHeapSizeExecutableIndex) \
@@ -85,7 +88,7 @@ static const size_t kHeapCodeStatisticsPropertiesCount =
8588
#undef V
8689

8790
BindingData::BindingData(Environment* env, Local<Object> obj)
88-
: BaseObject(env, obj),
91+
: SnapshotableObject(env, obj, type_int),
8992
heap_statistics_buffer(env->isolate(), kHeapStatisticsPropertiesCount),
9093
heap_space_statistics_buffer(env->isolate(),
9194
kHeapSpaceStatisticsPropertiesCount),
@@ -105,6 +108,32 @@ BindingData::BindingData(Environment* env, Local<Object> obj)
105108
.Check();
106109
}
107110

111+
void BindingData::PrepareForSerialization(Local<Context> context,
112+
v8::SnapshotCreator* creator) {
113+
// We'll just re-initialize the buffers in the constructor since their
114+
// contents can be thrown away once consumed in the previous call.
115+
heap_statistics_buffer.Release();
116+
heap_space_statistics_buffer.Release();
117+
heap_code_statistics_buffer.Release();
118+
}
119+
120+
void BindingData::Deserialize(Local<Context> context,
121+
Local<Object> holder,
122+
int index,
123+
InternalFieldInfo* info) {
124+
DCHECK_EQ(index, BaseObject::kSlot);
125+
HandleScope scope(context->GetIsolate());
126+
Environment* env = Environment::GetCurrent(context);
127+
BindingData* binding = env->AddBindingData<BindingData>(context, holder);
128+
CHECK_NOT_NULL(binding);
129+
}
130+
131+
InternalFieldInfo* BindingData::Serialize(int index) {
132+
DCHECK_EQ(index, BaseObject::kSlot);
133+
InternalFieldInfo* info = InternalFieldInfo::New(type());
134+
return info;
135+
}
136+
108137
void BindingData::MemoryInfo(MemoryTracker* tracker) const {
109138
tracker->TrackField("heap_statistics_buffer", heap_statistics_buffer);
110139
tracker->TrackField("heap_space_statistics_buffer",
@@ -168,7 +197,6 @@ void SetFlagsFromString(const FunctionCallbackInfo<Value>& args) {
168197
V8::SetFlagsFromString(*flags, static_cast<size_t>(flags.length()));
169198
}
170199

171-
172200
void Initialize(Local<Object> target,
173201
Local<Value> unused,
174202
Local<Context> context,
@@ -223,7 +251,16 @@ void Initialize(Local<Object> target,
223251
env->SetMethod(target, "setFlagsFromString", SetFlagsFromString);
224252
}
225253

254+
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
255+
registry->Register(CachedDataVersionTag);
256+
registry->Register(UpdateHeapStatisticsBuffer);
257+
registry->Register(UpdateHeapCodeStatisticsBuffer);
258+
registry->Register(UpdateHeapSpaceStatisticsBuffer);
259+
registry->Register(SetFlagsFromString);
260+
}
261+
226262
} // namespace v8_utils
227263
} // namespace node
228264

229265
NODE_MODULE_CONTEXT_AWARE_INTERNAL(v8, node::v8_utils::Initialize)
266+
NODE_MODULE_EXTERNAL_REFERENCE(v8, node::v8_utils::RegisterExternalReferences)

‎src/node_v8.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@
55

66
#include "aliased_buffer.h"
77
#include "base_object.h"
8+
#include "node_snapshotable.h"
89
#include "util.h"
910
#include "v8.h"
1011

1112
namespace node {
1213
class Environment;
14+
struct InternalFieldInfo;
1315

1416
namespace v8_utils {
15-
class BindingData : public BaseObject {
17+
class BindingData : public SnapshotableObject {
1618
public:
1719
BindingData(Environment* env, v8::Local<v8::Object> obj);
1820

21+
SERIALIZABLE_OBJECT_METHODS()
1922
static constexpr FastStringKey type_name{"node::v8::BindingData"};
23+
static constexpr EmbedderObjectType type_int =
24+
EmbedderObjectType::k_v8_binding_data;
2025

2126
AliasedFloat64Array heap_statistics_buffer;
2227
AliasedFloat64Array heap_space_statistics_buffer;

‎src/stream_wrap.cc

+13-7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "env-inl.h"
2626
#include "handle_wrap.h"
2727
#include "node_buffer.h"
28+
#include "node_external_reference.h"
2829
#include "pipe_wrap.h"
2930
#include "req_wrap-inl.h"
3031
#include "tcp_wrap.h"
@@ -51,20 +52,19 @@ using v8::ReadOnly;
5152
using v8::Signature;
5253
using v8::Value;
5354

55+
void IsConstructCallCallback(const FunctionCallbackInfo<Value>& args) {
56+
CHECK(args.IsConstructCall());
57+
StreamReq::ResetObject(args.This());
58+
}
5459

5560
void LibuvStreamWrap::Initialize(Local<Object> target,
5661
Local<Value> unused,
5762
Local<Context> context,
5863
void* priv) {
5964
Environment* env = Environment::GetCurrent(context);
6065

61-
auto is_construct_call_callback =
62-
[](const FunctionCallbackInfo<Value>& args) {
63-
CHECK(args.IsConstructCall());
64-
StreamReq::ResetObject(args.This());
65-
};
6666
Local<FunctionTemplate> sw =
67-
FunctionTemplate::New(env->isolate(), is_construct_call_callback);
67+
FunctionTemplate::New(env->isolate(), IsConstructCallCallback);
6868
sw->InstanceTemplate()->SetInternalFieldCount(StreamReq::kInternalFieldCount);
6969

7070
// we need to set handle and callback to null,
@@ -88,7 +88,7 @@ void LibuvStreamWrap::Initialize(Local<Object> target,
8888
env->set_shutdown_wrap_template(sw->InstanceTemplate());
8989

9090
Local<FunctionTemplate> ww =
91-
FunctionTemplate::New(env->isolate(), is_construct_call_callback);
91+
FunctionTemplate::New(env->isolate(), IsConstructCallCallback);
9292
ww->InstanceTemplate()->SetInternalFieldCount(
9393
StreamReq::kInternalFieldCount);
9494
ww->Inherit(AsyncWrap::GetConstructorTemplate(env));
@@ -103,6 +103,10 @@ void LibuvStreamWrap::Initialize(Local<Object> target,
103103
env->stream_base_state().GetJSArray()).Check();
104104
}
105105

106+
void LibuvStreamWrap::RegisterExternalReferences(
107+
ExternalReferenceRegistry* registry) {
108+
registry->Register(IsConstructCallCallback);
109+
}
106110

107111
LibuvStreamWrap::LibuvStreamWrap(Environment* env,
108112
Local<Object> object,
@@ -396,3 +400,5 @@ void LibuvStreamWrap::AfterUvWrite(uv_write_t* req, int status) {
396400

397401
NODE_MODULE_CONTEXT_AWARE_INTERNAL(stream_wrap,
398402
node::LibuvStreamWrap::Initialize)
403+
NODE_MODULE_EXTERNAL_REFERENCE(
404+
stream_wrap, node::LibuvStreamWrap::RegisterExternalReferences)

0 commit comments

Comments
 (0)
Please sign in to comment.