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 std::any::TypeId;
use value::{RuntimeValue, TryInto}; use value::{RuntimeValue, TryInto};
use Error; use {Error, Trap};
/// Safe wrapper for list of arguments. /// Safe wrapper for list of arguments.
#[derive(Debug)] #[derive(Debug)]
@ -188,7 +188,7 @@ impl Externals for NopExternals {
_index: usize, _index: usize,
_args: RuntimeArgs, _args: RuntimeArgs,
) -> Result<Option<RuntimeValue>, Error> { ) -> 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::error;
use std::collections::HashMap; use std::collections::HashMap;
#[derive(Debug)]
pub enum Trap {
Unreachable,
}
/// Internal interpreter error. /// Internal interpreter error.
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
@ -127,7 +132,7 @@ pub enum Error {
/// Value-level error. /// Value-level error.
Value(String), Value(String),
/// Trap. /// Trap.
Trap(String), Trap(Trap),
/// Custom embedder error. /// Custom embedder error.
Host(Box<host::HostError>), Host(Box<host::HostError>),
} }
@ -143,7 +148,7 @@ impl Into<String> for Error {
Error::Global(s) => s, Error::Global(s) => s,
Error::Stack(s) => s, Error::Stack(s) => s,
Error::Value(s) => s, Error::Value(s) => s,
Error::Trap(s) => format!("trap: {}", s), Error::Trap(s) => format!("trap: {:?}", s),
Error::Host(e) => format!("user: {}", e), Error::Host(e) => format!("user: {}", e),
} }
} }
@ -160,7 +165,7 @@ impl fmt::Display for Error {
Error::Global(ref s) => write!(f, "Global: {}", s), Error::Global(ref s) => write!(f, "Global: {}", s),
Error::Stack(ref s) => write!(f, "Stack: {}", s), Error::Stack(ref s) => write!(f, "Stack: {}", s),
Error::Value(ref s) => write!(f, "Value: {}", 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), Error::Host(ref e) => write!(f, "User: {}", e),
} }
} }
@ -179,7 +184,7 @@ impl error::Error for Error {
Error::Global(ref s) => s, Error::Global(ref s) => s,
Error::Stack(ref s) => s, Error::Stack(ref s) => s,
Error::Value(ref s) => s, Error::Value(ref s) => s,
Error::Trap(ref s) => s, Error::Trap(_) => "Trap",
Error::Host(_) => "Host error", Error::Host(_) => "Host error",
} }
} }

View File

@ -5,7 +5,7 @@ use std::fmt::{self, Display};
use std::iter::repeat; use std::iter::repeat;
use std::collections::{HashMap, VecDeque}; use std::collections::{HashMap, VecDeque};
use parity_wasm::elements::{Opcode, BlockType, Local}; use parity_wasm::elements::{Opcode, BlockType, Local};
use {Error, Signature}; use {Error, Trap, Signature};
use module::ModuleRef; use module::ModuleRef;
use func::{FuncRef, FuncInstance, FuncInstanceInternal}; use func::{FuncRef, FuncInstance, FuncInstanceInternal};
use value::{ use value::{
@ -351,7 +351,7 @@ impl<'a, E: Externals> Interpreter<'a, E> {
} }
fn run_unreachable(&mut self, _context: &mut FunctionContext) -> Result<InstructionOutcome, Error> { 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> { fn run_nop(&mut self, _context: &mut FunctionContext) -> Result<InstructionOutcome, Error> {