Skip to content

Commit

Permalink
vm: refactor to avoid unsafe array iteration
Browse files Browse the repository at this point in the history
PR-URL: #36752
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
aduh95 authored and targos committed Jun 5, 2021
1 parent 8cbd4cf commit 79bf95a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
3 changes: 2 additions & 1 deletion lib/internal/vm/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
ObjectGetPrototypeOf,
ObjectSetPrototypeOf,
PromiseAll,
ReflectApply,
SafeWeakMap,
Symbol,
SymbolToStringTag,
Expand Down Expand Up @@ -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;
}
Expand Down
13 changes: 7 additions & 6 deletions lib/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

const {
ArrayPrototypeForEach,
ArrayPrototypeUnshift,
Symbol,
PromiseReject,
ReflectApply,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
});
}
}

Expand Down

0 comments on commit 79bf95a

Please sign in to comment.