Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions ext/src/ruby_api/wasi_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -436,6 +425,11 @@ impl WasiConfig {
}
}

pub fn file_r(path: RString) -> Result<File, Error> {
// 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<File, Error> {
// SAFETY: &str copied before calling in to Ruby, no GC can happen before.
File::create(unsafe { path.as_str()? })
Expand Down
Loading