Skip to content

Commit

Permalink
feat(interface-types) Reformat the instructions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hywan committed Mar 10, 2020
1 parent 2b94d6d commit 5088382
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 117 deletions.
93 changes: 43 additions & 50 deletions lib/interface-types/src/interpreter/instructions/call_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>>();
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();

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 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>>();

match local_or_import.call(&inputs) {
Ok(outputs) => {
for output in outputs.iter() {
runtime.stack.push(output.clone());
}
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![]),
},
));
}

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 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(())
}
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 memory = runtime
.wasm_instance
.memory(memory_index as usize)
.ok_or_else(|| {
InstructionError::new(
instruction,
InstructionErrorKind::MemoryIsMissing { memory_index },
)
})?;

let data: Vec<u8> = (&memory_view[pointer..pointer + length])
.iter()
.map(Cell::get)
.collect();
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();

match String::from_utf8(data) {
Ok(string) => {
runtime.stack.push(InterfaceValue::String(string));
if memory_view.len() < pointer + length {
return Err(InstructionError::new(
instruction,
InstructionErrorKind::MemoryOutOfBoundsAccess {
index: pointer + length,
length: memory_view.len(),
},
));
}

Ok(())
}
Err(utf8_error) => Err(
InstructionError::new(
instruction,
InstructionErrorKind::String(utf8_error)
),
)
}
}
None => Err(
InstructionError::new(
instruction,
InstructionErrorKind::MemoryIsMissing { memory_index }
),
)
}
}
let data: Vec<u8> = (&memory_view[pointer..pointer + length])
.iter()
.map(Cell::get)
.collect();

None => Err(
InstructionError::new(
instruction,
InstructionErrorKind::StackIsTooSmall { needed: 2 }
),
)
}
let string = String::from_utf8(data)
.map_err(|error| InstructionError::new(instruction, InstructionErrorKind::String(error)))?;

runtime.stack.push(InterfaceValue::String(string));

Ok(())
}
}
);
Expand Down

0 comments on commit 5088382

Please sign in to comment.