Skip to content

Commit 28d7101

Browse files
aduh95codebytere
authored andcommitted
async_hooks: refactor to use more primordials
PR-URL: #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>
1 parent 8a0c3b9 commit 28d7101

3 files changed

Lines changed: 20 additions & 12 deletions

File tree

‎lib/async_hooks.js‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
'use strict';
22

33
const{
4+
ArrayPrototypeIncludes,
5+
ArrayPrototypeIndexOf,
6+
ArrayPrototypePush,
7+
ArrayPrototypeSplice,
8+
FunctionPrototypeBind,
49
NumberIsSafeInteger,
510
ObjectDefineProperties,
611
ObjectIs,
@@ -85,7 +90,7 @@ class AsyncHook {
8590
const[hooks_array,hook_fields]=getHookArrays();
8691

8792
// Each hook is only allowed to be added once.
88-
if(hooks_array.includes(this))
93+
if(ArrayPrototypeIncludes(hooks_array,this))
8994
returnthis;
9095

9196
constprev_kTotals=hook_fields[kTotals];
@@ -99,7 +104,7 @@ class AsyncHook {
99104
hook_fields[kTotals]+=hook_fields[kDestroy]+=+!!this[destroy_symbol];
100105
hook_fields[kTotals]+=
101106
hook_fields[kPromiseResolve]+=+!!this[promise_resolve_symbol];
102-
hooks_array.push(this);
107+
ArrayPrototypePush(hooks_array,this);
103108

104109
if(prev_kTotals===0&&hook_fields[kTotals]>0){
105110
enableHooks();
@@ -113,7 +118,7 @@ class AsyncHook {
113118
disable(){
114119
const[hooks_array,hook_fields]=getHookArrays();
115120

116-
constindex=hooks_array.indexOf(this);
121+
constindex=ArrayPrototypeIndexOf(hooks_array,this);
117122
if(index===-1)
118123
returnthis;
119124

@@ -125,7 +130,7 @@ class AsyncHook {
125130
hook_fields[kTotals]+=hook_fields[kDestroy]-=+!!this[destroy_symbol];
126131
hook_fields[kTotals]+=
127132
hook_fields[kPromiseResolve]-=+!!this[promise_resolve_symbol];
128-
hooks_array.splice(index,1);
133+
ArrayPrototypeSplice(hooks_array,index,1);
129134

130135
if(prev_kTotals>0&&hook_fields[kTotals]===0){
131136
disableHooks();
@@ -218,7 +223,7 @@ class AsyncResource {
218223
bind(fn){
219224
if(typeoffn!=='function')
220225
thrownewERR_INVALID_ARG_TYPE('fn','Function',fn);
221-
constret=this.runInAsyncScope.bind(this,fn);
226+
constret=FunctionPrototypeBind(this.runInAsyncScope,this,fn);
222227
ObjectDefineProperties(ret,{
223228
'length': {
224229
configurable: true,
@@ -264,7 +269,8 @@ class AsyncLocalStorage {
264269
if(this.enabled){
265270
this.enabled=false;
266271
// If this.enabled, the instance must be in storageList
267-
storageList.splice(storageList.indexOf(this),1);
272+
ArrayPrototypeSplice(storageList,
273+
ArrayPrototypeIndexOf(storageList,this),1);
268274
if(storageList.length===0){
269275
storageHook.disable();
270276
}
@@ -274,7 +280,7 @@ class AsyncLocalStorage {
274280
_enable(){
275281
if(!this.enabled){
276282
this.enabled=true;
277-
storageList.push(this);
283+
ArrayPrototypePush(storageList,this);
278284
storageHook.enable();
279285
}
280286
}

‎lib/internal/async_hooks.js‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict';
22

33
const{
4+
ArrayPrototypePop,
5+
ArrayPrototypeSlice,
46
ArrayPrototypeUnshift,
57
ErrorCaptureStackTrace,
68
FunctionPrototypeBind,
@@ -132,7 +134,7 @@ function callbackTrampoline(asyncId, resource, cb, ...args) {
132134
if(asyncId!==0&&hasHooks(kAfter))
133135
emitAfterNative(asyncId);
134136

135-
execution_async_resources.pop();
137+
ArrayPrototypePop(execution_async_resources);
136138
returnresult;
137139
}
138140

@@ -270,7 +272,7 @@ function getHookArrays() {
270272

271273

272274
functionstoreActiveHooks(){
273-
active_hooks.tmp_array=active_hooks.array.slice();
275+
active_hooks.tmp_array=ArrayPrototypeSlice(active_hooks.array);
274276
// Don't want to make the assumption that kInit to kDestroy are indexes 0 to
275277
// 4. So do this the long way.
276278
active_hooks.tmp_fields=[];
@@ -522,7 +524,7 @@ function popAsyncContext(asyncId) {
522524
constoffset=stackLength-1;
523525
async_id_fields[kExecutionAsyncId]=async_wrap.async_ids_stack[2*offset];
524526
async_id_fields[kTriggerAsyncId]=async_wrap.async_ids_stack[2*offset+1];
525-
execution_async_resources.pop();
527+
ArrayPrototypePop(execution_async_resources);
526528
async_hook_fields[kStackLength]=offset;
527529
returnoffset>0;
528530
}

‎lib/internal/inspector_async_hook.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ let hook;
44
letconfig;
55

66
const{
7-
Set,
7+
SafeSet,
88
}=primordials;
99

1010
functionlazyHookCreation(){
@@ -44,7 +44,7 @@ function lazyHookCreation() {
4444
},
4545
});
4646

47-
hook.promiseIds=newSet();
47+
hook.promiseIds=newSafeSet();
4848
}
4949

5050
functionenable(){

0 commit comments

Comments
 (0)