From daf7d95a65aaac4d45bb18758846660d510d97a2 Mon Sep 17 00:00:00 2001 From: Sergey Pepyakin Date: Mon, 5 Feb 2018 18:09:20 +0300 Subject: [PATCH] Rename Trap to TrapKind --- examples/tictactoe.rs | 4 +- spec/src/run.rs | 8 +- spec/src/test.rs | 2 +- src/func.rs | 10 +-- src/host.rs | 20 ++--- src/lib.rs | 26 +++---- src/module.rs | 6 +- src/runner.rs | 174 +++++++++++++++++++++--------------------- src/tests/host.rs | 18 ++--- src/value.rs | 26 +++---- 10 files changed, 147 insertions(+), 147 deletions(-) diff --git a/examples/tictactoe.rs b/examples/tictactoe.rs index 86d7781..1fc561f 100644 --- a/examples/tictactoe.rs +++ b/examples/tictactoe.rs @@ -8,7 +8,7 @@ use wasmi::{ Error as InterpreterError, ModuleInstance, ModuleRef, Externals, RuntimeValue, FuncRef, ModuleImportResolver, FuncInstance, HostError, ImportsBuilder, Signature, ValueType, - RuntimeArgs, Trap, + RuntimeArgs, TrapKind, }; #[derive(Debug)] @@ -149,7 +149,7 @@ impl<'a> Externals for Runtime<'a> { &mut self, index: usize, args: RuntimeArgs, - ) -> Result, Trap> { + ) -> Result, TrapKind> { match index { SET_FUNC_INDEX => { let idx: i32 = args.nth(0); diff --git a/spec/src/run.rs b/spec/src/run.rs index 7f56ce7..04eebe7 100644 --- a/spec/src/run.rs +++ b/spec/src/run.rs @@ -13,14 +13,14 @@ use wasmi::{ GlobalInstance, GlobalRef, ImportResolver, ImportsBuilder, MemoryInstance, MemoryRef, ModuleImportResolver, ModuleInstance, ModuleRef, RuntimeValue, TableInstance, TableRef, ValueType, - Module, Signature, MemoryDescriptor, Trap, + Module, Signature, MemoryDescriptor, TrapKind, TableDescriptor, GlobalDescriptor, FuncInstance, RuntimeArgs, }; #[derive(Debug)] enum Error { Load(String), - Start(Trap), + Start(TrapKind), Interpreter(InterpreterError), } @@ -59,7 +59,7 @@ impl Externals for SpecModule { &mut self, index: usize, args: RuntimeArgs, - ) -> Result, Trap> { + ) -> Result, TrapKind> { match index { PRINT_FUNC_INDEX => { println!("print: {:?}", args); @@ -495,7 +495,7 @@ pub fn spec(name: &str) { Err(e) => println!("assert_exhaustion at line {} - success ({:?})", line, e), } } - &test::Command::AssertTrap { + &test::Command::AssertTrapKind { line, ref action, .. } => { let result = run_action(&mut spec_driver, action); diff --git a/spec/src/test.rs b/spec/src/test.rs index 8c4a2b3..a1d60fc 100644 --- a/spec/src/test.rs +++ b/spec/src/test.rs @@ -49,7 +49,7 @@ pub enum Command { action: Action, }, #[serde(rename = "assert_trap")] - AssertTrap { + AssertTrapKind { line: u64, action: Action, text: String, diff --git a/src/func.rs b/src/func.rs index 5b5b038..aef0505 100644 --- a/src/func.rs +++ b/src/func.rs @@ -2,7 +2,7 @@ use std::rc::{Rc, Weak}; use std::fmt; use std::collections::HashMap; use parity_wasm::elements::{Local, Opcodes}; -use {Trap, Signature}; +use {TrapKind, Signature}; use host::Externals; use runner::{check_function_args, Interpreter}; use value::RuntimeValue; @@ -132,16 +132,16 @@ impl FuncInstance { /// # Errors /// /// Returns `Err` if `args` types is not match function [`signature`] or - /// if [`Trap`] at execution time occured. + /// if [`TrapKind`] at execution time occured. /// /// [`signature`]: #method.signature - /// [`Trap`]: #enum.Trap.html + /// [`TrapKind`]: #enum.TrapKind.html pub fn invoke( func: &FuncRef, args: &[RuntimeValue], externals: &mut E, - ) -> Result, Trap> { - check_function_args(func.signature(), &args).map_err(|_| Trap::UnexpectedSignature)?; + ) -> Result, TrapKind> { + check_function_args(func.signature(), &args).map_err(|_| TrapKind::UnexpectedSignature)?; match *func.as_internal() { FuncInstanceInternal::Internal { .. } => { let mut interpreter = Interpreter::new(externals); diff --git a/src/host.rs b/src/host.rs index 4838dbb..fb2934e 100644 --- a/src/host.rs +++ b/src/host.rs @@ -1,6 +1,6 @@ use std::any::TypeId; use value::{RuntimeValue, TryInto}; -use Trap; +use TrapKind; /// Safe wrapper for list of arguments. #[derive(Debug)] @@ -18,8 +18,8 @@ impl<'a> RuntimeArgs<'a> { /// # Errors /// /// Returns `Err` if cast is invalid or not enough arguments. - pub fn nth_checked(&self, idx: usize) -> Result where RuntimeValue: TryInto { - Ok(self.nth_value_checked(idx)?.try_into().map_err(|_| Trap::UnexpectedSignature)?) + pub fn nth_checked(&self, idx: usize) -> Result where RuntimeValue: TryInto { + Ok(self.nth_value_checked(idx)?.try_into().map_err(|_| TrapKind::UnexpectedSignature)?) } /// Extract argument as a [`RuntimeValue`] by index `idx`. @@ -27,9 +27,9 @@ impl<'a> RuntimeArgs<'a> { /// # Errors /// /// Returns `Err` if this list has not enough arguments. - pub fn nth_value_checked(&self, idx: usize) -> Result { + pub fn nth_value_checked(&self, idx: usize) -> Result { if self.0.len() <= idx { - return Err(Trap::UnexpectedSignature); + return Err(TrapKind::UnexpectedSignature); } Ok(self.0[idx]) } @@ -122,7 +122,7 @@ impl HostError { /// ```rust /// use wasmi::{ /// Externals, RuntimeValue, RuntimeArgs, Error, ModuleImportResolver, -/// FuncRef, ValueType, Signature, FuncInstance, Trap, +/// FuncRef, ValueType, Signature, FuncInstance, TrapKind, /// }; /// /// struct HostExternals { @@ -136,7 +136,7 @@ impl HostError { /// &mut self, /// index: usize, /// args: RuntimeArgs, -/// ) -> Result, Trap> { +/// ) -> Result, TrapKind> { /// match index { /// ADD_FUNC_INDEX => { /// let a: u32 = args.nth(0); @@ -192,7 +192,7 @@ pub trait Externals { &mut self, index: usize, args: RuntimeArgs, - ) -> Result, Trap>; + ) -> Result, TrapKind>; } /// Implementation of [`Externals`] that just traps on [`invoke_index`]. @@ -206,8 +206,8 @@ impl Externals for NopExternals { &mut self, _index: usize, _args: RuntimeArgs, - ) -> Result, Trap> { - Err(Trap::Unreachable) + ) -> Result, TrapKind> { + Err(TrapKind::Unreachable) } } diff --git a/src/lib.rs b/src/lib.rs index 6f6595a..a552cd9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -107,10 +107,10 @@ use std::collections::HashMap; /// Error type which can thrown by wasm code or by host environment. /// -/// Under some conditions, wasm execution may produce a `Trap`, which immediately aborts execution. -/// Traps can't be handled by WebAssembly code, but are reported to the embedder. +/// Under some conditions, wasm execution may produce a `TrapKind`, which immediately aborts execution. +/// TrapKinds can't be handled by WebAssembly code, but are reported to the embedder. #[derive(Debug)] -pub enum Trap { +pub enum TrapKind { /// Wasm code executed `unreachable` opcode. /// /// `unreachable` is a special opcode which always traps upon execution. @@ -201,8 +201,8 @@ pub enum Error { Global(String), /// Value-level error. Value(String), - /// Trap. - Trap(Trap), + /// TrapKind. + TrapKind(TrapKind), /// Custom embedder error. Host(Box), } @@ -217,7 +217,7 @@ impl Into for Error { Error::Memory(s) => s, Error::Global(s) => s, Error::Value(s) => s, - Error::Trap(s) => format!("trap: {:?}", s), + Error::TrapKind(s) => format!("trap: {:?}", s), Error::Host(e) => format!("user: {}", e), } } @@ -233,7 +233,7 @@ impl fmt::Display for Error { Error::Memory(ref s) => write!(f, "Memory: {}", s), Error::Global(ref s) => write!(f, "Global: {}", s), Error::Value(ref s) => write!(f, "Value: {}", s), - Error::Trap(ref s) => write!(f, "Trap: {:?}", s), + Error::TrapKind(ref s) => write!(f, "TrapKind: {:?}", s), Error::Host(ref e) => write!(f, "User: {}", e), } } @@ -249,7 +249,7 @@ impl error::Error for Error { Error::Memory(ref s) => s, Error::Global(ref s) => s, Error::Value(ref s) => s, - Error::Trap(_) => "Trap", + Error::TrapKind(_) => "TrapKind", Error::Host(_) => "Host error", } } @@ -262,15 +262,15 @@ impl From for Error where U: host::HostError + Sized { } } -impl From for Trap where U: host::HostError + Sized { +impl From for TrapKind where U: host::HostError + Sized { fn from(e: U) -> Self { - Trap::Host(Box::new(e)) + TrapKind::Host(Box::new(e)) } } -impl From for Error { - fn from(e: Trap) -> Error { - Error::Trap(e) +impl From for Error { + fn from(e: TrapKind) -> Error { + Error::TrapKind(e) } } diff --git a/src/module.rs b/src/module.rs index b7b3ed7..5c1b327 100644 --- a/src/module.rs +++ b/src/module.rs @@ -1,5 +1,5 @@ use runner::check_function_args; -use Trap; +use TrapKind; use std::rc::Rc; use std::cell::RefCell; use std::fmt; @@ -594,7 +594,7 @@ impl ModuleInstance { check_function_args(func_instance.signature(), &args)?; FuncInstance::invoke(&func_instance, args, externals) - .map_err(|t| Error::Trap(t)) + .map_err(|t| Error::TrapKind(t)) } /// Find export by a name. @@ -647,7 +647,7 @@ impl<'a> NotStartedModuleRef<'a> { /// # Errors /// /// Returns `Err` if start function traps. - pub fn run_start(self, state: &mut E) -> Result { + pub fn run_start(self, state: &mut E) -> Result { if let Some(start_fn_idx) = self.loaded_module.module().start_section() { let start_func = self.instance.func_by_index(start_fn_idx).expect( "Due to validation start function should exists", diff --git a/src/runner.rs b/src/runner.rs index bf56dbe..0bf8432 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, Trap, Signature}; +use {Error, TrapKind, Signature}; use module::ModuleRef; use func::{FuncRef, FuncInstance, FuncInstanceInternal}; use value::{ @@ -51,7 +51,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { } } - pub fn start_execution(&mut self, func: &FuncRef, args: &[RuntimeValue]) -> Result, Trap> { + pub fn start_execution(&mut self, func: &FuncRef, args: &[RuntimeValue]) -> Result, TrapKind> { let context = FunctionContext::new( func.clone(), DEFAULT_VALUE_STACK_LIMIT, @@ -66,7 +66,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { self.run_interpreter_loop(&mut function_stack) } - fn run_interpreter_loop(&mut self, function_stack: &mut VecDeque) -> Result, Trap> { + fn run_interpreter_loop(&mut self, function_stack: &mut VecDeque) -> Result, TrapKind> { loop { let mut function_context = function_stack.pop_back().expect("on loop entry - not empty; on loop continue - checking for emptiness; qed"); let function_ref = function_context.function.clone(); @@ -113,7 +113,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { } } - fn do_run_function(&mut self, function_context: &mut FunctionContext, function_body: &[Opcode], function_labels: &HashMap) -> Result { + fn do_run_function(&mut self, function_context: &mut FunctionContext, function_body: &[Opcode], function_labels: &HashMap) -> Result { loop { let instruction = &function_body[function_context.position]; @@ -155,7 +155,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { })) } - fn run_instruction(&mut self, context: &mut FunctionContext, labels: &HashMap, opcode: &Opcode) -> Result { + fn run_instruction(&mut self, context: &mut FunctionContext, labels: &HashMap, opcode: &Opcode) -> Result { match opcode { &Opcode::Unreachable => self.run_unreachable(context), &Opcode::Nop => self.run_nop(context), @@ -349,25 +349,25 @@ impl<'a, E: Externals> Interpreter<'a, E> { } } - fn run_unreachable(&mut self, _context: &mut FunctionContext) -> Result { - Err(Trap::Unreachable) + fn run_unreachable(&mut self, _context: &mut FunctionContext) -> Result { + Err(TrapKind::Unreachable) } - fn run_nop(&mut self, _context: &mut FunctionContext) -> Result { + fn run_nop(&mut self, _context: &mut FunctionContext) -> Result { Ok(InstructionOutcome::RunNextInstruction) } - fn run_block(&mut self, context: &mut FunctionContext, labels: &HashMap, block_type: BlockType) -> Result { + fn run_block(&mut self, context: &mut FunctionContext, labels: &HashMap, block_type: BlockType) -> Result { context.push_frame(labels, BlockFrameType::Block, block_type)?; Ok(InstructionOutcome::RunNextInstruction) } - fn run_loop(&mut self, context: &mut FunctionContext, labels: &HashMap, block_type: BlockType) -> Result { + fn run_loop(&mut self, context: &mut FunctionContext, labels: &HashMap, block_type: BlockType) -> Result { context.push_frame(labels, BlockFrameType::Loop, block_type)?; Ok(InstructionOutcome::RunNextInstruction) } - fn run_if(&mut self, context: &mut FunctionContext, labels: &HashMap, block_type: BlockType) -> Result { + fn run_if(&mut self, context: &mut FunctionContext, labels: &HashMap, block_type: BlockType) -> Result { let condition: bool = context .value_stack_mut() .pop_as(); @@ -386,23 +386,23 @@ impl<'a, E: Externals> Interpreter<'a, E> { Ok(InstructionOutcome::RunNextInstruction) } - fn run_else(&mut self, context: &mut FunctionContext, labels: &HashMap) -> Result { + fn run_else(&mut self, context: &mut FunctionContext, labels: &HashMap) -> Result { let end_pos = labels[&context.position]; context.pop_frame(false)?; context.position = end_pos; Ok(InstructionOutcome::RunNextInstruction) } - fn run_end(&mut self, context: &mut FunctionContext) -> Result { + fn run_end(&mut self, context: &mut FunctionContext) -> Result { context.pop_frame(false)?; Ok(InstructionOutcome::End) } - fn run_br(&mut self, _context: &mut FunctionContext, label_idx: u32) -> Result { + fn run_br(&mut self, _context: &mut FunctionContext, label_idx: u32) -> Result { Ok(InstructionOutcome::Branch(label_idx as usize)) } - fn run_br_if(&mut self, context: &mut FunctionContext, label_idx: u32) -> Result { + fn run_br_if(&mut self, context: &mut FunctionContext, label_idx: u32) -> Result { let condition = context.value_stack_mut().pop_as(); if condition { Ok(InstructionOutcome::Branch(label_idx as usize)) @@ -411,13 +411,13 @@ impl<'a, E: Externals> Interpreter<'a, E> { } } - fn run_br_table(&mut self, context: &mut FunctionContext, table: &[u32], default: u32) -> Result { + fn run_br_table(&mut self, context: &mut FunctionContext, table: &[u32], default: u32) -> Result { let index: u32 = context.value_stack_mut() .pop_as(); Ok(InstructionOutcome::Branch(table.get(index as usize).cloned().unwrap_or(default) as usize)) } - fn run_return(&mut self, _context: &mut FunctionContext) -> Result { + fn run_return(&mut self, _context: &mut FunctionContext) -> Result { Ok(InstructionOutcome::Return) } @@ -425,7 +425,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { &mut self, context: &mut FunctionContext, func_idx: u32, - ) -> Result { + ) -> Result { let func = context .module() .func_by_index(func_idx) @@ -437,7 +437,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { &mut self, context: &mut FunctionContext, signature_idx: u32, - ) -> Result { + ) -> Result { let table_func_idx: u32 = context .value_stack_mut() .pop_as(); @@ -446,8 +446,8 @@ impl<'a, E: Externals> Interpreter<'a, E> { .table_by_index(DEFAULT_TABLE_INDEX) .expect("Due to validation table should exists"); let func_ref = table.get(table_func_idx) - .map_err(|_| Trap::TableAccessOutOfBounds)? - .ok_or_else(|| Trap::ElemUninitialized)?; + .map_err(|_| TrapKind::TableAccessOutOfBounds)? + .ok_or_else(|| TrapKind::ElemUninitialized)?; { let actual_function_type = func_ref.signature(); @@ -457,21 +457,21 @@ impl<'a, E: Externals> Interpreter<'a, E> { .expect("Due to validation type should exists"); if &*required_function_type != actual_function_type { - return Err(Trap::ElemSignatureMismatch); + return Err(TrapKind::ElemSignatureMismatch); } } Ok(InstructionOutcome::ExecuteCall(func_ref)) } - fn run_drop(&mut self, context: &mut FunctionContext) -> Result { + fn run_drop(&mut self, context: &mut FunctionContext) -> Result { let _ = context .value_stack_mut() .pop(); Ok(InstructionOutcome::RunNextInstruction) } - fn run_select(&mut self, context: &mut FunctionContext) -> Result { + fn run_select(&mut self, context: &mut FunctionContext) -> Result { let (left, mid, right) = context .value_stack_mut() .pop_triple(); @@ -484,13 +484,13 @@ impl<'a, E: Externals> Interpreter<'a, E> { Ok(InstructionOutcome::RunNextInstruction) } - fn run_get_local(&mut self, context: &mut FunctionContext, index: u32) -> Result { + fn run_get_local(&mut self, context: &mut FunctionContext, index: u32) -> Result { let val = context.get_local(index as usize); context.value_stack_mut().push(val)?; Ok(InstructionOutcome::RunNextInstruction) } - fn run_set_local(&mut self, context: &mut FunctionContext, index: u32) -> Result { + fn run_set_local(&mut self, context: &mut FunctionContext, index: u32) -> Result { let arg = context .value_stack_mut() .pop(); @@ -498,7 +498,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { Ok(InstructionOutcome::RunNextInstruction) } - fn run_tee_local(&mut self, context: &mut FunctionContext, index: u32) -> Result { + fn run_tee_local(&mut self, context: &mut FunctionContext, index: u32) -> Result { let arg = context .value_stack() .top() @@ -511,7 +511,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { &mut self, context: &mut FunctionContext, index: u32, - ) -> Result { + ) -> Result { let global = context .module() .global_by_index(index) @@ -525,7 +525,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { &mut self, context: &mut FunctionContext, index: u32, - ) -> Result { + ) -> Result { let val = context .value_stack_mut() .pop(); @@ -537,7 +537,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { Ok(InstructionOutcome::RunNextInstruction) } - fn run_load(&mut self, context: &mut FunctionContext, _align: u32, offset: u32) -> Result + fn run_load(&mut self, context: &mut FunctionContext, _align: u32, offset: u32) -> Result where RuntimeValue: From, T: LittleEndianConvert { let raw_address = context .value_stack_mut() @@ -551,14 +551,14 @@ impl<'a, E: Externals> Interpreter<'a, E> { .memory_by_index(DEFAULT_MEMORY_INDEX) .expect("Due to validation memory should exists"); let b = m.get(address, mem::size_of::()) - .map_err(|_| Trap::MemoryAccessOutOfBounds)?; + .map_err(|_| TrapKind::MemoryAccessOutOfBounds)?; let n = T::from_little_endian(&b) .expect("Can't fail since buffer length should be size_of::"); context.value_stack_mut().push(n.into())?; Ok(InstructionOutcome::RunNextInstruction) } - fn run_load_extend(&mut self, context: &mut FunctionContext, _align: u32, offset: u32) -> Result + fn run_load_extend(&mut self, context: &mut FunctionContext, _align: u32, offset: u32) -> Result where T: ExtendInto, RuntimeValue: From, T: LittleEndianConvert { let raw_address = context .value_stack_mut() @@ -572,7 +572,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { .memory_by_index(DEFAULT_MEMORY_INDEX) .expect("Due to validation memory should exists"); let b = m.get(address, mem::size_of::()) - .map_err(|_| Trap::MemoryAccessOutOfBounds)?; + .map_err(|_| TrapKind::MemoryAccessOutOfBounds)?; let v = T::from_little_endian(&b) .expect("Can't fail since buffer length should be size_of::"); let stack_value: U = v.extend_into(); @@ -583,7 +583,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { .map(|_| InstructionOutcome::RunNextInstruction) } - fn run_store(&mut self, context: &mut FunctionContext, _align: u32, offset: u32) -> Result + fn run_store(&mut self, context: &mut FunctionContext, _align: u32, offset: u32) -> Result where RuntimeValue: TryInto, T: LittleEndianConvert { let stack_value = context .value_stack_mut() @@ -602,7 +602,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { .memory_by_index(DEFAULT_MEMORY_INDEX) .expect("Due to validation memory should exists"); m.set(address, &stack_value) - .map_err(|_| Trap::MemoryAccessOutOfBounds)?; + .map_err(|_| TrapKind::MemoryAccessOutOfBounds)?; Ok(InstructionOutcome::RunNextInstruction) } @@ -611,7 +611,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { context: &mut FunctionContext, _align: u32, offset: u32, - ) -> Result + ) -> Result where RuntimeValue: TryInto, T: WrapInto, @@ -635,11 +635,11 @@ impl<'a, E: Externals> Interpreter<'a, E> { .memory_by_index(DEFAULT_MEMORY_INDEX) .expect("Due to validation memory should exists"); m.set(address, &stack_value) - .map_err(|_| Trap::MemoryAccessOutOfBounds)?; + .map_err(|_| TrapKind::MemoryAccessOutOfBounds)?; Ok(InstructionOutcome::RunNextInstruction) } - fn run_current_memory(&mut self, context: &mut FunctionContext) -> Result { + fn run_current_memory(&mut self, context: &mut FunctionContext) -> Result { let m = context.module() .memory_by_index(DEFAULT_MEMORY_INDEX) .expect("Due to validation memory should exists"); @@ -650,7 +650,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { Ok(InstructionOutcome::RunNextInstruction) } - fn run_grow_memory(&mut self, context: &mut FunctionContext) -> Result { + fn run_grow_memory(&mut self, context: &mut FunctionContext) -> Result { let pages: u32 = context .value_stack_mut() .pop_as(); @@ -666,7 +666,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { Ok(InstructionOutcome::RunNextInstruction) } - fn run_const(&mut self, context: &mut FunctionContext, val: RuntimeValue) -> Result { + fn run_const(&mut self, context: &mut FunctionContext, val: RuntimeValue) -> Result { context .value_stack_mut() .push(val) @@ -674,7 +674,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { .map(|_| InstructionOutcome::RunNextInstruction) } - fn run_relop(&mut self, context: &mut FunctionContext, f: F) -> Result + fn run_relop(&mut self, context: &mut FunctionContext, f: F) -> Result where RuntimeValue: TryInto, F: FnOnce(T, T) -> bool, @@ -692,7 +692,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { Ok(InstructionOutcome::RunNextInstruction) } - fn run_eqz(&mut self, context: &mut FunctionContext) -> Result + fn run_eqz(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: TryInto, T: PartialEq + Default { let v = context .value_stack_mut() @@ -702,38 +702,38 @@ impl<'a, E: Externals> Interpreter<'a, E> { Ok(InstructionOutcome::RunNextInstruction) } - fn run_eq(&mut self, context: &mut FunctionContext) -> Result + fn run_eq(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: TryInto, T: PartialEq { self.run_relop(context, |left, right| left == right) } - fn run_ne(&mut self, context: &mut FunctionContext) -> Result + fn run_ne(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: TryInto, T: PartialEq { self.run_relop(context, |left, right| left != right) } - fn run_lt(&mut self, context: &mut FunctionContext) -> Result + fn run_lt(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: TryInto, T: PartialOrd + Display { self.run_relop(context, |left, right| left < right) } - fn run_gt(&mut self, context: &mut FunctionContext) -> Result + fn run_gt(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: TryInto, T: PartialOrd { self.run_relop(context, |left, right| left > right) } - fn run_lte(&mut self, context: &mut FunctionContext) -> Result + fn run_lte(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: TryInto, T: PartialOrd { self.run_relop(context, |left, right| left <= right) } - fn run_gte(&mut self, context: &mut FunctionContext) -> Result + fn run_gte(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: TryInto, T: PartialOrd { self.run_relop(context, |left, right| left >= right) } - fn run_unop(&mut self, context: &mut FunctionContext, f: F) -> Result + fn run_unop(&mut self, context: &mut FunctionContext, f: F) -> Result where F: FnOnce(T) -> U, RuntimeValue: From + TryInto @@ -746,22 +746,22 @@ impl<'a, E: Externals> Interpreter<'a, E> { Ok(InstructionOutcome::RunNextInstruction) } - fn run_clz(&mut self, context: &mut FunctionContext) -> Result + fn run_clz(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: Integer { self.run_unop(context, |v| v.leading_zeros()) } - fn run_ctz(&mut self, context: &mut FunctionContext) -> Result + fn run_ctz(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: Integer { self.run_unop(context, |v| v.trailing_zeros()) } - fn run_popcnt(&mut self, context: &mut FunctionContext) -> Result + fn run_popcnt(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: Integer { self.run_unop(context, |v| v.count_ones()) } - fn run_add(&mut self, context: &mut FunctionContext) -> Result + fn run_add(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: ArithmeticOps { let (left, right) = context .value_stack_mut() @@ -772,7 +772,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { Ok(InstructionOutcome::RunNextInstruction) } - fn run_sub(&mut self, context: &mut FunctionContext) -> Result + fn run_sub(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: ArithmeticOps { let (left, right) = context .value_stack_mut() @@ -783,7 +783,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { Ok(InstructionOutcome::RunNextInstruction) } - fn run_mul(&mut self, context: &mut FunctionContext) -> Result + fn run_mul(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: ArithmeticOps { let (left, right) = context .value_stack_mut() @@ -794,7 +794,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { Ok(InstructionOutcome::RunNextInstruction) } - fn run_div(&mut self, context: &mut FunctionContext) -> Result + fn run_div(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: TransmuteInto + Display, U: ArithmeticOps + TransmuteInto { let (left, right) = context .value_stack_mut() @@ -807,7 +807,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { Ok(InstructionOutcome::RunNextInstruction) } - fn run_rem(&mut self, context: &mut FunctionContext) -> Result + fn run_rem(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: TransmuteInto, U: Integer + TransmuteInto { let (left, right) = context .value_stack_mut() @@ -820,7 +820,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { Ok(InstructionOutcome::RunNextInstruction) } - fn run_and(&mut self, context: &mut FunctionContext) -> Result + fn run_and(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: From<::Output> + TryInto, T: ops::BitAnd { let (left, right) = context .value_stack_mut() @@ -831,7 +831,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { Ok(InstructionOutcome::RunNextInstruction) } - fn run_or(&mut self, context: &mut FunctionContext) -> Result + fn run_or(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: From<::Output> + TryInto, T: ops::BitOr { let (left, right) = context .value_stack_mut() @@ -842,7 +842,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { Ok(InstructionOutcome::RunNextInstruction) } - fn run_xor(&mut self, context: &mut FunctionContext) -> Result + fn run_xor(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: From<::Output> + TryInto, T: ops::BitXor { let (left, right) = context .value_stack_mut() @@ -853,7 +853,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { Ok(InstructionOutcome::RunNextInstruction) } - fn run_shl(&mut self, context: &mut FunctionContext, mask: T) -> Result + fn run_shl(&mut self, context: &mut FunctionContext, mask: T) -> Result where RuntimeValue: From<>::Output> + TryInto, T: ops::Shl + ops::BitAnd { let (left, right) = context .value_stack_mut() @@ -864,7 +864,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { Ok(InstructionOutcome::RunNextInstruction) } - fn run_shr(&mut self, context: &mut FunctionContext, mask: U) -> Result + fn run_shr(&mut self, context: &mut FunctionContext, mask: U) -> Result where RuntimeValue: From + TryInto, T: TransmuteInto, U: ops::Shr + ops::BitAnd, >::Output: TransmuteInto { let (left, right) = context .value_stack_mut() @@ -877,7 +877,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { Ok(InstructionOutcome::RunNextInstruction) } - fn run_rotl(&mut self, context: &mut FunctionContext) -> Result + fn run_rotl(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: Integer { let (left, right) = context .value_stack_mut() @@ -888,7 +888,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { Ok(InstructionOutcome::RunNextInstruction) } - fn run_rotr(&mut self, context: &mut FunctionContext) -> Result + fn run_rotr(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: Integer { let (left, right) = context @@ -900,13 +900,13 @@ impl<'a, E: Externals> Interpreter<'a, E> { Ok(InstructionOutcome::RunNextInstruction) } - fn run_abs(&mut self, context: &mut FunctionContext) -> Result + fn run_abs(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: Float { self.run_unop(context, |v| v.abs()) } - fn run_neg(&mut self, context: &mut FunctionContext) -> Result + fn run_neg(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: From<::Output> + TryInto, T: ops::Neg @@ -914,37 +914,37 @@ impl<'a, E: Externals> Interpreter<'a, E> { self.run_unop(context, |v| v.neg()) } - fn run_ceil(&mut self, context: &mut FunctionContext) -> Result + fn run_ceil(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: Float { self.run_unop(context, |v| v.ceil()) } - fn run_floor(&mut self, context: &mut FunctionContext) -> Result + fn run_floor(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: Float { self.run_unop(context, |v| v.floor()) } - fn run_trunc(&mut self, context: &mut FunctionContext) -> Result + fn run_trunc(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: Float { self.run_unop(context, |v| v.trunc()) } - fn run_nearest(&mut self, context: &mut FunctionContext) -> Result + fn run_nearest(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: Float { self.run_unop(context, |v| v.nearest()) } - fn run_sqrt(&mut self, context: &mut FunctionContext) -> Result + fn run_sqrt(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: Float { self.run_unop(context, |v| v.sqrt()) } - fn run_min(&mut self, context: &mut FunctionContext) -> Result + fn run_min(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: Float { let (left, right) = context @@ -956,7 +956,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { Ok(InstructionOutcome::RunNextInstruction) } - fn run_max(&mut self, context: &mut FunctionContext) -> Result + fn run_max(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: Float { let (left, right) = context .value_stack_mut() @@ -967,7 +967,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { Ok(InstructionOutcome::RunNextInstruction) } - fn run_copysign(&mut self, context: &mut FunctionContext) -> Result + fn run_copysign(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: Float { let (left, right) = context .value_stack_mut() @@ -978,13 +978,13 @@ impl<'a, E: Externals> Interpreter<'a, E> { Ok(InstructionOutcome::RunNextInstruction) } - fn run_wrap(&mut self, context: &mut FunctionContext) -> Result + fn run_wrap(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: WrapInto { self.run_unop(context, |v| v.wrap_into()) } - fn run_trunc_to_int(&mut self, context: &mut FunctionContext) -> Result - where RuntimeValue: From + TryInto, T: TryTruncateInto, U: TransmuteInto, { + fn run_trunc_to_int(&mut self, context: &mut FunctionContext) -> Result + where RuntimeValue: From + TryInto, T: TryTruncateInto, U: TransmuteInto, { let v = context .value_stack_mut() .pop_as::(); @@ -995,7 +995,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { .map(|_| InstructionOutcome::RunNextInstruction) } - fn run_extend(&mut self, context: &mut FunctionContext) -> Result + fn run_extend(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: From + TryInto, T: ExtendInto, U: TransmuteInto { @@ -1009,7 +1009,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { Ok(InstructionOutcome::RunNextInstruction) } - fn run_reinterpret(&mut self, context: &mut FunctionContext) -> Result + fn run_reinterpret(&mut self, context: &mut FunctionContext) -> Result where RuntimeValue: From, RuntimeValue: TryInto, T: TransmuteInto { @@ -1061,7 +1061,7 @@ impl FunctionContext { } } - pub fn nested(&mut self, function: FuncRef) -> Result { + pub fn nested(&mut self, function: FuncRef) -> Result { let (function_locals, module, function_return_type) = { let module = match *function.as_internal() { FuncInstanceInternal::Internal { ref module, .. } => module.upgrade().expect("module deallocated"), @@ -1127,7 +1127,7 @@ impl FunctionContext { &self.frame_stack } - pub fn push_frame(&mut self, labels: &HashMap, frame_type: BlockFrameType, block_type: BlockType) -> Result<(), Trap> { + pub fn push_frame(&mut self, labels: &HashMap, frame_type: BlockFrameType, block_type: BlockType) -> Result<(), TrapKind> { let begin_position = self.position; let branch_position = match frame_type { BlockFrameType::Function => usize::MAX, @@ -1153,7 +1153,7 @@ impl FunctionContext { branch_position: branch_position, end_position: end_position, value_stack_len: self.value_stack.len(), - }).map_err(|_| Trap::StackOverflow)?; + }).map_err(|_| TrapKind::StackOverflow)?; Ok(()) } @@ -1162,7 +1162,7 @@ impl FunctionContext { let _ = self.frame_stack.pop().expect("Due to validation frame stack shouldn't be empty"); } - pub fn pop_frame(&mut self, is_branch: bool) -> Result<(), Trap> { + pub fn pop_frame(&mut self, is_branch: bool) -> Result<(), TrapKind> { let frame = self.frame_stack .pop() .expect("Due to validation frame stack shouldn't be empty"); @@ -1189,9 +1189,9 @@ impl fmt::Debug for FunctionContext { } } -fn effective_address(address: u32, offset: u32) -> Result { +fn effective_address(address: u32, offset: u32) -> Result { match offset.checked_add(address) { - None => Err(Trap::MemoryAccessOutOfBounds), + None => Err(TrapKind::MemoryAccessOutOfBounds), Some(address) => Ok(address), } } @@ -1277,9 +1277,9 @@ impl ValueStack { self.stack_with_limit.pop().expect("Due to validation stack shouldn't be empty") } - fn push(&mut self, value: RuntimeValue) -> Result<(), Trap> { + fn push(&mut self, value: RuntimeValue) -> Result<(), TrapKind> { self.stack_with_limit.push(value) - .map_err(|_| Trap::StackOverflow) + .map_err(|_| TrapKind::StackOverflow) } fn resize(&mut self, new_len: usize) { diff --git a/src/tests/host.rs b/src/tests/host.rs index 211df56..0013cc5 100644 --- a/src/tests/host.rs +++ b/src/tests/host.rs @@ -1,7 +1,7 @@ use { Error, Signature, Externals, FuncInstance, FuncRef, HostError, ImportsBuilder, MemoryInstance, MemoryRef, TableInstance, TableRef, ModuleImportResolver, ModuleInstance, ModuleRef, - RuntimeValue, RuntimeArgs, TableDescriptor, MemoryDescriptor, Trap, + RuntimeValue, RuntimeArgs, TableDescriptor, MemoryDescriptor, TrapKind, }; use types::ValueType; use super::parse_wat; @@ -79,7 +79,7 @@ impl Externals for TestHost { &mut self, index: usize, args: RuntimeArgs, - ) -> Result, Trap> { + ) -> Result, TrapKind> { match index { SUB_FUNC_INDEX => { let a: i32 = args.nth(0); @@ -92,7 +92,7 @@ impl Externals for TestHost { ERR_FUNC_INDEX => { let error_code: u32 = args.nth(0); let error = HostErrorWithCode { error_code }; - Err(Trap::Host(Box::new(error))) + Err(TrapKind::Host(Box::new(error))) } INC_MEM_FUNC_INDEX => { let ptr: u32 = args.nth(0); @@ -131,7 +131,7 @@ impl Externals for TestHost { .expect("expected to be Some"); if val.value_type() != result.value_type() { - return Err(Trap::Host(Box::new(HostErrorWithCode { error_code: 123 }))); + return Err(TrapKind::Host(Box::new(HostErrorWithCode { error_code: 123 }))); } Ok(Some(result)) } @@ -258,7 +258,7 @@ fn host_err() { ); let host_error: Box = match error { - Error::Trap(Trap::Host(err)) => err, + Error::TrapKind(TrapKind::Host(err)) => err, err => panic!("Unexpected error {:?}", err), }; @@ -459,7 +459,7 @@ fn defer_providing_externals() { &mut self, index: usize, args: RuntimeArgs, - ) -> Result, Trap> { + ) -> Result, TrapKind> { match index { INC_FUNC_INDEX => { let a = args.nth::(0); @@ -523,7 +523,7 @@ fn two_envs_one_externals() { &mut self, index: usize, _args: RuntimeArgs, - ) -> Result, Trap> { + ) -> Result, TrapKind> { match index { PRIVILEGED_FUNC_INDEX => { println!("privileged!"); @@ -643,7 +643,7 @@ fn dynamically_add_host_func() { &mut self, index: usize, _args: RuntimeArgs, - ) -> Result, Trap> { + ) -> Result, TrapKind> { match index { ADD_FUNC_FUNC_INDEX => { // Allocate indicies for the new function. @@ -657,7 +657,7 @@ fn dynamically_add_host_func() { host_func_index as usize, ); self.table.set(table_index, Some(added_func)) - .map_err(|_| Trap::TableAccessOutOfBounds)?; + .map_err(|_| TrapKind::TableAccessOutOfBounds)?; Ok(Some(RuntimeValue::I32(table_index as i32))) } diff --git a/src/value.rs b/src/value.rs index 608f7ba..dd8f782 100644 --- a/src/value.rs +++ b/src/value.rs @@ -1,7 +1,7 @@ use std::{i32, i64, u32, u64, f32}; use std::io; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; -use {Trap}; +use {TrapKind}; use parity_wasm::elements::ValueType; #[derive(Debug)] @@ -72,7 +72,7 @@ pub trait ArithmeticOps { /// Multiply two values. fn mul(self, other: T) -> T; /// Divide two values. - fn div(self, other: T) -> Result; + fn div(self, other: T) -> Result; } /// Integer value. @@ -88,7 +88,7 @@ pub trait Integer: ArithmeticOps { /// Get right bit rotation result. fn rotr(self, other: T) -> T; /// Get division remainder. - fn rem(self, other: T) -> Result; + fn rem(self, other: T) -> Result; } /// Float-point value. @@ -270,19 +270,19 @@ impl_wrap_into!(f64, f32); macro_rules! impl_try_truncate_into { ($from: ident, $into: ident) => { - impl TryTruncateInto<$into, Trap> for $from { - fn try_truncate_into(self) -> Result<$into, Trap> { + impl TryTruncateInto<$into, TrapKind> for $from { + fn try_truncate_into(self) -> Result<$into, TrapKind> { // Casting from a float to an integer will round the float towards zero // NOTE: currently this will cause Undefined Behavior if the rounded value cannot be represented by the // target integer type. This includes Inf and NaN. This is a bug and will be fixed. if self.is_nan() || self.is_infinite() { - return Err(Trap::InvalidConversionToInt); + return Err(TrapKind::InvalidConversionToInt); } // range check let result = self as $into; if result as $from != self.trunc() { - return Err(Trap::InvalidConversionToInt); + return Err(TrapKind::InvalidConversionToInt); } Ok(self as $into) @@ -544,14 +544,14 @@ macro_rules! impl_integer_arithmetic_ops { fn add(self, other: $type) -> $type { self.wrapping_add(other) } fn sub(self, other: $type) -> $type { self.wrapping_sub(other) } fn mul(self, other: $type) -> $type { self.wrapping_mul(other) } - fn div(self, other: $type) -> Result<$type, Trap> { + fn div(self, other: $type) -> Result<$type, TrapKind> { if other == 0 { - Err(Trap::DivisionByZero) + Err(TrapKind::DivisionByZero) } else { let (result, overflow) = self.overflowing_div(other); if overflow { - Err(Trap::InvalidConversionToInt) + Err(TrapKind::InvalidConversionToInt) } else { Ok(result) } @@ -572,7 +572,7 @@ macro_rules! impl_float_arithmetic_ops { fn add(self, other: $type) -> $type { self + other } fn sub(self, other: $type) -> $type { self - other } fn mul(self, other: $type) -> $type { self * other } - fn div(self, other: $type) -> Result<$type, Trap> { Ok(self / other) } + fn div(self, other: $type) -> Result<$type, TrapKind> { Ok(self / other) } } } } @@ -588,8 +588,8 @@ macro_rules! impl_integer { fn count_ones(self) -> $type { self.count_ones() as $type } fn rotl(self, other: $type) -> $type { self.rotate_left(other as u32) } fn rotr(self, other: $type) -> $type { self.rotate_right(other as u32) } - fn rem(self, other: $type) -> Result<$type, Trap> { - if other == 0 { Err(Trap::DivisionByZero) } + fn rem(self, other: $type) -> Result<$type, TrapKind> { + if other == 0 { Err(TrapKind::DivisionByZero) } else { Ok(self.wrapping_rem(other)) } } }