Skip to content

Commit

Permalink
lib: add validation for options in compileFunction
Browse files Browse the repository at this point in the history
PR-URL: #56023
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
  • Loading branch information
kimtaejin3 authored and aduh95 committed Dec 18, 2024
1 parent 9852480 commit 31e5037
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
assert.throws(() => {
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 31e5037

Please sign in to comment.