From 1e8a122dddc977f6d35ada6e624c7b8dfa1f7087 Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Mon, 20 Dec 2021 12:17:19 -0600 Subject: [PATCH] wasm: fix odd WASI readline behavior. 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). --- impls/wasm/platform_wasi.wam | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/impls/wasm/platform_wasi.wam b/impls/wasm/platform_wasi.wam index e5d064c9..c52c0f2b 100644 --- a/impls/wasm/platform_wasi.wam +++ b/impls/wasm/platform_wasi.wam @@ -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 )