feat(interface-types) Reformat the instructions.

This commit is contained in:
Ivan Enderlin 2020-03-10 15:53:46 +01:00
parent 864ac79123
commit 7d473b7bdc
3 changed files with 100 additions and 121 deletions

View File

@ -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::<Vec<InterfaceType>>();
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::<Vec<InterfaceType>>();
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(())
}
}
);

View File

@ -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 },
))
}
}

View File

@ -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::<i32>(&inputs[0], instruction)? as usize;
let pointer = to_native::<i32>(&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<u8> = (&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::<i32>(&inputs[0], instruction)? as usize;
let pointer = to_native::<i32>(&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<u8> = (&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(())
}
}
);