Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vm: refactor to avoid unsafe array iteration #36752

Merged
merged 1 commit into from Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/internal/vm/module.js
Expand Up @@ -11,6 +11,7 @@ const {
ObjectGetPrototypeOf,
ObjectSetPrototypeOf,
PromiseAll,
ReflectApply,
SafeWeakMap,
Symbol,
SymbolToStringTag,
Expand Down Expand Up @@ -445,7 +446,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
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