| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 75107e2 commit 410b233
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,15 +22,54 @@ const wasi = new WASI({ | |||
| 22 | 22 | const importObject = { wasi_snapshot_preview1: wasi.wasiImport }; | |
| 23 | 23 | ||
| 24 | 24 | (async () => { | |
| 25 | - const wasm = await WebAssembly.compile(fs.readFileSync('./binary.wasm')); | ||
| 25 | + const wasm = await WebAssembly.compile(fs.readFileSync('./demo.wasm')); | ||
| 26 | 26 | const instance = await WebAssembly.instantiate(wasm, importObject); | |
| 27 | 27 | ||
| 28 | 28 | wasi.start(instance); | |
| 29 | 29 | })(); | |
| 30 | 30 | ``` | |
| 31 | 31 | ||
| 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 | + | ||
| 32 | 71 | 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. | ||
| 34 | 73 | ||
| 35 | 74 | ## Class: `WASI` | |
| 36 | 75 | <!-- YAML | |
| Back | FazBrowse Home | New Git URL |
0 commit comments