call_indirect traps.

This commit is contained in:
Sergey Pepyakin 2018-01-29 17:23:44 +03:00
parent 19c56c42b6
commit 4765aecf78
3 changed files with 9 additions and 13 deletions

View File

@ -110,6 +110,9 @@ use std::collections::HashMap;
pub enum Trap {
Unreachable,
MemoryAccessOutOfBounds,
TableAccessOutOfBounds,
ElemUninitialized,
ElemSignatureMismatch,
}
/// Internal interpreter error.

View File

@ -438,7 +438,9 @@ impl<'a, E: Externals> Interpreter<'a, E> {
.module()
.table_by_index(DEFAULT_TABLE_INDEX)
.expect("Due to validation table should exists");
let func_ref = table.get(table_func_idx)?;
let func_ref = table.get(table_func_idx)
.map_err(|_| Error::Trap(Trap::TableAccessOutOfBounds))?
.ok_or_else(|| Error::Trap(Trap::ElemUninitialized))?;
{
let actual_function_type = func_ref.signature();
@ -448,13 +450,7 @@ impl<'a, E: Externals> Interpreter<'a, E> {
.expect("Due to validation type should exists");
if &*required_function_type != actual_function_type {
return Err(Error::Function(format!(
"expected function with signature ({:?}) -> {:?} when got with ({:?}) -> {:?}",
required_function_type.params(),
required_function_type.return_type(),
actual_function_type.params(),
actual_function_type.return_type()
)));
return Err(Error::Trap(Trap::ElemSignatureMismatch));
}
}

View File

@ -107,7 +107,7 @@ impl TableInstance {
}
/// Get the specific value in the table
pub fn get(&self, offset: u32) -> Result<FuncRef, Error> {
pub fn get(&self, offset: u32) -> Result<Option<FuncRef>, Error> {
let buffer = self.buffer.borrow();
let buffer_len = buffer.len();
let table_elem = buffer.get(offset as usize).cloned().ok_or_else(||
@ -117,10 +117,7 @@ impl TableInstance {
buffer_len
)),
)?;
Ok(table_elem.ok_or_else(|| Error::Table(format!(
"trying to read uninitialized element on index {}",
offset
)))?)
Ok(table_elem)
}
/// Set the table element to the specified function.