Skip to content

Commit

Permalink
async_hooks: eliminate require side effects
Browse files Browse the repository at this point in the history
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 targos committed Nov 21, 2021
1 parent 82b4226 commit 4a971f6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
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

0 comments on commit 4a971f6

Please sign in to comment.