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: throw error when duplicated exportNames in SyntheticModule #32810

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/internal/vm/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const {
} = require('internal/util');
const {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_VM_MODULE_ALREADY_LINKED,
ERR_VM_MODULE_DIFFERENT_CONTEXT,
ERR_VM_MODULE_CANNOT_CREATE_CACHED_DATA,
Expand Down Expand Up @@ -379,8 +380,17 @@ class SyntheticModule extends Module {
constructor(exportNames, evaluateCallback, options = {}) {
if (!ArrayIsArray(exportNames) ||
exportNames.some((e) => typeof e !== 'string')) {
throw new ERR_INVALID_ARG_TYPE('exportNames', 'Array of strings',
throw new ERR_INVALID_ARG_TYPE('exportNames',
'Array of unique strings',
exportNames);
} else {
exportNames.forEach((name, i) => {
if (exportNames.indexOf(name, i + 1) !== -1) {
throw new ERR_INVALID_ARG_VALUE(`exportNames.${name}`,
name,
'is duplicated');
}
});
}
if (typeof evaluateCallback !== 'function') {
throw new ERR_INVALID_ARG_TYPE('evaluateCallback', 'function',
Expand Down
14 changes: 12 additions & 2 deletions test/parallel/test-vm-module-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,22 @@ const util = require('util');
// Check to throws invalid exportNames
{
assert.throws(() => new SyntheticModule(undefined, () => {}, {}), {
message: 'The "exportNames" argument must be an Array of strings.' +
' Received undefined',
message: 'The "exportNames" argument must be an ' +
'Array of unique strings.' +
' Received undefined',
name: 'TypeError'
});
}

// Check to throws duplicated exportNames
// https://github.com/nodejs/node/issues/32806
{
assert.throws(() => new SyntheticModule(['x', 'x'], () => {}, {}), {
message: 'The argument \'exportNames.x\' is duplicated. Received \'x\'',
name: 'TypeError',
});
}

himself65 marked this conversation as resolved.
Show resolved Hide resolved
// Check to throws invalid evaluateCallback
{
assert.throws(() => new SyntheticModule([], undefined, {}), {
Expand Down