Skip to content

Commit d1fff95

Browse files
committed
For incoming function calls while instance is sleeping, process after instance is resumed
1 parent 8704225 commit d1fff95

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Runtime/src/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ interface ExportedMemory {
44

55
type ref = number;
66
type pointer = number;
7+
// Function invocation call, using a host function ID and array of parameters
8+
type function_call = [number, any[]];
79

810
interface GlobalVariable {}
911
declare const window: GlobalVariable;
@@ -156,10 +158,14 @@ export class SwiftRuntime {
156158
private instance: WebAssembly.Instance | null;
157159
private heap: SwiftRuntimeHeap;
158160
private version: number = 701;
161+
162+
// Support Asyncified modules
159163
private isSleeping: boolean;
160164
private instanceIsAsyncified: boolean;
161165
private resumeCallback: () => void;
162166
private asyncifyBufferPointer: pointer | null;
167+
// Keeps track of function calls requested while instance is sleeping
168+
private pendingHostFunctionCalls: function_call[];
163169

164170
constructor() {
165171
this.instance = null;
@@ -168,6 +174,7 @@ export class SwiftRuntime {
168174
this.instanceIsAsyncified = false;
169175
this.resumeCallback = () => { };
170176
this.asyncifyBufferPointer = null;
177+
this.pendingHostFunctionCalls = [];
171178
}
172179

173180
/**
@@ -211,6 +218,10 @@ export class SwiftRuntime {
211218
const callHostFunction = (host_func_id: number, args: any[]) => {
212219
if (!this.instance)
213220
throw new Error("WebAssembly instance is not set yet");
221+
if (this.isSleeping) {
222+
this.pendingHostFunctionCalls.push([host_func_id, args]);
223+
return;
224+
}
214225
const exports = (this.instance
215226
.exports as any) as SwiftRuntimeExportedFunctions;
216227
const argc = args.length;
@@ -409,6 +420,11 @@ export class SwiftRuntime {
409420
// We are called as part of a resume/rewind. Stop sleeping.
410421
exports.asyncify_stop_rewind();
411422
this.isSleeping = false;
423+
const pendingCalls = this.pendingHostFunctionCalls;
424+
this.pendingHostFunctionCalls = [];
425+
pendingCalls.forEach(call => {
426+
callHostFunction(call[0], call[1]);
427+
});
412428
return;
413429
}
414430

0 commit comments

Comments
 (0)