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