Skip to content

Commit

Permalink
vm: throw error when duplicated exportNames in SyntheticModule
Browse files Browse the repository at this point in the history
Fixes: #32806

PR-URL: #32810
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
himself65 authored and targos committed May 13, 2020
1 parent a1fa180 commit 6a9f867
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
12 changes: 11 additions & 1 deletion lib/internal/vm/module.js
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
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',
});
}

// Check to throws invalid evaluateCallback
{
assert.throws(() => new SyntheticModule([], undefined, {}), {
Expand Down

0 comments on commit 6a9f867

Please sign in to comment.