Misc fixes

This commit is contained in:
Lachlan Sneff
2019-04-01 17:50:53 -07:00
parent 8bab9f1bea
commit 287c81d7a5
4 changed files with 37 additions and 15 deletions

View File

@ -119,7 +119,6 @@ impl std::error::Error for LinkError {}
/// The main way to do this is `Instance.call`. /// The main way to do this is `Instance.call`.
/// ///
/// Comparing two `RuntimeError`s always evaluates to false. /// Comparing two `RuntimeError`s always evaluates to false.
#[derive(Debug)]
pub enum RuntimeError { pub enum RuntimeError {
Trap { msg: Box<str> }, Trap { msg: Box<str> },
Exception { data: Box<[Value]> }, Exception { data: Box<[Value]> },
@ -141,11 +140,27 @@ impl std::fmt::Display for RuntimeError {
RuntimeError::Exception { ref data } => { RuntimeError::Exception { ref data } => {
write!(f, "Uncaught WebAssembly exception: {:?}", data) write!(f, "Uncaught WebAssembly exception: {:?}", data)
} }
RuntimeError::Panic { data: _ } => write!(f, "User-defined \"panic\""), RuntimeError::Panic { data } => {
let msg = if let Some(s) = data.downcast_ref::<String>() {
s
} else if let Some(s) = data.downcast_ref::<&str>() {
s
} else {
"user-defined, opaque"
};
write!(f, "{}", msg)
}
} }
} }
} }
impl std::fmt::Debug for RuntimeError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}", self)
}
}
impl std::error::Error for RuntimeError {} impl std::error::Error for RuntimeError {}
/// This error type is produced by resolving a wasm function /// This error type is produced by resolving a wasm function
@ -197,7 +212,6 @@ impl std::error::Error for ResolveError {}
/// be the `CallError::Runtime(RuntimeError)` variant. /// be the `CallError::Runtime(RuntimeError)` variant.
/// ///
/// Comparing two `CallError`s always evaluates to false. /// Comparing two `CallError`s always evaluates to false.
#[derive(Debug)]
pub enum CallError { pub enum CallError {
Resolve(ResolveError), Resolve(ResolveError),
Runtime(RuntimeError), Runtime(RuntimeError),
@ -218,6 +232,15 @@ impl std::fmt::Display for CallError {
} }
} }
impl std::fmt::Debug for CallError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
CallError::Resolve(resolve_err) => write!(f, "ResolveError: {:?}", resolve_err),
CallError::Runtime(runtime_err) => write!(f, "RuntimeError: {:?}", runtime_err),
}
}
}
impl std::error::Error for CallError {} impl std::error::Error for CallError {}
/// This error type is produced when creating something, /// This error type is produced when creating something,

View File

@ -129,7 +129,8 @@ unsafe impl WasmExternType for f64 {
pub unsafe trait ValueType: Copy pub unsafe trait ValueType: Copy
where where
Self: Sized, Self: Sized,
{} {
}
macro_rules! convert_value_impl { macro_rules! convert_value_impl {
($t:ty) => { ($t:ty) => {

View File

@ -8,11 +8,11 @@ use hashbrown::hash_map::{Entry, HashMap};
use std::{ use std::{
cell::{Cell, RefCell}, cell::{Cell, RefCell},
ops::{Index, IndexMut}, ops::{Index, IndexMut},
path::PathBuf,
rc::Rc, rc::Rc,
time::SystemTime, time::SystemTime,
path::PathBuf,
}; };
use zbox::{File, FileType, OpenOptions, Repo, RepoOpener, init_env as zbox_init_env}; use zbox::{init_env as zbox_init_env, File, FileType, OpenOptions, Repo, RepoOpener};
pub const MAX_SYMLINKS: usize = 100; pub const MAX_SYMLINKS: usize = 100;

View File

@ -815,8 +815,8 @@ pub fn poll_oneoff(
) -> __wasi_errno_t { ) -> __wasi_errno_t {
unimplemented!() unimplemented!()
} }
pub fn proc_exit(ctx: &mut Ctx, rval: __wasi_exitcode_t) { pub fn proc_exit(ctx: &mut Ctx, rval: __wasi_exitcode_t) -> Result<(), &'static str> {
unimplemented!() Err("Instance exited")
} }
pub fn proc_raise(ctx: &mut Ctx, sig: __wasi_signal_t) -> __wasi_errno_t { pub fn proc_raise(ctx: &mut Ctx, sig: __wasi_signal_t) -> __wasi_errno_t {
unimplemented!() unimplemented!()
@ -833,13 +833,11 @@ pub fn random_get(ctx: &mut Ctx, buf: WasmPtr<u8, Array>, buf_len: u32) -> __was
let mut rng = thread_rng(); let mut rng = thread_rng();
let memory = ctx.memory(0); let memory = ctx.memory(0);
if let Ok(buf) = buf.deref(memory, 0, buf_len) { let buf = wasi_try!(buf.deref(memory, 0, buf_len));
for i in 0..(buf_len as usize) {
let random_byte = rng.gen::<u8>(); unsafe {
buf[i].set(random_byte); let u8_buffer = &mut *(buf as *const [_] as *mut [_] as *mut [u8]);
} thread_rng().fill(u8_buffer);
} else {
return __WASI_EFAULT;
} }
__WASI_ESUCCESS __WASI_ESUCCESS