1
1
'use strict' ;
2
2
/* global WebAssembly */
3
3
const {
4
- ArrayIsArray,
5
4
ArrayPrototypeMap,
6
5
ArrayPrototypePush,
7
6
FunctionPrototypeBind,
@@ -13,6 +12,11 @@ const {
13
12
ERR_WASI_ALREADY_STARTED
14
13
} = require ( 'internal/errors' ) . codes ;
15
14
const { emitExperimentalWarning } = require ( 'internal/util' ) ;
15
+ const {
16
+ validateArray,
17
+ validateBoolean,
18
+ validateObject,
19
+ } = require ( 'internal/validators' ) ;
16
20
const { WASI : _WASI } = internalBinding ( 'wasi' ) ;
17
21
const kExitCode = Symbol ( 'exitCode' ) ;
18
22
const kSetMemory = Symbol ( 'setMemory' ) ;
@@ -23,52 +27,39 @@ emitExperimentalWarning('WASI');
23
27
24
28
class WASI {
25
29
constructor ( options = { } ) {
26
- if ( options === null || typeof options !== 'object' )
27
- throw new ERR_INVALID_ARG_TYPE ( 'options' , 'object' , options ) ;
28
-
29
- const { env, preopens, returnOnExit = false } = options ;
30
- let { args = [ ] } = options ;
30
+ validateObject ( options , 'options' ) ;
31
31
32
- if ( ArrayIsArray ( args ) )
33
- args = ArrayPrototypeMap ( args , ( arg ) => { return String ( arg ) ; } ) ;
34
- else
35
- throw new ERR_INVALID_ARG_TYPE ( 'options.args' , 'Array' , args ) ;
32
+ if ( options . args !== undefined )
33
+ validateArray ( options . args , 'options.args' ) ;
34
+ const args = ArrayPrototypeMap ( options . args || [ ] , String ) ;
36
35
37
- const envPairs = [ ] ;
38
-
39
- if ( env !== null && typeof env === 'object' ) {
40
- for ( const key in env ) {
41
- const value = env [ key ] ;
36
+ const env = [ ] ;
37
+ if ( options . env !== undefined ) {
38
+ validateObject ( options . env , 'options.env' ) ;
39
+ for ( const [ key , value ] of ObjectEntries ( options . env ) ) {
42
40
if ( value !== undefined )
43
- ArrayPrototypePush ( envPairs , `${ key } =${ value } ` ) ;
41
+ ArrayPrototypePush ( env , `${ key } =${ value } ` ) ;
44
42
}
45
- } else if ( env !== undefined ) {
46
- throw new ERR_INVALID_ARG_TYPE ( 'options.env' , 'Object' , env ) ;
47
43
}
48
44
49
- const preopenArray = [ ] ;
50
-
51
- if ( typeof preopens === 'object' && preopens !== null ) {
52
- for ( const [ key , value ] of ObjectEntries ( preopens ) ) {
53
- ArrayPrototypePush ( preopenArray , String ( key ) , String ( value ) ) ;
45
+ const preopens = [ ] ;
46
+ if ( options . preopens !== undefined ) {
47
+ validateObject ( options . preopens , 'options. preopens' ) ;
48
+ for ( const [ key , value ] of ObjectEntries ( options . preopens ) ) {
49
+ ArrayPrototypePush ( preopens , String ( key ) , String ( value ) ) ;
54
50
}
55
- } else if ( preopens !== undefined ) {
56
- throw new ERR_INVALID_ARG_TYPE ( 'options.preopens' , 'Object' , preopens ) ;
57
- }
58
-
59
- if ( typeof returnOnExit !== 'boolean' ) {
60
- throw new ERR_INVALID_ARG_TYPE (
61
- 'options.returnOnExit' , 'boolean' , returnOnExit ) ;
62
51
}
63
52
64
- const wrap = new _WASI ( args , envPairs , preopenArray ) ;
53
+ const wrap = new _WASI ( args , env , preopens ) ;
65
54
66
55
for ( const prop in wrap ) {
67
56
wrap [ prop ] = FunctionPrototypeBind ( wrap [ prop ] , wrap ) ;
68
57
}
69
58
70
- if ( returnOnExit ) {
71
- wrap . proc_exit = FunctionPrototypeBind ( wasiReturnOnProcExit , this ) ;
59
+ if ( options . returnOnExit !== undefined ) {
60
+ validateBoolean ( options . returnOnExit , 'options.returnOnExit' ) ;
61
+ if ( options . returnOnExit )
62
+ wrap . proc_exit = FunctionPrototypeBind ( wasiReturnOnProcExit , this ) ;
72
63
}
73
64
74
65
this [ kSetMemory ] = wrap . _setMemory ;
@@ -86,8 +77,7 @@ class WASI {
86
77
87
78
const exports = instance . exports ;
88
79
89
- if ( exports === null || typeof exports !== 'object' )
90
- throw new ERR_INVALID_ARG_TYPE ( 'instance.exports' , 'Object' , exports ) ;
80
+ validateObject ( exports , 'instance.exports' ) ;
91
81
92
82
const { memory } = exports ;
93
83
0 commit comments