/
wasm.worker.js
323 lines (294 loc) · 8.75 KB
/
wasm.worker.js
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// @ts-check
"use strict";
/** @type {WorkerData | undefined} */
let workerData;
/** @type {number | undefined} */
let workerId;
/**
* @param {PostMessage} data
*/
function sendMessage(data) {
self.postMessage(data);
}
/**
* @typedef {Object} MapImportsArgs
* @property {WebAssembly.Imports} imports
* @property {number} workerId
* @property {WebAssembly.Module} module
* @property {(message: any) => void} postMessage
*/
/**
* @param {WasmImportsLoadParam} param
* @param {SharedArrayBuffer} waitBuffer
* @returns {WebAssembly.Imports}
*/
function mapImports(param, waitBuffer) {
/** @type {WebAssembly.Imports} */
const wasmImports = {};
if (param) {
for (const moduleName in param) {
const map = wasmImports[moduleName] || {};
wasmImports[moduleName] = map;
const moduleImports = param[moduleName];
for (const importName in moduleImports) {
const value = moduleImports[importName];
if (value === null || value === undefined) {
continue;
}
if (
typeof value === "number" ||
typeof value === "function" ||
value instanceof WebAssembly.Global ||
value instanceof WebAssembly.Memory ||
value instanceof WebAssembly.Table
) {
map[importName] = value;
} else if ("functionId" in value) {
map[importName] = createWasmFunctionImport(value, waitBuffer);
} else if ("element" in value) {
value.initial = value.initial || value["minimum"];
delete value["minimum"];
map[importName] = new WebAssembly.Table(value, value.initialValue);
} else if ("value" in value) {
map[importName] = new WebAssembly.Global(value, value.initialValue);
} else if ("shared" in value) {
value.initial = value.initial || value["minimum"];
delete value["minimum"];
map[importName] = new WebAssembly.Memory(value);
}
}
}
}
return wasmImports;
}
/**
* @param {FunctionImport} value
* @param {SharedArrayBuffer} waitBuffer
* @returns {(...args: Array<unknown>) => Array<unknown> | unknown | undefined}
*/
function createWasmFunctionImport(value, waitBuffer) {
return function () {
const args = Array.prototype.slice.call(arguments);
const workerId_ = /** @type {number} */ (workerId);
sendMessage({
cmd: "call",
args,
workerId: workerId_,
functionId: value.functionId,
});
Atomics.wait(new Int32Array(waitBuffer), 0, 0);
if (value.resultTypes.length == 0) return undefined;
const dataView = new DataView(waitBuffer);
let offset = 4;
const result = value.resultTypes.map((kind) => {
switch (kind) {
case "i32":
return dataView.getInt32((offset += 4), true);
case "f32":
return dataView.getFloat32((offset += 4), true);
case "i64":
return dataView.getBigInt64((offset += 8), true);
case "f64":
return dataView.getFloat64((offset += 8), true);
// TODO: support other types
default:
throw Error("Unimplemented parsing type " + kind);
}
});
if (result.length === 1) return result[0];
return result;
};
}
onmessage = (e) => {
/** @type {number | undefined} */
let taskId;
try {
/** @type {MessageData} */
const data = e.data;
if (data.cmd === "load") {
workerId = data.workerId;
let wasmImports = mapImports(data.wasmImports, data.sharedBuffer);
if (data.workerMapImportsScriptUrl) {
importScripts(data.workerMapImportsScriptUrl);
/** @type {MapImportsArgs} */
const args = {
imports: wasmImports,
module: data.wasmModule,
postMessage: (arg) => {
sendMessage({
cmd: "event",
data: arg,
});
},
workerId: data.workerId,
};
if (typeof self["mapWorkerWasmImports"] === "function") {
wasmImports = self["mapWorkerWasmImports"](args);
} else {
throw new Error(
"mapWorkerWasmImports is not defined in workerMapImportsScriptUrl"
);
}
}
const instance = new WebAssembly.Instance(data.wasmModule, wasmImports);
workerData = {
wasmInstance: instance,
wasmModule: data.wasmModule,
workerId: data.workerId,
};
sendMessage({
cmd: "loaded",
workerId: workerData.workerId,
});
} else if (data.cmd === "run") {
taskId = data.taskId;
if (!workerData) {
throw new Error("Worker Instance not loaded");
}
try {
const func = workerData.wasmInstance.exports[data.functionExport];
if (!func) {
throw new Error(
"functionExport " + data.functionExport + " not found"
);
} else if (typeof func !== "function") {
throw new Error(
"functionExport " + data.functionExport + " is not a function"
);
} else if (func.length !== data.args.length) {
throw new Error(
"functionExport " +
data.functionExport +
" has different number of arguments"
);
}
const result = func(...data.args);
sendMessage({
cmd: "result",
taskId: data.taskId,
result,
isUndefined: result === undefined,
workerId: workerData.workerId,
});
} catch (ex) {
sendMessage({
cmd: "alert",
didThrow: true,
taskId: data.taskId,
text: ex.message,
workerId: workerData.workerId,
});
}
// } else if (data.cmd === "cancel") {
// if (Module["_pthread_self"]()) {
// Module["__emscripten_thread_exit"](-1);
// }
} else {
threadPrintErr("wasm.worker.js received unknown command" + data);
}
} catch (ex) {
let text =
"wasm.worker.js onmessage() captured an uncaught exception: " + ex;
if (ex && ex.stack) text = text + ex.stack;
sendMessage({
cmd: "alert",
didThrow: true,
taskId,
text,
workerId,
});
throw ex;
}
};
function threadPrintErr() {
const text = Array.prototype.slice.call(arguments).join(" ");
console.error(text);
sendMessage({
cmd: "alert",
text: text,
workerId,
didThrow: false,
taskId: undefined,
});
}
/**
* @typedef {Object} WorkerData
* @property {number} workerId
* @property {WebAssembly.Instance} wasmInstance
* @property {WebAssembly.Module} wasmModule
*/
/**
* @typedef {Object} FunctionImport
* @property {number} functionId
* @property {Array<"i32" | "i64" | "f32"| "f64">} resultTypes
*/
/**
* @typedef {Record<string, Record<string, WebAssembly.ImportValue
* | (WebAssembly.TableDescriptor & {initialValue?: any})
* | (WebAssembly.GlobalDescriptor & {initialValue?: any})
* | WebAssembly.MemoryDescriptor
* | FunctionImport>> | undefined} WasmImportsLoadParam
*/
/**
* @typedef {Object} MessageDataLoad
* @property {"load"} cmd
* @property {number} workerId
* @property {SharedArrayBuffer} sharedBuffer
* @property {WebAssembly.Module} wasmModule
* @property {WasmImportsLoadParam} wasmImports
* @property {string | null} workerMapImportsScriptUrl
*/
/**
* Message that stars the execution of a task
* @typedef {Object} MessageDataRun
* @property {"run"} cmd
* @property {number} taskId
* @property {string} functionExport
* @property {Array<unknown>} args
*/
/**
* Message received from the main process
* @typedef {(MessageDataLoad | MessageDataRun)} MessageData
*/
/**
* Message sent when the WasmInstance has been loaded
* @typedef {Object} PostMessageLoaded
* @property {"loaded"} cmd
* @property {number} workerId
*/
/**
* Message sent when a task execution has completed
* @typedef {Object} PostMessageResult
* @property {"result"} cmd
* @property {number} workerId
* @property {number} taskId
* @property {Array<unknown> | unknown} result
* @property {boolean} isUndefined
*/
/**
* Message sent to alert the main process
* @typedef {Object} PostMessageAlert
* @property {"alert"} cmd
* @property {number | undefined} workerId
* @property {number | undefined} taskId
* @property {string} text
* @property {boolean} didThrow
*/
/**
* Message sent from `mapWorkerWasmImports` in `workerMapImportsScriptUrl`
* @typedef {Object} PostMessageEvent
* @property {"event"} cmd
* @property {any} data
*/
/**
* Message sent to execute an import function
* @typedef {Object} PostMessageCall
* @property {"call"} cmd
* @property {Array<unknown>} args
* @property {number} functionId
* @property {number} workerId
*/
/**
* Message sent to main process
* @typedef {(PostMessageLoaded | PostMessageResult | PostMessageAlert | PostMessageEvent | PostMessageCall)} PostMessage
*/