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
|
#include "snapshot_builder.h"
#include <iostream>
#include <sstream>
#include "debug_utils-inl.h"
#include "env-inl.h"
#include "node_external_reference.h"
#include "node_internals.h"
#include "node_main_instance.h"
#include "node_v8_platform-inl.h"
namespace node {
using v8::Context;
using v8::HandleScope;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::SnapshotCreator;
using v8::StartupData;
template <typename T>
void WriteVector(std::stringstream* ss, const T* vec, size_t size) {
for (size_t i = 0; i < size; i++) {
*ss << std::to_string(vec[i]) << (i == size - 1 ? '\n' : ',');
}
}
std::string FormatBlob(v8::StartupData* blob,
const std::vector<size_t>& isolate_data_indexes,
const EnvSerializeInfo& env_info) {
std::stringstream ss;
ss << R"(#include <cstddef>
#include "env.h"
#include "node_main_instance.h"
#include "v8.h"
// This file is generated by tools/snapshot. Do not edit.
namespace node {
static const char blob_data[] = {
)";
WriteVector(&ss, blob->data, blob->raw_size);
ss << R"(};
static const int blob_size = )"
<< blob->raw_size << R"(;
static v8::StartupData blob = { blob_data, blob_size };
)";
ss << R"(v8::StartupData* NodeMainInstance::GetEmbeddedSnapshotBlob() {
return &blob;
}
static const std::vector<size_t> isolate_data_indexes {
)";
WriteVector(&ss, isolate_data_indexes.data(), isolate_data_indexes.size());
ss << R"(};
const std::vector<size_t>* NodeMainInstance::GetIsolateDataIndexes() {
return &isolate_data_indexes;
}
static const EnvSerializeInfo env_info )"
<< env_info << R"(;
const EnvSerializeInfo* NodeMainInstance::GetEnvSerializeInfo() {
return &env_info;
}
} // namespace node
)";
return ss.str();
}
static v8::StartupData SerializeNodeContextInternalFields(Local<Object> holder,
int index,
void* env) {
void* ptr = holder->GetAlignedPointerFromInternalField(index);
if (ptr == nullptr || ptr == env) {
return StartupData{nullptr, 0};
}
if (ptr == env && index == ContextEmbedderIndex::kEnvironment) {
return StartupData{nullptr, 0};
}
// No embedder objects in the builtin snapshot yet.
UNREACHABLE();
return StartupData{nullptr, 0};
}
std::string SnapshotBuilder::Generate(
const std::vector<std::string> args,
const std::vector<std::string> exec_args) {
Isolate* isolate = Isolate::Allocate();
per_process::v8_platform.Platform()->RegisterIsolate(isolate,
uv_default_loop());
std::unique_ptr<NodeMainInstance> main_instance;
std::string result;
{
std::vector<size_t> isolate_data_indexes;
EnvSerializeInfo env_info;
const std::vector<intptr_t>& external_references =
NodeMainInstance::CollectExternalReferences();
SnapshotCreator creator(isolate, external_references.data());
Environment* env;
{
main_instance =
NodeMainInstance::Create(isolate,
uv_default_loop(),
per_process::v8_platform.Platform(),
args,
exec_args);
HandleScope scope(isolate);
creator.SetDefaultContext(Context::New(isolate));
isolate_data_indexes = main_instance->isolate_data()->Serialize(&creator);
Local<Context> context = NewContext(isolate);
Context::Scope context_scope(context);
env = new Environment(main_instance->isolate_data(),
context,
args,
exec_args,
nullptr,
node::EnvironmentFlags::kDefaultFlags,
{});
if (per_process::enabled_debug_list.enabled(DebugCategory::MKSNAPSHOT)) {
env->PrintAllBaseObjects();
printf("Environment = %p\n", env);
}
env_info = env->Serialize(&creator);
size_t index = creator.AddContext(
context, {SerializeNodeContextInternalFields, env});
CHECK_EQ(index, NodeMainInstance::kNodeContextIndex);
}
// Must be out of HandleScope
StartupData blob =
creator.CreateBlob(SnapshotCreator::FunctionCodeHandling::kClear);
CHECK(blob.CanBeRehashed());
// Must be done while the snapshot creator isolate is entered i.e. the
// creator is still alive.
FreeEnvironment(env);
main_instance->Dispose();
result = FormatBlob(&blob, isolate_data_indexes, env_info);
delete[] blob.data;
}
per_process::v8_platform.Platform()->UnregisterIsolate(isolate);
return result;
}
} // namespace node
|