1 |
|
|
#include "env-inl.h" |
2 |
|
|
#include "node_internals.h" |
3 |
|
|
#include "node_process.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::Local; |
13 |
|
|
using v8::Object; |
14 |
|
|
using v8::String; |
15 |
|
|
using v8::Value; |
16 |
|
|
using v8::NewStringType; |
17 |
|
|
|
18 |
|
4699 |
void RunAtExit(Environment* env) { |
19 |
|
4699 |
env->RunAtExitCallbacks(); |
20 |
|
4699 |
} |
21 |
|
|
|
22 |
|
7 |
void AtExit(void (*cb)(void* arg), void* arg) { |
23 |
|
7 |
auto env = Environment::GetThreadLocalEnv(); |
24 |
|
7 |
AtExit(env, cb, arg); |
25 |
|
7 |
} |
26 |
|
|
|
27 |
|
7 |
void AtExit(Environment* env, void (*cb)(void* arg), void* arg) { |
28 |
✗✓ |
7 |
CHECK_NOT_NULL(env); |
29 |
|
7 |
env->AtExit(cb, arg); |
30 |
|
7 |
} |
31 |
|
|
|
32 |
|
4657 |
void EmitBeforeExit(Environment* env) { |
33 |
|
4657 |
HandleScope handle_scope(env->isolate()); |
34 |
|
4657 |
Context::Scope context_scope(env->context()); |
35 |
|
|
Local<Value> exit_code = env->process_object() |
36 |
|
18628 |
->Get(env->context(), env->exit_code_string()) |
37 |
|
9314 |
.ToLocalChecked() |
38 |
|
18628 |
->ToInteger(env->context()) |
39 |
|
9314 |
.ToLocalChecked(); |
40 |
|
13969 |
ProcessEmit(env, "beforeExit", exit_code).ToLocalChecked(); |
41 |
|
4656 |
} |
42 |
|
|
|
43 |
|
4642 |
int EmitExit(Environment* env) { |
44 |
|
|
// process.emit('exit') |
45 |
|
4642 |
HandleScope handle_scope(env->isolate()); |
46 |
|
4642 |
Context::Scope context_scope(env->context()); |
47 |
|
4642 |
Local<Object> process_object = env->process_object(); |
48 |
|
|
process_object |
49 |
|
|
->Set(env->context(), |
50 |
|
|
FIXED_ONE_BYTE_STRING(env->isolate(), "_exiting"), |
51 |
|
23210 |
True(env->isolate())) |
52 |
|
9284 |
.Check(); |
53 |
|
|
|
54 |
|
4642 |
Local<String> exit_code = env->exit_code_string(); |
55 |
|
13926 |
int code = process_object->Get(env->context(), exit_code) |
56 |
|
9284 |
.ToLocalChecked() |
57 |
|
18568 |
->Int32Value(env->context()) |
58 |
|
9284 |
.ToChecked(); |
59 |
|
9284 |
ProcessEmit(env, "exit", Integer::New(env->isolate(), code)); |
60 |
|
|
|
61 |
|
|
// Reload exit code, it may be changed by `emit('exit')` |
62 |
|
13911 |
return process_object->Get(env->context(), exit_code) |
63 |
|
9274 |
.ToLocalChecked() |
64 |
|
18548 |
->Int32Value(env->context()) |
65 |
|
13911 |
.ToChecked(); |
66 |
|
|
} |
67 |
|
|
|
68 |
|
31 |
void AddEnvironmentCleanupHook(Isolate* isolate, |
69 |
|
|
void (*fun)(void* arg), |
70 |
|
|
void* arg) { |
71 |
|
31 |
Environment* env = Environment::GetCurrent(isolate); |
72 |
✗✓ |
31 |
CHECK_NOT_NULL(env); |
73 |
|
31 |
env->AddCleanupHook(fun, arg); |
74 |
|
31 |
} |
75 |
|
|
|
76 |
|
24 |
void RemoveEnvironmentCleanupHook(Isolate* isolate, |
77 |
|
|
void (*fun)(void* arg), |
78 |
|
|
void* arg) { |
79 |
|
24 |
Environment* env = Environment::GetCurrent(isolate); |
80 |
✗✓ |
24 |
CHECK_NOT_NULL(env); |
81 |
|
24 |
env->RemoveCleanupHook(fun, arg); |
82 |
|
24 |
} |
83 |
|
|
|
84 |
|
2 |
async_id AsyncHooksGetExecutionAsyncId(Isolate* isolate) { |
85 |
|
2 |
Environment* env = Environment::GetCurrent(isolate); |
86 |
✗✓ |
2 |
if (env == nullptr) return -1; |
87 |
|
2 |
return env->execution_async_id(); |
88 |
|
|
} |
89 |
|
|
|
90 |
|
2 |
async_id AsyncHooksGetTriggerAsyncId(Isolate* isolate) { |
91 |
|
2 |
Environment* env = Environment::GetCurrent(isolate); |
92 |
✗✓ |
2 |
if (env == nullptr) return -1; |
93 |
|
2 |
return env->trigger_async_id(); |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
|
97 |
|
539 |
async_context EmitAsyncInit(Isolate* isolate, |
98 |
|
|
Local<Object> resource, |
99 |
|
|
const char* name, |
100 |
|
|
async_id trigger_async_id) { |
101 |
|
539 |
HandleScope handle_scope(isolate); |
102 |
|
|
Local<String> type = |
103 |
|
|
String::NewFromUtf8(isolate, name, NewStringType::kInternalized) |
104 |
|
1078 |
.ToLocalChecked(); |
105 |
|
539 |
return EmitAsyncInit(isolate, resource, type, trigger_async_id); |
106 |
|
|
} |
107 |
|
|
|
108 |
|
547 |
async_context EmitAsyncInit(Isolate* isolate, |
109 |
|
|
Local<Object> resource, |
110 |
|
|
Local<String> name, |
111 |
|
|
async_id trigger_async_id) { |
112 |
|
547 |
DebugSealHandleScope handle_scope(isolate); |
113 |
|
547 |
Environment* env = Environment::GetCurrent(isolate); |
114 |
✗✓ |
547 |
CHECK_NOT_NULL(env); |
115 |
|
|
|
116 |
|
|
// Initialize async context struct |
117 |
✓✓ |
547 |
if (trigger_async_id == -1) |
118 |
|
544 |
trigger_async_id = env->get_default_trigger_async_id(); |
119 |
|
|
|
120 |
|
|
async_context context = { |
121 |
|
547 |
env->new_async_id(), // async_id_ |
122 |
|
|
trigger_async_id // trigger_async_id_ |
123 |
|
547 |
}; |
124 |
|
|
|
125 |
|
|
// Run init hooks |
126 |
|
|
AsyncWrap::EmitAsyncInit(env, resource, name, context.async_id, |
127 |
|
547 |
context.trigger_async_id); |
128 |
|
|
|
129 |
|
547 |
return context; |
130 |
|
|
} |
131 |
|
|
|
132 |
|
3 |
void EmitAsyncDestroy(Isolate* isolate, async_context asyncContext) { |
133 |
|
3 |
EmitAsyncDestroy(Environment::GetCurrent(isolate), asyncContext); |
134 |
|
3 |
} |
135 |
|
|
|
136 |
|
542 |
void EmitAsyncDestroy(Environment* env, async_context asyncContext) { |
137 |
|
542 |
AsyncWrap::EmitDestroy(env, asyncContext.async_id); |
138 |
|
542 |
} |
139 |
|
|
|
140 |
|
|
} // namespace node |