wasmrt 5.0.0

wasmrt — a fast, small WebAssembly runtime CLI (run/assemble/validate/pin).
//! A WASI command reads its stdin (letter C56, rsxtk, 2026-11-30; 4.1.0). Before, the guest's stdin was
//! an empty buffer the CLI never filled, so `echo hi | wasmrt wasi echo.wat` printed nothing, exit 0,
//! where wasmtime and wazmrt print `hi`. Driven through the real CLI: the host's stdin is the subject.

use std::io::Write as _;
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};

/// Reads up to 100 bytes from fd 0 with ONE `fd_read`, writes them to fd 1.
const ECHO: &str = r#"(module
  (import "wasi_snapshot_preview1" "fd_read" (func $r (param i32 i32 i32 i32) (result i32)))
  (import "wasi_snapshot_preview1" "fd_write" (func $w (param i32 i32 i32 i32) (result i32)))
  (memory (export "memory") 1)
  (func (export "_start")
    (i32.store (i32.const 0) (i32.const 64)) (i32.store (i32.const 4) (i32.const 100))
    (drop (call $r (i32.const 0) (i32.const 0) (i32.const 1) (i32.const 8)))
    (i32.store (i32.const 4) (i32.load (i32.const 8)))
    (drop (call $w (i32.const 1) (i32.const 0) (i32.const 1) (i32.const 12)))))"#;

fn fixture() -> String {
    let d = std::path::Path::new(env!("CARGO_TARGET_TMPDIR")).join(format!("wasmrt_wasi_stdin_{}", std::process::id()));
    std::fs::create_dir_all(&d).expect("scratch dir");
    let p = d.join("echo.wat");
    std::fs::write(&p, ECHO).expect("write fixture");
    p.to_str().unwrap().to_string()
}

fn spawn(path: &str) -> std::process::Child {
    Command::new(env!("CARGO_BIN_EXE_wasmrt"))
        .args(["wasi", path])
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("spawn wasmrt")
}

/// Inverted (the CLI without `inherit_stdin()`), stdout is empty and this fails.
#[test]
fn a_wasi_command_reads_its_piped_stdin() {
    let path = fixture();
    let mut child = spawn(&path);
    child.stdin.take().unwrap().write_all(b"hi from stdin\n").unwrap(); // dropped: EOF after it
    let out = child.wait_with_output().unwrap();
    assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
    assert_eq!(out.stdout, b"hi from stdin\n");
}

/// Read ON DEMAND, not to end of file first: with the pipe still open, the guest gets the line that
/// is there and finishes — an interactive program would otherwise wait for input that is not coming.
#[test]
fn stdin_is_read_on_demand_not_to_end_of_file() {
    let path = fixture();
    let mut child = spawn(&path);
    let mut stdin = child.stdin.take().unwrap();
    stdin.write_all(b"first line\n").unwrap();
    stdin.flush().unwrap();
    let started = Instant::now();
    // Keep the pipe OPEN while waiting for the guest to finish on its own.
    let status = loop {
        if let Some(s) = child.try_wait().unwrap() {
            break s;
        }
        if started.elapsed() > Duration::from_secs(20) {
            let _ = child.kill();
            panic!("the guest waited for end of file instead of reading the line it had");
        }
        std::thread::sleep(Duration::from_millis(10));
    };
    drop(stdin);
    let out = child.wait_with_output().unwrap();
    assert!(status.success(), "{}", String::from_utf8_lossy(&out.stderr));
    assert_eq!(out.stdout, b"first line\n");
}

/// An empty stdin is end of file: nothing read, nothing written, exit 0.
#[test]
fn an_empty_stdin_is_end_of_file() {
    let path = fixture();
    let child = spawn(&path);
    let out = child.wait_with_output().unwrap(); // stdin's pipe dropped unwritten by wait_with_output
    assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
    assert!(out.stdout.is_empty());
}