-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathworker.js
28 lines (26 loc) · 855 Bytes
/
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
import { instantiate } from "./instantiate.js"
self.onmessage = async (event) => {
const { module, memory, tid, startArg, configuration } = event.data;
const { instance, wasi, swiftRuntime } = await instantiate({
module,
threadChannel: {
postMessageToMainThread: (message) => {
// Send the job to the main thread
postMessage(message);
},
listenMessageFromMainThread: (listener) => {
self.onmessage = (event) => listener(event.data);
}
},
addToImports(importObject) {
importObject["env"] = { memory }
importObject["wasi"] = {
"thread-spawn": () => { throw new Error("Cannot spawn a new thread from a worker thread"); }
};
},
configuration
});
swiftRuntime.setInstance(instance);
wasi.inst = instance;
swiftRuntime.startThread(tid, startArg);
}