Skip to content

Commit

Permalink
Implement CallIndirect instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
sile committed Jul 14, 2024
1 parent 96013dd commit 69e9ef9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,14 @@ impl<V: VectorFactory> Functype<V> {
}
}

impl<V: VectorFactory> PartialEq for Functype<V> {
fn eq(&self, other: &Self) -> bool {
self.params.as_ref() == other.params.as_ref() && self.result == other.result
}
}

impl<V: VectorFactory> Eq for Functype<V> {}

impl<V: VectorFactory> Decode<V> for Functype<V> {
fn decode(reader: &mut Reader) -> Result<Self, DecodeError> {
let tag = reader.read_u8()?;
Expand Down Expand Up @@ -473,7 +481,7 @@ impl<V: VectorFactory> Clone for Functype<V> {
}
}

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Resulttype(Option<Valtype>);

impl Resulttype {
Expand Down
22 changes: 20 additions & 2 deletions src/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub enum ExecuteError {
InvalidFuncidx,
InvalidTypeidx,
InvalidFuncArgs,
Trapped,
Trapped, // TODO: Trap
}

impl Display for ExecuteError {
Expand Down Expand Up @@ -318,7 +318,25 @@ impl<V: VectorFactory> Executor<V> {
self.call_function(*funcidx, funcs, module)?;
}
Instr::CallIndirect(typeidx) => {
todo!("CallIndirect: {typeidx:?}");
let expect_type = module
.types()
.get(typeidx.get())
.ok_or(ExecuteError::InvalidTypeidx)?;

let i = self.pop_value_i32() as usize;
let funcidx = self
.table
.get(i)
.ok_or(ExecuteError::Trapped)?
.ok_or(ExecuteError::Trapped)?;
let func = funcs
.get(funcidx.get())
.ok_or(ExecuteError::InvalidFuncidx)?;
let actual_type = func.get_type(module).ok_or(ExecuteError::InvalidFuncidx)?; // TODO
if expect_type != actual_type {
return Err(ExecuteError::Trapped);
}
self.call_function(funcidx, funcs, module)?;
}

// Parametric Instructions
Expand Down

0 comments on commit 69e9ef9

Please sign in to comment.