diff --git a/src/host.rs b/src/host.rs index a58abdb..3bdbf5d 100644 --- a/src/host.rs +++ b/src/host.rs @@ -1,6 +1,6 @@ use std::any::TypeId; use value::{RuntimeValue, TryInto}; -use Error; +use {Error, Trap}; /// Safe wrapper for list of arguments. #[derive(Debug)] @@ -188,7 +188,7 @@ impl Externals for NopExternals { _index: usize, _args: RuntimeArgs, ) -> Result, Error> { - Err(Error::Trap("invoke index on no-op externals".into())) + Err(Error::Trap(Trap::Unreachable)) } } diff --git a/src/lib.rs b/src/lib.rs index e76a343..054236f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -106,6 +106,11 @@ use std::fmt; use std::error; use std::collections::HashMap; +#[derive(Debug)] +pub enum Trap { + Unreachable, +} + /// Internal interpreter error. #[derive(Debug)] pub enum Error { @@ -127,7 +132,7 @@ pub enum Error { /// Value-level error. Value(String), /// Trap. - Trap(String), + Trap(Trap), /// Custom embedder error. Host(Box), } @@ -143,7 +148,7 @@ impl Into for Error { Error::Global(s) => s, Error::Stack(s) => s, Error::Value(s) => s, - Error::Trap(s) => format!("trap: {}", s), + Error::Trap(s) => format!("trap: {:?}", s), Error::Host(e) => format!("user: {}", e), } } @@ -160,7 +165,7 @@ impl fmt::Display for Error { Error::Global(ref s) => write!(f, "Global: {}", s), Error::Stack(ref s) => write!(f, "Stack: {}", s), Error::Value(ref s) => write!(f, "Value: {}", s), - Error::Trap(ref s) => write!(f, "Trap: {}", s), + Error::Trap(ref s) => write!(f, "Trap: {:?}", s), Error::Host(ref e) => write!(f, "User: {}", e), } } @@ -179,7 +184,7 @@ impl error::Error for Error { Error::Global(ref s) => s, Error::Stack(ref s) => s, Error::Value(ref s) => s, - Error::Trap(ref s) => s, + Error::Trap(_) => "Trap", Error::Host(_) => "Host error", } } diff --git a/src/runner.rs b/src/runner.rs index e2c2e7c..30af5bd 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -5,7 +5,7 @@ use std::fmt::{self, Display}; use std::iter::repeat; use std::collections::{HashMap, VecDeque}; use parity_wasm::elements::{Opcode, BlockType, Local}; -use {Error, Signature}; +use {Error, Trap, Signature}; use module::ModuleRef; use func::{FuncRef, FuncInstance, FuncInstanceInternal}; use value::{ @@ -351,7 +351,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { } fn run_unreachable(&mut self, _context: &mut FunctionContext) -> Result { - Err(Error::Trap("programmatic".into())) + Err(Error::Trap(Trap::Unreachable)) } fn run_nop(&mut self, _context: &mut FunctionContext) -> Result {