diff --git a/src/interpreter/instructions/call_core.rs b/src/interpreter/instructions/call_core.rs index dfaf520..7748649 100644 --- a/src/interpreter/instructions/call_core.rs +++ b/src/interpreter/instructions/call_core.rs @@ -13,61 +13,54 @@ executable_instruction!( let instance = &mut runtime.wasm_instance; let index = FunctionIndex::new(function_index); - match instance.local_or_import(index) { - Some(local_or_import) => { - let inputs_cardinality = local_or_import.inputs_cardinality(); - - match runtime.stack.pop(inputs_cardinality) { - Some(inputs) => { - let input_types = inputs - .iter() - .map(Into::into) - .collect::>(); - - if input_types != local_or_import.inputs() { - return Err( - InstructionError::new( - instruction, - InstructionErrorKind::LocalOrImportSignatureMismatch { - function_index: function_index as u32, - expected: (local_or_import.inputs().to_vec(), vec![]), - received: (input_types, vec![]), - } - ) - ) - } - - match local_or_import.call(&inputs) { - Ok(outputs) => { - for output in outputs.iter() { - runtime.stack.push(output.clone()); - } - - Ok(()) - } - Err(_) => Err( - InstructionError::new( - instruction, - InstructionErrorKind::LocalOrImportCall { function_index: function_index as u32, }, - ) - ) - } - } - None => Err( - InstructionError::new( - instruction, - InstructionErrorKind::StackIsTooSmall { needed: inputs_cardinality }, - ) - ) - } - } - None => Err( - InstructionError::new( - instruction, - InstructionErrorKind::LocalOrImportIsMissing { function_index: function_index as u32, }, - ) + let local_or_import = instance.local_or_import(index).ok_or_else(|| { + InstructionError::new( + instruction, + InstructionErrorKind::LocalOrImportIsMissing { + function_index: function_index as u32, + }, ) + })?; + let inputs_cardinality = local_or_import.inputs_cardinality(); + + let inputs = runtime.stack.pop(inputs_cardinality).ok_or_else(|| { + InstructionError::new( + instruction, + InstructionErrorKind::StackIsTooSmall { + needed: inputs_cardinality, + }, + ) + })?; + let input_types = inputs + .iter() + .map(Into::into) + .collect::>(); + + if input_types != local_or_import.inputs() { + return Err(InstructionError::new( + instruction, + InstructionErrorKind::LocalOrImportSignatureMismatch { + function_index: function_index as u32, + expected: (local_or_import.inputs().to_vec(), vec![]), + received: (input_types, vec![]), + }, + )); } + + let outputs = local_or_import.call(&inputs).map_err(|_| { + InstructionError::new( + instruction, + InstructionErrorKind::LocalOrImportCall { + function_index: function_index as u32, + }, + ) + })?; + + for output in outputs.iter() { + runtime.stack.push(output.clone()); + } + + Ok(()) } } ); diff --git a/src/interpreter/instructions/lowering_lifting.rs b/src/interpreter/instructions/lowering_lifting.rs index f18c243..d3903e6 100644 --- a/src/interpreter/instructions/lowering_lifting.rs +++ b/src/interpreter/instructions/lowering_lifting.rs @@ -18,31 +18,30 @@ macro_rules! lowering_lifting { |_| { InstructionError::new( instruction, - InstructionErrorKind::LoweringLifting { from: InterfaceType::$from_variant, to: InterfaceType::$to_variant }, + InstructionErrorKind::LoweringLifting { + from: InterfaceType::$from_variant, + to: InterfaceType::$to_variant + }, ) }, )?)) } Some(wrong_value) => { - return Err( - InstructionError::new( - instruction, - InstructionErrorKind::InvalidValueOnTheStack { - expected_type: InterfaceType::$from_variant, - received_type: (&wrong_value).into(), - } - ) - ) + return Err(InstructionError::new( + instruction, + InstructionErrorKind::InvalidValueOnTheStack { + expected_type: InterfaceType::$from_variant, + received_type: (&wrong_value).into(), + } + )) }, None => { - return Err( - InstructionError::new( - instruction, - InstructionErrorKind::StackIsTooSmall { needed: 1 }, - ) - ) + return Err(InstructionError::new( + instruction, + InstructionErrorKind::StackIsTooSmall { needed: 1 }, + )) } } diff --git a/src/interpreter/instructions/memory_to_string.rs b/src/interpreter/instructions/memory_to_string.rs index b1b9230..f96c47f 100644 --- a/src/interpreter/instructions/memory_to_string.rs +++ b/src/interpreter/instructions/memory_to_string.rs @@ -8,63 +8,50 @@ use std::cell::Cell; executable_instruction!( memory_to_string(instruction: Instruction) -> _ { move |runtime| -> _ { - match runtime.stack.pop(2) { - Some(inputs) => { - let memory_index: u32 = 0; + let inputs = runtime.stack.pop(2).ok_or_else(|| { + InstructionError::new( + instruction, + InstructionErrorKind::StackIsTooSmall { needed: 2 }, + ) + })?; - match runtime.wasm_instance.memory(memory_index as usize) { - Some(memory) => { - let length = to_native::(&inputs[0], instruction)? as usize; - let pointer = to_native::(&inputs[1], instruction)? as usize; - let memory_view = memory.view(); + let memory_index: u32 = 0; - if memory_view.len() < pointer + length { - return Err( - InstructionError::new( - instruction, - InstructionErrorKind::MemoryOutOfBoundsAccess { - index: pointer + length, - length: memory_view.len(), - } - ), - ) - } - - let data: Vec = (&memory_view[pointer..pointer + length]) - .iter() - .map(Cell::get) - .collect(); - - match String::from_utf8(data) { - Ok(string) => { - runtime.stack.push(InterfaceValue::String(string)); - - Ok(()) - } - Err(utf8_error) => Err( - InstructionError::new( - instruction, - InstructionErrorKind::String(utf8_error) - ), - ) - } - } - None => Err( - InstructionError::new( - instruction, - InstructionErrorKind::MemoryIsMissing { memory_index } - ), - ) - } - } - - None => Err( + let memory = runtime + .wasm_instance + .memory(memory_index as usize) + .ok_or_else(|| { InstructionError::new( instruction, - InstructionErrorKind::StackIsTooSmall { needed: 2 } - ), - ) + InstructionErrorKind::MemoryIsMissing { memory_index }, + ) + })?; + + let length = to_native::(&inputs[0], instruction)? as usize; + let pointer = to_native::(&inputs[1], instruction)? as usize; + let memory_view = memory.view(); + + if memory_view.len() < pointer + length { + return Err(InstructionError::new( + instruction, + InstructionErrorKind::MemoryOutOfBoundsAccess { + index: pointer + length, + length: memory_view.len(), + }, + )); } + + let data: Vec = (&memory_view[pointer..pointer + length]) + .iter() + .map(Cell::get) + .collect(); + + let string = String::from_utf8(data) + .map_err(|error| InstructionError::new(instruction, InstructionErrorKind::String(error)))?; + + runtime.stack.push(InterfaceValue::String(string)); + + Ok(()) } } );