Skip to content

Commit 0d71947

Browse files
aduh95ruyadorno
authored andcommitted
vm: refactor to avoid unsafe array iteration
PR-URL: #36752 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent bf695eb commit 0d71947

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

lib/internal/vm/module.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const {
1111
ObjectGetPrototypeOf,
1212
ObjectSetPrototypeOf,
1313
PromiseAll,
14+
ReflectApply,
1415
SafeWeakMap,
1516
Symbol,
1617
SymbolToStringTag,
@@ -445,7 +446,7 @@ class SyntheticModule extends Module {
445446

446447
function importModuleDynamicallyWrap(importModuleDynamically) {
447448
const importModuleDynamicallyWrapper = async (...args) => {
448-
const m = await importModuleDynamically(...args);
449+
const m = await ReflectApply(importModuleDynamically, this, args);
449450
if (isModuleNamespaceObject(m)) {
450451
return m;
451452
}

lib/vm.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
const {
2525
ArrayPrototypeForEach,
26+
ArrayPrototypeUnshift,
2627
Symbol,
2728
PromiseReject,
2829
ReflectApply,
@@ -130,17 +131,17 @@ class Script extends ContextifyScript {
130131
if (breakOnSigint && process.listenerCount('SIGINT') > 0) {
131132
return sigintHandlersWrap(super.runInThisContext, this, args);
132133
}
133-
return super.runInThisContext(...args);
134+
return ReflectApply(super.runInThisContext, this, args);
134135
}
135136

136137
runInContext(contextifiedObject, options) {
137138
validateContext(contextifiedObject);
138139
const { breakOnSigint, args } = getRunInContextArgs(options);
140+
ArrayPrototypeUnshift(args, contextifiedObject);
139141
if (breakOnSigint && process.listenerCount('SIGINT') > 0) {
140-
return sigintHandlersWrap(super.runInContext, this,
141-
[contextifiedObject, ...args]);
142+
return sigintHandlersWrap(super.runInContext, this, args);
142143
}
143-
return super.runInContext(contextifiedObject, ...args);
144+
return ReflectApply(super.runInContext, this, args);
144145
}
145146

146147
runInNewContext(contextObject, options) {
@@ -274,9 +275,9 @@ function sigintHandlersWrap(fn, thisArg, argsArray) {
274275
} finally {
275276
// Add using the public methods so that the `newListener` handler of
276277
// process can re-attach the listeners.
277-
for (const listener of sigintListeners) {
278+
ArrayPrototypeForEach(sigintListeners, (listener) => {
278279
process.addListener('SIGINT', listener);
279-
}
280+
});
280281
}
281282
}
282283

0 commit comments

Comments
 (0)