Introduce Trap struct.

This commit is contained in:
Sergey Pepyakin 2018-01-29 16:51:10 +03:00
parent 3ad9f07e93
commit e529e3208b
3 changed files with 13 additions and 8 deletions

View File

@ -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<Option<RuntimeValue>, Error> {
Err(Error::Trap("invoke index on no-op externals".into()))
Err(Error::Trap(Trap::Unreachable))
}
}

View File

@ -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<host::HostError>),
}
@ -143,7 +148,7 @@ impl Into<String> 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",
}
}

View File

@ -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<InstructionOutcome, Error> {
Err(Error::Trap("programmatic".into()))
Err(Error::Trap(Trap::Unreachable))
}
fn run_nop(&mut self, _context: &mut FunctionContext) -> Result<InstructionOutcome, Error> {