1 |
|
|
#include "node_main_instance.h" |
2 |
|
|
#include "node_internals.h" |
3 |
|
|
#include "node_options-inl.h" |
4 |
|
|
#include "node_v8_platform-inl.h" |
5 |
|
|
#include "util-inl.h" |
6 |
|
|
|
7 |
|
|
namespace node { |
8 |
|
|
|
9 |
|
|
using v8::Context; |
10 |
|
|
using v8::HandleScope; |
11 |
|
|
using v8::Isolate; |
12 |
|
|
using v8::Local; |
13 |
|
|
using v8::Locker; |
14 |
|
|
using v8::SealHandleScope; |
15 |
|
|
|
16 |
|
1 |
NodeMainInstance::NodeMainInstance(Isolate* isolate, |
17 |
|
|
uv_loop_t* event_loop, |
18 |
|
|
MultiIsolatePlatform* platform, |
19 |
|
|
const std::vector<std::string>& args, |
20 |
|
|
const std::vector<std::string>& exec_args) |
21 |
|
|
: args_(args), |
22 |
|
|
exec_args_(exec_args), |
23 |
|
|
array_buffer_allocator_(nullptr), |
24 |
|
|
isolate_(isolate), |
25 |
|
|
platform_(platform), |
26 |
|
|
isolate_data_(nullptr), |
27 |
|
|
owns_isolate_(false), |
28 |
|
1 |
deserialize_mode_(false) { |
29 |
|
1 |
isolate_data_.reset(new IsolateData(isolate_, event_loop, platform, nullptr)); |
30 |
|
1 |
SetIsolateUpForNode(isolate_, IsolateSettingCategories::kMisc); |
31 |
|
1 |
} |
32 |
|
|
|
33 |
|
1 |
std::unique_ptr<NodeMainInstance> NodeMainInstance::Create( |
34 |
|
|
Isolate* isolate, |
35 |
|
|
uv_loop_t* event_loop, |
36 |
|
|
MultiIsolatePlatform* platform, |
37 |
|
|
const std::vector<std::string>& args, |
38 |
|
|
const std::vector<std::string>& exec_args) { |
39 |
|
|
return std::unique_ptr<NodeMainInstance>( |
40 |
|
1 |
new NodeMainInstance(isolate, event_loop, platform, args, exec_args)); |
41 |
|
|
} |
42 |
|
|
|
43 |
|
4974 |
NodeMainInstance::NodeMainInstance( |
44 |
|
|
Isolate::CreateParams* params, |
45 |
|
|
uv_loop_t* event_loop, |
46 |
|
|
MultiIsolatePlatform* platform, |
47 |
|
|
const std::vector<std::string>& args, |
48 |
|
|
const std::vector<std::string>& exec_args, |
49 |
|
|
const std::vector<size_t>* per_isolate_data_indexes) |
50 |
|
|
: args_(args), |
51 |
|
|
exec_args_(exec_args), |
52 |
|
|
array_buffer_allocator_(ArrayBufferAllocator::Create()), |
53 |
|
|
isolate_(nullptr), |
54 |
|
|
platform_(platform), |
55 |
|
|
isolate_data_(nullptr), |
56 |
|
4974 |
owns_isolate_(true) { |
57 |
|
4974 |
params->array_buffer_allocator = array_buffer_allocator_.get(); |
58 |
|
4974 |
isolate_ = Isolate::Allocate(); |
59 |
✗✓ |
4974 |
CHECK_NOT_NULL(isolate_); |
60 |
|
|
// Register the isolate on the platform before the isolate gets initialized, |
61 |
|
|
// so that the isolate can access the platform during initialization. |
62 |
|
4974 |
platform->RegisterIsolate(isolate_, event_loop); |
63 |
|
4974 |
SetIsolateCreateParamsForNode(params); |
64 |
|
4974 |
Isolate::Initialize(isolate_, *params); |
65 |
|
|
|
66 |
|
4974 |
deserialize_mode_ = per_isolate_data_indexes != nullptr; |
67 |
|
|
// If the indexes are not nullptr, we are not deserializing |
68 |
✓✓✗✓ ✗✓ |
4974 |
CHECK_IMPLIES(deserialize_mode_, params->external_references != nullptr); |
69 |
|
|
isolate_data_.reset(new IsolateData(isolate_, |
70 |
|
|
event_loop, |
71 |
|
|
platform, |
72 |
|
4974 |
array_buffer_allocator_.get(), |
73 |
|
4974 |
per_isolate_data_indexes)); |
74 |
|
4974 |
SetIsolateUpForNode(isolate_, IsolateSettingCategories::kMisc); |
75 |
✓✓ |
4974 |
if (!deserialize_mode_) { |
76 |
|
|
// If in deserialize mode, delay until after the deserialization is |
77 |
|
|
// complete. |
78 |
|
1 |
SetIsolateUpForNode(isolate_, IsolateSettingCategories::kErrorHandlers); |
79 |
|
|
} |
80 |
|
4974 |
} |
81 |
|
|
|
82 |
|
1 |
void NodeMainInstance::Dispose() { |
83 |
✗✓ |
1 |
CHECK(!owns_isolate_); |
84 |
|
1 |
platform_->DrainTasks(isolate_); |
85 |
|
1 |
} |
86 |
|
|
|
87 |
✓✓✓✓ ✓✓✓✓
|
9138 |
NodeMainInstance::~NodeMainInstance() { |
88 |
✓✓ |
4569 |
if (!owns_isolate_) { |
89 |
|
1 |
return; |
90 |
|
|
} |
91 |
|
4568 |
isolate_->Dispose(); |
92 |
|
4568 |
platform_->UnregisterIsolate(isolate_); |
93 |
|
4568 |
} |
94 |
|
|
|
95 |
|
4974 |
int NodeMainInstance::Run() { |
96 |
|
4974 |
Locker locker(isolate_); |
97 |
|
9542 |
Isolate::Scope isolate_scope(isolate_); |
98 |
|
9542 |
HandleScope handle_scope(isolate_); |
99 |
|
|
|
100 |
|
4974 |
int exit_code = 0; |
101 |
|
9542 |
std::unique_ptr<Environment> env = CreateMainEnvironment(&exit_code); |
102 |
|
|
|
103 |
✗✓ |
4974 |
CHECK_NOT_NULL(env); |
104 |
|
4974 |
Context::Scope context_scope(env->context()); |
105 |
|
|
|
106 |
✓✓ |
4974 |
if (exit_code == 0) { |
107 |
|
|
{ |
108 |
|
4973 |
AsyncCallbackScope callback_scope(env.get()); |
109 |
|
4973 |
env->async_hooks()->push_async_ids(1, 0); |
110 |
|
4973 |
LoadEnvironment(env.get()); |
111 |
|
4722 |
env->async_hooks()->pop_async_id(1); |
112 |
|
|
} |
113 |
|
|
|
114 |
|
4721 |
env->set_trace_sync_io(env->options()->trace_sync_io); |
115 |
|
|
|
116 |
|
|
{ |
117 |
|
4721 |
SealHandleScope seal(isolate_); |
118 |
|
|
bool more; |
119 |
|
|
env->performance_state()->Mark( |
120 |
|
4721 |
node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_START); |
121 |
✓✓ |
4586 |
do { |
122 |
|
4735 |
uv_run(env->event_loop(), UV_RUN_DEFAULT); |
123 |
|
|
|
124 |
|
4587 |
per_process::v8_platform.DrainVMTasks(isolate_); |
125 |
|
|
|
126 |
|
4587 |
more = uv_loop_alive(env->event_loop()); |
127 |
✗✓✗✗ ✗✓ |
4587 |
if (more && !env->is_stopping()) continue; |
128 |
|
|
|
129 |
|
4587 |
env->RunBeforeExitCallbacks(); |
130 |
|
|
|
131 |
✓✗ |
4587 |
if (!uv_loop_alive(env->event_loop())) { |
132 |
|
4587 |
EmitBeforeExit(env.get()); |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
// Emit `beforeExit` if the loop became alive either after emitting |
136 |
|
|
// event, or after running some callbacks. |
137 |
|
4586 |
more = uv_loop_alive(env->event_loop()); |
138 |
✓✓✓✗
|
4586 |
} while (more == true && !env->is_stopping()); |
139 |
|
|
env->performance_state()->Mark( |
140 |
|
4572 |
node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_EXIT); |
141 |
|
|
} |
142 |
|
|
|
143 |
|
4572 |
env->set_trace_sync_io(false); |
144 |
|
4572 |
exit_code = EmitExit(env.get()); |
145 |
|
4567 |
WaitForInspectorDisconnect(env.get()); |
146 |
|
|
} |
147 |
|
|
|
148 |
|
4568 |
env->set_can_call_into_js(false); |
149 |
|
4568 |
env->stop_sub_worker_contexts(); |
150 |
|
4568 |
ResetStdio(); |
151 |
|
4568 |
env->RunCleanup(); |
152 |
|
4568 |
RunAtExit(env.get()); |
153 |
|
|
|
154 |
|
4568 |
per_process::v8_platform.DrainVMTasks(isolate_); |
155 |
|
4568 |
per_process::v8_platform.CancelVMTasks(isolate_); |
156 |
|
|
|
157 |
|
|
#if defined(LEAK_SANITIZER) |
158 |
|
|
__lsan_do_leak_check(); |
159 |
|
|
#endif |
160 |
|
|
|
161 |
|
9136 |
return exit_code; |
162 |
|
|
} |
163 |
|
|
|
164 |
|
|
// TODO(joyeecheung): align this with the CreateEnvironment exposed in node.h |
165 |
|
|
// and the environment creation routine in workers somehow. |
166 |
|
4974 |
std::unique_ptr<Environment> NodeMainInstance::CreateMainEnvironment( |
167 |
|
|
int* exit_code) { |
168 |
|
4974 |
*exit_code = 0; // Reset the exit code to 0 |
169 |
|
|
|
170 |
|
4974 |
HandleScope handle_scope(isolate_); |
171 |
|
|
|
172 |
|
|
// TODO(addaleax): This should load a real per-Isolate option, currently |
173 |
|
|
// this is still effectively per-process. |
174 |
✓✓ |
4974 |
if (isolate_data_->options()->track_heap_objects) { |
175 |
|
1 |
isolate_->GetHeapProfiler()->StartTrackingHeapObjects(true); |
176 |
|
|
} |
177 |
|
|
|
178 |
|
|
Local<Context> context; |
179 |
✓✓ |
4974 |
if (deserialize_mode_) { |
180 |
|
|
context = |
181 |
|
9946 |
Context::FromSnapshot(isolate_, kNodeContextIndex).ToLocalChecked(); |
182 |
|
4973 |
SetIsolateUpForNode(isolate_, IsolateSettingCategories::kErrorHandlers); |
183 |
|
|
} else { |
184 |
|
|
context = NewContext(isolate_); |
185 |
|
|
} |
186 |
|
|
|
187 |
✗✓ |
4974 |
CHECK(!context.IsEmpty()); |
188 |
|
|
Context::Scope context_scope(context); |
189 |
|
|
|
190 |
|
|
std::unique_ptr<Environment> env = std::make_unique<Environment>( |
191 |
|
4974 |
isolate_data_.get(), |
192 |
|
|
context, |
193 |
|
|
args_, |
194 |
|
|
exec_args_, |
195 |
|
|
static_cast<Environment::Flags>(Environment::kIsMainThread | |
196 |
|
|
Environment::kOwnsProcessState | |
197 |
|
9948 |
Environment::kOwnsInspector)); |
198 |
|
4974 |
env->InitializeLibuv(per_process::v8_is_profiling); |
199 |
|
4974 |
env->InitializeDiagnostics(); |
200 |
|
|
|
201 |
|
|
// TODO(joyeecheung): when we snapshot the bootstrapped context, |
202 |
|
|
// the inspector and diagnostics setup should after after deserialization. |
203 |
|
|
#if HAVE_INSPECTOR && NODE_USE_V8_PLATFORM |
204 |
|
4974 |
*exit_code = env->InitializeInspector(nullptr); |
205 |
|
|
#endif |
206 |
✓✓ |
4974 |
if (*exit_code != 0) { |
207 |
|
1 |
return env; |
208 |
|
|
} |
209 |
|
|
|
210 |
✗✓ |
9946 |
if (env->RunBootstrapping().IsEmpty()) { |
211 |
|
|
*exit_code = 1; |
212 |
|
|
} |
213 |
|
|
|
214 |
|
9947 |
return env; |
215 |
|
|
} |
216 |
|
|
|
217 |
|
|
} // namespace node |