add wasmtime adapter and stdio wrappers

the stdio wrappers will not work on windows, but thats a whole other can
of worms anyway
This commit is contained in:
Pat Hickey
2020-12-11 15:00:19 -08:00
parent 73058658f4
commit 22db10e643
8 changed files with 238 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
use crate::dir::{DirCaps, DirEntry, WasiDir};
use crate::file::{FileCaps, FileEntry, WasiFile};
use crate::table::Table;
use crate::Error;
use std::cell::{RefCell, RefMut};
use std::path::PathBuf;
use std::rc::Rc;
@@ -10,6 +11,10 @@ pub struct WasiCtx {
}
impl WasiCtx {
pub fn builder() -> WasiCtxBuilder {
WasiCtxBuilder(WasiCtx::new())
}
pub fn new() -> Self {
WasiCtx {
table: Rc::new(RefCell::new(Table::new())),
@@ -52,3 +57,36 @@ impl WasiCtx {
self.table.borrow_mut()
}
}
pub struct WasiCtxBuilder(WasiCtx);
impl WasiCtxBuilder {
pub fn build(self) -> Result<WasiCtx, Error> {
Ok(self.0)
}
pub fn arg(&mut self, _arg: &str) -> &mut Self {
// Intentionally left blank. We do not handle arguments yet.
self
}
pub fn inherit_stdio(&mut self) -> &mut Self {
self.0.insert_file(
0,
Box::new(crate::stdio::stdin()),
FileCaps::READ,
FileCaps::READ,
);
self.0.insert_file(
1,
Box::new(crate::stdio::stdout()),
FileCaps::WRITE,
FileCaps::WRITE,
);
self.0.insert_file(
2,
Box::new(crate::stdio::stderr()),
FileCaps::WRITE,
FileCaps::WRITE,
);
self
}
}

View File

@@ -5,7 +5,10 @@ mod dir;
mod error;
mod file;
pub mod snapshots;
pub mod stdio;
pub mod table;
pub use ctx::WasiCtx;
pub use dir::{DirCaps, WasiDir};
pub use error::Error;
pub use file::{FileCaps, WasiFile};

124
crates/wasi-c2/src/stdio.rs Normal file
View File

@@ -0,0 +1,124 @@
use crate::file::{FdFlags, Filestat, Filetype, OFlags, WasiFile};
use crate::Error;
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, RawFd};
pub struct Stdin(std::io::Stdin);
pub fn stdin() -> Stdin {
Stdin(std::io::stdin())
}
#[cfg(unix)]
impl AsRawFd for Stdin {
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}
}
impl WasiFile for Stdin {
fn datasync(&self) -> Result<(), Error> {
Ok(())
}
fn sync(&self) -> Result<(), Error> {
Ok(())
}
fn get_filetype(&self) -> Result<Filetype, Error> {
Ok(Filetype::CharacterDevice)
}
fn get_fdflags(&self) -> Result<FdFlags, Error> {
todo!()
}
fn get_oflags(&self) -> Result<OFlags, Error> {
todo!()
}
fn set_oflags(&self, _flags: OFlags) -> Result<(), Error> {
todo!()
}
fn get_filestat(&self) -> Result<Filestat, Error> {
todo!()
}
fn set_filestat_size(&self, _size: u64) -> Result<(), Error> {
todo!()
}
}
pub struct Stdout(std::io::Stdout);
pub fn stdout() -> Stdout {
Stdout(std::io::stdout())
}
#[cfg(unix)]
impl AsRawFd for Stdout {
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}
}
impl WasiFile for Stdout {
fn datasync(&self) -> Result<(), Error> {
Ok(())
}
fn sync(&self) -> Result<(), Error> {
Ok(())
}
fn get_filetype(&self) -> Result<Filetype, Error> {
Ok(Filetype::CharacterDevice)
}
fn get_fdflags(&self) -> Result<FdFlags, Error> {
todo!()
}
fn get_oflags(&self) -> Result<OFlags, Error> {
todo!()
}
fn set_oflags(&self, _flags: OFlags) -> Result<(), Error> {
todo!()
}
fn get_filestat(&self) -> Result<Filestat, Error> {
todo!()
}
fn set_filestat_size(&self, _size: u64) -> Result<(), Error> {
todo!()
}
}
pub struct Stderr(std::io::Stderr);
pub fn stderr() -> Stderr {
Stderr(std::io::stderr())
}
#[cfg(unix)]
impl AsRawFd for Stderr {
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}
}
impl WasiFile for Stderr {
fn datasync(&self) -> Result<(), Error> {
Ok(())
}
fn sync(&self) -> Result<(), Error> {
Ok(())
}
fn get_filetype(&self) -> Result<Filetype, Error> {
Ok(Filetype::CharacterDevice)
}
fn get_fdflags(&self) -> Result<FdFlags, Error> {
todo!()
}
fn get_oflags(&self) -> Result<OFlags, Error> {
todo!()
}
fn set_oflags(&self, _flags: OFlags) -> Result<(), Error> {
todo!()
}
fn get_filestat(&self) -> Result<Filestat, Error> {
todo!()
}
fn set_filestat_size(&self, _size: u64) -> Result<(), Error> {
todo!()
}
}