From f9c62af53ce98dcfc8dceb23bdd23bc7e01a0176 Mon Sep 17 00:00:00 2001 From: Volodymyr Shymanskyy Date: Fri, 30 Apr 2021 15:20:11 +0300 Subject: [PATCH] Update Cookbook.md --- docs/Cookbook.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/docs/Cookbook.md b/docs/Cookbook.md index 390f996..c3ea740 100644 --- a/docs/Cookbook.md +++ b/docs/Cookbook.md @@ -150,6 +150,44 @@ Limitations: - no support for `threads` and `atomics` - no support for `dynamic libraries` +### WAT WASI app + +Create `hello.wat`: +``` +(module + ;; wasi_unstable!fd_write(file_descriptor, *iovs, iovs_len, nwritten) -> status_code + (import "wasi_unstable" "fd_write" (func $fd_write (param i32 i32 i32 i32) (result i32))) + + (memory 1) + (export "memory" (memory 0)) + + ;; Put a message to linear memory at offset 32 + (data (i32.const 32) "Hello, world\n") + + (func $main (export "_start") + ;; Create a new io vector + (i32.store (i32.const 0) (i32.const 32)) ;; iov.iov_base - pointer to the start of the message + (i32.store (i32.const 4) (i32.const 13)) ;; iov.iov_len - length of the message + + (call $fd_write + (i32.const 1) ;; file_descriptor - 1 for stdout + (i32.const 0) ;; *iovs - pointer to the io vector + (i32.const 1) ;; iovs_len - count of items in io vector + (i32.const 20) ;; nwritten - where to store the number of bytes written + ) + drop ;; discard the WASI status code +) +``` + + +Build and run: +```sh +$ wat2wasm hello.wat -o hello.wasm + +$ wasm3 hello.wasm +Hello, world! +``` + ### WAT library Create `swap.wat`: