From 0db8f109b3117df5a618fc9dc463158777c4750f Mon Sep 17 00:00:00 2001 From: Sergey Pepyakin Date: Tue, 9 Jan 2018 18:11:02 +0300 Subject: [PATCH] Rename UserError to HostError and move to host --- src/interpreter/host.rs | 30 ++++++++++++++++++++++++++++++ src/interpreter/mod.rs | 38 ++++---------------------------------- 2 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/interpreter/host.rs b/src/interpreter/host.rs index ff35b6a..93cb62e 100644 --- a/src/interpreter/host.rs +++ b/src/interpreter/host.rs @@ -1,7 +1,37 @@ +use std::any::TypeId; use elements::FunctionType; use interpreter::value::RuntimeValue; use interpreter::Error; +/// Custom user error. +pub trait HostError: 'static + ::std::fmt::Display + ::std::fmt::Debug { + #[doc(hidden)] + fn __private_get_type_id__(&self) -> TypeId { + TypeId::of::() + } +} + +impl HostError { + /// Attempt to downcast this `HostError` to a concrete type by reference. + pub fn downcast_ref(&self) -> Option<&T> { + if self.__private_get_type_id__() == TypeId::of::() { + unsafe { Some(&*(self as *const HostError as *const T)) } + } else { + None + } + } + + /// Attempt to downcast this `HostError` to a concrete type by mutable + /// reference. + pub fn downcast_mut(&mut self) -> Option<&mut T> { + if self.__private_get_type_id__() == TypeId::of::() { + unsafe { Some(&mut *(self as *mut HostError as *mut T)) } + } else { + None + } + } +} + pub type HostFuncIndex = u32; pub trait Externals { diff --git a/src/interpreter/mod.rs b/src/interpreter/mod.rs index ca3d3ea..df04fee 100644 --- a/src/interpreter/mod.rs +++ b/src/interpreter/mod.rs @@ -4,39 +4,9 @@ #![allow(deprecated)] #![allow(missing_docs)] -use std::any::TypeId; use validation; use common; -/// Custom user error. -pub trait UserError: 'static + ::std::fmt::Display + ::std::fmt::Debug { - #[doc(hidden)] - fn __private_get_type_id__(&self) -> TypeId { - TypeId::of::() - } -} - -impl UserError { - /// Attempt to downcast this `UserError` to a concrete type by reference. - pub fn downcast_ref(&self) -> Option<&T> { - if self.__private_get_type_id__() == TypeId::of::() { - unsafe { Some(&*(self as *const UserError as *const T)) } - } else { - None - } - } - - /// Attempt to downcast this `UserError` to a concrete type by mutable - /// reference. - pub fn downcast_mut(&mut self) -> Option<&mut T> { - if self.__private_get_type_id__() == TypeId::of::() { - unsafe { Some(&mut *(self as *mut UserError as *mut T)) } - } else { - None - } - } -} - /// Internal interpreter error. #[derive(Debug)] pub enum Error { @@ -70,7 +40,7 @@ pub enum Error { /// Trap. Trap(String), /// Custom user error. - User(Box), + User(Box), } impl Into for Error { @@ -117,7 +87,7 @@ impl ::std::fmt::Display for Error { } } -impl From for Error where U: UserError + Sized { +impl From for Error where U: host::HostError + Sized { fn from(e: U) -> Self { Error::User(Box::new(e)) } @@ -153,8 +123,8 @@ pub use self::memory::{MemoryInstance, MemoryRef}; pub use self::table::{TableInstance, TableRef}; pub use self::program::ProgramInstance; pub use self::value::{RuntimeValue, TryInto}; -pub use self::host::{Externals, HostFuncIndex, EmptyExternals}; +pub use self::host::{Externals, HostFuncIndex, EmptyExternals, HostError}; pub use self::imports::{ImportResolver, Imports}; -pub use self::module::{ModuleInstance, ModuleRef}; +pub use self::module::{ModuleInstance, ModuleRef, ExternVal}; pub use self::global::{GlobalInstance, GlobalRef}; pub use self::func::{FuncInstance, FuncRef};