Created
March 2, 2020 19:00
-
-
Save subzey/6d6b13138337fb64216fbf922f617c2c to your computer and use it in GitHub Desktop.
Writes to stdout what it gets from stdin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(module | |
;; Expects a https://wasi.dev/ interface | |
;; Run this module with https://wasmer.io/ or https://wasmtime.dev/ | |
(func $fd_write (import "wasi_unstable" "fd_write") (param i32 i32 i32 i32) (result i32)) | |
(func $fd_read (import "wasi_unstable" "fd_read") (param i32 i32 i32 i32) (result i32)) | |
(func $proc_exit (import "wasi_unstable" "proc_exit") (param i32)) | |
(memory (export "memory") 1) | |
(func (export "_start") | |
(i32.store (i32.const 0x8000) (i32.const 0)) ;; iov.iov_base | |
(loop $loopcont | |
(i32.store (i32.const 0x8004) (i32.const 0x8000)) ;; iov.iov_len | |
(if | |
(call $fd_read | |
(i32.const 0) ;; fd, 0 = stdin | |
(i32.const 0x8000) (i32.const 1) ;; *iovs, iovs_len | |
(i32.const 0x8004) ;; nread | |
) | |
(then | |
(call $proc_exit (i32.const 1)) ;; exit with error code | |
) | |
) | |
(if | |
(i32.load (i32.const 0x8004)) ;; some nonzero count of bytes read | |
(then | |
(if | |
(call $fd_write | |
(i32.const 1) ;; fd, 1 = stdout | |
(i32.const 0x8000) (i32.const 1) ;; *iovs, iovs_len | |
(i32.const 0x8004) ;; nwritten | |
) | |
(then | |
(call $proc_exit (i32.const 1)) ;; exit with error code | |
) | |
) | |
(br $loopcont) ;; continue | |
) | |
) | |
) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment