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 4cdcf53
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
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
30 changes: 30 additions & 0 deletions test/parallel/test-vm-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,36 @@ const vm = require('vm');
'Received 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)'
});

// Test for valid options type
assert.doesNotThrow(() => {

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

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Do not use `assert.doesNotThrow()`. Write the code without the wrapper and add a comment instead
vm.compileFunction('', [], {});
});

// vm.compileFunction('', undefined, null);

const optionTypes = {
Expand Down

0 comments on commit 4cdcf53

Please sign in to comment.