mirror of https://github.com/nodejs/node.git
async_hooks: refactor to use more primordials
PR-URL: https://github.com/nodejs/node/pull/36168 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
parent
514f464a60
commit
f47d65538a
|
@ -1,6 +1,11 @@
|
|||
'use strict';
|
||||
|
||||
const {
|
||||
ArrayPrototypeIncludes,
|
||||
ArrayPrototypeIndexOf,
|
||||
ArrayPrototypePush,
|
||||
ArrayPrototypeSplice,
|
||||
FunctionPrototypeBind,
|
||||
NumberIsSafeInteger,
|
||||
ObjectDefineProperties,
|
||||
ObjectIs,
|
||||
|
@ -85,7 +90,7 @@ class AsyncHook {
|
|||
const [hooks_array, hook_fields] = getHookArrays();
|
||||
|
||||
// Each hook is only allowed to be added once.
|
||||
if (hooks_array.includes(this))
|
||||
if (ArrayPrototypeIncludes(hooks_array, this))
|
||||
return this;
|
||||
|
||||
const prev_kTotals = hook_fields[kTotals];
|
||||
|
@ -99,7 +104,7 @@ class AsyncHook {
|
|||
hook_fields[kTotals] += hook_fields[kDestroy] += +!!this[destroy_symbol];
|
||||
hook_fields[kTotals] +=
|
||||
hook_fields[kPromiseResolve] += +!!this[promise_resolve_symbol];
|
||||
hooks_array.push(this);
|
||||
ArrayPrototypePush(hooks_array, this);
|
||||
|
||||
if (prev_kTotals === 0 && hook_fields[kTotals] > 0) {
|
||||
enableHooks();
|
||||
|
@ -113,7 +118,7 @@ class AsyncHook {
|
|||
disable() {
|
||||
const [hooks_array, hook_fields] = getHookArrays();
|
||||
|
||||
const index = hooks_array.indexOf(this);
|
||||
const index = ArrayPrototypeIndexOf(hooks_array, this);
|
||||
if (index === -1)
|
||||
return this;
|
||||
|
||||
|
@ -125,7 +130,7 @@ class AsyncHook {
|
|||
hook_fields[kTotals] += hook_fields[kDestroy] -= +!!this[destroy_symbol];
|
||||
hook_fields[kTotals] +=
|
||||
hook_fields[kPromiseResolve] -= +!!this[promise_resolve_symbol];
|
||||
hooks_array.splice(index, 1);
|
||||
ArrayPrototypeSplice(hooks_array, index, 1);
|
||||
|
||||
if (prev_kTotals > 0 && hook_fields[kTotals] === 0) {
|
||||
disableHooks();
|
||||
|
@ -218,7 +223,7 @@ class AsyncResource {
|
|||
bind(fn) {
|
||||
if (typeof fn !== 'function')
|
||||
throw new ERR_INVALID_ARG_TYPE('fn', 'Function', fn);
|
||||
const ret = this.runInAsyncScope.bind(this, fn);
|
||||
const ret = FunctionPrototypeBind(this.runInAsyncScope, this, fn);
|
||||
ObjectDefineProperties(ret, {
|
||||
'length': {
|
||||
configurable: true,
|
||||
|
@ -264,7 +269,8 @@ class AsyncLocalStorage {
|
|||
if (this.enabled) {
|
||||
this.enabled = false;
|
||||
// If this.enabled, the instance must be in storageList
|
||||
storageList.splice(storageList.indexOf(this), 1);
|
||||
ArrayPrototypeSplice(storageList,
|
||||
ArrayPrototypeIndexOf(storageList, this), 1);
|
||||
if (storageList.length === 0) {
|
||||
storageHook.disable();
|
||||
}
|
||||
|
@ -274,7 +280,7 @@ class AsyncLocalStorage {
|
|||
_enable() {
|
||||
if (!this.enabled) {
|
||||
this.enabled = true;
|
||||
storageList.push(this);
|
||||
ArrayPrototypePush(storageList, this);
|
||||
storageHook.enable();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
'use strict';
|
||||
|
||||
const {
|
||||
ArrayPrototypePop,
|
||||
ArrayPrototypeSlice,
|
||||
ArrayPrototypeUnshift,
|
||||
ErrorCaptureStackTrace,
|
||||
FunctionPrototypeBind,
|
||||
|
@ -132,7 +134,7 @@ function callbackTrampoline(asyncId, resource, cb, ...args) {
|
|||
if (asyncId !== 0 && hasHooks(kAfter))
|
||||
emitAfterNative(asyncId);
|
||||
|
||||
execution_async_resources.pop();
|
||||
ArrayPrototypePop(execution_async_resources);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -270,7 +272,7 @@ function getHookArrays() {
|
|||
|
||||
|
||||
function storeActiveHooks() {
|
||||
active_hooks.tmp_array = active_hooks.array.slice();
|
||||
active_hooks.tmp_array = ArrayPrototypeSlice(active_hooks.array);
|
||||
// Don't want to make the assumption that kInit to kDestroy are indexes 0 to
|
||||
// 4. So do this the long way.
|
||||
active_hooks.tmp_fields = [];
|
||||
|
@ -522,7 +524,7 @@ function popAsyncContext(asyncId) {
|
|||
const offset = stackLength - 1;
|
||||
async_id_fields[kExecutionAsyncId] = async_wrap.async_ids_stack[2 * offset];
|
||||
async_id_fields[kTriggerAsyncId] = async_wrap.async_ids_stack[2 * offset + 1];
|
||||
execution_async_resources.pop();
|
||||
ArrayPrototypePop(execution_async_resources);
|
||||
async_hook_fields[kStackLength] = offset;
|
||||
return offset > 0;
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ let hook;
|
|||
let config;
|
||||
|
||||
const {
|
||||
Set,
|
||||
SafeSet,
|
||||
} = primordials;
|
||||
|
||||
function lazyHookCreation() {
|
||||
|
@ -44,7 +44,7 @@ function lazyHookCreation() {
|
|||
},
|
||||
});
|
||||
|
||||
hook.promiseIds = new Set();
|
||||
hook.promiseIds = new SafeSet();
|
||||
}
|
||||
|
||||
function enable() {
|
||||
|
|
Loading…
Reference in New Issue