From 0d026171efca8722e1766c2278d236961899d8b6 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Thu, 2 Apr 2020 11:51:34 +0200 Subject: [PATCH] chore(interface-types) Improve code readibility of `string` instructions. --- src/interpreter/instructions/strings.rs | 36 ++++++++++++------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/interpreter/instructions/strings.rs b/src/interpreter/instructions/strings.rs index 011cac9..f423dad 100644 --- a/src/interpreter/instructions/strings.rs +++ b/src/interpreter/instructions/strings.rs @@ -88,15 +88,15 @@ executable_instruction!( InstructionErrorKind::LocalOrImportSignatureMismatch { function_index: allocator_index, expected: (vec![InterfaceType::I32], vec![InterfaceType::I32]), - received: (allocator.inputs().to_vec(), allocator.outputs().to_vec()) - } + received: (allocator.inputs().to_vec(), allocator.outputs().to_vec()), + }, )) } let string = runtime.stack.pop1().ok_or_else(|| { InstructionError::new( instruction, - InstructionErrorKind::StackIsTooSmall { needed: 1 } + InstructionErrorKind::StackIsTooSmall { needed: 1 }, ) })?; @@ -118,7 +118,7 @@ executable_instruction!( .ok_or_else(|| { InstructionError::new( instruction, - InstructionErrorKind::MemoryIsMissing { memory_index } + InstructionErrorKind::MemoryIsMissing { memory_index }, ) })? .view(); @@ -138,26 +138,26 @@ executable_instruction!( executable_instruction!( string_size(instruction: Instruction) -> _ { move |runtime| -> _ { - let value = runtime.stack.peek1().ok_or_else(|| { - InstructionError::new( - instruction, - InstructionErrorKind::StackIsTooSmall { needed: 1 }, - ) - })?; + match runtime.stack.peek1() { + Some(InterfaceValue::String(string)) => { + let length = string.len() as i32; + runtime.stack.push(InterfaceValue::I32(length)); - if let InterfaceValue::String(string) = value { - let length = string.len() as i32; - runtime.stack.push(InterfaceValue::I32(length)); + Ok(()) + }, - Ok(()) - } else { - Err(InstructionError::new( + Some(value) => Err(InstructionError::new( instruction, InstructionErrorKind::InvalidValueOnTheStack { expected_type: InterfaceType::String, received_type: value.into(), - } - )) + }, + )), + + None => Err(InstructionError::new( + instruction, + InstructionErrorKind::StackIsTooSmall { needed: 1 }, + )), } } }