FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
node/src/api/hooks.cc at main · fetchapi/node · GitHub
fetchapi
/
node
Public
forked from
nodejs/node
Notifications
You must be signed in to change notification settings
Fork
1
Star
0
Code
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
node
/
src
/
api
/
hooks.cc
Copy path
More file actions
More file actions
Latest commit
History
History
History
237 lines (196 loc) · 7.07 KB
Breadcrumbs
node
/
src
/
api
/
hooks.cc
Copy path
File metadata and controls
237 lines (196 loc) · 7.07 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#
include
"
env-inl.h
"
#
include
"
node_internals.h
"
#
include
"
node_process-inl.h
"
#
include
"
async_wrap.h
"
namespace
node
{
using
v8::Context;
using
v8::HandleScope;
using
v8::Integer;
using
v8::Isolate;
using
v8::Just;
using
v8::Local;
using
v8::Maybe;
using
v8::NewStringType;
using
v8::Nothing;
using
v8::Object;
using
v8::String;
void
RunAtExit
(Environment* env) {
env->
RunAtExitCallbacks
();
}
void
AtExit
(Environment* env,
void
(*cb)(
void
* arg), void* arg) {
CHECK_NOT_NULL
(env);
env->
AtExit
(cb, arg);
}
void
EmitBeforeExit
(Environment* env) {
USE
(
EmitProcessBeforeExit
(env));
}
Maybe<
bool
>
EmitProcessBeforeExit
(Environment* env) {
TRACE_EVENT0
(
TRACING_CATEGORY_NODE1
(environment),
"
BeforeExit
"
);
if
(!env->
destroy_async_id_list
()->
empty
())
AsyncWrap::DestroyAsyncIdsCallback
(env);
Isolate* isolate = env->
isolate
();
HandleScope
handle_scope
(isolate);
Context::Scope
context_scope
(env->
context
());
if
(!env->
can_call_into_js
()) {
return
Nothing<
bool
>();
}
Local<Integer> exit_code =
Integer::New
(
isolate,
static_cast
<
int32_t
>(env->
exit_code
(ExitCode::
kNoFailure
)));
return
ProcessEmit
(env,
"
beforeExit
"
, exit_code).
IsEmpty
() ?
Nothing<
bool
>() :
Just
(
true
);
}
static
ExitCode
EmitExitInternal
(Environment* env) {
return
EmitProcessExitInternal
(env).
FromMaybe
(ExitCode::
kGenericUserError
);
}
int
EmitExit
(Environment* env) {
return
static_cast
<
int
>(
EmitExitInternal
(env));
}
Maybe<ExitCode>
EmitProcessExitInternal
(Environment* env) {
//
process.emit('exit')
Isolate* isolate = env->
isolate
();
HandleScope
handle_scope
(isolate);
Context::Scope
context_scope
(env->
context
());
env->
set_exiting
(
true
);
if
(!env->
can_call_into_js
()) {
return
Nothing<ExitCode>();
}
Local<Integer> exit_code =
Integer::New
(
isolate,
static_cast
<
int32_t
>(env->
exit_code
(ExitCode::
kNoFailure
)));
if
(
ProcessEmit
(env,
"
exit
"
, exit_code).
IsEmpty
()) {
return
Nothing<ExitCode>();
}
//
Reload exit code, it may be changed by `emit('exit')`
return
Just
(env->
exit_code
(ExitCode::
kNoFailure
));
}
Maybe<
int
>
EmitProcessExit
(Environment* env) {
Maybe<ExitCode> result =
EmitProcessExitInternal
(env);
if
(result.
IsNothing
()) {
return
Nothing<
int
>();
}
return
Just
(
static_cast
<
int
>(result.
FromJust
()));
}
typedef
void
(*CleanupHook)(
void
* arg);
typedef
void
(*AsyncCleanupHook)(
void
* arg,
void
(*)(
void
*), void*);
struct
AsyncCleanupHookInfo
final
{
Environment* env;
AsyncCleanupHook fun;
void
* arg;
bool
started =
false
;
//
Use a self-reference to make sure the storage is kept alive while the
//
cleanup hook is registered but not yet finished.
std::shared_ptr<AsyncCleanupHookInfo> self;
};
//
Opaque type that is basically an alias for `shared_ptr<AsyncCleanupHookInfo>`
//
(but not publicly so for easier ABI/API changes). In particular,
//
std::shared_ptr does not generally maintain a consistent ABI even on a
//
specific platform.
struct
ACHHandle
final
{
std::shared_ptr<AsyncCleanupHookInfo> info;
};
//
This is implemented as an operator on a struct because otherwise you can't
//
default-initialize AsyncCleanupHookHandle, because in C++ for a
//
std::unique_ptr to be default-initializable the deleter type also needs
//
to be default-initializable; in particular, function types don't satisfy
//
this.
void
DeleteACHHandle::operator
()(ACHHandle* handle)
const
{
delete
handle; }
void
AddEnvironmentCleanupHook
(Isolate* isolate,
CleanupHook fun,
void
* arg) {
Environment* env =
Environment::GetCurrent
(isolate);
CHECK_NOT_NULL
(env);
env->
AddCleanupHook
(fun, arg);
}
void
RemoveEnvironmentCleanupHook
(Isolate* isolate,
CleanupHook fun,
void
* arg) {
Environment* env =
Environment::GetCurrent
(isolate);
CHECK_NOT_NULL
(env);
env->
RemoveCleanupHook
(fun, arg);
}
static
void
FinishAsyncCleanupHook
(
void
* arg) {
AsyncCleanupHookInfo* info =
static_cast
<AsyncCleanupHookInfo*>(arg);
std::shared_ptr<AsyncCleanupHookInfo> keep_alive = info->
self
;
info->
env
->
DecreaseWaitingRequestCounter
();
info->
self
.
reset
();
}
static
void
RunAsyncCleanupHook
(
void
* arg) {
AsyncCleanupHookInfo* info =
static_cast
<AsyncCleanupHookInfo*>(arg);
info->
env
->
IncreaseWaitingRequestCounter
();
info->
started
=
true
;
info->
fun
(info->
arg
, FinishAsyncCleanupHook, info);
}
ACHHandle*
AddEnvironmentCleanupHookInternal
(
Isolate* isolate,
AsyncCleanupHook fun,
void
* arg) {
Environment* env =
Environment::GetCurrent
(isolate);
CHECK_NOT_NULL
(env);
auto
info = std::make_shared<AsyncCleanupHookInfo>();
info->
env
= env;
info->
fun
= fun;
info->
arg
= arg;
info->
self
= info;
env->
AddCleanupHook
(RunAsyncCleanupHook, info.
get
());
return
new
ACHHandle { info };
}
void
RemoveEnvironmentCleanupHookInternal
(
ACHHandle* handle) {
if
(handle->
info
->
started
)
return
;
handle->
info
->
self
.
reset
();
handle->
info
->
env
->
RemoveCleanupHook
(RunAsyncCleanupHook, handle->
info
.
get
());
}
void
RequestInterrupt
(Environment* env,
void
(*fun)(
void
* arg), void* arg) {
env->
RequestInterrupt
([fun, arg](Environment* env) {
//
Disallow JavaScript execution during interrupt.
Isolate::DisallowJavascriptExecutionScope
scope
(
env->
isolate
(),
Isolate::DisallowJavascriptExecutionScope::
CRASH_ON_FAILURE
);
fun
(arg);
});
}
async_id
AsyncHooksGetExecutionAsyncId
(Isolate* isolate) {
Environment* env =
Environment::GetCurrent
(isolate);
if
(env ==
nullptr
)
return
-
1
;
return
env->
execution_async_id
();
}
async_id
AsyncHooksGetTriggerAsyncId
(Isolate* isolate) {
Environment* env =
Environment::GetCurrent
(isolate);
if
(env ==
nullptr
)
return
-
1
;
return
env->
trigger_async_id
();
}
async_context
EmitAsyncInit
(Isolate* isolate,
Local<Object> resource,
const
char
* name,
async_id trigger_async_id) {
HandleScope
handle_scope
(isolate);
Local<String> type =
String::NewFromUtf8
(isolate, name, NewStringType::
kInternalized
)
.
ToLocalChecked
();
return
EmitAsyncInit
(isolate, resource, type, trigger_async_id);
}
async_context
EmitAsyncInit
(Isolate* isolate,
Local<Object> resource,
Local<String> name,
async_id trigger_async_id) {
DebugSealHandleScope
handle_scope
(isolate);
Environment* env =
Environment::GetCurrent
(isolate);
CHECK_NOT_NULL
(env);
//
Initialize async context struct
if
(trigger_async_id == -
1
)
trigger_async_id = env->
get_default_trigger_async_id
();
async_context context = {
env->
new_async_id
(),
//
async_id_
trigger_async_id
//
trigger_async_id_
};
//
Run init hooks
AsyncWrap::EmitAsyncInit
(env, resource, name, context.
async_id
,
context.
trigger_async_id
);
return
context;
}
void
EmitAsyncDestroy
(Isolate* isolate, async_context asyncContext) {
EmitAsyncDestroy
(
Environment::GetCurrent
(isolate), asyncContext);
}
void
EmitAsyncDestroy
(Environment* env, async_context asyncContext) {
AsyncWrap::EmitDestroy
(env, asyncContext.
async_id
);
}
}
//
namespace node
Back
|
FazBrowse Home
|
New Git URL