From 1c816f0c591cd0242930e8605c5473416cc94241 Mon Sep 17 00:00:00 2001 From: rickyes Date: Sat, 4 Apr 2020 19:40:36 +0800 Subject: [PATCH] async_hooks: use hasHooks function internally MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/32656 Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: Gerhard Stöbich Reviewed-By: James M Snell --- lib/internal/async_hooks.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/internal/async_hooks.js b/lib/internal/async_hooks.js index f7a7f7aad66df9..8439260f1af1a6 100644 --- a/lib/internal/async_hooks.js +++ b/lib/internal/async_hooks.js @@ -316,27 +316,31 @@ function defaultTriggerAsyncIdScope(triggerAsyncId, block, ...args) { } } +function hasHooks(key) { + return async_hook_fields[key] > 0; +} + function enabledHooksExist() { - return async_hook_fields[kCheck] > 0; + return hasHooks(kCheck); } function initHooksExist() { - return async_hook_fields[kInit] > 0; + return hasHooks(kInit); } function afterHooksExist() { - return async_hook_fields[kAfter] > 0; + return hasHooks(kAfter); } function destroyHooksExist() { - return async_hook_fields[kDestroy] > 0; + return hasHooks(kDestroy); } function emitInitScript(asyncId, type, triggerAsyncId, resource) { // Short circuit all checks for the common case. Which is that no hooks have // been set. Do this to remove performance impact for embedders (and core). - if (async_hook_fields[kInit] === 0) + if (!hasHooks(kInit)) return; if (triggerAsyncId === null) { @@ -350,13 +354,13 @@ function emitInitScript(asyncId, type, triggerAsyncId, resource) { function emitBeforeScript(asyncId, triggerAsyncId, resource) { pushAsyncContext(asyncId, triggerAsyncId, resource); - if (async_hook_fields[kBefore] > 0) + if (hasHooks(kBefore)) emitBeforeNative(asyncId); } function emitAfterScript(asyncId) { - if (async_hook_fields[kAfter] > 0) + if (hasHooks(kAfter)) emitAfterNative(asyncId); popAsyncContext(asyncId); @@ -365,7 +369,7 @@ function emitAfterScript(asyncId) { function emitDestroyScript(asyncId) { // Return early if there are no destroy callbacks, or invalid asyncId. - if (async_hook_fields[kDestroy] === 0 || asyncId <= 0) + if (!hasHooks(kDestroy) || asyncId <= 0) return; async_wrap.queueDestroyAsyncId(asyncId); } @@ -382,7 +386,7 @@ function clearAsyncIdStack() { function hasAsyncIdStack() { - return async_hook_fields[kStackLength] > 0; + return hasHooks(kStackLength); }