actually empty ready bytes of stdin

This commit is contained in:
Pat Hickey
2021-02-01 14:43:32 -08:00
parent b9a3f8694d
commit 0c4aec391e
3 changed files with 15 additions and 1 deletions

View File

@@ -23,6 +23,7 @@ os_pipe = "0.9"
anyhow = "1.0.19"
wat = "1.0.23"
cap-std = "0.12"
system-interface = "0.5.4"
[features]
test_programs = []

View File

@@ -1,4 +1,5 @@
use anyhow::Context;
use std::convert::TryInto;
use std::path::Path;
use wasi_cap_std_sync::WasiCtxBuilder;
use wasi_common::pipe::{ReadPipe, WritePipe};
@@ -90,7 +91,18 @@ pub fn instantiate_inherit_stdio(
// Tests assume that stdin does not have any bytes available to read. Make sure this is the
// case, regardless of the test environment:
let _ = std::io::stdin().read_to_end().expect("read stdin to end");
use std::io::Read;
use system_interface::io::ReadReady;
let nbytes = std::io::stdin()
.num_ready_bytes()
.expect("get stdin's ready bytes");
if nbytes > 0 {
let mut stdin_contents = Vec::new();
stdin_contents.resize(nbytes.try_into().expect("ready bytes fits in memory"), 0);
std::io::stdin()
.read(stdin_contents.as_mut_slice())
.expect("read stdin to end");
}
// Create our wasi context.
// Additionally register any preopened directories if we have them.