Skip to content

Commit

Permalink
USE node.js WASI module
Browse files Browse the repository at this point in the history
import object:
    * v19 or later: [wasi.getImportObject()](https://nodejs.dev/en/api/v19/wasi/#wasigetimportobject)
    * otherwise: [wasi.wasiImport](https://nodejs.org/dist/latest-v13.x/docs/api/wasi.html#wasi_wasi_wasiimport)
  • Loading branch information
tyfkda committed Mar 3, 2024
1 parent 3777663 commit 7518880
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions tool/runwasi.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

const fs = require('fs')
const fsPromises = require('fs').promises
const path = require('path')
const { WASI } = require('@wasmer/wasi')
const { WASI } = require('wasi')

async function getRealpaths(map) {
const promises = Object.keys(map).map(async key => {
Expand Down Expand Up @@ -48,22 +47,20 @@ async function getRealpaths(map) {

const wasmFileName = program.args[0]
const wasi = new WASI({
version: 'preview1',
args: program.args,
env: process.env,
bindings: {
...WASI.defaultBindings,
fs,
path,
},
preopens: await getRealpaths(preopens),
})

try {
const wasmBin = await fsPromises.readFile(wasmFileName)
const wasmModule = await WebAssembly.compile(wasmBin)
const importObject = Object.assign({}, wasi.getImports(wasmModule))
const importObject = wasi.getImportObject?.call(wasi) ??
{ wasi_snapshot_preview1: wasi.wasiImport }
const instance = await WebAssembly.instantiate(wasmModule, importObject)
wasi.start(instance)
const result = wasi.start(instance)
process.exit(result)
} catch (e) {
console.error(e)
process.exit(1)
Expand Down

0 comments on commit 7518880

Please sign in to comment.