github.com/wasilibs/wazerox@v0.0.0-20240124024944-4923be63ab5f/imports/wasi_snapshot_preview1/testdata/print_prestat_dirname.wat (about)

     1  ;; $$print_prestat_dirname is a WASI command which copies the first preopen dirname to stdout.
     2  (module $print_prestat_dirname
     3      (import "wasi_snapshot_preview1" "fd_prestat_get"
     4          (func $wasi.fd_prestat_get (param $fd i32) (param $result.prestat i32) (result (;errno;) i32)))
     5  
     6      (import "wasi_snapshot_preview1" "fd_prestat_dir_name"
     7          (func $wasi.fd_prestat_dir_name (param $fd i32) (param $result.path i32)  (param $result.path_len i32) (result (;errno;) i32)))
     8  
     9      (import "wasi_snapshot_preview1" "fd_write"
    10          (func $wasi.fd_write (param $fd i32) (param $iovs i32) (param $iovs_len i32) (param $result.size i32) (result (;errno;) i32)))
    11  
    12      (memory (export "memory") 1 1)
    13  
    14      (func $main (export "_start")
    15          ;; First, we need to know the size of the prestat dir name.
    16          (call $wasi.fd_prestat_get
    17              (i32.const 3) ;; preopen FD
    18              (i32.const 0) ;; where to write prestat
    19          )
    20          drop ;; ignore the errno returned
    21  
    22          ;; Next, write the dir name to offset 8 (past the prestat).
    23          (call $wasi.fd_prestat_dir_name
    24              (i32.const 3) ;; preopen FD
    25              (i32.const 8) ;; where to write dir_name
    26              (i32.load (i32.const 4)) ;; length is the last part of the prestat
    27          )
    28          drop ;; ignore the errno returned
    29  
    30          ;; Now, convert the prestat to an iovec [offset, len] writing offset=8.
    31          (i32.store (i32.const 0) (i32.const 8))
    32  
    33          ;; Finally, write the dirname to stdout via its iovec [offset, len].
    34          (call $wasi.fd_write
    35              (i32.const 1) ;; stdout
    36              (i32.const 0) ;; where's the iovec
    37              (i32.const 1) ;; only one iovec
    38              (i32.const 0) ;; overwrite the iovec with the ignored result.
    39          )
    40          drop ;; ignore the errno returned
    41      )
    42  )