Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
async_hooks: eliminate require side effects
PR-URL: #40782
Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de>
Reviewed-By: Andrey Pechkurov <apechkurov@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
  • Loading branch information
Stephen Belanger authored and Trott committed Nov 13, 2021
commit 689405c1c04cc3084cba678de28ec73653e4dc7d
6 changes: 4 additions & 2 deletions lib/internal/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@ function callbackTrampoline(asyncId, resource, cb, ...args) {
return result;
}

setCallbackTrampoline(callbackTrampoline);

const topLevelResource = {};

function executionAsyncResource() {
Expand Down Expand Up @@ -372,6 +370,8 @@ function promiseResolveHook(promise) {
let wantPromiseHook = false;
function enableHooks() {
async_hook_fields[kCheck] += 1;

setCallbackTrampoline(callbackTrampoline);
}

let stopPromiseHook;
Expand All @@ -398,6 +398,8 @@ function disableHooks() {

wantPromiseHook = false;

setCallbackTrampoline();

// Delay the call to `disablePromiseHook()` because we might currently be
// between the `before` and `after` calls of a Promise.
enqueueMicrotask(disablePromiseHookIfNecessary);
Expand Down
9 changes: 6 additions & 3 deletions src/async_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,11 @@ void AsyncWrap::QueueDestroyAsyncId(const FunctionCallbackInfo<Value>& args) {
void AsyncWrap::SetCallbackTrampoline(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

CHECK(args[0]->IsFunction());

env->set_async_hooks_callback_trampoline(args[0].As<Function>());
if (args[0]->IsFunction()) {
env->set_async_hooks_callback_trampoline(args[0].As<Function>());
} else {
env->set_async_hooks_callback_trampoline(Local<Function>());
}
}

Local<FunctionTemplate> AsyncWrap::GetConstructorTemplate(Environment* env) {
Expand Down Expand Up @@ -439,6 +441,7 @@ void AsyncWrap::Initialize(Local<Object> target,
env->set_async_hooks_after_function(Local<Function>());
env->set_async_hooks_destroy_function(Local<Function>());
env->set_async_hooks_promise_resolve_function(Local<Function>());
env->set_async_hooks_callback_trampoline(Local<Function>());
env->set_async_hooks_binding(target);
}

Expand Down