2222'use strict' ;
2323
2424const {
25- ArrayIsArray,
2625 ArrayPrototypeForEach,
2726 Symbol,
2827} = primordials ;
@@ -42,7 +41,11 @@ const {
4241const {
4342 validateInt32,
4443 validateUint32,
45- validateString
44+ validateString,
45+ validateArray,
46+ validateBoolean,
47+ validateBuffer,
48+ validateObject,
4649} = require ( 'internal/validators' ) ;
4750const { kVmBreakFirstLineSymbol } = require ( 'internal/util' ) ;
4851const kParsingContext = Symbol ( 'script parsing context' ) ;
@@ -139,30 +142,14 @@ class Script extends ContextifyScript {
139142}
140143
141144function 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 ) ) {
147146 throw new ERR_INVALID_ARG_TYPE ( 'contextifiedObject' , 'vm.Context' ,
148147 contextifiedObject ) ;
149148 }
150149}
151150
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-
162151function getRunInContextArgs ( options = { } ) {
163- if ( typeof options !== 'object' || options === null ) {
164- throw new ERR_INVALID_ARG_TYPE ( 'options' , 'Object' , options ) ;
165- }
152+ validateObject ( options , 'options' ) ;
166153
167154 let timeout = options . timeout ;
168155 if ( timeout === undefined ) {
@@ -177,14 +164,8 @@ function getRunInContextArgs(options = {}) {
177164 [ kVmBreakFirstLineSymbol ] : breakFirstLine = false ,
178165 } = options ;
179166
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' ) ;
188169
189170 return {
190171 breakOnSigint,
@@ -193,30 +174,28 @@ function getRunInContextArgs(options = {}) {
193174}
194175
195176function 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 ) {
197189 validateObject ( options . contextCodeGeneration ,
198190 '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 } ;
218197 }
219- return { } ;
198+ return contextOptions ;
220199}
221200
222201function isContext ( object ) {
@@ -232,9 +211,7 @@ function createContext(contextObject = {}, options = {}) {
232211 return contextObject ;
233212 }
234213
235- if ( typeof options !== 'object' || options === null ) {
236- throw new ERR_INVALID_ARG_TYPE ( 'options' , 'Object' , options ) ;
237- }
214+ validateObject ( options , 'options' ) ;
238215
239216 const {
240217 name = `VM Context ${ defaultContextNameIndex ++ } ` ,
@@ -245,14 +222,15 @@ function createContext(contextObject = {}, options = {}) {
245222 validateString ( name , 'options.name' ) ;
246223 if ( origin !== undefined )
247224 validateString ( origin , 'options.origin' ) ;
248- validateObject ( codeGeneration , 'options.codeGeneration' ) ;
225+ if ( codeGeneration !== undefined )
226+ validateObject ( codeGeneration , 'options.codeGeneration' ) ;
249227
250228 let strings = true ;
251229 let wasm = true ;
252230 if ( codeGeneration !== undefined ) {
253231 ( { 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' ) ;
256234 }
257235
258236 makeContext ( contextObject , name , origin , strings , wasm ) ;
@@ -314,9 +292,7 @@ function runInThisContext(code, options) {
314292function compileFunction ( code , params , options = { } ) {
315293 validateString ( code , 'code' ) ;
316294 if ( params !== undefined ) {
317- if ( ! ArrayIsArray ( params ) ) {
318- throw new ERR_INVALID_ARG_TYPE ( 'params' , 'Array' , params ) ;
319- }
295+ validateArray ( params , 'params' ) ;
320296 ArrayPrototypeForEach ( params ,
321297 ( param , i ) => validateString ( param , `params[${ i } ]` ) ) ;
322298 }
@@ -334,20 +310,9 @@ function compileFunction(code, params, options = {}) {
334310 validateString ( filename , 'options.filename' ) ;
335311 validateUint32 ( columnOffset , 'options.columnOffset' ) ;
336312 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' ) ;
351316 if ( parsingContext !== undefined ) {
352317 if (
353318 typeof parsingContext !== 'object' ||
@@ -361,21 +326,10 @@ function compileFunction(code, params, options = {}) {
361326 ) ;
362327 }
363328 }
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' ) ;
371330 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 } ) ;
379333 } ) ;
380334
381335 const result = _compileFunction (
0 commit comments