1 |
|
|
#include "env-inl.h" |
2 |
|
|
#include "node_internals.h" |
3 |
|
|
#include "node_process-inl.h" |
4 |
|
|
#include "async_wrap.h" |
5 |
|
|
|
6 |
|
|
namespace node { |
7 |
|
|
|
8 |
|
|
using v8::Context; |
9 |
|
|
using v8::HandleScope; |
10 |
|
|
using v8::Integer; |
11 |
|
|
using v8::Isolate; |
12 |
|
|
using v8::Just; |
13 |
|
|
using v8::Local; |
14 |
|
|
using v8::Maybe; |
15 |
|
|
using v8::NewStringType; |
16 |
|
|
using v8::Nothing; |
17 |
|
|
using v8::Object; |
18 |
|
|
using v8::String; |
19 |
|
|
using v8::Value; |
20 |
|
|
|
21 |
|
5657 |
void RunAtExit(Environment* env) { |
22 |
|
5657 |
env->RunAtExitCallbacks(); |
23 |
|
5657 |
} |
24 |
|
|
|
25 |
|
11132 |
void AtExit(Environment* env, void (*cb)(void* arg), void* arg) { |
26 |
✗✓ |
11132 |
CHECK_NOT_NULL(env); |
27 |
|
11132 |
env->AtExit(cb, arg); |
28 |
|
11132 |
} |
29 |
|
|
|
30 |
|
|
void EmitBeforeExit(Environment* env) { |
31 |
|
|
USE(EmitProcessBeforeExit(env)); |
32 |
|
|
} |
33 |
|
|
|
34 |
|
4828 |
Maybe<bool> EmitProcessBeforeExit(Environment* env) { |
35 |
|
|
TraceEventScope trace_scope(TRACING_CATEGORY_NODE1(environment), |
36 |
|
9654 |
"BeforeExit", env); |
37 |
✓✓ |
4828 |
if (!env->destroy_async_id_list()->empty()) |
38 |
|
165 |
AsyncWrap::DestroyAsyncIdsCallback(env); |
39 |
|
|
|
40 |
|
9654 |
HandleScope handle_scope(env->isolate()); |
41 |
|
4828 |
Local<Context> context = env->context(); |
42 |
|
4828 |
Context::Scope context_scope(context); |
43 |
|
|
|
44 |
|
|
Local<Value> exit_code_v; |
45 |
|
14484 |
if (!env->process_object()->Get(context, env->exit_code_string()) |
46 |
✓✓ |
4828 |
.ToLocal(&exit_code_v)) return Nothing<bool>(); |
47 |
|
|
|
48 |
|
|
Local<Integer> exit_code; |
49 |
✗✓ |
9654 |
if (!exit_code_v->ToInteger(context).ToLocal(&exit_code)) { |
50 |
|
|
return Nothing<bool>(); |
51 |
|
|
} |
52 |
|
|
|
53 |
|
4827 |
return ProcessEmit(env, "beforeExit", exit_code).IsEmpty() ? |
54 |
✓✓ |
4825 |
Nothing<bool>() : Just(true); |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
int EmitExit(Environment* env) { |
58 |
|
|
return EmitProcessExit(env).FromMaybe(1); |
59 |
|
|
} |
60 |
|
|
|
61 |
|
4803 |
Maybe<int> EmitProcessExit(Environment* env) { |
62 |
|
|
// process.emit('exit') |
63 |
|
4803 |
Isolate* isolate = env->isolate(); |
64 |
|
9596 |
HandleScope handle_scope(isolate); |
65 |
|
4803 |
Local<Context> context = env->context(); |
66 |
|
4803 |
Context::Scope context_scope(context); |
67 |
|
4803 |
Local<Object> process_object = env->process_object(); |
68 |
|
|
|
69 |
|
|
// TODO(addaleax): It might be nice to share process._exiting and |
70 |
|
|
// process.exitCode via getter/setter pairs that pass data directly to the |
71 |
|
|
// native side, so that we don't manually have to read and write JS properties |
72 |
|
|
// here. These getters could use e.g. a typed array for performance. |
73 |
|
4803 |
if (process_object |
74 |
|
4803 |
->Set(context, |
75 |
|
|
FIXED_ONE_BYTE_STRING(isolate, "_exiting"), |
76 |
✗✓ |
19212 |
True(isolate)).IsNothing()) return Nothing<int>(); |
77 |
|
|
|
78 |
|
4803 |
Local<String> exit_code = env->exit_code_string(); |
79 |
|
|
Local<Value> code_v; |
80 |
|
|
int code; |
81 |
|
4803 |
if (!process_object->Get(context, exit_code).ToLocal(&code_v) || |
82 |
✓✗ |
9606 |
!code_v->Int32Value(context).To(&code) || |
83 |
✓✓ |
14399 |
ProcessEmit(env, "exit", Integer::New(isolate, code)).IsEmpty() || |
84 |
|
|
// Reload exit code, it may be changed by `emit('exit')` |
85 |
✓✗✓✗
|
19174 |
!process_object->Get(context, exit_code).ToLocal(&code_v) || |
86 |
✗✓✓✓
|
14371 |
!code_v->Int32Value(context).To(&code)) { |
87 |
|
4 |
return Nothing<int>(); |
88 |
|
|
} |
89 |
|
|
|
90 |
|
4789 |
return Just(code); |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
typedef void (*CleanupHook)(void* arg); |
94 |
|
|
typedef void (*AsyncCleanupHook)(void* arg, void(*)(void*), void*); |
95 |
|
|
|
96 |
|
|
struct AsyncCleanupHookInfo final { |
97 |
|
|
Environment* env; |
98 |
|
|
AsyncCleanupHook fun; |
99 |
|
|
void* arg; |
100 |
|
|
bool started = false; |
101 |
|
|
// Use a self-reference to make sure the storage is kept alive while the |
102 |
|
|
// cleanup hook is registered but not yet finished. |
103 |
|
|
std::shared_ptr<AsyncCleanupHookInfo> self; |
104 |
|
|
}; |
105 |
|
|
|
106 |
|
|
// Opaque type that is basically an alias for `shared_ptr<AsyncCleanupHookInfo>` |
107 |
|
|
// (but not publicly so for easier ABI/API changes). In particular, |
108 |
|
|
// std::shared_ptr does not generally maintain a consistent ABI even on a |
109 |
|
|
// specific platform. |
110 |
|
|
struct ACHHandle final { |
111 |
|
|
std::shared_ptr<AsyncCleanupHookInfo> info; |
112 |
|
|
}; |
113 |
|
|
// This is implemented as an operator on a struct because otherwise you can't |
114 |
|
|
// default-initialize AsyncCleanupHookHandle, because in C++ for a |
115 |
|
|
// std::unique_ptr to be default-initializable the deleter type also needs |
116 |
|
|
// to be default-initializable; in particular, function types don't satisfy |
117 |
|
|
// this. |
118 |
✓✗ |
10 |
void DeleteACHHandle::operator ()(ACHHandle* handle) const { delete handle; } |
119 |
|
|
|
120 |
|
39 |
void AddEnvironmentCleanupHook(Isolate* isolate, |
121 |
|
|
CleanupHook fun, |
122 |
|
|
void* arg) { |
123 |
|
39 |
Environment* env = Environment::GetCurrent(isolate); |
124 |
✗✓ |
39 |
CHECK_NOT_NULL(env); |
125 |
|
39 |
env->AddCleanupHook(fun, arg); |
126 |
|
39 |
} |
127 |
|
|
|
128 |
|
25 |
void RemoveEnvironmentCleanupHook(Isolate* isolate, |
129 |
|
|
CleanupHook fun, |
130 |
|
|
void* arg) { |
131 |
|
25 |
Environment* env = Environment::GetCurrent(isolate); |
132 |
✗✓ |
25 |
CHECK_NOT_NULL(env); |
133 |
|
25 |
env->RemoveCleanupHook(fun, arg); |
134 |
|
25 |
} |
135 |
|
|
|
136 |
|
6 |
static void FinishAsyncCleanupHook(void* arg) { |
137 |
|
6 |
AsyncCleanupHookInfo* info = static_cast<AsyncCleanupHookInfo*>(arg); |
138 |
|
12 |
std::shared_ptr<AsyncCleanupHookInfo> keep_alive = info->self; |
139 |
|
|
|
140 |
|
6 |
info->env->DecreaseWaitingRequestCounter(); |
141 |
|
6 |
info->self.reset(); |
142 |
|
6 |
} |
143 |
|
|
|
144 |
|
6 |
static void RunAsyncCleanupHook(void* arg) { |
145 |
|
6 |
AsyncCleanupHookInfo* info = static_cast<AsyncCleanupHookInfo*>(arg); |
146 |
|
6 |
info->env->IncreaseWaitingRequestCounter(); |
147 |
|
6 |
info->started = true; |
148 |
|
6 |
info->fun(info->arg, FinishAsyncCleanupHook, info); |
149 |
|
6 |
} |
150 |
|
|
|
151 |
|
10 |
ACHHandle* AddEnvironmentCleanupHookInternal( |
152 |
|
|
Isolate* isolate, |
153 |
|
|
AsyncCleanupHook fun, |
154 |
|
|
void* arg) { |
155 |
|
10 |
Environment* env = Environment::GetCurrent(isolate); |
156 |
✗✓ |
10 |
CHECK_NOT_NULL(env); |
157 |
|
10 |
auto info = std::make_shared<AsyncCleanupHookInfo>(); |
158 |
|
10 |
info->env = env; |
159 |
|
10 |
info->fun = fun; |
160 |
|
10 |
info->arg = arg; |
161 |
|
10 |
info->self = info; |
162 |
|
10 |
env->AddCleanupHook(RunAsyncCleanupHook, info.get()); |
163 |
|
10 |
return new ACHHandle { info }; |
164 |
|
|
} |
165 |
|
|
|
166 |
|
10 |
void RemoveEnvironmentCleanupHookInternal( |
167 |
|
|
ACHHandle* handle) { |
168 |
✓✓ |
10 |
if (handle->info->started) return; |
169 |
|
4 |
handle->info->self.reset(); |
170 |
|
4 |
handle->info->env->RemoveCleanupHook(RunAsyncCleanupHook, handle->info.get()); |
171 |
|
|
} |
172 |
|
|
|
173 |
|
2 |
async_id AsyncHooksGetExecutionAsyncId(Isolate* isolate) { |
174 |
|
2 |
Environment* env = Environment::GetCurrent(isolate); |
175 |
✗✓ |
2 |
if (env == nullptr) return -1; |
176 |
|
2 |
return env->execution_async_id(); |
177 |
|
|
} |
178 |
|
|
|
179 |
|
2 |
async_id AsyncHooksGetTriggerAsyncId(Isolate* isolate) { |
180 |
|
2 |
Environment* env = Environment::GetCurrent(isolate); |
181 |
✗✓ |
2 |
if (env == nullptr) return -1; |
182 |
|
2 |
return env->trigger_async_id(); |
183 |
|
|
} |
184 |
|
|
|
185 |
|
|
|
186 |
|
540 |
async_context EmitAsyncInit(Isolate* isolate, |
187 |
|
|
Local<Object> resource, |
188 |
|
|
const char* name, |
189 |
|
|
async_id trigger_async_id) { |
190 |
|
1080 |
HandleScope handle_scope(isolate); |
191 |
|
|
Local<String> type = |
192 |
|
540 |
String::NewFromUtf8(isolate, name, NewStringType::kInternalized) |
193 |
|
540 |
.ToLocalChecked(); |
194 |
|
540 |
return EmitAsyncInit(isolate, resource, type, trigger_async_id); |
195 |
|
|
} |
196 |
|
|
|
197 |
|
540 |
async_context EmitAsyncInit(Isolate* isolate, |
198 |
|
|
Local<Object> resource, |
199 |
|
|
Local<String> name, |
200 |
|
|
async_id trigger_async_id) { |
201 |
|
540 |
DebugSealHandleScope handle_scope(isolate); |
202 |
|
540 |
Environment* env = Environment::GetCurrent(isolate); |
203 |
✗✓ |
540 |
CHECK_NOT_NULL(env); |
204 |
|
|
|
205 |
|
|
// Initialize async context struct |
206 |
✓✓ |
540 |
if (trigger_async_id == -1) |
207 |
|
537 |
trigger_async_id = env->get_default_trigger_async_id(); |
208 |
|
|
|
209 |
|
|
async_context context = { |
210 |
|
540 |
env->new_async_id(), // async_id_ |
211 |
|
|
trigger_async_id // trigger_async_id_ |
212 |
|
540 |
}; |
213 |
|
|
|
214 |
|
|
// Run init hooks |
215 |
|
540 |
AsyncWrap::EmitAsyncInit(env, resource, name, context.async_id, |
216 |
|
|
context.trigger_async_id); |
217 |
|
|
|
218 |
|
540 |
return context; |
219 |
|
|
} |
220 |
|
|
|
221 |
|
3 |
void EmitAsyncDestroy(Isolate* isolate, async_context asyncContext) { |
222 |
|
3 |
EmitAsyncDestroy(Environment::GetCurrent(isolate), asyncContext); |
223 |
|
3 |
} |
224 |
|
|
|
225 |
|
536 |
void EmitAsyncDestroy(Environment* env, async_context asyncContext) { |
226 |
|
536 |
AsyncWrap::EmitDestroy(env, asyncContext.async_id); |
227 |
|
536 |
} |
228 |
|
|
|
229 |
|
|
} // namespace node |