Skip to content

Commit

Permalink
wasi: Implement fd_write for stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
benediktwerner committed Jul 26, 2019
1 parent 624d487 commit 31bf702
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
16 changes: 13 additions & 3 deletions src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,15 +340,15 @@ impl VM {
&mut self.memory
}

fn push(&mut self, val: Value) -> VMResult<()> {
pub(crate) fn push(&mut self, val: Value) -> VMResult<()> {
if self.value_stack.len() >= VALUE_STACK_LIMIT {
return Err(Trap::ValueStackOverflow);
}
self.value_stack.push(val);
Ok(())
}

fn pop(&mut self) -> VMResult<Value> {
pub(crate) fn pop(&mut self) -> VMResult<Value> {
self.value_stack.pop().ok_or(Trap::PopFromEmptyStack)
}

Expand Down Expand Up @@ -651,7 +651,17 @@ impl VM {
let func = self.module.get_func(self.ip.func_index).unwrap();
if func.is_imported() {
if let Some(wasi_func) = func.wasi_function() {
return wasi_func.handle(self);
wasi_func.handle(self)?;
loop {
if let Some(Label::Return) = self.label_stack.pop() {
if !self.label_stack.is_empty() {
let frame = self.function_stack.pop().unwrap();
self.ip = frame.ret_addr;
}
break;
}
}
return Ok(());
}
return Err(Trap::UnsupportedCallToImportedFunction(self.ip.func_index));
}
Expand Down
26 changes: 24 additions & 2 deletions src/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl WasiFunction {
// "fd_prestat_dir_name" => WasiFunction::FdPrestatDirName,
// "fd_seek" => WasiFunction::FdSeek,
// "fd_read" => WasiFunction::FdRead,
// "fd_write" => WasiFunction::FdWrite,
"fd_write" => WasiFunction::FdWrite,
// "fd_close" => WasiFunction::FdClose,
// "path_open" => WasiFunction::PathOpen,
"proc_exit" => WasiFunction::ProcExit,
Expand All @@ -46,7 +46,29 @@ impl WasiFunction {
WasiFunction::FdPrestatDirName => (),
WasiFunction::FdSeek => (),
WasiFunction::FdRead => (),
WasiFunction::FdWrite => (),
WasiFunction::FdWrite => {
let fd = vm.locals()?[0].to::<u32>().unwrap();
let iovs = vm.locals()?[1].to::<u32>().unwrap();
let iovs_len = vm.locals()?[2].to::<u32>().unwrap();
let nwritten_out = vm.locals()?[3].to::<u32>().unwrap();
let mut nwritten = 0;
if fd != 1 {
panic!("wasi.fd_write call with fd != stdout");
}
for i in 0..iovs_len {
let iov = iovs + i * 8;
let str_addr: u32 = vm.memory().load(iov)?;
let len: u32 = vm.memory().load(iov + 4)?;
let start = str_addr as usize;
let end = (str_addr + len) as usize;
let s = &vm.memory().data()[start..end];
print!("{}", String::from_utf8(s.to_vec()).unwrap());
nwritten += len;
}
let errno: u32 = 0;
vm.push(errno.into())?;
vm.memory_mut().store(nwritten_out, nwritten)?;
}
WasiFunction::FdClose => (),
WasiFunction::PathOpen => (),
WasiFunction::ProcExit => return Err(Trap::WasiExit(vm.pop_as()?)),
Expand Down

0 comments on commit 31bf702

Please sign in to comment.