Skip to content

Commit 29c4b07

Browse files
guybedfordtargos
authored andcommitted
doc: update WASI example to use import.meta.url
PR-URL: #39925 Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 3588f07 commit 29c4b07

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

doc/api/wasi.md

+15-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ specification. WASI gives sandboxed WebAssembly applications access to the
1111
underlying operating system via a collection of POSIX-like functions.
1212

1313
```mjs
14-
import fs from 'fs';
14+
import { readFile } from 'fs/promises';
1515
import { WASI } from 'wasi';
1616
import { 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 };
2528
const 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+
);
2833
const instance = await WebAssembly.instantiate(wasm, importObject);
2934

3035
wasi.start(instance);
3136
```
3237
3338
```cjs
3439
'use strict';
35-
const fs = require('fs');
40+
const { readFile } = require('fs/promises');
3641
const { WASI } = require('wasi');
3742
const { argv, env } = require('process');
43+
const { join } = require('path');
3844

3945
const 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 };
4655
const 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

Comments
 (0)