-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathruntime.d.ts
214 lines (210 loc) · 7.36 KB
/
runtime.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
declare class Memory {
readonly rawMemory: WebAssembly.Memory;
private readonly heap;
constructor(exports: WebAssembly.Exports);
retain: (value: any) => number;
getObject: (ref: number) => any;
release: (ref: number) => void;
bytes: () => Uint8Array<ArrayBuffer>;
dataView: () => DataView<ArrayBuffer>;
writeBytes: (ptr: pointer, bytes: Uint8Array) => void;
readUint32: (ptr: pointer) => number;
readUint64: (ptr: pointer) => bigint;
readInt64: (ptr: pointer) => bigint;
readFloat64: (ptr: pointer) => number;
writeUint32: (ptr: pointer, value: number) => void;
writeUint64: (ptr: pointer, value: bigint) => void;
writeInt64: (ptr: pointer, value: bigint) => void;
writeFloat64: (ptr: pointer, value: number) => void;
}
type ref = number;
type pointer = number;
/**
* A thread channel is a set of functions that are used to communicate between
* the main thread and the worker thread. The main thread and the worker thread
* can send messages to each other using these functions.
*
* @example
* ```javascript
* // worker.js
* const runtime = new SwiftRuntime({
* threadChannel: {
* postMessageToMainThread: postMessage,
* listenMessageFromMainThread: (listener) => {
* self.onmessage = (event) => {
* listener(event.data);
* };
* }
* }
* });
*
* // main.js
* const worker = new Worker("worker.js");
* const runtime = new SwiftRuntime({
* threadChannel: {
* postMessageToWorkerThread: (tid, data) => {
* worker.postMessage(data);
* },
* listenMessageFromWorkerThread: (tid, listener) => {
* worker.onmessage = (event) => {
listener(event.data);
* };
* }
* }
* });
* ```
*/
type SwiftRuntimeThreadChannel = {
/**
* This function is used to send messages from the worker thread to the main thread.
* The message submitted by this function is expected to be listened by `listenMessageFromWorkerThread`.
* @param message The message to be sent to the main thread.
* @param transfer The array of objects to be transferred to the main thread.
*/
postMessageToMainThread: (message: WorkerToMainMessage, transfer: any[]) => void;
/**
* This function is expected to be set in the worker thread and should listen
* to messages from the main thread sent by `postMessageToWorkerThread`.
* @param listener The listener function to be called when a message is received from the main thread.
*/
listenMessageFromMainThread: (listener: (message: MainToWorkerMessage) => void) => void;
} | {
/**
* This function is expected to be set in the main thread.
* The message submitted by this function is expected to be listened by `listenMessageFromMainThread`.
* @param tid The thread ID of the worker thread.
* @param message The message to be sent to the worker thread.
* @param transfer The array of objects to be transferred to the worker thread.
*/
postMessageToWorkerThread: (tid: number, message: MainToWorkerMessage, transfer: any[]) => void;
/**
* This function is expected to be set in the main thread and should listen
* to messages sent by `postMessageToMainThread` from the worker thread.
* @param tid The thread ID of the worker thread.
* @param listener The listener function to be called when a message is received from the worker thread.
*/
listenMessageFromWorkerThread: (tid: number, listener: (message: WorkerToMainMessage) => void) => void;
/**
* This function is expected to be set in the main thread and called
* when the worker thread is terminated.
* @param tid The thread ID of the worker thread.
*/
terminateWorkerThread?: (tid: number) => void;
};
declare class ITCInterface {
private memory;
constructor(memory: Memory);
send(sendingObject: ref, transferringObjects: ref[], sendingContext: pointer): {
object: any;
sendingContext: pointer;
transfer: Transferable[];
};
sendObjects(sendingObjects: ref[], transferringObjects: ref[], sendingContext: pointer): {
object: any[];
sendingContext: pointer;
transfer: Transferable[];
};
release(objectRef: ref): {
object: undefined;
transfer: Transferable[];
};
}
type AllRequests<Interface extends Record<string, any>> = {
[K in keyof Interface]: {
method: K;
parameters: Parameters<Interface[K]>;
};
};
type ITCRequest<Interface extends Record<string, any>> = AllRequests<Interface>[keyof AllRequests<Interface>];
type AllResponses<Interface extends Record<string, any>> = {
[K in keyof Interface]: ReturnType<Interface[K]>;
};
type ITCResponse<Interface extends Record<string, any>> = AllResponses<Interface>[keyof AllResponses<Interface>];
type RequestMessage = {
type: "request";
data: {
/** The TID of the thread that sent the request */
sourceTid: number;
/** The TID of the thread that should respond to the request */
targetTid: number;
/** The context pointer of the request */
context: pointer;
/** The request content */
request: ITCRequest<ITCInterface>;
};
};
type SerializedError = {
isError: true;
value: Error;
} | {
isError: false;
value: unknown;
};
type ResponseMessage = {
type: "response";
data: {
/** The TID of the thread that sent the response */
sourceTid: number;
/** The context pointer of the request */
context: pointer;
/** The response content */
response: {
ok: true;
value: ITCResponse<ITCInterface>;
} | {
ok: false;
error: SerializedError;
};
};
};
type MainToWorkerMessage = {
type: "wake";
} | RequestMessage | ResponseMessage;
type WorkerToMainMessage = {
type: "job";
data: number;
} | RequestMessage | ResponseMessage;
type SwiftRuntimeOptions = {
/**
* If `true`, the memory space of the WebAssembly instance can be shared
* between the main thread and the worker thread.
*/
sharedMemory?: boolean;
/**
* The thread channel is a set of functions that are used to communicate
* between the main thread and the worker thread.
*/
threadChannel?: SwiftRuntimeThreadChannel;
};
declare class SwiftRuntime {
private _instance;
private _memory;
private _closureDeallocator;
private options;
private version;
private textDecoder;
private textEncoder;
/** The thread ID of the current thread. */
private tid;
constructor(options?: SwiftRuntimeOptions);
setInstance(instance: WebAssembly.Instance): void;
main(): void;
/**
* Start a new thread with the given `tid` and `startArg`, which
* is forwarded to the `wasi_thread_start` function.
* This function is expected to be called from the spawned Web Worker thread.
*/
startThread(tid: number, startArg: number): void;
private get instance();
private get exports();
private get memory();
private get closureDeallocator();
private callHostFunction;
/** @deprecated Use `wasmImports` instead */
importObjects: () => WebAssembly.ModuleImports;
get wasmImports(): WebAssembly.ModuleImports;
private postMessageToMainThread;
private postMessageToWorkerThread;
}
export { SwiftRuntime };
export type { SwiftRuntimeOptions, SwiftRuntimeThreadChannel };