Skip to content

Commit

Permalink
wasm: fix odd WASI readline behavior.
Browse files Browse the repository at this point in the history
Most of the time, the WASI fd_read call returns the whole line including
the newline. Other times, it returns everything except the newline. So
before stompiing the newline, check for that condition. This fixes the
WASI based wasm implementations (wasmtime, wasmer, lucet).
  • Loading branch information
kanaka committed Dec 20, 2021
1 parent 58a6054 commit 1e8a122
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion impls/wasm/platform_wasi.wam
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@
(if (i32.le_s (i32.load $nread_ptr) 0)
(return 0))
;; Replace ending newline with NULL
(i32.store8 (i32.add $buf (i32.sub (i32.load $nread_ptr) 1)) 0)
;; NOTE: oddly, there isn't always a newline so check first
;; Specifically, this input chops too much:
;; (abcd abcdefg (abc (n) (if (> n 0) (+ n (abcdefg (- n 1))) 0)))
(if (i32.eq (CHR "\n")
(i32.load8_u (i32.add $buf (i32.sub (i32.load $nread_ptr) 1)) 0))
(i32.store8 (i32.add $buf (i32.sub (i32.load $nread_ptr) 1)) 0))
1
)

Expand Down

0 comments on commit 1e8a122

Please sign in to comment.