@@ -4,6 +4,8 @@ interface ExportedMemory {
4
4
5
5
type ref = number ;
6
6
type pointer = number ;
7
+ // Function invocation call, using a host function ID and array of parameters
8
+ type function_call = [ number , any [ ] ] ;
7
9
8
10
interface GlobalVariable { }
9
11
declare const window : GlobalVariable ;
@@ -156,10 +158,14 @@ export class SwiftRuntime {
156
158
private instance : WebAssembly . Instance | null ;
157
159
private heap : SwiftRuntimeHeap ;
158
160
private version : number = 701 ;
161
+
162
+ // Support Asyncified modules
159
163
private isSleeping : boolean ;
160
164
private instanceIsAsyncified : boolean ;
161
165
private resumeCallback : ( ) => void ;
162
166
private asyncifyBufferPointer : pointer | null ;
167
+ // Keeps track of function calls requested while instance is sleeping
168
+ private pendingHostFunctionCalls : function_call [ ] ;
163
169
164
170
constructor ( ) {
165
171
this . instance = null ;
@@ -168,6 +174,7 @@ export class SwiftRuntime {
168
174
this . instanceIsAsyncified = false ;
169
175
this . resumeCallback = ( ) => { } ;
170
176
this . asyncifyBufferPointer = null ;
177
+ this . pendingHostFunctionCalls = [ ] ;
171
178
}
172
179
173
180
/**
@@ -211,6 +218,10 @@ export class SwiftRuntime {
211
218
const callHostFunction = ( host_func_id : number , args : any [ ] ) => {
212
219
if ( ! this . instance )
213
220
throw new Error ( "WebAssembly instance is not set yet" ) ;
221
+ if ( this . isSleeping ) {
222
+ this . pendingHostFunctionCalls . push ( [ host_func_id , args ] ) ;
223
+ return ;
224
+ }
214
225
const exports = ( this . instance
215
226
. exports as any ) as SwiftRuntimeExportedFunctions ;
216
227
const argc = args . length ;
@@ -409,6 +420,11 @@ export class SwiftRuntime {
409
420
// We are called as part of a resume/rewind. Stop sleeping.
410
421
exports . asyncify_stop_rewind ( ) ;
411
422
this . isSleeping = false ;
423
+ const pendingCalls = this . pendingHostFunctionCalls ;
424
+ this . pendingHostFunctionCalls = [ ] ;
425
+ pendingCalls . forEach ( call => {
426
+ callHostFunction ( call [ 0 ] , call [ 1 ] ) ;
427
+ } ) ;
412
428
return ;
413
429
}
414
430
0 commit comments