11'use strict' ;
22/* global WebAssembly */
33const {
4- ArrayIsArray,
54 ArrayPrototypeMap,
65 ArrayPrototypePush,
76 FunctionPrototypeBind,
@@ -14,6 +13,11 @@ const {
1413 ERR_WASI_ALREADY_STARTED
1514} = require ( 'internal/errors' ) . codes ;
1615const { emitExperimentalWarning } = require ( 'internal/util' ) ;
16+ const {
17+ validateArray,
18+ validateBoolean,
19+ validateObject,
20+ } = require ( 'internal/validators' ) ;
1721const { WASI : _WASI } = internalBinding ( 'wasi' ) ;
1822const kExitCode = Symbol ( 'exitCode' ) ;
1923const kSetMemory = Symbol ( 'setMemory' ) ;
@@ -24,52 +28,39 @@ emitExperimentalWarning('WASI');
2428
2529class WASI {
2630 constructor ( options = { } ) {
27- if ( options === null || typeof options !== 'object' )
28- throw new ERR_INVALID_ARG_TYPE ( 'options' , 'object' , options ) ;
29-
30- const { env, preopens, returnOnExit = false } = options ;
31- let { args = [ ] } = options ;
31+ validateObject ( options , 'options' ) ;
3232
33- if ( ArrayIsArray ( args ) )
34- args = ArrayPrototypeMap ( args , ( arg ) => { return String ( arg ) ; } ) ;
35- else
36- throw new ERR_INVALID_ARG_TYPE ( 'options.args' , 'Array' , args ) ;
33+ if ( options . args !== undefined )
34+ validateArray ( options . args , 'options.args' ) ;
35+ const args = ArrayPrototypeMap ( options . args || [ ] , String ) ;
3736
38- const envPairs = [ ] ;
39-
40- if ( env !== null && typeof env === 'object' ) {
41- for ( const key in env ) {
42- const value = env [ key ] ;
37+ const env = [ ] ;
38+ if ( options . env !== undefined ) {
39+ validateObject ( options . env , 'options.env' ) ;
40+ for ( const [ key , value ] of ObjectEntries ( options . env ) ) {
4341 if ( value !== undefined )
44- ArrayPrototypePush ( envPairs , `${ key } =${ value } ` ) ;
42+ ArrayPrototypePush ( env , `${ key } =${ value } ` ) ;
4543 }
46- } else if ( env !== undefined ) {
47- throw new ERR_INVALID_ARG_TYPE ( 'options.env' , 'Object' , env ) ;
4844 }
4945
50- const preopenArray = [ ] ;
51-
52- if ( typeof preopens === 'object' && preopens !== null ) {
53- for ( const [ key , value ] of ObjectEntries ( preopens ) ) {
54- ArrayPrototypePush ( preopenArray , String ( key ) , String ( value ) ) ;
46+ const preopens = [ ] ;
47+ if ( options . preopens !== undefined ) {
48+ validateObject ( options . preopens , 'options. preopens' ) ;
49+ for ( const [ key , value ] of ObjectEntries ( options . preopens ) ) {
50+ ArrayPrototypePush ( preopens , String ( key ) , String ( value ) ) ;
5551 }
56- } else if ( preopens !== undefined ) {
57- throw new ERR_INVALID_ARG_TYPE ( 'options.preopens' , 'Object' , preopens ) ;
58- }
59-
60- if ( typeof returnOnExit !== 'boolean' ) {
61- throw new ERR_INVALID_ARG_TYPE (
62- 'options.returnOnExit' , 'boolean' , returnOnExit ) ;
6352 }
6453
65- const wrap = new _WASI ( args , envPairs , preopenArray ) ;
54+ const wrap = new _WASI ( args , env , preopens ) ;
6655
6756 for ( const prop in wrap ) {
6857 wrap [ prop ] = FunctionPrototypeBind ( wrap [ prop ] , wrap ) ;
6958 }
7059
71- if ( returnOnExit ) {
72- wrap . proc_exit = FunctionPrototypeBind ( wasiReturnOnProcExit , this ) ;
60+ if ( options . returnOnExit !== undefined ) {
61+ validateBoolean ( options . returnOnExit , 'options.returnOnExit' ) ;
62+ if ( options . returnOnExit )
63+ wrap . proc_exit = FunctionPrototypeBind ( wasiReturnOnProcExit , this ) ;
7364 }
7465
7566 this [ kSetMemory ] = wrap . _setMemory ;
@@ -87,8 +78,7 @@ class WASI {
8778
8879 const exports = instance . exports ;
8980
90- if ( exports === null || typeof exports !== 'object' )
91- throw new ERR_INVALID_ARG_TYPE ( 'instance.exports' , 'Object' , exports ) ;
81+ validateObject ( exports , 'instance.exports' ) ;
9282
9383 const { memory } = exports ;
9484
0 commit comments