Skip to content

Commit

Permalink
feature/named_invoke - initial draft
Browse files Browse the repository at this point in the history
  • Loading branch information
george-cosma committed Jul 17, 2024
1 parent eb2c7d0 commit dc86604
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/core/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::core::reader::types::ValType;
pub enum RuntimeError {
DivideBy0,
UnrepresentableResult,
FunctionNotFound,
}

#[derive(Debug, PartialEq, Eq, Clone)]
Expand Down Expand Up @@ -107,6 +108,7 @@ impl Display for Error {
Error::RuntimeError(err) => match err {
RuntimeError::DivideBy0 => f.write_str("Divide by zero is not permitted"),
RuntimeError::UnrepresentableResult => f.write_str("Result is unrepresentable"),
RuntimeError::FunctionNotFound => f.write_str("Function not found"),
},
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/reader/types/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::core::reader::{WasmReadable, WasmReader};
use crate::execution::assert_validated::UnwrapValidatedExt;
use crate::{unreachable_validated, Error, Result};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Export {
#[allow(dead_code)]
pub name: String,
Expand All @@ -28,7 +28,7 @@ impl WasmReadable for Export {
}
}

#[derive(Debug)]
#[derive(Debug, Clone)]
#[allow(clippy::all)]
pub enum ExportDesc {
#[allow(warnings)]
Expand Down
28 changes: 27 additions & 1 deletion src/execution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use alloc::vec::Vec;
use value_stack::Stack;

use crate::core::indices::{FuncIdx, GlobalIdx, LocalIdx};
use crate::core::reader::types::export::{Export, ExportDesc};
use crate::core::reader::types::memarg::MemArg;
use crate::core::reader::types::{FuncType, NumType, ValType};
use crate::core::reader::{WasmReadable, WasmReader};
Expand All @@ -14,7 +15,7 @@ use crate::execution::value::Value;
use crate::validation::code::read_declared_locals;
use crate::value::InteropValueList;
use crate::Error::RuntimeError;
use crate::RuntimeError::{DivideBy0, UnrepresentableResult};
use crate::RuntimeError::{DivideBy0, FunctionNotFound, UnrepresentableResult};
use crate::{Result, ValidationInfo};

// TODO
Expand All @@ -32,6 +33,7 @@ where
{
pub wasm_bytecode: &'b [u8],
types: Vec<FuncType>,
exports: Vec<Export>,
store: Store,
pub hook_set: H,
}
Expand All @@ -54,6 +56,7 @@ where
let mut instance = RuntimeInstance {
wasm_bytecode: validation_info.wasm,
types: validation_info.types.clone(),
exports: validation_info.exports.clone(),
store,
hook_set,
};
Expand All @@ -68,6 +71,29 @@ where
Ok(instance)
}

pub fn invoke_named<Param: InteropValueList, Returns: InteropValueList>(
&mut self,
func_name: &str,
param: Param,
) -> Result<Returns> {
let func_idx = self.exports.iter().find_map(|export| {
if export.name == func_name {
match export.desc {
ExportDesc::FuncIdx(idx) => Some(idx),
_ => None,
}
} else {
None
}
});

if let Some(func_idx) = func_idx {
self.invoke_func(func_idx, param)
} else {
Err(RuntimeError(FunctionNotFound))
}
}

/// Can only invoke functions with signature `[t1] -> [t2]` as of now.
pub fn invoke_func<Param: InteropValueList, Returns: InteropValueList>(
&mut self,
Expand Down
6 changes: 3 additions & 3 deletions tests/add_one.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn add_one() {
let validation_info = validate(&wasm_bytes).expect("validation failed");
let mut instance = RuntimeInstance::new(&validation_info).expect("instantiation failed");

assert_eq!(12, instance.invoke_func(0, 11).unwrap());
assert_eq!(1, instance.invoke_func(0, 0).unwrap());
assert_eq!(-5, instance.invoke_func(0, -6).unwrap());
assert_eq!(12, instance.invoke_named("add_one", 11).unwrap());
assert_eq!(1, instance.invoke_named("add_one", 0).unwrap());
assert_eq!(-5, instance.invoke_named("add_one", -6).unwrap());
}

0 comments on commit dc86604

Please sign in to comment.