Skip to content

Commit 4cdcf53

Browse files
committed
lib: add validation for options in compileFunction
1 parent db8ff56 commit 4cdcf53

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

lib/vm.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ function runInThisContext(code, options) {
319319

320320
function compileFunction(code, params, options = kEmptyObject) {
321321
validateString(code, 'code');
322+
validateObject(options, 'options');
322323
if (params !== undefined) {
323324
validateStringArray(params, 'params');
324325
}

test/parallel/test-vm-basic.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,36 @@ const vm = require('vm');
172172
'Received null'
173173
});
174174

175+
// Test for invalid options type
176+
assert.throws(() => {
177+
vm.compileFunction('', [], null);
178+
}, {
179+
name: 'TypeError',
180+
code: 'ERR_INVALID_ARG_TYPE',
181+
message: 'The "options" argument must be of type object. Received null'
182+
});
183+
184+
assert.throws(() => {
185+
vm.compileFunction('', [], 'string');
186+
}, {
187+
name: 'TypeError',
188+
code: 'ERR_INVALID_ARG_TYPE',
189+
message: 'The "options" argument must be of type object. Received type string (\'string\')'
190+
});
191+
192+
assert.throws(() => {
193+
vm.compileFunction('', [], 123);
194+
}, {
195+
name: 'TypeError',
196+
code: 'ERR_INVALID_ARG_TYPE',
197+
message: 'The "options" argument must be of type object. Received type number (123)'
198+
});
199+
200+
// Test for valid options type
201+
assert.doesNotThrow(() => {
202+
vm.compileFunction('', [], {});
203+
});
204+
175205
// vm.compileFunction('', undefined, null);
176206

177207
const optionTypes = {

0 commit comments

Comments
 (0)