summaryrefslogtreecommitdiff
path: root/src/node_task_queue.cc
diff options
context:
space:
mode:
authorJoyee Cheung <joyeec9h3@gmail.com>2018-12-23 08:45:07 +0800
committerJoyee Cheung <joyeec9h3@gmail.com>2019-01-06 10:28:47 +0800
commitd18b0a01320e392e48f0c475effd8d4db2bb71a8 (patch)
tree91801c7d10b4adc09fe3dbc93cd1c962a7938533 /src/node_task_queue.cc
parentbf566718b29a6fae8cef8d0fecd7e77948726d5a (diff)
downloadandroid-node-v8-d18b0a01320e392e48f0c475effd8d4db2bb71a8.tar.gz
android-node-v8-d18b0a01320e392e48f0c475effd8d4db2bb71a8.tar.bz2
android-node-v8-d18b0a01320e392e48f0c475effd8d4db2bb71a8.zip
process: make tick callback and promise rejection callback more robust
- Rename `internalTickCallback` to `processTicksAndRejections`, make sure it does not get called if it's not set in C++. - Rename `emitPromiseRejectionWarnings` to `processPromiseRejections` since it also emit events that are not warnings. - Sets `SetPromiseRejectCallback` in the `Environment` constructor to make sure it only gets called once per-isolate, and make sure it does not get called if it's not set in C++. - Wrap promise rejection callback initialization into `listenForRejections()`. - Add comments. PR-URL: https://github.com/nodejs/node/pull/25200 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'src/node_task_queue.cc')
-rw-r--r--src/node_task_queue.cc17
1 files changed, 8 insertions, 9 deletions
diff --git a/src/node_task_queue.cc b/src/node_task_queue.cc
index 5bfa281068..43d8e1cc24 100644
--- a/src/node_task_queue.cc
+++ b/src/node_task_queue.cc
@@ -36,7 +36,7 @@ static void SetTickCallback(const FunctionCallbackInfo<Value>& args) {
env->set_tick_callback_function(args[0].As<Function>());
}
-static void PromiseRejectCallback(PromiseRejectMessage message) {
+void PromiseRejectCallback(PromiseRejectMessage message) {
static std::atomic<uint64_t> unhandledRejections{0};
static std::atomic<uint64_t> rejectionsHandledAfter{0};
@@ -49,6 +49,10 @@ static void PromiseRejectCallback(PromiseRejectMessage message) {
if (env == nullptr) return;
Local<Function> callback = env->promise_reject_callback();
+ // The promise is rejected before JS land calls SetPromiseRejectCallback
+ // to initializes the promise reject callback during bootstrap.
+ CHECK(!callback.IsEmpty());
+
Local<Value> value;
Local<Value> type = Number::New(env->isolate(), event);
@@ -83,17 +87,12 @@ static void PromiseRejectCallback(PromiseRejectMessage message) {
env->context(), Undefined(isolate), arraysize(args), args));
}
-static void InitializePromiseRejectCallback(
+static void SetPromiseRejectCallback(
const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
CHECK(args[0]->IsFunction());
-
- // TODO(joyeecheung): this may be moved to somewhere earlier in the bootstrap
- // to make sure it's only called once
- isolate->SetPromiseRejectCallback(PromiseRejectCallback);
-
env->set_promise_reject_callback(args[0].As<Function>());
}
@@ -120,8 +119,8 @@ static void Initialize(Local<Object> target,
FIXED_ONE_BYTE_STRING(isolate, "promiseRejectEvents"),
events).FromJust();
env->SetMethod(target,
- "initializePromiseRejectCallback",
- InitializePromiseRejectCallback);
+ "setPromiseRejectCallback",
+ SetPromiseRejectCallback);
}
} // namespace task_queue