22
22
'use strict' ;
23
23
24
24
const {
25
- ArrayIsArray,
26
25
ArrayPrototypeForEach,
27
26
Symbol,
28
27
} = primordials ;
@@ -42,7 +41,11 @@ const {
42
41
const {
43
42
validateInt32,
44
43
validateUint32,
45
- validateString
44
+ validateString,
45
+ validateArray,
46
+ validateBoolean,
47
+ validateBuffer,
48
+ validateObject,
46
49
} = require ( 'internal/validators' ) ;
47
50
const { kVmBreakFirstLineSymbol } = require ( 'internal/util' ) ;
48
51
const kParsingContext = Symbol ( 'script parsing context' ) ;
@@ -139,30 +142,14 @@ class Script extends ContextifyScript {
139
142
}
140
143
141
144
function validateContext ( contextifiedObject ) {
142
- if ( typeof contextifiedObject !== 'object' || contextifiedObject === null ) {
143
- throw new ERR_INVALID_ARG_TYPE ( 'contextifiedObject' , 'Object' ,
144
- contextifiedObject ) ;
145
- }
146
- if ( ! _isContext ( contextifiedObject ) ) {
145
+ if ( ! isContext ( contextifiedObject ) ) {
147
146
throw new ERR_INVALID_ARG_TYPE ( 'contextifiedObject' , 'vm.Context' ,
148
147
contextifiedObject ) ;
149
148
}
150
149
}
151
150
152
- function validateBool ( prop , propName ) {
153
- if ( prop !== undefined && typeof prop !== 'boolean' )
154
- throw new ERR_INVALID_ARG_TYPE ( propName , 'boolean' , prop ) ;
155
- }
156
-
157
- function validateObject ( prop , propName ) {
158
- if ( prop !== undefined && ( typeof prop !== 'object' || prop === null ) )
159
- throw new ERR_INVALID_ARG_TYPE ( propName , 'Object' , prop ) ;
160
- }
161
-
162
151
function getRunInContextArgs ( options = { } ) {
163
- if ( typeof options !== 'object' || options === null ) {
164
- throw new ERR_INVALID_ARG_TYPE ( 'options' , 'Object' , options ) ;
165
- }
152
+ validateObject ( options , 'options' ) ;
166
153
167
154
let timeout = options . timeout ;
168
155
if ( timeout === undefined ) {
@@ -177,14 +164,8 @@ function getRunInContextArgs(options = {}) {
177
164
[ kVmBreakFirstLineSymbol ] : breakFirstLine = false ,
178
165
} = options ;
179
166
180
- if ( typeof displayErrors !== 'boolean' ) {
181
- throw new ERR_INVALID_ARG_TYPE ( 'options.displayErrors' , 'boolean' ,
182
- displayErrors ) ;
183
- }
184
- if ( typeof breakOnSigint !== 'boolean' ) {
185
- throw new ERR_INVALID_ARG_TYPE ( 'options.breakOnSigint' , 'boolean' ,
186
- breakOnSigint ) ;
187
- }
167
+ validateBoolean ( displayErrors , 'options.displayErrors' ) ;
168
+ validateBoolean ( breakOnSigint , 'options.breakOnSigint' ) ;
188
169
189
170
return {
190
171
breakOnSigint,
@@ -193,30 +174,28 @@ function getRunInContextArgs(options = {}) {
193
174
}
194
175
195
176
function getContextOptions ( options ) {
196
- if ( options ) {
177
+ if ( ! options )
178
+ return { } ;
179
+ const contextOptions = {
180
+ name : options . contextName ,
181
+ origin : options . contextOrigin ,
182
+ codeGeneration : undefined ,
183
+ } ;
184
+ if ( contextOptions . name !== undefined )
185
+ validateString ( contextOptions . name , 'options.contextName' ) ;
186
+ if ( contextOptions . origin !== undefined )
187
+ validateString ( contextOptions . origin , 'options.contextOrigin' ) ;
188
+ if ( options . contextCodeGeneration !== undefined ) {
197
189
validateObject ( options . contextCodeGeneration ,
198
190
'options.contextCodeGeneration' ) ;
199
- const contextOptions = {
200
- name : options . contextName ,
201
- origin : options . contextOrigin ,
202
- codeGeneration : typeof options . contextCodeGeneration === 'object' ? {
203
- strings : options . contextCodeGeneration . strings ,
204
- wasm : options . contextCodeGeneration . wasm ,
205
- } : undefined ,
206
- } ;
207
- if ( contextOptions . name !== undefined )
208
- validateString ( contextOptions . name , 'options.contextName' ) ;
209
- if ( contextOptions . origin !== undefined )
210
- validateString ( contextOptions . origin , 'options.contextOrigin' ) ;
211
- if ( contextOptions . codeGeneration ) {
212
- validateBool ( contextOptions . codeGeneration . strings ,
213
- 'options.contextCodeGeneration.strings' ) ;
214
- validateBool ( contextOptions . codeGeneration . wasm ,
215
- 'options.contextCodeGeneration.wasm' ) ;
216
- }
217
- return contextOptions ;
191
+ const { strings, wasm } = options . contextCodeGeneration ;
192
+ if ( strings !== undefined )
193
+ validateBoolean ( strings , 'options.contextCodeGeneration.strings' ) ;
194
+ if ( wasm !== undefined )
195
+ validateBoolean ( wasm , 'options.contextCodeGeneration.wasm' ) ;
196
+ contextOptions . codeGeneration = { strings, wasm } ;
218
197
}
219
- return { } ;
198
+ return contextOptions ;
220
199
}
221
200
222
201
function isContext ( object ) {
@@ -232,9 +211,7 @@ function createContext(contextObject = {}, options = {}) {
232
211
return contextObject ;
233
212
}
234
213
235
- if ( typeof options !== 'object' || options === null ) {
236
- throw new ERR_INVALID_ARG_TYPE ( 'options' , 'Object' , options ) ;
237
- }
214
+ validateObject ( options , 'options' ) ;
238
215
239
216
const {
240
217
name = `VM Context ${ defaultContextNameIndex ++ } ` ,
@@ -245,14 +222,15 @@ function createContext(contextObject = {}, options = {}) {
245
222
validateString ( name , 'options.name' ) ;
246
223
if ( origin !== undefined )
247
224
validateString ( origin , 'options.origin' ) ;
248
- validateObject ( codeGeneration , 'options.codeGeneration' ) ;
225
+ if ( codeGeneration !== undefined )
226
+ validateObject ( codeGeneration , 'options.codeGeneration' ) ;
249
227
250
228
let strings = true ;
251
229
let wasm = true ;
252
230
if ( codeGeneration !== undefined ) {
253
231
( { strings = true , wasm = true } = codeGeneration ) ;
254
- validateBool ( strings , 'options.codeGeneration.strings' ) ;
255
- validateBool ( wasm , 'options.codeGeneration.wasm' ) ;
232
+ validateBoolean ( strings , 'options.codeGeneration.strings' ) ;
233
+ validateBoolean ( wasm , 'options.codeGeneration.wasm' ) ;
256
234
}
257
235
258
236
makeContext ( contextObject , name , origin , strings , wasm ) ;
@@ -314,9 +292,7 @@ function runInThisContext(code, options) {
314
292
function compileFunction ( code , params , options = { } ) {
315
293
validateString ( code , 'code' ) ;
316
294
if ( params !== undefined ) {
317
- if ( ! ArrayIsArray ( params ) ) {
318
- throw new ERR_INVALID_ARG_TYPE ( 'params' , 'Array' , params ) ;
319
- }
295
+ validateArray ( params , 'params' ) ;
320
296
ArrayPrototypeForEach ( params ,
321
297
( param , i ) => validateString ( param , `params[${ i } ]` ) ) ;
322
298
}
@@ -334,20 +310,9 @@ function compileFunction(code, params, options = {}) {
334
310
validateString ( filename , 'options.filename' ) ;
335
311
validateUint32 ( columnOffset , 'options.columnOffset' ) ;
336
312
validateUint32 ( lineOffset , 'options.lineOffset' ) ;
337
- if ( cachedData !== undefined && ! isArrayBufferView ( cachedData ) ) {
338
- throw new ERR_INVALID_ARG_TYPE (
339
- 'options.cachedData' ,
340
- [ 'Buffer' , 'TypedArray' , 'DataView' ] ,
341
- cachedData
342
- ) ;
343
- }
344
- if ( typeof produceCachedData !== 'boolean' ) {
345
- throw new ERR_INVALID_ARG_TYPE (
346
- 'options.produceCachedData' ,
347
- 'boolean' ,
348
- produceCachedData
349
- ) ;
350
- }
313
+ if ( cachedData !== undefined )
314
+ validateBuffer ( cachedData , 'options.cachedData' ) ;
315
+ validateBoolean ( produceCachedData , 'options.produceCachedData' ) ;
351
316
if ( parsingContext !== undefined ) {
352
317
if (
353
318
typeof parsingContext !== 'object' ||
@@ -361,21 +326,10 @@ function compileFunction(code, params, options = {}) {
361
326
) ;
362
327
}
363
328
}
364
- if ( ! ArrayIsArray ( contextExtensions ) ) {
365
- throw new ERR_INVALID_ARG_TYPE (
366
- 'options.contextExtensions' ,
367
- 'Array' ,
368
- contextExtensions
369
- ) ;
370
- }
329
+ validateArray ( contextExtensions , 'options.contextExtensions' ) ;
371
330
ArrayPrototypeForEach ( contextExtensions , ( extension , i ) => {
372
- if ( typeof extension !== 'object' ) {
373
- throw new ERR_INVALID_ARG_TYPE (
374
- `options.contextExtensions[${ i } ]` ,
375
- 'object' ,
376
- extension
377
- ) ;
378
- }
331
+ const name = `options.contextExtensions[${ i } ]` ;
332
+ validateObject ( extension , name , { nullable : true } ) ;
379
333
} ) ;
380
334
381
335
const result = _compileFunction (
0 commit comments