@@ -11,7 +11,7 @@ specification. WASI gives sandboxed WebAssembly applications access to the
11
11
underlying operating system via a collection of POSIX-like functions.
12
12
13
13
``` mjs
14
- import fs from ' fs' ;
14
+ import { readFile } from ' fs/promises ' ;
15
15
import { WASI } from ' wasi' ;
16
16
import { argv , env } from ' process' ;
17
17
@@ -22,19 +22,25 @@ const wasi = new WASI({
22
22
' /sandbox' : ' /some/real/path/that/wasm/can/access'
23
23
}
24
24
});
25
+
26
+ // Some WASI binaries require:
27
+ // const importObject = { wasi_unstable: wasi.wasiImport };
25
28
const importObject = { wasi_snapshot_preview1: wasi .wasiImport };
26
29
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
+ );
28
33
const instance = await WebAssembly .instantiate (wasm, importObject);
29
34
30
35
wasi .start (instance);
31
36
` ` `
32
37
33
38
` ` ` cjs
34
39
' use strict' ;
35
- const fs = require (' fs' );
40
+ const { readFile } = require (' fs/promises ' );
36
41
const { WASI } = require (' wasi' );
37
42
const { argv , env } = require (' process' );
43
+ const { join } = require (' path' );
38
44
39
45
const wasi = new WASI ({
40
46
args: argv,
@@ -43,10 +49,15 @@ const wasi = new WASI({
43
49
' /sandbox' : ' /some/real/path/that/wasm/can/access'
44
50
}
45
51
});
52
+
53
+ // Some WASI binaries require:
54
+ // const importObject = { wasi_unstable: wasi.wasiImport };
46
55
const importObject = { wasi_snapshot_preview1: wasi .wasiImport };
47
56
48
57
(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
+ );
50
61
const instance = await WebAssembly .instantiate (wasm, importObject);
51
62
52
63
wasi .start (instance);
0 commit comments