Skip to content

Instantly share code, notes, and snippets.

@subzey
Created March 2, 2020 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save subzey/6d6b13138337fb64216fbf922f617c2c to your computer and use it in GitHub Desktop.
Save subzey/6d6b13138337fb64216fbf922f617c2c to your computer and use it in GitHub Desktop.
Writes to stdout what it gets from stdin
(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