@@ -33,7 +33,11 @@ const {
33
33
ERR_VM_MODULE_NOT_MODULE ,
34
34
} = require ( 'internal/errors' ) . codes ;
35
35
const { isModuleNamespaceObject, isArrayBufferView } = require ( 'util' ) . types ;
36
- const { validateInt32, validateUint32 } = require ( 'internal/validators' ) ;
36
+ const {
37
+ validateInt32,
38
+ validateUint32,
39
+ validateString
40
+ } = require ( 'internal/validators' ) ;
37
41
const kParsingContext = Symbol ( 'script parsing context' ) ;
38
42
39
43
const ArrayForEach = Function . call . bind ( Array . prototype . forEach ) ;
@@ -58,9 +62,7 @@ class Script extends ContextifyScript {
58
62
[ kParsingContext ] : parsingContext ,
59
63
} = options ;
60
64
61
- if ( typeof filename !== 'string' ) {
62
- throw new ERR_INVALID_ARG_TYPE ( 'options.filename' , 'string' , filename ) ;
63
- }
65
+ validateString ( filename , 'options.filename' ) ;
64
66
validateInt32 ( lineOffset , 'options.lineOffset' ) ;
65
67
validateInt32 ( columnOffset , 'options.columnOffset' ) ;
66
68
if ( cachedData !== undefined && ! isArrayBufferView ( cachedData ) ) {
@@ -149,11 +151,6 @@ function validateContext(sandbox) {
149
151
}
150
152
}
151
153
152
- function validateString ( prop , propName ) {
153
- if ( prop !== undefined && typeof prop !== 'string' )
154
- throw new ERR_INVALID_ARG_TYPE ( propName , 'string' , prop ) ;
155
- }
156
-
157
154
function validateBool ( prop , propName ) {
158
155
if ( prop !== undefined && typeof prop !== 'boolean' )
159
156
throw new ERR_INVALID_ARG_TYPE ( propName , 'boolean' , prop ) ;
@@ -208,8 +205,10 @@ function getContextOptions(options) {
208
205
wasm : options . contextCodeGeneration . wasm ,
209
206
} : undefined ,
210
207
} ;
211
- validateString ( contextOptions . name , 'options.contextName' ) ;
212
- validateString ( contextOptions . origin , 'options.contextOrigin' ) ;
208
+ if ( contextOptions . name !== undefined )
209
+ validateString ( contextOptions . name , 'options.contextName' ) ;
210
+ if ( contextOptions . origin !== undefined )
211
+ validateString ( contextOptions . origin , 'options.contextOrigin' ) ;
213
212
if ( contextOptions . codeGeneration ) {
214
213
validateBool ( contextOptions . codeGeneration . strings ,
215
214
'options.contextCodeGeneration.strings' ) ;
@@ -244,10 +243,9 @@ function createContext(sandbox = {}, options = {}) {
244
243
codeGeneration
245
244
} = options ;
246
245
247
- if ( typeof name !== 'string' ) {
248
- throw new ERR_INVALID_ARG_TYPE ( 'options.name' , 'string' , options . name ) ;
249
- }
250
- validateString ( origin , 'options.origin' ) ;
246
+ validateString ( name , 'options.name' ) ;
247
+ if ( origin !== undefined )
248
+ validateString ( origin , 'options.origin' ) ;
251
249
validateObject ( codeGeneration , 'options.codeGeneration' ) ;
252
250
253
251
let strings = true ;
@@ -319,18 +317,12 @@ function runInThisContext(code, options) {
319
317
}
320
318
321
319
function compileFunction ( code , params , options = { } ) {
322
- if ( typeof code !== 'string' ) {
323
- throw new ERR_INVALID_ARG_TYPE ( 'code' , 'string' , code ) ;
324
- }
320
+ validateString ( code , 'code' ) ;
325
321
if ( params !== undefined ) {
326
322
if ( ! ArrayIsArray ( params ) ) {
327
323
throw new ERR_INVALID_ARG_TYPE ( 'params' , 'Array' , params ) ;
328
324
}
329
- ArrayForEach ( params , ( param , i ) => {
330
- if ( typeof param !== 'string' ) {
331
- throw new ERR_INVALID_ARG_TYPE ( `params[${ i } ]` , 'string' , param ) ;
332
- }
333
- } ) ;
325
+ ArrayForEach ( params , ( param , i ) => validateString ( param , `params[${ i } ]` ) ) ;
334
326
}
335
327
336
328
const {
@@ -343,9 +335,7 @@ function compileFunction(code, params, options = {}) {
343
335
contextExtensions = [ ] ,
344
336
} = options ;
345
337
346
- if ( typeof filename !== 'string' ) {
347
- throw new ERR_INVALID_ARG_TYPE ( 'options.filename' , 'string' , filename ) ;
348
- }
338
+ validateString ( filename , 'options.filename' ) ;
349
339
validateUint32 ( columnOffset , 'options.columnOffset' ) ;
350
340
validateUint32 ( lineOffset , 'options.lineOffset' ) ;
351
341
if ( cachedData !== undefined && ! isArrayBufferView ( cachedData ) ) {
0 commit comments