wa-lang.org/wazero@v1.0.2/cmd/wazero/testdata/wasi_arg.wat (about)

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