@desert-ant-labs/gist 0.1.0

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