2024-04-23 13:14:06 +09:00
|
|
|
import { instantiateNapiModuleSync, MessageHandler, WASI, createFsProxy } from '@napi-rs/wasm-runtime'
|
|
|
|
import { memfsExported as __memfsExported } from '@napi-rs/wasm-runtime/fs'
|
2024-01-08 22:02:46 +09:00
|
|
|
|
2024-04-23 13:14:06 +09:00
|
|
|
const fs = createFsProxy(__memfsExported)
|
2024-01-08 22:02:46 +09:00
|
|
|
|
|
|
|
const handler = new MessageHandler({
|
|
|
|
onLoad({ wasmModule, wasmMemory }) {
|
|
|
|
const wasi = new WASI({
|
|
|
|
fs,
|
2024-04-23 13:14:06 +09:00
|
|
|
preopens: {
|
|
|
|
'/': '/',
|
|
|
|
},
|
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)
|
|
|
|
}
|