| 1 |
;; $wasi_fd is a WASI command which reads from bear.txt |
| 2 |
(module $wasi_fd |
| 3 |
;; path_open returns a file descriptor to a path |
| 4 |
;; |
| 5 |
;; See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-args_sizes_get---errno-size-size |
| 6 |
(import "wasi_snapshot_preview1" "path_open" |
| 7 |
(func $wasi.path_open (param $fd i32) (param $dirflags i32) (param $path i32) (param $path_len i32) (param $oflags i32) (param $fs_rights_base i64) (param $fs_rights_inheriting i64) (param $fdflags i32) (param $result.opened_fd i32) (result (;errno;) i32))) |
| 8 |
|
| 9 |
;; fd_read reads bytes from a file descriptor. |
| 10 |
;; |
| 11 |
;; See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#fd_read |
| 12 |
(import "wasi_snapshot_preview1" "fd_read" |
| 13 |
(func $wasi.fd_read (param $fd i32) (param $iovs i32) (param $iovs_len i32) (param $result.size 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 descriptor. |
| 28 |
;; $main will only write one offset/length pair, corresponding to a read buffer |
| 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 |
;; First open the path |
| 39 |
(call $wasi.path_open |
| 40 |
(i32.const 3) ;; fd of fs root |
| 41 |
(i32.const 0) ;; ignore $dirflags |
| 42 |
(i32.const 5000) ;; path is in data |
| 43 |
(i32.const 8) ;; path len |
| 44 |
(i32.const 0) ;; ignore $oflags |
| 45 |
(i64.const 0) ;; ignore $fs_rights_base |
| 46 |
(i64.const 0) ;; ignore $fs_rights_inheriting |
| 47 |
(i32.const 0) ;; ignore $fdflags |
| 48 |
(i32.const 0) ;; write result fd to memory offset 0 |
| 49 |
) |
| 50 |
drop ;; ignore the errno returned |
| 51 |
|
| 52 |
;; set iovs to a 50 byte read buffer at offset 100 |
| 53 |
(i32.store (global.get $iovs) (i32.const 100)) |
| 54 |
(i32.store (i32.add (global.get $iovs) (i32.const 4)) (i32.const 50)) |
| 55 |
|
| 56 |
(call $wasi.fd_read |
| 57 |
(i32.load (i32.const 0)) ;; load from offset 0 which has fd of file |
| 58 |
(global.get $iovs) ;; $iovs is the start offset of the IO vectors to read into. |
| 59 |
(i32.const 1) ;; $iovs_len is the count of offset/length pairs to read from iovs |
| 60 |
(i32.add (global.get $iovs) (i32.const 4)) ;; set number of bytes read as buffer length for fd_write below |
| 61 |
) |
| 62 |
drop ;; ignore the errno returned |
| 63 |
|
| 64 |
;; Finally, write the memory region to the file. |
| 65 |
(call $wasi.fd_write |
| 66 |
(i32.const 1) ;; $fd is a file descriptor and 1 is stdout (console). |
| 67 |
(global.get $iovs) ;; $iovs is the start offset of the IO vectors to copy. |
| 68 |
(i32.const 1) ;; $iovs_len is the count of offset/length pairs to copy to memory. |
| 69 |
(global.get $ignored) ;; ignore $result.size as we aren't verifying it. |
| 70 |
) |
| 71 |
drop ;; ignore the errno returned |
| 72 |
) |
| 73 |
(data $.rodata (i32.const 5000) "bear.txt") |
| 74 |
) |