Return type normalization, and change return errors from imported functions

This commit is contained in:
Lachlan Sneff
2019-04-22 11:42:52 -07:00
parent 85d9ca9ad2
commit 74b6ba4eaf
8 changed files with 115 additions and 109 deletions

View File

@ -1,4 +1,4 @@
use crate::types::{FuncSig, GlobalDescriptor, MemoryDescriptor, TableDescriptor, Type, Value};
use crate::types::{FuncSig, GlobalDescriptor, MemoryDescriptor, TableDescriptor, Type};
use core::borrow::Borrow;
use std::any::Any;
@ -121,8 +121,7 @@ impl std::error::Error for LinkError {}
/// Comparing two `RuntimeError`s always evaluates to false.
pub enum RuntimeError {
Trap { msg: Box<str> },
Exception { data: Box<[Value]> },
Panic { data: Box<dyn Any> },
Error { data: Box<dyn Any> },
}
impl PartialEq for RuntimeError {
@ -137,10 +136,7 @@ impl std::fmt::Display for RuntimeError {
RuntimeError::Trap { ref msg } => {
write!(f, "WebAssembly trap occured during runtime: {}", msg)
}
RuntimeError::Exception { ref data } => {
write!(f, "Uncaught WebAssembly exception: {:?}", data)
}
RuntimeError::Panic { data } => {
RuntimeError::Error { data } => {
let msg = if let Some(s) = data.downcast_ref::<String>() {
s
} else if let Some(s) = data.downcast_ref::<&str>() {

View File

@ -7,6 +7,7 @@ use crate::{
};
use std::{
any::Any,
convert::Infallible,
ffi::c_void,
fmt,
marker::PhantomData,
@ -120,14 +121,16 @@ pub trait TrapEarly<Rets>
where
Rets: WasmTypeList,
{
fn report(self) -> Result<Rets, Box<dyn Any>>;
type Error: 'static;
fn report(self) -> Result<Rets, Self::Error>;
}
impl<Rets> TrapEarly<Rets> for Rets
where
Rets: WasmTypeList,
{
fn report(self) -> Result<Rets, Box<dyn Any>> {
type Error = Infallible;
fn report(self) -> Result<Rets, Infallible> {
Ok(self)
}
}
@ -135,10 +138,11 @@ where
impl<Rets, E> TrapEarly<Rets> for Result<Rets, E>
where
Rets: WasmTypeList,
E: Into<Box<Any>> + 'static,
E: 'static,
{
fn report(self) -> Result<Rets, Box<dyn Any>> {
self.map_err(|err| err.into())
type Error = E;
fn report(self) -> Result<Rets, E> {
self
}
}
@ -329,11 +333,13 @@ macro_rules! impl_traits {
let f: FN = unsafe { mem::transmute_copy(&()) };
let err = match panic::catch_unwind(panic::AssertUnwindSafe(|| {
let res = f( ctx $( ,WasmExternType::from_native($x) )* ).report();
res
f( ctx $( ,WasmExternType::from_native($x) )* ).report()
})) {
Ok(Ok(returns)) => return returns.into_c_struct(),
Ok(Err(err)) => err,
Ok(Err(err)) => {
let b: Box<_> = err.into();
b as Box<dyn Any>
},
Err(err) => err,
};