Skip to content

Commit

Permalink
lib: add validation for options in compileFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
kimtaejin3 committed Dec 4, 2024
1 parent db8ff56 commit bda151e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ function runInThisContext(code, options) {

function compileFunction(code, params, options = kEmptyObject) {
validateString(code, 'code');
validateObject(options, 'options');
if (params !== undefined) {
validateStringArray(params, 'params');
}
Expand Down
25 changes: 24 additions & 1 deletion test/parallel/test-vm-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,30 @@ const vm = require('vm');
'Received null'
});

// vm.compileFunction('', undefined, null);
// Test for invalid options type

Check failure on line 175 in test/parallel/test-vm-basic.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected indentation of 2 spaces but found 3
assert.throws(() => {

Check failure on line 176 in test/parallel/test-vm-basic.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected indentation of 2 spaces but found 3
vm.compileFunction('', [], null);
}, {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options" argument must be of type object. Received null'
});

assert.throws(() => {
vm.compileFunction('', [], 'string');
}, {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options" argument must be of type object. Received type string (\'string\')'
});

assert.throws(() => {
vm.compileFunction('', [], 123);
}, {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options" argument must be of type object. Received type number (123)'
});

const optionTypes = {
'filename': 'string',
Expand Down

0 comments on commit bda151e

Please sign in to comment.