Skip to content

Commit 1085b92

Browse files
committed
feat(interface-json-node-wasi): support modules that use threads
1 parent 31d33fb commit 1085b92

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

packages/core/typescript/itk-wasm/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
"typescript": "^5.3.2"
8888
},
8989
"dependencies": {
90+
"@emnapi/wasi-threads": "^1.0.1",
9091
"@itk-wasm/dam": "^1.1.1",
9192
"@thewtex/zstddec": "^0.2.1",
9293
"@types/emscripten": "^1.39.10",
Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,49 @@
1+
#!/usr/bin/env node
2+
13
import { readFile } from 'node:fs/promises'
24
import { WASI } from 'wasi'
5+
import { WASIThreads } from '@emnapi/wasi-threads'
36

47
const wasi = new WASI({
58
version: 'preview1',
9+
returnOnExit: true,
610
args: ['--interface-json'],
711
env: process.env,
812
preopens: {}
913
})
1014

11-
const importObject = { wasi_snapshot_preview1: wasi.wasiImport }
15+
const wasiThreads = new WASIThreads({
16+
wasi
17+
});
18+
19+
const memory = new WebAssembly.Memory({
20+
initial: 128,
21+
maximum: 0x10000,
22+
shared: true
23+
})
24+
25+
const importObject = {
26+
env: {
27+
memory
28+
},
29+
wasi_snapshot_preview1: wasi.wasiImport,
30+
...wasiThreads.getImportObject()
31+
}
32+
33+
try {
34+
const wasm = await WebAssembly.compile(
35+
// await readFile(new URL(process.argv[2], import.meta.url))
36+
await readFile(process.argv[2])
37+
)
38+
const instance = await WebAssembly.instantiate(wasm, importObject)
1239

13-
const wasm = await WebAssembly.compile(
14-
// await readFile(new URL(process.argv[2], import.meta.url))
15-
await readFile(process.argv[2])
16-
)
17-
const instance = await WebAssembly.instantiate(wasm, importObject)
40+
wasiThreads.setup(instance, wasm, memory);
41+
await wasiThreads.preloadWorkers();
1842

19-
wasi.initialize(instance)
20-
instance.exports['']()
43+
wasi.initialize(instance)
44+
instance.exports['']()
45+
} catch (error) {
46+
if (error.toString() !== Symbol('kExitCode').toString()) {
47+
console.error('Error: ', error)
48+
}
49+
}

pnpm-lock.yaml

Lines changed: 13 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)