From 3aee77d279123eebf21fa737abc3b2d7b253297b Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Thu, 31 Dec 2020 13:12:33 +0100 Subject: [PATCH] vm: refactor to avoid unsafe array iteration PR-URL: https://github.com/nodejs/node/pull/36752 Reviewed-By: Rich Trott Reviewed-By: James M Snell --- lib/internal/vm/module.js | 3 ++- lib/vm.js | 13 +++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/internal/vm/module.js b/lib/internal/vm/module.js index 12a7f8af8554b0..6bf73291be00c7 100644 --- a/lib/internal/vm/module.js +++ b/lib/internal/vm/module.js @@ -11,6 +11,7 @@ const { ObjectGetPrototypeOf, ObjectSetPrototypeOf, PromiseAll, + ReflectApply, SafeWeakMap, Symbol, SymbolToStringTag, @@ -447,7 +448,7 @@ class SyntheticModule extends Module { function importModuleDynamicallyWrap(importModuleDynamically) { const importModuleDynamicallyWrapper = async (...args) => { - const m = await importModuleDynamically(...args); + const m = await ReflectApply(importModuleDynamically, this, args); if (isModuleNamespaceObject(m)) { return m; } diff --git a/lib/vm.js b/lib/vm.js index 33893845084141..79c97f3af3ff02 100644 --- a/lib/vm.js +++ b/lib/vm.js @@ -23,6 +23,7 @@ const { ArrayPrototypeForEach, + ArrayPrototypeUnshift, Symbol, PromiseReject, ReflectApply, @@ -130,17 +131,17 @@ class Script extends ContextifyScript { if (breakOnSigint && process.listenerCount('SIGINT') > 0) { return sigintHandlersWrap(super.runInThisContext, this, args); } - return super.runInThisContext(...args); + return ReflectApply(super.runInThisContext, this, args); } runInContext(contextifiedObject, options) { validateContext(contextifiedObject); const { breakOnSigint, args } = getRunInContextArgs(options); + ArrayPrototypeUnshift(args, contextifiedObject); if (breakOnSigint && process.listenerCount('SIGINT') > 0) { - return sigintHandlersWrap(super.runInContext, this, - [contextifiedObject, ...args]); + return sigintHandlersWrap(super.runInContext, this, args); } - return super.runInContext(contextifiedObject, ...args); + return ReflectApply(super.runInContext, this, args); } runInNewContext(contextObject, options) { @@ -274,9 +275,9 @@ function sigintHandlersWrap(fn, thisArg, argsArray) { } finally { // Add using the public methods so that the `newListener` handler of // process can re-attach the listeners. - for (const listener of sigintListeners) { + ArrayPrototypeForEach(sigintListeners, (listener) => { process.addListener('SIGINT', listener); - } + }); } }