From 8d2cd3afd7b039a3ca8ee3a63c64f912f44d3846 Mon Sep 17 00:00:00 2001 From: Jeff Charles Date: Fri, 14 Nov 2025 17:17:13 -0500 Subject: [PATCH] Use InputFile for reading from stdin --- ext/src/ruby_api/wasi_config.rs | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/ext/src/ruby_api/wasi_config.rs b/ext/src/ruby_api/wasi_config.rs index 46392321..6d907bd6 100644 --- a/ext/src/ruby_api/wasi_config.rs +++ b/ext/src/ruby_api/wasi_config.rs @@ -14,7 +14,7 @@ use std::convert::TryFrom; use std::fs; use std::path::Path; use std::{fs::File, path::PathBuf}; -use wasmtime_wasi::cli::OutputFile; +use wasmtime_wasi::cli::{InputFile, OutputFile}; use wasmtime_wasi::p1::WasiP1Ctx; use wasmtime_wasi::p2::pipe::MemoryInputPipe; use wasmtime_wasi::{DirPerms, FilePerms, WasiCtx, WasiCtxBuilder}; @@ -352,18 +352,7 @@ impl WasiConfig { match stdin { ReadStream::Inherit => builder.inherit_stdin(), ReadStream::Path(path) => { - // Reading the whole file into memory and passing it as an - // in-memory buffer because I cannot find a public struct - // to use a file as an input that implements `StdinStream` - // and the implementation would not be trivial. - // TODO: Use - // https://github.com/bytecodealliance/wasmtime/pull/10968 - // when it's in a published version. - // SAFETY: &[u8] copied before calling in to Ruby, no GC can happen before. - let inner = ruby.get_inner(*path); - let path = PathBuf::from(unsafe { inner.as_str() }?); - let contents = fs::read(path).map_err(|e| error!("{e}"))?; - builder.stdin(MemoryInputPipe::new(contents)) + builder.stdin(file_r(ruby.get_inner(*path)).map(InputFile::new)?) } ReadStream::String(input) => { // SAFETY: &[u8] copied before calling in to Ruby, no GC can happen before. @@ -436,6 +425,11 @@ impl WasiConfig { } } +pub fn file_r(path: RString) -> Result { + // SAFETY: &str copied before calling in to Ruby, no GC can happen before. + File::open(unsafe { path.as_str()? }).map_err(|e| error!("Failed to open file {}\n{}", path, e)) +} + pub fn file_w(path: RString) -> Result { // SAFETY: &str copied before calling in to Ruby, no GC can happen before. File::create(unsafe { path.as_str()? })