2024-01-17 01:57:29 +09:00
|
|
|
import { instantiateNapiModuleSync, MessageHandler, WASI } from '@napi-rs/wasm-runtime'
|
2024-01-17 00:28:40 +09:00
|
|
|
import { Volume, createFsFromVolume } from '@napi-rs/wasm-runtime/fs'
|
2024-01-08 22:02:46 +09:00
|
|
|
|
2024-01-09 01:34:59 +09:00
|
|
|
const fs = createFsFromVolume(
|
|
|
|
Volume.fromJSON({
|
|
|
|
'/': null,
|
|
|
|
}),
|
|
|
|
)
|
2024-01-08 22:02:46 +09:00
|
|
|
|
|
|
|
const handler = new MessageHandler({
|
|
|
|
onLoad({ wasmModule, wasmMemory }) {
|
|
|
|
const wasi = new WASI({
|
|
|
|
fs,
|
2024-01-10 12:18:13 +09:00
|
|
|
print: function () {
|
2024-01-09 01:34:59 +09:00
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log.apply(console, arguments)
|
|
|
|
},
|
2024-01-17 01:57:29 +09:00
|
|
|
printErr: function() {
|
2024-01-10 12:18:13 +09:00
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error.apply(console, arguments)
|
|
|
|
},
|
2024-01-08 22:02:46 +09:00
|
|
|
})
|
|
|
|
return instantiateNapiModuleSync(wasmModule, {
|
|
|
|
childThread: true,
|
|
|
|
wasi,
|
|
|
|
overwriteImports(importObject) {
|
|
|
|
importObject.env = {
|
|
|
|
...importObject.env,
|
|
|
|
...importObject.napi,
|
|
|
|
...importObject.emnapi,
|
2024-01-09 01:34:59 +09:00
|
|
|
memory: wasmMemory,
|
2024-01-08 22:02:46 +09:00
|
|
|
}
|
2024-01-09 01:34:59 +09:00
|
|
|
},
|
2024-01-08 22:02:46 +09:00
|
|
|
})
|
2024-01-09 01:34:59 +09:00
|
|
|
},
|
2024-01-08 22:02:46 +09:00
|
|
|
})
|
|
|
|
|
2024-01-10 12:18:13 +09:00
|
|
|
globalThis.onmessage = function (e) {
|
2024-01-08 22:02:46 +09:00
|
|
|
handler.handle(e)
|
|
|
|
}
|