From 24d028e2a2eeb332c03f5b8c84fde085d4c9edc4 Mon Sep 17 00:00:00 2001 From: Mackenzie Clark Date: Thu, 14 Feb 2019 18:19:18 -0800 Subject: [PATCH] impl error trait for ResolveError (#179) --- lib/runtime-core/src/error.rs | 26 ++++++++++++++++++++++++++ lib/runtime-core/src/types.rs | 24 ++++++++++++++++++++++++ 2 files changed, 50 insertions(+) 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;