From 1906f19e49d6d7bbf2e835c1c4c3e4a2cf6f358a Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sat, 7 Nov 2020 10:27:46 +0100 Subject: [PATCH] vm: refactor to use more primordials PR-URL: https://github.com/nodejs/node/pull/36023 Reviewed-By: Rich Trott --- lib/internal/vm/module.js | 17 ++++++++++------- lib/vm.js | 5 +++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/internal/vm/module.js b/lib/internal/vm/module.js index c10433a36f6dcb..72f53727d86138 100644 --- a/lib/internal/vm/module.js +++ b/lib/internal/vm/module.js @@ -3,14 +3,17 @@ const assert = require('internal/assert'); const { ArrayIsArray, + ArrayPrototypeForEach, + ArrayPrototypeIndexOf, + ArrayPrototypeSome, ObjectCreate, ObjectDefineProperty, ObjectGetPrototypeOf, ObjectSetPrototypeOf, - SafePromise, + PromiseAll, + SafeWeakMap, Symbol, TypeError, - WeakMap, } = primordials; const { isContext } = internalBinding('contextify'); @@ -61,7 +64,7 @@ const STATUS_MAP = { let globalModuleId = 0; const defaultModuleName = 'vm:module'; -const wrapToModuleMap = new WeakMap(); +const wrapToModuleMap = new SafeWeakMap(); const kWrap = Symbol('kWrap'); const kContext = Symbol('kContext'); @@ -331,7 +334,7 @@ class SourceTextModule extends Module { try { if (promises !== undefined) { - await SafePromise.all(promises); + await PromiseAll(promises); } } catch (e) { this.#error = e; @@ -391,13 +394,13 @@ class SourceTextModule extends Module { class SyntheticModule extends Module { constructor(exportNames, evaluateCallback, options = {}) { if (!ArrayIsArray(exportNames) || - exportNames.some((e) => typeof e !== 'string')) { + ArrayPrototypeSome(exportNames, (e) => typeof e !== 'string')) { throw new ERR_INVALID_ARG_TYPE('exportNames', 'Array of unique strings', exportNames); } else { - exportNames.forEach((name, i) => { - if (exportNames.indexOf(name, i + 1) !== -1) { + ArrayPrototypeForEach(exportNames, (name, i) => { + if (ArrayPrototypeIndexOf(exportNames, name, i + 1) !== -1) { throw new ERR_INVALID_ARG_VALUE(`exportNames.${name}`, name, 'is duplicated'); diff --git a/lib/vm.js b/lib/vm.js index 45a2edf0bb20b3..33893845084141 100644 --- a/lib/vm.js +++ b/lib/vm.js @@ -24,7 +24,8 @@ const { ArrayPrototypeForEach, Symbol, - PromiseReject + PromiseReject, + ReflectApply, } = primordials; const { @@ -269,7 +270,7 @@ function sigintHandlersWrap(fn, thisArg, argsArray) { process.removeAllListeners('SIGINT'); try { - return fn.apply(thisArg, argsArray); + return ReflectApply(fn, thisArg, argsArray); } finally { // Add using the public methods so that the `newListener` handler of // process can re-attach the listeners.