1 |
|
|
#include "node.h" |
2 |
|
|
#include "env-inl.h" |
3 |
|
|
#include "debug_utils-inl.h" |
4 |
|
|
|
5 |
|
|
using v8::Context; |
6 |
|
|
using v8::Function; |
7 |
|
|
using v8::Global; |
8 |
|
|
using v8::HandleScope; |
9 |
|
|
using v8::Isolate; |
10 |
|
|
using v8::Just; |
11 |
|
|
using v8::Local; |
12 |
|
|
using v8::Locker; |
13 |
|
|
using v8::Maybe; |
14 |
|
|
using v8::Nothing; |
15 |
|
|
using v8::SealHandleScope; |
16 |
|
|
|
17 |
|
|
namespace node { |
18 |
|
|
|
19 |
|
6123 |
Maybe<ExitCode> SpinEventLoopInternal(Environment* env) { |
20 |
✗✓ |
6123 |
CHECK_NOT_NULL(env); |
21 |
|
6123 |
MultiIsolatePlatform* platform = GetMultiIsolatePlatform(env); |
22 |
✗✓ |
6123 |
CHECK_NOT_NULL(platform); |
23 |
|
|
|
24 |
|
6123 |
Isolate* isolate = env->isolate(); |
25 |
|
11988 |
HandleScope handle_scope(isolate); |
26 |
|
6123 |
Context::Scope context_scope(env->context()); |
27 |
|
11988 |
SealHandleScope seal(isolate); |
28 |
|
|
|
29 |
✗✓ |
6123 |
if (env->is_stopping()) return Nothing<ExitCode>(); |
30 |
|
|
|
31 |
|
6123 |
env->set_trace_sync_io(env->options()->trace_sync_io); |
32 |
|
|
{ |
33 |
|
|
bool more; |
34 |
|
6123 |
env->performance_state()->Mark( |
35 |
|
|
node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_START); |
36 |
|
64 |
do { |
37 |
✗✓ |
6187 |
if (env->is_stopping()) break; |
38 |
|
6187 |
uv_run(env->event_loop(), UV_RUN_DEFAULT); |
39 |
✓✓ |
5950 |
if (env->is_stopping()) break; |
40 |
|
|
|
41 |
|
5635 |
platform->DrainTasks(isolate); |
42 |
|
|
|
43 |
|
5626 |
more = uv_loop_alive(env->event_loop()); |
44 |
✓✓✓✗ ✓✓ |
5626 |
if (more && !env->is_stopping()) continue; |
45 |
|
|
|
46 |
✓✓ |
11152 |
if (EmitProcessBeforeExit(env).IsNothing()) |
47 |
|
8 |
break; |
48 |
|
|
|
49 |
|
|
{ |
50 |
|
5567 |
HandleScope handle_scope(isolate); |
51 |
✗✓ |
11134 |
if (env->RunSnapshotSerializeCallback().IsEmpty()) { |
52 |
|
|
break; |
53 |
|
|
} |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
// Emit `beforeExit` if the loop became alive either after emitting |
57 |
|
|
// event, or after running some callbacks. |
58 |
|
5567 |
more = uv_loop_alive(env->event_loop()); |
59 |
✓✓✓✗ ✓✓ |
5616 |
} while (more == true && !env->is_stopping()); |
60 |
|
5875 |
env->performance_state()->Mark( |
61 |
|
|
node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_EXIT); |
62 |
|
|
} |
63 |
✓✓ |
5875 |
if (env->is_stopping()) return Nothing<ExitCode>(); |
64 |
|
|
|
65 |
|
5543 |
env->set_trace_sync_io(false); |
66 |
|
|
// Clear the serialize callback even though the JS-land queue should |
67 |
|
|
// be empty this point so that the deserialized instance won't |
68 |
|
|
// attempt to call into JS again. |
69 |
|
11086 |
env->set_snapshot_serialize_callback(Local<Function>()); |
70 |
|
|
|
71 |
|
5543 |
env->PrintInfoForSnapshotIfDebug(); |
72 |
|
11086 |
env->ForEachRealm([](Realm* realm) { realm->VerifyNoStrongBaseObjects(); }); |
73 |
|
5543 |
return EmitProcessExitInternal(env); |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
struct CommonEnvironmentSetup::Impl { |
77 |
|
|
MultiIsolatePlatform* platform = nullptr; |
78 |
|
|
uv_loop_t loop; |
79 |
|
|
std::shared_ptr<ArrayBufferAllocator> allocator; |
80 |
|
|
Isolate* isolate = nullptr; |
81 |
|
|
DeleteFnPtr<IsolateData, FreeIsolateData> isolate_data; |
82 |
|
|
DeleteFnPtr<Environment, FreeEnvironment> env; |
83 |
|
|
Global<Context> context; |
84 |
|
|
}; |
85 |
|
|
|
86 |
|
9 |
CommonEnvironmentSetup::CommonEnvironmentSetup( |
87 |
|
|
MultiIsolatePlatform* platform, |
88 |
|
|
std::vector<std::string>* errors, |
89 |
|
9 |
std::function<Environment*(const CommonEnvironmentSetup*)> make_env) |
90 |
|
9 |
: impl_(new Impl()) { |
91 |
✗✓ |
9 |
CHECK_NOT_NULL(platform); |
92 |
✗✓ |
9 |
CHECK_NOT_NULL(errors); |
93 |
|
|
|
94 |
|
9 |
impl_->platform = platform; |
95 |
|
9 |
uv_loop_t* loop = &impl_->loop; |
96 |
|
|
// Use `data` to tell the destructor whether the loop was initialized or not. |
97 |
|
9 |
loop->data = nullptr; |
98 |
|
9 |
int ret = uv_loop_init(loop); |
99 |
✗✓ |
9 |
if (ret != 0) { |
100 |
|
|
errors->push_back( |
101 |
|
|
SPrintF("Failed to initialize loop: %s", uv_err_name(ret))); |
102 |
|
|
return; |
103 |
|
|
} |
104 |
|
9 |
loop->data = this; |
105 |
|
|
|
106 |
|
9 |
impl_->allocator = ArrayBufferAllocator::Create(); |
107 |
|
9 |
impl_->isolate = NewIsolate(impl_->allocator, &impl_->loop, platform); |
108 |
|
9 |
Isolate* isolate = impl_->isolate; |
109 |
|
|
|
110 |
|
|
{ |
111 |
|
|
Locker locker(isolate); |
112 |
|
9 |
Isolate::Scope isolate_scope(isolate); |
113 |
|
9 |
impl_->isolate_data.reset(CreateIsolateData( |
114 |
|
9 |
isolate, loop, platform, impl_->allocator.get())); |
115 |
|
|
|
116 |
|
9 |
HandleScope handle_scope(isolate); |
117 |
|
9 |
Local<Context> context = NewContext(isolate); |
118 |
|
9 |
impl_->context.Reset(isolate, context); |
119 |
✗✓ |
9 |
if (context.IsEmpty()) { |
120 |
|
|
errors->push_back("Failed to initialize V8 Context"); |
121 |
|
|
return; |
122 |
|
|
} |
123 |
|
|
|
124 |
|
9 |
Context::Scope context_scope(context); |
125 |
|
9 |
impl_->env.reset(make_env(this)); |
126 |
|
|
} |
127 |
|
|
} |
128 |
|
|
|
129 |
|
14 |
CommonEnvironmentSetup::~CommonEnvironmentSetup() { |
130 |
✓✗ |
7 |
if (impl_->isolate != nullptr) { |
131 |
|
7 |
Isolate* isolate = impl_->isolate; |
132 |
|
|
{ |
133 |
|
7 |
Locker locker(isolate); |
134 |
|
14 |
Isolate::Scope isolate_scope(isolate); |
135 |
|
|
|
136 |
|
7 |
impl_->context.Reset(); |
137 |
|
7 |
impl_->env.reset(); |
138 |
|
7 |
impl_->isolate_data.reset(); |
139 |
|
|
} |
140 |
|
|
|
141 |
|
7 |
bool platform_finished = false; |
142 |
|
7 |
impl_->platform->AddIsolateFinishedCallback(isolate, [](void* data) { |
143 |
|
7 |
*static_cast<bool*>(data) = true; |
144 |
|
7 |
}, &platform_finished); |
145 |
|
7 |
impl_->platform->UnregisterIsolate(isolate); |
146 |
|
7 |
isolate->Dispose(); |
147 |
|
|
|
148 |
|
|
// Wait until the platform has cleaned up all relevant resources. |
149 |
✓✓ |
14 |
while (!platform_finished) |
150 |
|
7 |
uv_run(&impl_->loop, UV_RUN_ONCE); |
151 |
|
|
} |
152 |
|
|
|
153 |
✗✓✗✗
|
7 |
if (impl_->isolate || impl_->loop.data != nullptr) |
154 |
|
7 |
CheckedUvLoopClose(&impl_->loop); |
155 |
|
|
|
156 |
✓✗ |
7 |
delete impl_; |
157 |
|
7 |
} |
158 |
|
|
|
159 |
|
8 |
Maybe<int> SpinEventLoop(Environment* env) { |
160 |
|
8 |
Maybe<ExitCode> result = SpinEventLoopInternal(env); |
161 |
✗✓ |
8 |
if (result.IsNothing()) { |
162 |
|
|
return Nothing<int>(); |
163 |
|
|
} |
164 |
|
8 |
return Just(static_cast<int>(result.FromJust())); |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
uv_loop_t* CommonEnvironmentSetup::event_loop() const { |
168 |
|
|
return &impl_->loop; |
169 |
|
|
} |
170 |
|
|
|
171 |
|
|
std::shared_ptr<ArrayBufferAllocator> |
172 |
|
|
CommonEnvironmentSetup::array_buffer_allocator() const { |
173 |
|
|
return impl_->allocator; |
174 |
|
|
} |
175 |
|
|
|
176 |
|
17 |
Isolate* CommonEnvironmentSetup::isolate() const { |
177 |
|
17 |
return impl_->isolate; |
178 |
|
|
} |
179 |
|
|
|
180 |
|
9 |
IsolateData* CommonEnvironmentSetup::isolate_data() const { |
181 |
|
9 |
return impl_->isolate_data.get(); |
182 |
|
|
} |
183 |
|
|
|
184 |
|
13 |
Environment* CommonEnvironmentSetup::env() const { |
185 |
|
13 |
return impl_->env.get(); |
186 |
|
|
} |
187 |
|
|
|
188 |
|
22 |
v8::Local<v8::Context> CommonEnvironmentSetup::context() const { |
189 |
✗✓ |
44 |
return impl_->context.Get(impl_->isolate); |
190 |
|
|
} |
191 |
|
|
|
192 |
|
|
} // namespace node |