From 6d43c8dd69c541f0f7378a57756d77573f5b2923 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Wed, 18 Nov 2020 10:53:25 +0100 Subject: [PATCH] async_hooks: refactor to use more primordials PR-URL: https://github.com/nodejs/node/pull/36168 Reviewed-By: Rich Trott Reviewed-By: James M Snell Reviewed-By: Benjamin Gruenbaum Reviewed-By: Trivikram Kamat --- lib/async_hooks.js | 20 +++++++++++++------- lib/internal/async_hooks.js | 8 +++++--- lib/internal/inspector_async_hook.js | 4 ++-- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/lib/async_hooks.js b/lib/async_hooks.js index b6865b6f1cd03e..90b48ebe4b2754 100644 --- a/lib/async_hooks.js +++ b/lib/async_hooks.js @@ -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(); } } diff --git a/lib/internal/async_hooks.js b/lib/internal/async_hooks.js index adc87f9ed9662d..84e73280ccec48 100644 --- a/lib/internal/async_hooks.js +++ b/lib/internal/async_hooks.js @@ -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; } diff --git a/lib/internal/inspector_async_hook.js b/lib/internal/inspector_async_hook.js index a6112697cfdaa2..bd3aa635051c5b 100644 --- a/lib/internal/inspector_async_hook.js +++ b/lib/internal/inspector_async_hook.js @@ -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() {