22
22
'use strict' ;
23
23
24
24
const {
25
- ArrayIsArray,
26
25
ArrayPrototypeForEach,
27
26
Symbol,
28
27
PromiseReject
@@ -47,7 +46,11 @@ const {
47
46
const {
48
47
validateInt32,
49
48
validateUint32,
50
- validateString
49
+ validateString,
50
+ validateArray,
51
+ validateBoolean,
52
+ validateBuffer,
53
+ validateObject,
51
54
} = require ( 'internal/validators' ) ;
52
55
const {
53
56
kVmBreakFirstLineSymbol,
@@ -147,30 +150,14 @@ class Script extends ContextifyScript {
147
150
}
148
151
149
152
function validateContext ( contextifiedObject ) {
150
- if ( typeof contextifiedObject !== 'object' || contextifiedObject === null ) {
151
- throw new ERR_INVALID_ARG_TYPE ( 'contextifiedObject' , 'Object' ,
152
- contextifiedObject ) ;
153
- }
154
- if ( ! _isContext ( contextifiedObject ) ) {
153
+ if ( ! isContext ( contextifiedObject ) ) {
155
154
throw new ERR_INVALID_ARG_TYPE ( 'contextifiedObject' , 'vm.Context' ,
156
155
contextifiedObject ) ;
157
156
}
158
157
}
159
158
160
- function validateBool ( prop , propName ) {
161
- if ( prop !== undefined && typeof prop !== 'boolean' )
162
- throw new ERR_INVALID_ARG_TYPE ( propName , 'boolean' , prop ) ;
163
- }
164
-
165
- function validateObject ( prop , propName ) {
166
- if ( prop !== undefined && ( typeof prop !== 'object' || prop === null ) )
167
- throw new ERR_INVALID_ARG_TYPE ( propName , 'Object' , prop ) ;
168
- }
169
-
170
159
function getRunInContextArgs ( options = { } ) {
171
- if ( typeof options !== 'object' || options === null ) {
172
- throw new ERR_INVALID_ARG_TYPE ( 'options' , 'Object' , options ) ;
173
- }
160
+ validateObject ( options , 'options' ) ;
174
161
175
162
let timeout = options . timeout ;
176
163
if ( timeout === undefined ) {
@@ -185,14 +172,8 @@ function getRunInContextArgs(options = {}) {
185
172
[ kVmBreakFirstLineSymbol ] : breakFirstLine = false ,
186
173
} = options ;
187
174
188
- if ( typeof displayErrors !== 'boolean' ) {
189
- throw new ERR_INVALID_ARG_TYPE ( 'options.displayErrors' , 'boolean' ,
190
- displayErrors ) ;
191
- }
192
- if ( typeof breakOnSigint !== 'boolean' ) {
193
- throw new ERR_INVALID_ARG_TYPE ( 'options.breakOnSigint' , 'boolean' ,
194
- breakOnSigint ) ;
195
- }
175
+ validateBoolean ( displayErrors , 'options.displayErrors' ) ;
176
+ validateBoolean ( breakOnSigint , 'options.breakOnSigint' ) ;
196
177
197
178
return {
198
179
breakOnSigint,
@@ -201,30 +182,28 @@ function getRunInContextArgs(options = {}) {
201
182
}
202
183
203
184
function getContextOptions ( options ) {
204
- if ( options ) {
185
+ if ( ! options )
186
+ return { } ;
187
+ const contextOptions = {
188
+ name : options . contextName ,
189
+ origin : options . contextOrigin ,
190
+ codeGeneration : undefined ,
191
+ } ;
192
+ if ( contextOptions . name !== undefined )
193
+ validateString ( contextOptions . name , 'options.contextName' ) ;
194
+ if ( contextOptions . origin !== undefined )
195
+ validateString ( contextOptions . origin , 'options.contextOrigin' ) ;
196
+ if ( options . contextCodeGeneration !== undefined ) {
205
197
validateObject ( options . contextCodeGeneration ,
206
198
'options.contextCodeGeneration' ) ;
207
- const contextOptions = {
208
- name : options . contextName ,
209
- origin : options . contextOrigin ,
210
- codeGeneration : typeof options . contextCodeGeneration === 'object' ? {
211
- strings : options . contextCodeGeneration . strings ,
212
- wasm : options . contextCodeGeneration . wasm ,
213
- } : undefined ,
214
- } ;
215
- if ( contextOptions . name !== undefined )
216
- validateString ( contextOptions . name , 'options.contextName' ) ;
217
- if ( contextOptions . origin !== undefined )
218
- validateString ( contextOptions . origin , 'options.contextOrigin' ) ;
219
- if ( contextOptions . codeGeneration ) {
220
- validateBool ( contextOptions . codeGeneration . strings ,
221
- 'options.contextCodeGeneration.strings' ) ;
222
- validateBool ( contextOptions . codeGeneration . wasm ,
223
- 'options.contextCodeGeneration.wasm' ) ;
224
- }
225
- return contextOptions ;
199
+ const { strings, wasm } = options . contextCodeGeneration ;
200
+ if ( strings !== undefined )
201
+ validateBoolean ( strings , 'options.contextCodeGeneration.strings' ) ;
202
+ if ( wasm !== undefined )
203
+ validateBoolean ( wasm , 'options.contextCodeGeneration.wasm' ) ;
204
+ contextOptions . codeGeneration = { strings, wasm } ;
226
205
}
227
- return { } ;
206
+ return contextOptions ;
228
207
}
229
208
230
209
function isContext ( object ) {
@@ -240,9 +219,7 @@ function createContext(contextObject = {}, options = {}) {
240
219
return contextObject ;
241
220
}
242
221
243
- if ( typeof options !== 'object' || options === null ) {
244
- throw new ERR_INVALID_ARG_TYPE ( 'options' , 'Object' , options ) ;
245
- }
222
+ validateObject ( options , 'options' ) ;
246
223
247
224
const {
248
225
name = `VM Context ${ defaultContextNameIndex ++ } ` ,
@@ -253,14 +230,15 @@ function createContext(contextObject = {}, options = {}) {
253
230
validateString ( name , 'options.name' ) ;
254
231
if ( origin !== undefined )
255
232
validateString ( origin , 'options.origin' ) ;
256
- validateObject ( codeGeneration , 'options.codeGeneration' ) ;
233
+ if ( codeGeneration !== undefined )
234
+ validateObject ( codeGeneration , 'options.codeGeneration' ) ;
257
235
258
236
let strings = true ;
259
237
let wasm = true ;
260
238
if ( codeGeneration !== undefined ) {
261
239
( { strings = true , wasm = true } = codeGeneration ) ;
262
- validateBool ( strings , 'options.codeGeneration.strings' ) ;
263
- validateBool ( wasm , 'options.codeGeneration.wasm' ) ;
240
+ validateBoolean ( strings , 'options.codeGeneration.strings' ) ;
241
+ validateBoolean ( wasm , 'options.codeGeneration.wasm' ) ;
264
242
}
265
243
266
244
makeContext ( contextObject , name , origin , strings , wasm ) ;
@@ -322,9 +300,7 @@ function runInThisContext(code, options) {
322
300
function compileFunction ( code , params , options = { } ) {
323
301
validateString ( code , 'code' ) ;
324
302
if ( params !== undefined ) {
325
- if ( ! ArrayIsArray ( params ) ) {
326
- throw new ERR_INVALID_ARG_TYPE ( 'params' , 'Array' , params ) ;
327
- }
303
+ validateArray ( params , 'params' ) ;
328
304
ArrayPrototypeForEach ( params ,
329
305
( param , i ) => validateString ( param , `params[${ i } ]` ) ) ;
330
306
}
@@ -342,20 +318,9 @@ function compileFunction(code, params, options = {}) {
342
318
validateString ( filename , 'options.filename' ) ;
343
319
validateUint32 ( columnOffset , 'options.columnOffset' ) ;
344
320
validateUint32 ( lineOffset , 'options.lineOffset' ) ;
345
- if ( cachedData !== undefined && ! isArrayBufferView ( cachedData ) ) {
346
- throw new ERR_INVALID_ARG_TYPE (
347
- 'options.cachedData' ,
348
- [ 'Buffer' , 'TypedArray' , 'DataView' ] ,
349
- cachedData
350
- ) ;
351
- }
352
- if ( typeof produceCachedData !== 'boolean' ) {
353
- throw new ERR_INVALID_ARG_TYPE (
354
- 'options.produceCachedData' ,
355
- 'boolean' ,
356
- produceCachedData
357
- ) ;
358
- }
321
+ if ( cachedData !== undefined )
322
+ validateBuffer ( cachedData , 'options.cachedData' ) ;
323
+ validateBoolean ( produceCachedData , 'options.produceCachedData' ) ;
359
324
if ( parsingContext !== undefined ) {
360
325
if (
361
326
typeof parsingContext !== 'object' ||
@@ -369,21 +334,10 @@ function compileFunction(code, params, options = {}) {
369
334
) ;
370
335
}
371
336
}
372
- if ( ! ArrayIsArray ( contextExtensions ) ) {
373
- throw new ERR_INVALID_ARG_TYPE (
374
- 'options.contextExtensions' ,
375
- 'Array' ,
376
- contextExtensions
377
- ) ;
378
- }
337
+ validateArray ( contextExtensions , 'options.contextExtensions' ) ;
379
338
ArrayPrototypeForEach ( contextExtensions , ( extension , i ) => {
380
- if ( typeof extension !== 'object' ) {
381
- throw new ERR_INVALID_ARG_TYPE (
382
- `options.contextExtensions[${ i } ]` ,
383
- 'object' ,
384
- extension
385
- ) ;
386
- }
339
+ const name = `options.contextExtensions[${ i } ]` ;
340
+ validateObject ( extension , name , { nullable : true } ) ;
387
341
} ) ;
388
342
389
343
const result = _compileFunction (
0 commit comments