async_hooks: refactor to avoid unsafe array iteration

PR-URL: https://github.com/nodejs/node/pull/37125
Reviewed-By: Zijian Liu <lxxyxzj@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
This commit is contained in:
Antoine du Hamel 2021-01-29 15:03:42 +01:00
parent 814f97186f
commit f8853dd8cc
2 changed files with 9 additions and 10 deletions

View file

@ -89,7 +89,7 @@ class AsyncHook {
// enable()/disable() are run during their execution. The following
// references are reassigned to the tmp arrays if a hook is currently being
// processed.
const [hooks_array, hook_fields] = getHookArrays();
const { 0: hooks_array, 1: hook_fields } = getHookArrays();
// Each hook is only allowed to be added once.
if (ArrayPrototypeIncludes(hooks_array, this))
@ -118,7 +118,7 @@ class AsyncHook {
}
disable() {
const [hooks_array, hook_fields] = getHookArrays();
const { 0: hooks_array, 1: hook_fields } = getHookArrays();
const index = ArrayPrototypeIndexOf(hooks_array, this);
if (index === -1)
@ -195,8 +195,7 @@ class AsyncResource {
emitBefore(asyncId, this[trigger_async_id_symbol], this);
try {
const ret = thisArg === undefined ?
fn(...args) :
const ret =
ReflectApply(fn, thisArg, args);
return ret;
@ -308,7 +307,7 @@ class AsyncLocalStorage {
run(store, callback, ...args) {
// Avoid creation of an AsyncResource if store is already active
if (ObjectIs(store, this.getStore())) {
return callback(...args);
return ReflectApply(callback, null, args);
}
const resource = new AsyncResource('AsyncLocalStorage',
defaultAlsResourceOpts);
@ -316,17 +315,17 @@ class AsyncLocalStorage {
// It is ok because emitDestroy only schedules calling the hook
return resource.emitDestroy().runInAsyncScope(() => {
this.enterWith(store);
return callback(...args);
return ReflectApply(callback, null, args);
});
}
exit(callback, ...args) {
if (!this.enabled) {
return callback(...args);
return ReflectApply(callback, null, args);
}
this.disable();
try {
return callback(...args);
return ReflectApply(callback, null, args);
} finally {
this._enable();
}