@@ -114,17 +114,21 @@ class WASI {
114114 this [ kInstance ] = undefined ;
115115 }
116116
117- setupInstance ( instance , memory ) {
117+ finalizeBindings ( instance , {
118+ memory = instance . exports . memory
119+ } = { } ) {
120+ if ( this [ kStarted ] ) {
121+ throw new ERR_WASI_ALREADY_STARTED ( ) ;
122+ }
123+
118124 validateObject ( instance , 'instance' ) ;
119125 validateObject ( instance . exports , 'instance.exports' ) ;
120126
121127 if ( memory != null ) {
122128 if ( ! ( memory instanceof WebAssembly . Memory ) ) {
123- throw new ERR_INVALID_ARG_TYPE ( 'memory' , 'WebAssembly.Memory' , memory ) ;
129+ throw new ERR_INVALID_ARG_TYPE ( 'options. memory' , 'WebAssembly.Memory' , memory ) ;
124130 }
125131 this [ kSetMemory ] ( memory ) ;
126- } else {
127- this [ kSetMemory ] ( instance . exports . memory ) ;
128132 }
129133
130134 this [ kInstance ] = instance ;
@@ -133,11 +137,7 @@ class WASI {
133137
134138 // Must not export _initialize, must export _start
135139 start ( instance ) {
136- if ( this [ kStarted ] ) {
137- throw new ERR_WASI_ALREADY_STARTED ( ) ;
138- }
139-
140- this . setupInstance ( instance ) ;
140+ this . finalizeBindings ( instance ) ;
141141
142142 const { _start, _initialize } = this [ kInstance ] . exports ;
143143
@@ -157,11 +157,7 @@ class WASI {
157157
158158 // Must not export _start, may optionally export _initialize
159159 initialize ( instance ) {
160- if ( this [ kStarted ] ) {
161- throw new ERR_WASI_ALREADY_STARTED ( ) ;
162- }
163-
164- this . setupInstance ( instance ) ;
160+ this . finalizeBindings ( instance ) ;
165161
166162 const { _start, _initialize } = this [ kInstance ] . exports ;
167163
0 commit comments