-
Notifications
You must be signed in to change notification settings - Fork 844
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #217 from wasmerio/feature/wasi-for-the-wasm-c-api
Implement WASI for the Wasm C API
- Loading branch information
Showing
15 changed files
with
870 additions
and
35 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#[macro_export] | ||
macro_rules! c_try { | ||
($expr:expr) => {{ | ||
let res: Result<_, _> = $expr; | ||
match res { | ||
Ok(val) => val, | ||
Err(err) => { | ||
crate::error::update_last_error(err); | ||
return None; | ||
} | ||
} | ||
}}; | ||
($expr:expr, $e:expr) => {{ | ||
let opt: Option<_> = $expr; | ||
c_try!(opt.ok_or_else(|| $e)) | ||
}}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
//! Default implementations for capturing the stdout/stderr output of a WASI program. | ||
use serde::{Deserialize, Serialize}; | ||
use std::collections::VecDeque; | ||
use std::io::{self, Read, Seek, Write}; | ||
use wasmer_wasi::{WasiFile, WasiFsError}; | ||
|
||
/// For capturing stdout/stderr. Stores all output in a string. | ||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct OutputCapturer { | ||
pub(crate) buffer: VecDeque<u8>, | ||
} | ||
|
||
impl OutputCapturer { | ||
pub fn new() -> Self { | ||
Self { | ||
buffer: VecDeque::new(), | ||
} | ||
} | ||
} | ||
|
||
#[typetag::serde] | ||
impl WasiFile for OutputCapturer { | ||
fn last_accessed(&self) -> u64 { | ||
0 | ||
} | ||
fn last_modified(&self) -> u64 { | ||
0 | ||
} | ||
fn created_time(&self) -> u64 { | ||
0 | ||
} | ||
fn size(&self) -> u64 { | ||
0 | ||
} | ||
fn set_len(&mut self, _len: u64) -> Result<(), WasiFsError> { | ||
Ok(()) | ||
} | ||
fn unlink(&mut self) -> Result<(), WasiFsError> { | ||
Ok(()) | ||
} | ||
fn bytes_available(&self) -> Result<usize, WasiFsError> { | ||
// return an arbitrary amount | ||
Ok(1024) | ||
} | ||
} | ||
|
||
// fail when reading or Seeking | ||
impl Read for OutputCapturer { | ||
fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> { | ||
Err(io::Error::new( | ||
io::ErrorKind::Other, | ||
"can not read from capturing stdout", | ||
)) | ||
} | ||
fn read_to_end(&mut self, _buf: &mut Vec<u8>) -> std::io::Result<usize> { | ||
Err(std::io::Error::new( | ||
std::io::ErrorKind::Other, | ||
"can not read from capturing stdout", | ||
)) | ||
} | ||
fn read_to_string(&mut self, _buf: &mut String) -> io::Result<usize> { | ||
Err(io::Error::new( | ||
io::ErrorKind::Other, | ||
"can not read from capturing stdout", | ||
)) | ||
} | ||
fn read_exact(&mut self, _buf: &mut [u8]) -> io::Result<()> { | ||
Err(io::Error::new( | ||
io::ErrorKind::Other, | ||
"can not read from capturing stdout", | ||
)) | ||
} | ||
} | ||
impl Seek for OutputCapturer { | ||
fn seek(&mut self, _pos: io::SeekFrom) -> io::Result<u64> { | ||
Err(io::Error::new( | ||
io::ErrorKind::Other, | ||
"can not seek capturing stdout", | ||
)) | ||
} | ||
} | ||
impl Write for OutputCapturer { | ||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { | ||
self.buffer.extend(buf); | ||
Ok(buf.len()) | ||
} | ||
fn flush(&mut self) -> io::Result<()> { | ||
Ok(()) | ||
} | ||
fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { | ||
self.buffer.extend(buf); | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.