Skip to content

Commit 6c05686

Browse files
Update Multithreading example to use the new PackageToJS plugin
1 parent a7a7772 commit 6c05686

File tree

4 files changed

+11
-15
lines changed

4 files changed

+11
-15
lines changed

Examples/Multithreading/Sources/JavaScript/index.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ class ThreadRegistry {
99
workers = new Map();
1010
nextTid = 1;
1111

12-
constructor({ configuration }) {
13-
this.configuration = configuration;
14-
}
12+
constructor() {}
1513

1614
spawnThread(worker, module, memory, startArg) {
1715
const tid = this.nextTid++;
1816
this.workers.set(tid, worker);
19-
worker.postMessage({ module, memory, tid, startArg, configuration: this.configuration });
17+
worker.postMessage({ module, memory, tid, startArg });
2018
return tid;
2119
}
2220

@@ -39,8 +37,8 @@ class ThreadRegistry {
3937
}
4038
}
4139

42-
async function start(configuration = "release") {
43-
const response = await fetch(`./.build/${configuration}/MyApp.wasm`);
40+
async function start() {
41+
const response = await fetch(`./.build/plugins/PackageToJS/outputs/Package/main.wasm`);
4442
const module = await WebAssembly.compileStreaming(response);
4543
const memoryImport = WebAssembly.Module.imports(module).find(i => i.module === "env" && i.name === "memory");
4644
if (!memoryImport) {
@@ -51,7 +49,7 @@ async function start(configuration = "release") {
5149
}
5250
const memoryType = memoryImport.type;
5351
const memory = new WebAssembly.Memory({ initial: memoryType.minimum, maximum: memoryType.maximum, shared: true });
54-
const threads = new ThreadRegistry({ configuration });
52+
const threads = new ThreadRegistry();
5553
const { instance, swiftRuntime, wasi } = await instantiate({
5654
module,
5755
threadChannel: threads,
@@ -63,8 +61,7 @@ async function start(configuration = "release") {
6361
return threads.spawnThread(worker, module, memory, startArg);
6462
}
6563
};
66-
},
67-
configuration
64+
}
6865
});
6966
wasi.initialize(instance);
7067

Examples/Multithreading/Sources/JavaScript/instantiate.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { WASI, File, OpenFile, ConsoleStdout, PreopenDirectory } from 'https://esm.run/@bjorn3/[email protected]';
2+
import { SwiftRuntime } from "../../.build/plugins/PackageToJS/outputs/Package/runtime.js"
23

3-
export async function instantiate({ module, addToImports, threadChannel, configuration }) {
4+
export async function instantiate({ module, addToImports, threadChannel }) {
45
const args = ["main.wasm"]
56
const env = []
67
const fds = [
@@ -15,7 +16,6 @@ export async function instantiate({ module, addToImports, threadChannel, configu
1516
];
1617
const wasi = new WASI(args, env, fds);
1718

18-
const { SwiftRuntime } = await import(`/.build/${configuration}/JavaScriptKit_JavaScriptKit.resources/Runtime/index.mjs`);
1919
const swiftRuntime = new SwiftRuntime({ sharedMemory: true, threadChannel });
2020
const importObject = {
2121
wasi_snapshot_preview1: wasi.wasiImport,

Examples/Multithreading/Sources/JavaScript/worker.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { instantiate } from "./instantiate.js"
22

33
self.onmessage = async (event) => {
4-
const { module, memory, tid, startArg, configuration } = event.data;
4+
const { module, memory, tid, startArg } = event.data;
55
const { instance, wasi, swiftRuntime } = await instantiate({
66
module,
77
threadChannel: {
@@ -18,8 +18,7 @@ self.onmessage = async (event) => {
1818
importObject["wasi"] = {
1919
"thread-spawn": () => { throw new Error("Cannot spawn a new thread from a worker thread"); }
2020
};
21-
},
22-
configuration
21+
}
2322
});
2423

2524
swiftRuntime.setInstance(instance);

Examples/Multithreading/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
swift build --swift-sdk DEVELOPMENT-SNAPSHOT-2024-07-09-a-wasm32-unknown-wasip1-threads -Xswiftc -Xclang-linker -Xswiftc -mexec-model=reactor -Xlinker --export=__main_argc_argv -c release -Xswiftc -g
1+
swift package --swift-sdk "${SWIFT_SDK_ID:-wasm32-unknown-wasip1-threads}" -c release js

0 commit comments

Comments
 (0)