Skip to content

Commit f5ec98c

Browse files
pragyandasaddaleax
authored andcommitted
doc: update wasi code sample
- Code sample updated by adding a hello-world (`demo.wat`) code example - Step for compiling `.wat` to `.wasm` added (with reference to `wabt` tools) - The sample code prints "hello world\n" in the console Note: This update adds a very minimal change to the existing sample and can be treated as an extension. PR-URL: #33626 Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 646e5a4 commit f5ec98c

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

doc/api/wasi.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,51 @@ const wasi = new WASI({
2222
const importObject = { wasi_snapshot_preview1: wasi.wasiImport };
2323

2424
(async () => {
25-
const wasm = await WebAssembly.compile(fs.readFileSync('./binary.wasm'));
25+
const wasm = await WebAssembly.compile(fs.readFileSync('./demo.wasm'));
2626
const instance = await WebAssembly.instantiate(wasm, importObject);
2727

2828
wasi.start(instance);
2929
})();
3030
```
3131

32+
To run the above example, create a new WebAssembly text format file i.e. `demo.wat`
33+
```wat
34+
(module
35+
;; Import the required fd_write WASI function which will write the given io vectors to stdout
36+
;; The function signature for fd_write is:
37+
;; (File Descriptor, *iovs, iovs_len, nwritten) -> Returns number of bytes written
38+
(import "wasi_snapshot_preview1" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32)))
39+
40+
(memory 1)
41+
(export "memory" (memory 0))
42+
43+
;; Write 'hello world\n' to memory at an offset of 8 bytes
44+
;; Note the trailing newline which is required for the text to appear
45+
(data (i32.const 8) "hello world\n")
46+
47+
(func $main (export "_start")
48+
;; Creating a new io vector within linear memory
49+
(i32.store (i32.const 0) (i32.const 8)) ;; iov.iov_base - This is a pointer to the start of the 'hello world\n' string
50+
(i32.store (i32.const 4) (i32.const 12)) ;; iov.iov_len - The length of the 'hello world\n' string
51+
52+
(call $fd_write
53+
(i32.const 1) ;; file_descriptor - 1 for stdout
54+
(i32.const 0) ;; *iovs - The pointer to the iov array, which is stored at memory location 0
55+
(i32.const 1) ;; iovs_len - We're printing 1 string stored in an iov - so one.
56+
(i32.const 20) ;; nwritten - A place in memory to store the number of bytes written
57+
)
58+
drop ;; Discard the number of bytes written from the top of the stack
59+
)
60+
)
61+
```
62+
Use [wabt](https://github.com/WebAssembly/wabt) to compile `.wat` to `.wasm`
63+
64+
```sh
65+
$ wat2wasm demo.wat
66+
```
67+
3268
The `--experimental-wasi-unstable-preview1` and `--experimental-wasm-bigint`
33-
CLI arguments are needed for the previous example to run.
69+
CLI arguments are needed for this example to run.
3470

3571
## Class: `WASI`
3672
<!-- YAML

0 commit comments

Comments
 (0)