@@ -11,7 +11,7 @@ specification. WASI gives sandboxed WebAssembly applications access to the
1111underlying operating system via a collection of POSIX-like functions.
1212
1313``` mjs
14- import fs from ' fs' ;
14+ import { readFile } from ' fs/promises ' ;
1515import { WASI } from ' wasi' ;
1616import { argv , env } from ' process' ;
1717
@@ -22,19 +22,25 @@ const wasi = new WASI({
2222 ' /sandbox' : ' /some/real/path/that/wasm/can/access'
2323 }
2424});
25+
26+ // Some WASI binaries require:
27+ // const importObject = { wasi_unstable: wasi.wasiImport };
2528const importObject = { wasi_snapshot_preview1: wasi .wasiImport };
2629
27- const wasm = await WebAssembly .compile (fs .readFileSync (' ./demo.wasm' ));
30+ const wasm = await WebAssembly .compile (
31+ await readFile (new URL (' ./demo.wasm' , import .meta.url))
32+ );
2833const instance = await WebAssembly .instantiate (wasm, importObject);
2934
3035wasi .start (instance);
3136` ` `
3237
3338` ` ` cjs
3439' use strict' ;
35- const fs = require (' fs' );
40+ const { readFile } = require (' fs/promises ' );
3641const { WASI } = require (' wasi' );
3742const { argv , env } = require (' process' );
43+ const { join } = require (' path' );
3844
3945const wasi = new WASI ({
4046 args: argv,
@@ -43,10 +49,15 @@ const wasi = new WASI({
4349 ' /sandbox' : ' /some/real/path/that/wasm/can/access'
4450 }
4551});
52+
53+ // Some WASI binaries require:
54+ // const importObject = { wasi_unstable: wasi.wasiImport };
4655const importObject = { wasi_snapshot_preview1: wasi .wasiImport };
4756
4857(async () => {
49- const wasm = await WebAssembly .compile (fs .readFileSync (' ./demo.wasm' ));
58+ const wasm = await WebAssembly .compile (
59+ await readFile (join (__dirname , ' demo.wasm' ))
60+ );
5061 const instance = await WebAssembly .instantiate (wasm, importObject);
5162
5263 wasi .start (instance);
0 commit comments