Skip to content

Commit 410b233

Browse files
pragyandascodebytere
authored andcommitted
doc: update WASM 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 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 <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 75107e2 commit 410b233

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

doc/api/wasi.md

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

3574
## Class: `WASI`
3675
<!-- YAML

0 commit comments

Comments
 (0)