1'use strict'; 2 3require('../common'); 4const assert = require('assert'); 5const vm = require('vm'); 6 7const invalidArgType = { 8 name: 'TypeError', 9 code: 'ERR_INVALID_ARG_TYPE' 10}; 11 12const outOfRange = { 13 name: 'RangeError', 14 code: 'ERR_OUT_OF_RANGE' 15}; 16 17assert.throws(() => { 18 new vm.Script('void 0', 42); 19}, invalidArgType); 20 21[null, {}, [1], 'bad', true].forEach((value) => { 22 assert.throws(() => { 23 new vm.Script('void 0', { lineOffset: value }); 24 }, invalidArgType); 25 26 assert.throws(() => { 27 new vm.Script('void 0', { columnOffset: value }); 28 }, invalidArgType); 29}); 30 31[0.1, 2 ** 32].forEach((value) => { 32 assert.throws(() => { 33 new vm.Script('void 0', { lineOffset: value }); 34 }, outOfRange); 35 36 assert.throws(() => { 37 new vm.Script('void 0', { columnOffset: value }); 38 }, outOfRange); 39}); 40 41assert.throws(() => { 42 new vm.Script('void 0', { lineOffset: Number.MAX_SAFE_INTEGER }); 43}, outOfRange); 44 45assert.throws(() => { 46 new vm.Script('void 0', { columnOffset: Number.MAX_SAFE_INTEGER }); 47}, outOfRange); 48 49assert.throws(() => { 50 new vm.Script('void 0', { filename: 123 }); 51}, invalidArgType); 52 53assert.throws(() => { 54 new vm.Script('void 0', { produceCachedData: 1 }); 55}, invalidArgType); 56 57[[0], {}, true, 'bad', 42].forEach((value) => { 58 assert.throws(() => { 59 new vm.Script('void 0', { cachedData: value }); 60 }, invalidArgType); 61}); 62 63{ 64 const script = new vm.Script('void 0'); 65 const sandbox = vm.createContext(); 66 67 function assertErrors(options, errCheck) { 68 assert.throws(() => { 69 script.runInThisContext(options); 70 }, errCheck); 71 72 assert.throws(() => { 73 script.runInContext(sandbox, options); 74 }, errCheck); 75 76 assert.throws(() => { 77 script.runInNewContext({}, options); 78 }, errCheck); 79 } 80 81 [null, 'bad', 42].forEach((value) => { 82 assertErrors(value, invalidArgType); 83 }); 84 [{}, [1], 'bad', null].forEach((value) => { 85 assertErrors({ timeout: value }, invalidArgType); 86 }); 87 [-1, 0, NaN].forEach((value) => { 88 assertErrors({ timeout: value }, outOfRange); 89 }); 90 [{}, [1], 'bad', 1, null].forEach((value) => { 91 assertErrors({ displayErrors: value }, invalidArgType); 92 assertErrors({ breakOnSigint: value }, invalidArgType); 93 }); 94} 95