Skip to content

Commit dd18def

Browse files
aduh95targos
authored andcommitted
async_hooks: refactor to avoid unsafe array iteration
PR-URL: #37125 Reviewed-By: Zijian Liu <lxxyxzj@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com>
1 parent d85b70f commit dd18def

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

lib/async_hooks.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class AsyncHook {
8787
// enable()/disable() are run during their execution. The following
8888
// references are reassigned to the tmp arrays if a hook is currently being
8989
// processed.
90-
const [hooks_array, hook_fields] = getHookArrays();
90+
const { 0: hooks_array, 1: hook_fields } = getHookArrays();
9191

9292
// Each hook is only allowed to be added once.
9393
if (ArrayPrototypeIncludes(hooks_array, this))
@@ -116,7 +116,7 @@ class AsyncHook {
116116
}
117117

118118
disable() {
119-
const [hooks_array, hook_fields] = getHookArrays();
119+
const { 0: hooks_array, 1: hook_fields } = getHookArrays();
120120

121121
const index = ArrayPrototypeIndexOf(hooks_array, this);
122122
if (index === -1)
@@ -193,8 +193,7 @@ class AsyncResource {
193193
emitBefore(asyncId, this[trigger_async_id_symbol], this);
194194

195195
try {
196-
const ret = thisArg === undefined ?
197-
fn(...args) :
196+
const ret =
198197
ReflectApply(fn, thisArg, args);
199198

200199
return ret;
@@ -302,25 +301,25 @@ class AsyncLocalStorage {
302301
run(store, callback, ...args) {
303302
// Avoid creation of an AsyncResource if store is already active
304303
if (ObjectIs(store, this.getStore())) {
305-
return callback(...args);
304+
return ReflectApply(callback, null, args);
306305
}
307306
const resource = new AsyncResource('AsyncLocalStorage',
308307
defaultAlsResourceOpts);
309308
// Calling emitDestroy before runInAsyncScope avoids a try/finally
310309
// It is ok because emitDestroy only schedules calling the hook
311310
return resource.emitDestroy().runInAsyncScope(() => {
312311
this.enterWith(store);
313-
return callback(...args);
312+
return ReflectApply(callback, null, args);
314313
});
315314
}
316315

317316
exit(callback, ...args) {
318317
if (!this.enabled) {
319-
return callback(...args);
318+
return ReflectApply(callback, null, args);
320319
}
321320
this.disable();
322321
try {
323-
return callback(...args);
322+
return ReflectApply(callback, null, args);
324323
} finally {
325324
this._enable();
326325
}

lib/internal/async_hooks.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -428,14 +428,14 @@ function clearDefaultTriggerAsyncId() {
428428

429429
function defaultTriggerAsyncIdScope(triggerAsyncId, block, ...args) {
430430
if (triggerAsyncId === undefined)
431-
return block(...args);
431+
return ReflectApply(block, null, args);
432432
// CHECK(NumberIsSafeInteger(triggerAsyncId))
433433
// CHECK(triggerAsyncId > 0)
434434
const oldDefaultTriggerAsyncId = async_id_fields[kDefaultTriggerAsyncId];
435435
async_id_fields[kDefaultTriggerAsyncId] = triggerAsyncId;
436436

437437
try {
438-
return block(...args);
438+
return ReflectApply(block, null, args);
439439
} finally {
440440
async_id_fields[kDefaultTriggerAsyncId] = oldDefaultTriggerAsyncId;
441441
}

0 commit comments

Comments
 (0)