diff --git a/src/runner.rs b/src/runner.rs index 8c9eb2f..171410f 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -4,7 +4,7 @@ use std::{u32, usize}; use std::fmt; use std::iter::repeat; use std::collections::VecDeque; -use parity_wasm::elements::{BlockType, Local}; +use parity_wasm::elements::Local; use {Error, Trap, TrapKind, Signature}; use module::ModuleRef; use func::{FuncRef, FuncInstance, FuncInstanceInternal}; @@ -62,7 +62,13 @@ impl<'a, E: Externals> Interpreter<'a, E> { pub fn start_execution(&mut self, func: &FuncRef, args: &[RuntimeValue]) -> Result, Trap> { for arg in args { - self.value_stack.push(*arg).expect("TODO"); + self.value_stack + .push(*arg) + .map_err( + // There is not enough space for pushing initial arguments. + // Weird, but bail out anyway. + |_| Trap::from(TrapKind::StackOverflow) + )?; } let context = FunctionContext::new( @@ -197,20 +203,20 @@ impl<'a, E: Externals> Interpreter<'a, E> { &isa::Instruction::Unreachable => self.run_unreachable(context), &isa::Instruction::Br(ref target) => self.run_br(context, target.clone()), - &isa::Instruction::BrIfEqz(ref target) => self.run_br_eqz(context, target.clone()), - &isa::Instruction::BrIfNez(ref target) => self.run_br_nez(context, target.clone()), - &isa::Instruction::BrTable(ref targets) => self.run_br_table(context, targets), - &isa::Instruction::Return { drop, keep } => self.run_return(context, drop, keep), + &isa::Instruction::BrIfEqz(ref target) => self.run_br_eqz(target.clone()), + &isa::Instruction::BrIfNez(ref target) => self.run_br_nez(target.clone()), + &isa::Instruction::BrTable(ref targets) => self.run_br_table(targets), + &isa::Instruction::Return { drop, keep } => self.run_return(drop, keep), &isa::Instruction::Call(index) => self.run_call(context, index), &isa::Instruction::CallIndirect(index) => self.run_call_indirect(context, index), - &isa::Instruction::Drop => self.run_drop(context), - &isa::Instruction::Select => self.run_select(context), + &isa::Instruction::Drop => self.run_drop(), + &isa::Instruction::Select => self.run_select(), - &isa::Instruction::GetLocal(depth) => self.run_get_local(context, depth), - &isa::Instruction::SetLocal(depth) => self.run_set_local(context, depth), - &isa::Instruction::TeeLocal(depth) => self.run_tee_local(context, depth), + &isa::Instruction::GetLocal(depth) => self.run_get_local(depth), + &isa::Instruction::SetLocal(depth) => self.run_set_local(depth), + &isa::Instruction::TeeLocal(depth) => self.run_tee_local(depth), &isa::Instruction::GetGlobal(index) => self.run_get_global(context, index), &isa::Instruction::SetGlobal(index) => self.run_set_global(context, index), @@ -242,143 +248,143 @@ impl<'a, E: Externals> Interpreter<'a, E> { &isa::Instruction::CurrentMemory => self.run_current_memory(context), &isa::Instruction::GrowMemory => self.run_grow_memory(context), - &isa::Instruction::I32Const(val) => self.run_const(context, val.into()), - &isa::Instruction::I64Const(val) => self.run_const(context, val.into()), - &isa::Instruction::F32Const(val) => self.run_const(context, RuntimeValue::decode_f32(val)), - &isa::Instruction::F64Const(val) => self.run_const(context, RuntimeValue::decode_f64(val)), + &isa::Instruction::I32Const(val) => self.run_const(val.into()), + &isa::Instruction::I64Const(val) => self.run_const(val.into()), + &isa::Instruction::F32Const(val) => self.run_const(RuntimeValue::decode_f32(val)), + &isa::Instruction::F64Const(val) => self.run_const(RuntimeValue::decode_f64(val)), - &isa::Instruction::I32Eqz => self.run_eqz::(context), - &isa::Instruction::I32Eq => self.run_eq::(context), - &isa::Instruction::I32Ne => self.run_ne::(context), - &isa::Instruction::I32LtS => self.run_lt::(context), - &isa::Instruction::I32LtU => self.run_lt::(context), - &isa::Instruction::I32GtS => self.run_gt::(context), - &isa::Instruction::I32GtU => self.run_gt::(context), - &isa::Instruction::I32LeS => self.run_lte::(context), - &isa::Instruction::I32LeU => self.run_lte::(context), - &isa::Instruction::I32GeS => self.run_gte::(context), - &isa::Instruction::I32GeU => self.run_gte::(context), + &isa::Instruction::I32Eqz => self.run_eqz::(), + &isa::Instruction::I32Eq => self.run_eq::(), + &isa::Instruction::I32Ne => self.run_ne::(), + &isa::Instruction::I32LtS => self.run_lt::(), + &isa::Instruction::I32LtU => self.run_lt::(), + &isa::Instruction::I32GtS => self.run_gt::(), + &isa::Instruction::I32GtU => self.run_gt::(), + &isa::Instruction::I32LeS => self.run_lte::(), + &isa::Instruction::I32LeU => self.run_lte::(), + &isa::Instruction::I32GeS => self.run_gte::(), + &isa::Instruction::I32GeU => self.run_gte::(), - &isa::Instruction::I64Eqz => self.run_eqz::(context), - &isa::Instruction::I64Eq => self.run_eq::(context), - &isa::Instruction::I64Ne => self.run_ne::(context), - &isa::Instruction::I64LtS => self.run_lt::(context), - &isa::Instruction::I64LtU => self.run_lt::(context), - &isa::Instruction::I64GtS => self.run_gt::(context), - &isa::Instruction::I64GtU => self.run_gt::(context), - &isa::Instruction::I64LeS => self.run_lte::(context), - &isa::Instruction::I64LeU => self.run_lte::(context), - &isa::Instruction::I64GeS => self.run_gte::(context), - &isa::Instruction::I64GeU => self.run_gte::(context), + &isa::Instruction::I64Eqz => self.run_eqz::(), + &isa::Instruction::I64Eq => self.run_eq::(), + &isa::Instruction::I64Ne => self.run_ne::(), + &isa::Instruction::I64LtS => self.run_lt::(), + &isa::Instruction::I64LtU => self.run_lt::(), + &isa::Instruction::I64GtS => self.run_gt::(), + &isa::Instruction::I64GtU => self.run_gt::(), + &isa::Instruction::I64LeS => self.run_lte::(), + &isa::Instruction::I64LeU => self.run_lte::(), + &isa::Instruction::I64GeS => self.run_gte::(), + &isa::Instruction::I64GeU => self.run_gte::(), - &isa::Instruction::F32Eq => self.run_eq::(context), - &isa::Instruction::F32Ne => self.run_ne::(context), - &isa::Instruction::F32Lt => self.run_lt::(context), - &isa::Instruction::F32Gt => self.run_gt::(context), - &isa::Instruction::F32Le => self.run_lte::(context), - &isa::Instruction::F32Ge => self.run_gte::(context), + &isa::Instruction::F32Eq => self.run_eq::(), + &isa::Instruction::F32Ne => self.run_ne::(), + &isa::Instruction::F32Lt => self.run_lt::(), + &isa::Instruction::F32Gt => self.run_gt::(), + &isa::Instruction::F32Le => self.run_lte::(), + &isa::Instruction::F32Ge => self.run_gte::(), - &isa::Instruction::F64Eq => self.run_eq::(context), - &isa::Instruction::F64Ne => self.run_ne::(context), - &isa::Instruction::F64Lt => self.run_lt::(context), - &isa::Instruction::F64Gt => self.run_gt::(context), - &isa::Instruction::F64Le => self.run_lte::(context), - &isa::Instruction::F64Ge => self.run_gte::(context), + &isa::Instruction::F64Eq => self.run_eq::(), + &isa::Instruction::F64Ne => self.run_ne::(), + &isa::Instruction::F64Lt => self.run_lt::(), + &isa::Instruction::F64Gt => self.run_gt::(), + &isa::Instruction::F64Le => self.run_lte::(), + &isa::Instruction::F64Ge => self.run_gte::(), - &isa::Instruction::I32Clz => self.run_clz::(context), - &isa::Instruction::I32Ctz => self.run_ctz::(context), - &isa::Instruction::I32Popcnt => self.run_popcnt::(context), - &isa::Instruction::I32Add => self.run_add::(context), - &isa::Instruction::I32Sub => self.run_sub::(context), - &isa::Instruction::I32Mul => self.run_mul::(context), - &isa::Instruction::I32DivS => self.run_div::(context), - &isa::Instruction::I32DivU => self.run_div::(context), - &isa::Instruction::I32RemS => self.run_rem::(context), - &isa::Instruction::I32RemU => self.run_rem::(context), - &isa::Instruction::I32And => self.run_and::(context), - &isa::Instruction::I32Or => self.run_or::(context), - &isa::Instruction::I32Xor => self.run_xor::(context), - &isa::Instruction::I32Shl => self.run_shl::(context, 0x1F), - &isa::Instruction::I32ShrS => self.run_shr::(context, 0x1F), - &isa::Instruction::I32ShrU => self.run_shr::(context, 0x1F), - &isa::Instruction::I32Rotl => self.run_rotl::(context), - &isa::Instruction::I32Rotr => self.run_rotr::(context), + &isa::Instruction::I32Clz => self.run_clz::(), + &isa::Instruction::I32Ctz => self.run_ctz::(), + &isa::Instruction::I32Popcnt => self.run_popcnt::(), + &isa::Instruction::I32Add => self.run_add::(), + &isa::Instruction::I32Sub => self.run_sub::(), + &isa::Instruction::I32Mul => self.run_mul::(), + &isa::Instruction::I32DivS => self.run_div::(), + &isa::Instruction::I32DivU => self.run_div::(), + &isa::Instruction::I32RemS => self.run_rem::(), + &isa::Instruction::I32RemU => self.run_rem::(), + &isa::Instruction::I32And => self.run_and::(), + &isa::Instruction::I32Or => self.run_or::(), + &isa::Instruction::I32Xor => self.run_xor::(), + &isa::Instruction::I32Shl => self.run_shl::(0x1F), + &isa::Instruction::I32ShrS => self.run_shr::(0x1F), + &isa::Instruction::I32ShrU => self.run_shr::(0x1F), + &isa::Instruction::I32Rotl => self.run_rotl::(), + &isa::Instruction::I32Rotr => self.run_rotr::(), - &isa::Instruction::I64Clz => self.run_clz::(context), - &isa::Instruction::I64Ctz => self.run_ctz::(context), - &isa::Instruction::I64Popcnt => self.run_popcnt::(context), - &isa::Instruction::I64Add => self.run_add::(context), - &isa::Instruction::I64Sub => self.run_sub::(context), - &isa::Instruction::I64Mul => self.run_mul::(context), - &isa::Instruction::I64DivS => self.run_div::(context), - &isa::Instruction::I64DivU => self.run_div::(context), - &isa::Instruction::I64RemS => self.run_rem::(context), - &isa::Instruction::I64RemU => self.run_rem::(context), - &isa::Instruction::I64And => self.run_and::(context), - &isa::Instruction::I64Or => self.run_or::(context), - &isa::Instruction::I64Xor => self.run_xor::(context), - &isa::Instruction::I64Shl => self.run_shl::(context, 0x3F), - &isa::Instruction::I64ShrS => self.run_shr::(context, 0x3F), - &isa::Instruction::I64ShrU => self.run_shr::(context, 0x3F), - &isa::Instruction::I64Rotl => self.run_rotl::(context), - &isa::Instruction::I64Rotr => self.run_rotr::(context), + &isa::Instruction::I64Clz => self.run_clz::(), + &isa::Instruction::I64Ctz => self.run_ctz::(), + &isa::Instruction::I64Popcnt => self.run_popcnt::(), + &isa::Instruction::I64Add => self.run_add::(), + &isa::Instruction::I64Sub => self.run_sub::(), + &isa::Instruction::I64Mul => self.run_mul::(), + &isa::Instruction::I64DivS => self.run_div::(), + &isa::Instruction::I64DivU => self.run_div::(), + &isa::Instruction::I64RemS => self.run_rem::(), + &isa::Instruction::I64RemU => self.run_rem::(), + &isa::Instruction::I64And => self.run_and::(), + &isa::Instruction::I64Or => self.run_or::(), + &isa::Instruction::I64Xor => self.run_xor::(), + &isa::Instruction::I64Shl => self.run_shl::(0x3F), + &isa::Instruction::I64ShrS => self.run_shr::(0x3F), + &isa::Instruction::I64ShrU => self.run_shr::(0x3F), + &isa::Instruction::I64Rotl => self.run_rotl::(), + &isa::Instruction::I64Rotr => self.run_rotr::(), - &isa::Instruction::F32Abs => self.run_abs::(context), - &isa::Instruction::F32Neg => self.run_neg::(context), - &isa::Instruction::F32Ceil => self.run_ceil::(context), - &isa::Instruction::F32Floor => self.run_floor::(context), - &isa::Instruction::F32Trunc => self.run_trunc::(context), - &isa::Instruction::F32Nearest => self.run_nearest::(context), - &isa::Instruction::F32Sqrt => self.run_sqrt::(context), - &isa::Instruction::F32Add => self.run_add::(context), - &isa::Instruction::F32Sub => self.run_sub::(context), - &isa::Instruction::F32Mul => self.run_mul::(context), - &isa::Instruction::F32Div => self.run_div::(context), - &isa::Instruction::F32Min => self.run_min::(context), - &isa::Instruction::F32Max => self.run_max::(context), - &isa::Instruction::F32Copysign => self.run_copysign::(context), + &isa::Instruction::F32Abs => self.run_abs::(), + &isa::Instruction::F32Neg => self.run_neg::(), + &isa::Instruction::F32Ceil => self.run_ceil::(), + &isa::Instruction::F32Floor => self.run_floor::(), + &isa::Instruction::F32Trunc => self.run_trunc::(), + &isa::Instruction::F32Nearest => self.run_nearest::(), + &isa::Instruction::F32Sqrt => self.run_sqrt::(), + &isa::Instruction::F32Add => self.run_add::(), + &isa::Instruction::F32Sub => self.run_sub::(), + &isa::Instruction::F32Mul => self.run_mul::(), + &isa::Instruction::F32Div => self.run_div::(), + &isa::Instruction::F32Min => self.run_min::(), + &isa::Instruction::F32Max => self.run_max::(), + &isa::Instruction::F32Copysign => self.run_copysign::(), - &isa::Instruction::F64Abs => self.run_abs::(context), - &isa::Instruction::F64Neg => self.run_neg::(context), - &isa::Instruction::F64Ceil => self.run_ceil::(context), - &isa::Instruction::F64Floor => self.run_floor::(context), - &isa::Instruction::F64Trunc => self.run_trunc::(context), - &isa::Instruction::F64Nearest => self.run_nearest::(context), - &isa::Instruction::F64Sqrt => self.run_sqrt::(context), - &isa::Instruction::F64Add => self.run_add::(context), - &isa::Instruction::F64Sub => self.run_sub::(context), - &isa::Instruction::F64Mul => self.run_mul::(context), - &isa::Instruction::F64Div => self.run_div::(context), - &isa::Instruction::F64Min => self.run_min::(context), - &isa::Instruction::F64Max => self.run_max::(context), - &isa::Instruction::F64Copysign => self.run_copysign::(context), + &isa::Instruction::F64Abs => self.run_abs::(), + &isa::Instruction::F64Neg => self.run_neg::(), + &isa::Instruction::F64Ceil => self.run_ceil::(), + &isa::Instruction::F64Floor => self.run_floor::(), + &isa::Instruction::F64Trunc => self.run_trunc::(), + &isa::Instruction::F64Nearest => self.run_nearest::(), + &isa::Instruction::F64Sqrt => self.run_sqrt::(), + &isa::Instruction::F64Add => self.run_add::(), + &isa::Instruction::F64Sub => self.run_sub::(), + &isa::Instruction::F64Mul => self.run_mul::(), + &isa::Instruction::F64Div => self.run_div::(), + &isa::Instruction::F64Min => self.run_min::(), + &isa::Instruction::F64Max => self.run_max::(), + &isa::Instruction::F64Copysign => self.run_copysign::(), - &isa::Instruction::I32WrapI64 => self.run_wrap::(context), - &isa::Instruction::I32TruncSF32 => self.run_trunc_to_int::(context), - &isa::Instruction::I32TruncUF32 => self.run_trunc_to_int::(context), - &isa::Instruction::I32TruncSF64 => self.run_trunc_to_int::(context), - &isa::Instruction::I32TruncUF64 => self.run_trunc_to_int::(context), - &isa::Instruction::I64ExtendSI32 => self.run_extend::(context), - &isa::Instruction::I64ExtendUI32 => self.run_extend::(context), - &isa::Instruction::I64TruncSF32 => self.run_trunc_to_int::(context), - &isa::Instruction::I64TruncUF32 => self.run_trunc_to_int::(context), - &isa::Instruction::I64TruncSF64 => self.run_trunc_to_int::(context), - &isa::Instruction::I64TruncUF64 => self.run_trunc_to_int::(context), - &isa::Instruction::F32ConvertSI32 => self.run_extend::(context), - &isa::Instruction::F32ConvertUI32 => self.run_extend::(context), - &isa::Instruction::F32ConvertSI64 => self.run_wrap::(context), - &isa::Instruction::F32ConvertUI64 => self.run_wrap::(context), - &isa::Instruction::F32DemoteF64 => self.run_wrap::(context), - &isa::Instruction::F64ConvertSI32 => self.run_extend::(context), - &isa::Instruction::F64ConvertUI32 => self.run_extend::(context), - &isa::Instruction::F64ConvertSI64 => self.run_extend::(context), - &isa::Instruction::F64ConvertUI64 => self.run_extend::(context), - &isa::Instruction::F64PromoteF32 => self.run_extend::(context), + &isa::Instruction::I32WrapI64 => self.run_wrap::(), + &isa::Instruction::I32TruncSF32 => self.run_trunc_to_int::(), + &isa::Instruction::I32TruncUF32 => self.run_trunc_to_int::(), + &isa::Instruction::I32TruncSF64 => self.run_trunc_to_int::(), + &isa::Instruction::I32TruncUF64 => self.run_trunc_to_int::(), + &isa::Instruction::I64ExtendSI32 => self.run_extend::(), + &isa::Instruction::I64ExtendUI32 => self.run_extend::(), + &isa::Instruction::I64TruncSF32 => self.run_trunc_to_int::(), + &isa::Instruction::I64TruncUF32 => self.run_trunc_to_int::(), + &isa::Instruction::I64TruncSF64 => self.run_trunc_to_int::(), + &isa::Instruction::I64TruncUF64 => self.run_trunc_to_int::(), + &isa::Instruction::F32ConvertSI32 => self.run_extend::(), + &isa::Instruction::F32ConvertUI32 => self.run_extend::(), + &isa::Instruction::F32ConvertSI64 => self.run_wrap::(), + &isa::Instruction::F32ConvertUI64 => self.run_wrap::(), + &isa::Instruction::F32DemoteF64 => self.run_wrap::(), + &isa::Instruction::F64ConvertSI32 => self.run_extend::(), + &isa::Instruction::F64ConvertUI32 => self.run_extend::(), + &isa::Instruction::F64ConvertSI64 => self.run_extend::(), + &isa::Instruction::F64ConvertUI64 => self.run_extend::(), + &isa::Instruction::F64PromoteF32 => self.run_extend::(), - &isa::Instruction::I32ReinterpretF32 => self.run_reinterpret::(context), - &isa::Instruction::I64ReinterpretF64 => self.run_reinterpret::(context), - &isa::Instruction::F32ReinterpretI32 => self.run_reinterpret::(context), - &isa::Instruction::F64ReinterpretI64 => self.run_reinterpret::(context), + &isa::Instruction::I32ReinterpretF32 => self.run_reinterpret::(), + &isa::Instruction::I64ReinterpretF64 => self.run_reinterpret::(), + &isa::Instruction::F32ReinterpretI32 => self.run_reinterpret::(), + &isa::Instruction::F64ReinterpretI64 => self.run_reinterpret::(), } } @@ -390,7 +396,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { Ok(InstructionOutcome::Branch(target)) } - fn run_br_nez(&mut self, context: &mut FunctionContext, target: isa::Target) -> Result { + fn run_br_nez(&mut self, target: isa::Target) -> Result { let condition = self.value_stack.pop_as(); if condition { Ok(InstructionOutcome::Branch(target)) @@ -399,7 +405,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { } } - fn run_br_eqz(&mut self, context: &mut FunctionContext, target: isa::Target) -> Result { + fn run_br_eqz(&mut self, target: isa::Target) -> Result { let condition = self.value_stack.pop_as(); if condition { Ok(InstructionOutcome::RunNextInstruction) @@ -409,7 +415,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { } } - fn run_br_table(&mut self, context: &mut FunctionContext, table: &[isa::Target]) -> Result { + fn run_br_table(&mut self, table: &[isa::Target]) -> Result { let index: u32 = self.value_stack .pop_as(); @@ -423,7 +429,7 @@ impl<'a, E: Externals> Interpreter<'a, E> { Ok(InstructionOutcome::Branch(dst)) } - fn run_return(&mut self, _context: &mut FunctionContext, drop: u32, keep: u8) -> Result { + fn run_return(&mut self, drop: u32, keep: u8) -> Result { Ok(InstructionOutcome::Return(drop, keep)) } @@ -470,14 +476,14 @@ impl<'a, E: Externals> Interpreter<'a, E> { Ok(InstructionOutcome::ExecuteCall(func_ref)) } - fn run_drop(&mut self, context: &mut FunctionContext) -> Result { + fn run_drop(&mut self) -> Result { let _ = self .value_stack .pop(); Ok(InstructionOutcome::RunNextInstruction) } - fn run_select(&mut self, context: &mut FunctionContext) -> Result { + fn run_select(&mut self) -> Result { let (left, mid, right) = self .value_stack .pop_triple(); @@ -490,13 +496,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, index: u32) -> Result { let val = *self.value_stack.pick_mut(index as usize - 1); self.value_stack.push(val)?; Ok(InstructionOutcome::RunNextInstruction) } - fn run_set_local(&mut self, context: &mut FunctionContext, index: u32) -> Result { + fn run_set_local(&mut self, index: u32) -> Result { let val = self .value_stack .pop(); @@ -504,7 +510,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, index: u32) -> Result { let val = self .value_stack .top() @@ -672,7 +678,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, val: RuntimeValue) -> Result { self .value_stack .push(val) @@ -680,7 +686,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, f: F) -> Result where T: FromRuntimeValue, F: FnOnce(T, T) -> bool, @@ -698,7 +704,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) -> Result where T: FromRuntimeValue, T: PartialEq + Default { let v = self .value_stack @@ -708,38 +714,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) -> Result where T: FromRuntimeValue + PartialEq { - self.run_relop(context, |left: T, right: T| left == right) + self.run_relop(|left: T, right: T| left == right) } - fn run_ne(&mut self, context: &mut FunctionContext) -> Result + fn run_ne(&mut self) -> Result where T: FromRuntimeValue + PartialEq { - self.run_relop(context, |left: T, right: T| left != right) + self.run_relop(|left: T, right: T| left != right) } - fn run_lt(&mut self, context: &mut FunctionContext) -> Result + fn run_lt(&mut self) -> Result where T: FromRuntimeValue + PartialOrd { - self.run_relop(context, |left: T, right: T| left < right) + self.run_relop(|left: T, right: T| left < right) } - fn run_gt(&mut self, context: &mut FunctionContext) -> Result + fn run_gt(&mut self) -> Result where T: FromRuntimeValue + PartialOrd { - self.run_relop(context, |left: T, right: T| left > right) + self.run_relop(|left: T, right: T| left > right) } - fn run_lte(&mut self, context: &mut FunctionContext) -> Result + fn run_lte(&mut self) -> Result where T: FromRuntimeValue + PartialOrd { - self.run_relop(context, |left: T, right: T| left <= right) + self.run_relop(|left: T, right: T| left <= right) } - fn run_gte(&mut self, context: &mut FunctionContext) -> Result + fn run_gte(&mut self) -> Result where T: FromRuntimeValue + PartialOrd { - self.run_relop(context, |left: T, right: T| left >= right) + self.run_relop(|left: T, right: T| left >= right) } - fn run_unop(&mut self, context: &mut FunctionContext, f: F) -> Result + fn run_unop(&mut self, f: F) -> Result where F: FnOnce(T) -> U, T: FromRuntimeValue, @@ -753,22 +759,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) -> Result where RuntimeValue: From, T: Integer + FromRuntimeValue { - self.run_unop(context, |v: T| v.leading_zeros()) + self.run_unop(|v: T| v.leading_zeros()) } - fn run_ctz(&mut self, context: &mut FunctionContext) -> Result + fn run_ctz(&mut self) -> Result where RuntimeValue: From, T: Integer + FromRuntimeValue { - self.run_unop(context, |v: T| v.trailing_zeros()) + self.run_unop(|v: T| v.trailing_zeros()) } - fn run_popcnt(&mut self, context: &mut FunctionContext) -> Result + fn run_popcnt(&mut self) -> Result where RuntimeValue: From, T: Integer + FromRuntimeValue { - self.run_unop(context, |v: T| v.count_ones()) + self.run_unop(|v: T| v.count_ones()) } - fn run_add(&mut self, context: &mut FunctionContext) -> Result + fn run_add(&mut self) -> Result where RuntimeValue: From, T: ArithmeticOps + FromRuntimeValue { let (left, right) = self .value_stack @@ -779,7 +785,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) -> Result where RuntimeValue: From, T: ArithmeticOps + FromRuntimeValue { let (left, right) = self .value_stack @@ -790,7 +796,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) -> Result where RuntimeValue: From, T: ArithmeticOps + FromRuntimeValue { let (left, right) = self .value_stack @@ -801,7 +807,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) -> Result where RuntimeValue: From, T: TransmuteInto + FromRuntimeValue, U: ArithmeticOps + TransmuteInto { let (left, right) = self .value_stack @@ -814,7 +820,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) -> Result where RuntimeValue: From, T: TransmuteInto + FromRuntimeValue, U: Integer + TransmuteInto { let (left, right) = self .value_stack @@ -827,7 +833,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) -> Result where RuntimeValue: From<::Output>, T: ops::BitAnd + FromRuntimeValue { let (left, right) = self .value_stack @@ -838,7 +844,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) -> Result where RuntimeValue: From<::Output>, T: ops::BitOr + FromRuntimeValue { let (left, right) = self .value_stack @@ -849,7 +855,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) -> Result where RuntimeValue: From<::Output>, T: ops::BitXor + FromRuntimeValue { let (left, right) = self .value_stack @@ -860,7 +866,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, mask: T) -> Result where RuntimeValue: From<>::Output>, T: ops::Shl + ops::BitAnd + FromRuntimeValue { let (left, right) = self .value_stack @@ -871,7 +877,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, mask: U) -> Result where RuntimeValue: From, T: TransmuteInto + FromRuntimeValue, U: ops::Shr + ops::BitAnd, >::Output: TransmuteInto { let (left, right) = self .value_stack @@ -884,7 +890,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) -> Result where RuntimeValue: From, T: Integer + FromRuntimeValue { let (left, right) = self .value_stack @@ -895,7 +901,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) -> Result where RuntimeValue: From, T: Integer + FromRuntimeValue { let (left, right) = self @@ -907,51 +913,51 @@ impl<'a, E: Externals> Interpreter<'a, E> { Ok(InstructionOutcome::RunNextInstruction) } - fn run_abs(&mut self, context: &mut FunctionContext) -> Result + fn run_abs(&mut self) -> Result where RuntimeValue: From, T: Float + FromRuntimeValue { - self.run_unop(context, |v: T| v.abs()) + self.run_unop(|v: T| v.abs()) } - fn run_neg(&mut self, context: &mut FunctionContext) -> Result + fn run_neg(&mut self) -> Result where RuntimeValue: From<::Output>, T: ops::Neg + FromRuntimeValue { - self.run_unop(context, |v: T| v.neg()) + self.run_unop(|v: T| v.neg()) } - fn run_ceil(&mut self, context: &mut FunctionContext) -> Result + fn run_ceil(&mut self) -> Result where RuntimeValue: From, T: Float + FromRuntimeValue { - self.run_unop(context, |v: T| v.ceil()) + self.run_unop(|v: T| v.ceil()) } - fn run_floor(&mut self, context: &mut FunctionContext) -> Result + fn run_floor(&mut self) -> Result where RuntimeValue: From, T: Float + FromRuntimeValue { - self.run_unop(context, |v: T| v.floor()) + self.run_unop(|v: T| v.floor()) } - fn run_trunc(&mut self, context: &mut FunctionContext) -> Result + fn run_trunc(&mut self) -> Result where RuntimeValue: From, T: Float + FromRuntimeValue { - self.run_unop(context, |v: T| v.trunc()) + self.run_unop(|v: T| v.trunc()) } - fn run_nearest(&mut self, context: &mut FunctionContext) -> Result + fn run_nearest(&mut self) -> Result where RuntimeValue: From, T: Float + FromRuntimeValue { - self.run_unop(context, |v: T| v.nearest()) + self.run_unop(|v: T| v.nearest()) } - fn run_sqrt(&mut self, context: &mut FunctionContext) -> Result + fn run_sqrt(&mut self) -> Result where RuntimeValue: From, T: Float + FromRuntimeValue { - self.run_unop(context, |v: T| v.sqrt()) + self.run_unop(|v: T| v.sqrt()) } - fn run_min(&mut self, context: &mut FunctionContext) -> Result + fn run_min(&mut self) -> Result where RuntimeValue: From, T: Float + FromRuntimeValue { let (left, right) = self @@ -963,7 +969,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) -> Result where RuntimeValue: From, T: Float + FromRuntimeValue { let (left, right) = self .value_stack @@ -974,7 +980,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) -> Result where RuntimeValue: From, T: Float + FromRuntimeValue { let (left, right) = self .value_stack @@ -985,12 +991,12 @@ impl<'a, E: Externals> Interpreter<'a, E> { Ok(InstructionOutcome::RunNextInstruction) } - fn run_wrap(&mut self, context: &mut FunctionContext) -> Result + fn run_wrap(&mut self) -> Result where RuntimeValue: From, T: WrapInto + FromRuntimeValue { - self.run_unop(context, |v: T| v.wrap_into()) + self.run_unop(|v: T| v.wrap_into()) } - fn run_trunc_to_int(&mut self, context: &mut FunctionContext) -> Result + fn run_trunc_to_int(&mut self) -> Result where RuntimeValue: From, T: TryTruncateInto + FromRuntimeValue, U: TransmuteInto, { let v = self .value_stack @@ -1002,7 +1008,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) -> Result where RuntimeValue: From, T: ExtendInto + FromRuntimeValue, U: TransmuteInto { @@ -1016,7 +1022,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) -> Result where RuntimeValue: From, T: FromRuntimeValue, T: TransmuteInto { @@ -1057,14 +1063,9 @@ impl FunctionContext { } pub fn nested(&mut self, function: FuncRef) -> Result { - let module = { - let module = match *function.as_internal() { - FuncInstanceInternal::Internal { ref module, .. } => module.upgrade().expect("module deallocated"), - FuncInstanceInternal::Host { .. } => panic!("Host functions can't be called as internally defined functions; Thus FunctionContext can be created only with internally defined functions; qed"), - }; - let function_type = function.signature(); - let function_return_type = function_type.return_type().map(|vt| BlockType::Value(vt.into_elements())).unwrap_or(BlockType::NoResult); - module + let module = match *function.as_internal() { + FuncInstanceInternal::Internal { ref module, .. } => module.upgrade().expect("module deallocated"), + FuncInstanceInternal::Host { .. } => panic!("Host functions can't be called as internally defined functions; Thus FunctionContext can be created only with internally defined functions; qed"), }; Ok(FunctionContext {