1 |
|
|
#include "env-inl.h" |
2 |
|
|
#include "node.h" |
3 |
|
|
#include "node_errors.h" |
4 |
|
|
#include "node_external_reference.h" |
5 |
|
|
#include "node_internals.h" |
6 |
|
|
#include "node_process.h" |
7 |
|
|
#include "util-inl.h" |
8 |
|
|
#include "v8.h" |
9 |
|
|
|
10 |
|
|
#include <atomic> |
11 |
|
|
|
12 |
|
|
namespace node { |
13 |
|
|
|
14 |
|
|
using errors::TryCatchScope; |
15 |
|
|
using v8::Context; |
16 |
|
|
using v8::Function; |
17 |
|
|
using v8::FunctionCallbackInfo; |
18 |
|
|
using v8::Isolate; |
19 |
|
|
using v8::kPromiseHandlerAddedAfterReject; |
20 |
|
|
using v8::kPromiseRejectAfterResolved; |
21 |
|
|
using v8::kPromiseRejectWithNoHandler; |
22 |
|
|
using v8::kPromiseResolveAfterResolved; |
23 |
|
|
using v8::Local; |
24 |
|
|
using v8::MicrotasksScope; |
25 |
|
|
using v8::Number; |
26 |
|
|
using v8::Object; |
27 |
|
|
using v8::Promise; |
28 |
|
|
using v8::PromiseRejectEvent; |
29 |
|
|
using v8::PromiseRejectMessage; |
30 |
|
|
using v8::Value; |
31 |
|
|
|
32 |
|
15773 |
void PromiseRejectCallback(PromiseRejectMessage message) { |
33 |
|
|
static std::atomic<uint64_t> unhandledRejections{0}; |
34 |
|
|
static std::atomic<uint64_t> rejectionsHandledAfter{0}; |
35 |
|
|
|
36 |
|
15773 |
Local<Promise> promise = message.GetPromise(); |
37 |
|
15773 |
Isolate* isolate = promise->GetIsolate(); |
38 |
|
15773 |
PromiseRejectEvent event = message.GetEvent(); |
39 |
|
|
|
40 |
|
15773 |
Environment* env = Environment::GetCurrent(isolate); |
41 |
|
|
|
42 |
✓✗✓✓ ✓✓ |
15774 |
if (env == nullptr || !env->can_call_into_js()) return; |
43 |
|
|
|
44 |
|
15772 |
Local<Function> callback = env->promise_reject_callback(); |
45 |
|
|
// The promise is rejected before JS land calls SetPromiseRejectCallback |
46 |
|
|
// to initializes the promise reject callback during bootstrap. |
47 |
✗✓ |
15772 |
CHECK(!callback.IsEmpty()); |
48 |
|
|
|
49 |
|
|
Local<Value> value; |
50 |
|
15772 |
Local<Value> type = Number::New(env->isolate(), event); |
51 |
|
|
|
52 |
✓✓ |
15772 |
if (event == kPromiseRejectWithNoHandler) { |
53 |
|
8545 |
value = message.GetValue(); |
54 |
|
8545 |
unhandledRejections++; |
55 |
✓✓ |
8545 |
TRACE_COUNTER2(TRACING_CATEGORY_NODE2(promises, rejections), |
56 |
|
|
"rejections", |
57 |
|
|
"unhandled", unhandledRejections, |
58 |
|
138 |
"handledAfter", rejectionsHandledAfter); |
59 |
✓✓✓✓
|
24455 |
} else if (event == kPromiseHandlerAddedAfterReject) { |
60 |
|
15696 |
value = Undefined(isolate); |
61 |
|
7151 |
rejectionsHandledAfter++; |
62 |
|
4 |
TRACE_COUNTER2(TRACING_CATEGORY_NODE2(promises, rejections), |
63 |
✓✓ |
7155 |
"rejections", |
64 |
|
|
"unhandled", unhandledRejections, |
65 |
|
119 |
"handledAfter", rejectionsHandledAfter); |
66 |
✓✓✓✓
|
14497 |
} else if (event == kPromiseResolveAfterResolved) { |
67 |
|
7222 |
value = message.GetValue(); |
68 |
✓✗ |
5 |
} else if (event == kPromiseRejectAfterResolved) { |
69 |
|
7 |
value = message.GetValue(); |
70 |
|
2 |
} else { |
71 |
|
|
return; |
72 |
|
|
} |
73 |
|
|
|
74 |
✗✓ |
15772 |
if (value.IsEmpty()) { |
75 |
|
|
value = Undefined(isolate); |
76 |
|
|
} |
77 |
|
|
|
78 |
|
31544 |
Local<Value> args[] = { type, promise, value }; |
79 |
|
|
|
80 |
|
|
// V8 does not expect this callback to have a scheduled exceptions once it |
81 |
|
|
// returns, so we print them out in a best effort to do something about it |
82 |
|
|
// without failing silently and without crashing the process. |
83 |
|
31544 |
TryCatchScope try_catch(env); |
84 |
|
31544 |
USE(callback->Call( |
85 |
|
78860 |
env->context(), Undefined(isolate), arraysize(args), args)); |
86 |
✓✓✓✗ ✓✓ |
15772 |
if (try_catch.HasCaught() && !try_catch.HasTerminated()) { |
87 |
|
394 |
fprintf(stderr, "Exception in PromiseRejectCallback:\n"); |
88 |
|
394 |
PrintCaughtException(isolate, env->context(), try_catch); |
89 |
|
|
} |
90 |
|
|
} |
91 |
|
|
namespace task_queue { |
92 |
|
|
|
93 |
|
313 |
static void EnqueueMicrotask(const FunctionCallbackInfo<Value>& args) { |
94 |
|
313 |
Environment* env = Environment::GetCurrent(args); |
95 |
|
313 |
Isolate* isolate = env->isolate(); |
96 |
|
|
|
97 |
✗✓ |
626 |
CHECK(args[0]->IsFunction()); |
98 |
|
|
|
99 |
|
626 |
isolate->EnqueueMicrotask(args[0].As<Function>()); |
100 |
|
313 |
} |
101 |
|
|
|
102 |
|
442372 |
static void RunMicrotasks(const FunctionCallbackInfo<Value>& args) { |
103 |
|
442372 |
MicrotasksScope::PerformCheckpoint(args.GetIsolate()); |
104 |
|
442369 |
} |
105 |
|
|
|
106 |
|
438 |
static void SetTickCallback(const FunctionCallbackInfo<Value>& args) { |
107 |
|
438 |
Environment* env = Environment::GetCurrent(args); |
108 |
✗✓ |
876 |
CHECK(args[0]->IsFunction()); |
109 |
|
876 |
env->set_tick_callback_function(args[0].As<Function>()); |
110 |
|
438 |
} |
111 |
|
|
|
112 |
|
438 |
static void SetPromiseRejectCallback( |
113 |
|
|
const FunctionCallbackInfo<Value>& args) { |
114 |
|
438 |
Environment* env = Environment::GetCurrent(args); |
115 |
|
|
|
116 |
✗✓ |
876 |
CHECK(args[0]->IsFunction()); |
117 |
|
876 |
env->set_promise_reject_callback(args[0].As<Function>()); |
118 |
|
438 |
} |
119 |
|
|
|
120 |
|
438 |
static void Initialize(Local<Object> target, |
121 |
|
|
Local<Value> unused, |
122 |
|
|
Local<Context> context, |
123 |
|
|
void* priv) { |
124 |
|
438 |
Environment* env = Environment::GetCurrent(context); |
125 |
|
438 |
Isolate* isolate = env->isolate(); |
126 |
|
|
|
127 |
|
438 |
env->SetMethod(target, "enqueueMicrotask", EnqueueMicrotask); |
128 |
|
438 |
env->SetMethod(target, "setTickCallback", SetTickCallback); |
129 |
|
438 |
env->SetMethod(target, "runMicrotasks", RunMicrotasks); |
130 |
|
876 |
target->Set(env->context(), |
131 |
|
|
FIXED_ONE_BYTE_STRING(isolate, "tickInfo"), |
132 |
|
2190 |
env->tick_info()->fields().GetJSArray()).Check(); |
133 |
|
|
|
134 |
|
438 |
Local<Object> events = Object::New(isolate); |
135 |
|
876 |
NODE_DEFINE_CONSTANT(events, kPromiseRejectWithNoHandler); |
136 |
|
438 |
NODE_DEFINE_CONSTANT(events, kPromiseHandlerAddedAfterReject); |
137 |
|
2190 |
NODE_DEFINE_CONSTANT(events, kPromiseResolveAfterResolved); |
138 |
|
2190 |
NODE_DEFINE_CONSTANT(events, kPromiseRejectAfterResolved); |
139 |
|
2628 |
|
140 |
|
3066 |
target->Set(env->context(), |
141 |
|
1314 |
FIXED_ONE_BYTE_STRING(isolate, "promiseRejectEvents"), |
142 |
|
2628 |
events).Check(); |
143 |
|
|
env->SetMethod(target, |
144 |
|
|
"setPromiseRejectCallback", |
145 |
|
438 |
SetPromiseRejectCallback); |
146 |
|
438 |
} |
147 |
|
|
|
148 |
|
4601 |
void RegisterExternalReferences(ExternalReferenceRegistry* registry) { |
149 |
|
4601 |
registry->Register(EnqueueMicrotask); |
150 |
|
4601 |
registry->Register(SetTickCallback); |
151 |
|
4601 |
registry->Register(RunMicrotasks); |
152 |
|
4601 |
registry->Register(SetPromiseRejectCallback); |
153 |
|
4601 |
} |
154 |
|
|
|
155 |
|
|
} // namespace task_queue |
156 |
|
|
} // namespace node |
157 |
|
|
|
158 |
|
4670 |
NODE_MODULE_CONTEXT_AWARE_INTERNAL(task_queue, node::task_queue::Initialize) |
159 |
✓✗✓✗
|
18635 |
NODE_MODULE_EXTERNAL_REFERENCE(task_queue, |
160 |
|
|
node::task_queue::RegisterExternalReferences) |