diff --git a/lib/vm.js b/lib/vm.js index f7444764ed5c3f..e50b08d336b9b5 100644 --- a/lib/vm.js +++ b/lib/vm.js @@ -33,7 +33,11 @@ const { ERR_VM_MODULE_NOT_MODULE, } = require('internal/errors').codes; const { isModuleNamespaceObject, isArrayBufferView } = require('util').types; -const { validateInt32, validateUint32 } = require('internal/validators'); +const { + validateInt32, + validateUint32, + validateString +} = require('internal/validators'); const kParsingContext = Symbol('script parsing context'); const ArrayForEach = Function.call.bind(Array.prototype.forEach); @@ -58,9 +62,7 @@ class Script extends ContextifyScript { [kParsingContext]: parsingContext, } = options; - if (typeof filename !== 'string') { - throw new ERR_INVALID_ARG_TYPE('options.filename', 'string', filename); - } + validateString(filename, 'options.filename'); validateInt32(lineOffset, 'options.lineOffset'); validateInt32(columnOffset, 'options.columnOffset'); if (cachedData !== undefined && !isArrayBufferView(cachedData)) { @@ -149,11 +151,6 @@ function validateContext(sandbox) { } } -function validateString(prop, propName) { - if (prop !== undefined && typeof prop !== 'string') - throw new ERR_INVALID_ARG_TYPE(propName, 'string', prop); -} - function validateBool(prop, propName) { if (prop !== undefined && typeof prop !== 'boolean') throw new ERR_INVALID_ARG_TYPE(propName, 'boolean', prop); @@ -208,8 +205,10 @@ function getContextOptions(options) { wasm: options.contextCodeGeneration.wasm, } : undefined, }; - validateString(contextOptions.name, 'options.contextName'); - validateString(contextOptions.origin, 'options.contextOrigin'); + if (contextOptions.name !== undefined) + validateString(contextOptions.name, 'options.contextName'); + if (contextOptions.origin !== undefined) + validateString(contextOptions.origin, 'options.contextOrigin'); if (contextOptions.codeGeneration) { validateBool(contextOptions.codeGeneration.strings, 'options.contextCodeGeneration.strings'); @@ -244,10 +243,9 @@ function createContext(sandbox = {}, options = {}) { codeGeneration } = options; - if (typeof name !== 'string') { - throw new ERR_INVALID_ARG_TYPE('options.name', 'string', options.name); - } - validateString(origin, 'options.origin'); + validateString(name, 'options.name'); + if (origin !== undefined) + validateString(origin, 'options.origin'); validateObject(codeGeneration, 'options.codeGeneration'); let strings = true; @@ -319,18 +317,12 @@ function runInThisContext(code, options) { } function compileFunction(code, params, options = {}) { - if (typeof code !== 'string') { - throw new ERR_INVALID_ARG_TYPE('code', 'string', code); - } + validateString(code, 'code'); if (params !== undefined) { if (!ArrayIsArray(params)) { throw new ERR_INVALID_ARG_TYPE('params', 'Array', params); } - ArrayForEach(params, (param, i) => { - if (typeof param !== 'string') { - throw new ERR_INVALID_ARG_TYPE(`params[${i}]`, 'string', param); - } - }); + ArrayForEach(params, (param, i) => validateString(param, `params[${i}]`)); } const { @@ -343,9 +335,7 @@ function compileFunction(code, params, options = {}) { contextExtensions = [], } = options; - if (typeof filename !== 'string') { - throw new ERR_INVALID_ARG_TYPE('options.filename', 'string', filename); - } + validateString(filename, 'options.filename'); validateUint32(columnOffset, 'options.columnOffset'); validateUint32(lineOffset, 'options.lineOffset'); if (cachedData !== undefined && !isArrayBufferView(cachedData)) {