github.com/wasilibs/wazerox@v0.0.0-20240124024944-4923be63ab5f/imports/wasi_snapshot_preview1/testdata/print_args.wat (about) 1 ;; $print_args is a WASI command which copies null-terminated args to stdout. 2 (module $print_args 3 ;; args_get reads command-line argument data. 4 ;; 5 ;; See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-args_getargv-pointerpointeru8-argv_buf-pointeru8---errno 6 (import "wasi_snapshot_preview1" "args_get" 7 (func $wasi.args_get (param $argv i32) (param $argv_buf i32) (result (;errno;) i32))) 8 9 ;; args_sizes_get returns command-line argument data sizes. 10 ;; 11 ;; See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-args_sizes_get---errno-size-size 12 (import "wasi_snapshot_preview1" "args_sizes_get" 13 (func $wasi.args_sizes_get (param $result.argc i32) (param $result.argv_len i32) (result (;errno;) i32))) 14 15 ;; fd_write write bytes to a file descriptor. 16 ;; 17 ;; See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#fd_write 18 (import "wasi_snapshot_preview1" "fd_write" 19 (func $wasi.fd_write (param $fd i32) (param $iovs i32) (param $iovs_len i32) (param $result.size i32) (result (;errno;) i32))) 20 21 ;; WASI commands are required to export "memory". Particularly, imported functions mutate this. 22 ;; 23 ;; Note: 1 is the size in pages (64KB), not bytes! 24 ;; See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#memories%E2%91%A7 25 (memory (export "memory") 1) 26 27 ;; $iovs are offset/length pairs in memory fd_write copies to the file 28 ;; descriptor. $main will only write one offset/length pair. 29 (global $iovs i32 i32.const 1024) ;; 1024 is an arbitrary offset 30 31 ;; WASI parameters are usually memory offsets, you can ignore values by writing them to an unread offset. 32 (global $ignored i32 i32.const 32768) 33 34 ;; _start is a special function defined by a WASI Command that runs like a main function would. 35 ;; 36 ;; See https://github.com/WebAssembly/WASI/blob/snapshot-01/design/application-abi.md#current-unstable-abi 37 (func $main (export "_start") 38 ;; To copy an argument to a file, we first need to load it into memory. 39 (call $wasi.args_get 40 (global.get $ignored) ;; ignore $argv as we only read the argv_buf 41 (i32.const 0) ;; Write $argv_buf (null-terminated args) to memory offset zero. 42 ) 43 drop ;; ignore the errno returned 44 45 ;; Next, we need to know how many bytes were loaded, as that's how much we'll copy to the file. 46 (call $wasi.args_sizes_get 47 (global.get $ignored) ;; ignore $result.argc as we only read the argv_buf. 48 (i32.add (global.get $iovs) (i32.const 4)) ;; store $result.argv_len as the length to copy 49 ) 50 drop ;; ignore the errno returned 51 52 ;; Finally, write the memory region to the file. 53 (call $wasi.fd_write 54 (i32.const 1) ;; $fd is a file descriptor and 1 is stdout (console). 55 (global.get $iovs) ;; $iovs is the start offset of the IO vectors to copy. 56 (i32.const 1) ;; $iovs_len is the count of offset/length pairs to copy to memory. 57 (global.get $ignored) ;; ignore $result.size as we aren't verifying it. 58 ) 59 drop ;; ignore the errno returned 60 ) 61 )