Skip to content

Commit

Permalink
async_hooks: refactor to use more primordials
Browse files Browse the repository at this point in the history
PR-URL: #36168
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
  • Loading branch information
aduh95 authored and codebytere committed Nov 22, 2020
1 parent 8a0c3b9 commit 28d7101
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
20 changes: 13 additions & 7 deletions lib/async_hooks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
'use strict';

const {
ArrayPrototypeIncludes,
ArrayPrototypeIndexOf,
ArrayPrototypePush,
ArrayPrototypeSplice,
FunctionPrototypeBind,
NumberIsSafeInteger,
ObjectDefineProperties,
ObjectIs,
Expand Down Expand Up @@ -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];
Expand All @@ -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();
Expand All @@ -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;

Expand All @@ -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();
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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();
}
Expand All @@ -274,7 +280,7 @@ class AsyncLocalStorage {
_enable() {
if (!this.enabled) {
this.enabled = true;
storageList.push(this);
ArrayPrototypePush(storageList, this);
storageHook.enable();
}
}
Expand Down
8 changes: 5 additions & 3 deletions lib/internal/async_hooks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

const {
ArrayPrototypePop,
ArrayPrototypeSlice,
ArrayPrototypeUnshift,
ErrorCaptureStackTrace,
FunctionPrototypeBind,
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/inspector_async_hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ let hook;
let config;

const {
Set,
SafeSet,
} = primordials;

function lazyHookCreation() {
Expand Down Expand Up @@ -44,7 +44,7 @@ function lazyHookCreation() {
},
});

hook.promiseIds = new Set();
hook.promiseIds = new SafeSet();
}

function enable() {
Expand Down

0 comments on commit 28d7101

Please sign in to comment.