diff --git a/lib/runtime-core/src/error.rs b/lib/runtime-core/src/error.rs index b72c74242..734e11cff 100644 --- a/lib/runtime-core/src/error.rs +++ b/lib/runtime-core/src/error.rs @@ -1,6 +1,7 @@ use crate::types::{ FuncSig, GlobalDescriptor, MemoryDescriptor, MemoryIndex, TableDescriptor, TableIndex, Type, }; +use core::borrow::Borrow; use std::sync::Arc; pub type Result = std::result::Result; @@ -135,6 +136,31 @@ impl PartialEq for ResolveError { } } +impl std::fmt::Display for ResolveError { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match self { + ResolveError::ExportNotFound { name } => write!(f, "Export not found: {}", name), + ResolveError::ExportWrongType { name } => write!(f, "Export wrong type: {}", name), + ResolveError::Signature { expected, found } => { + let found = found + .as_slice() + .iter() + .map(|p| p.to_string()) + .collect::>() + .join(", "); + let expected: &FuncSig = expected.borrow(); + write!( + f, + "Parameters of type [{}] did not match signature {}", + found, expected + ) + } + } + } +} + +impl std::error::Error for ResolveError {} + /// This error type is produced by calling a wasm function /// exported from a module. /// diff --git a/lib/runtime-core/src/types.rs b/lib/runtime-core/src/types.rs index c6b3f0a2b..fd57bf12c 100644 --- a/lib/runtime-core/src/types.rs +++ b/lib/runtime-core/src/types.rs @@ -15,6 +15,12 @@ pub enum Type { F64, } +impl std::fmt::Display for Type { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + write!(f, "{:?}", self) + } +} + /// Represents a WebAssembly value. /// /// As the number of types in WebAssembly expand, @@ -292,6 +298,24 @@ impl FuncSig { } } +impl std::fmt::Display for FuncSig { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + let params = self + .params + .iter() + .map(|p| p.to_string()) + .collect::>() + .join(", "); + let returns = self + .returns + .iter() + .map(|p| p.to_string()) + .collect::>() + .join(", "); + write!(f, "[{}] -> [{}]", params, returns) + } +} + pub trait LocalImport { type Local: TypedIndex; type Import: TypedIndex;