Skip to content

Commit

Permalink
chore: translate tests, exmaple and benchmark to new api
Browse files Browse the repository at this point in the history
Signed-off-by: George Cosma <[email protected]>
  • Loading branch information
george-cosma committed Sep 24, 2024
1 parent c35be60 commit a973d0f
Show file tree
Hide file tree
Showing 22 changed files with 5,647 additions and 1,299 deletions.
10 changes: 6 additions & 4 deletions benches/hook_performance_impact.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};

use wasm::{hooks::HookSet, validate, RuntimeInstance};
use wasm::{hooks::HookSet, validate, RuntimeInstance, DEFAULT_MODULE};

fn criterion_benchmark(c: &mut Criterion) {
let wat = r#"
Expand Down Expand Up @@ -52,14 +52,16 @@ fn criterion_benchmark(c: &mut Criterion) {
}

let mut instance_non_empty_hookset =
RuntimeInstance::new_with_hooks(&validation_info, MyCustomHookSet).unwrap();
RuntimeInstance::new_with_hooks(DEFAULT_MODULE, &validation_info, MyCustomHookSet).unwrap();

c.bench_function("invoke_func EmptyHookSet", |b| {
b.iter(|| instance_empty_hookset.invoke_func::<_, ()>(black_box(2), black_box(42_i32)))
let test_fn = instance_empty_hookset.get_fn_idx(0, 2).unwrap();
b.iter(|| instance_empty_hookset.invoke::<_, ()>(&test_fn, black_box(42_i32)))
});

c.bench_function("invoke_func MyCustomHookSet", |b| {
b.iter(|| instance_non_empty_hookset.invoke_func::<_, ()>(black_box(2), black_box(42_i32)))
let test_fn = instance_non_empty_hookset.get_fn_idx(0, 2).unwrap();
b.iter(|| instance_non_empty_hookset.invoke::<_, ()>(&test_fn, black_box(42_i32)))
});
}

Expand Down
19 changes: 15 additions & 4 deletions examples/stuff/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,26 @@ fn main() -> ExitCode {
}
};

let twelve: i32 = instance.invoke_func(1, (5, 7)).unwrap();
let twelve: i32 = instance
.invoke(&instance.get_fn_idx(0, 1).unwrap(), (5, 7))
.unwrap();
assert_eq!(twelve, 12);

let twelve_plus_one: i32 = instance.invoke_func(0, twelve).unwrap();
let twelve_plus_one: i32 = instance
.invoke(&instance.get_fn_idx(0, 0).unwrap(), twelve)
.unwrap();
assert_eq!(twelve_plus_one, 13);

instance.invoke_func::<_, ()>(2, 42_i32).unwrap();
instance
.invoke::<_, ()>(&instance.get_fn_idx(0, 2).unwrap(), 42_i32)
.unwrap();

assert_eq!(instance.invoke_func::<(), i32>(3, ()).unwrap(), 42_i32);
assert_eq!(
instance
.invoke::<(), i32>(&instance.get_fn_idx(0, 3).unwrap(), ())
.unwrap(),
42_i32
);

ExitCode::SUCCESS
}
16 changes: 1 addition & 15 deletions src/execution/function_ref.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloc::string::{String, ToString};
use alloc::string::String;

use crate::execution::{hooks::HookSet, value::InteropValueList, RuntimeInstance};
use crate::RuntimeError;
Expand All @@ -11,20 +11,6 @@ pub struct FunctionRef {
}

impl FunctionRef {
pub(crate) fn new(
module_name: &str,
function_name: &str,
module_index: usize,
function_index: usize,
) -> Self {
Self {
module_name: module_name.to_string(),
function_name: function_name.to_string(),
module_index,
function_index,
}
}

pub fn invoke<H: HookSet, Param: InteropValueList, Returns: InteropValueList>(
&self,
runtime: &mut RuntimeInstance<H>,
Expand Down
5 changes: 2 additions & 3 deletions src/execution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ use interpreter_loop::run;
use locals::Locals;
use value_stack::Stack;

use crate::core::indices::FuncIdx;
use crate::core::reader::types::export::{Export, ExportDesc};
use crate::core::reader::types::{FuncType, ValType};
use crate::core::reader::types::FuncType;
use crate::core::reader::WasmReader;
use crate::execution::assert_validated::UnwrapValidatedExt;
use crate::execution::hooks::{EmptyHookSet, HookSet};
Expand Down Expand Up @@ -196,7 +195,7 @@ where
// TODO: replace this with the lookup table when implmenting the linker
fn get_indicies(
&self,
module_name: &str,
_module_name: &str,
function_name: &str,
) -> Result<(usize, usize), RuntimeError> {
let func_idx = self
Expand Down
44 changes: 37 additions & 7 deletions tests/add_one.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use wasm::{validate, RuntimeInstance};
use wasm::{validate, RuntimeInstance, DEFAULT_MODULE};

const MULTIPLY_WAT_TEMPLATE: &'static str = r#"
(module
Expand All @@ -18,9 +18,24 @@ fn i32_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_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());
assert_eq!(
12,
instance
.invoke(&instance.get_fn(DEFAULT_MODULE, "add_one").unwrap(), 11)
.unwrap()
);
assert_eq!(
1,
instance
.invoke(&instance.get_fn(DEFAULT_MODULE, "add_one").unwrap(), 0)
.unwrap()
);
assert_eq!(
-5,
instance
.invoke(&instance.get_fn(DEFAULT_MODULE, "add_one").unwrap(), -6)
.unwrap()
);
}

/// A simple function to add 1 to an i64 and return the result
Expand All @@ -32,7 +47,22 @@ fn i64_add_one() {
let validation_info = validate(&wasm_bytes).expect("validation failed");
let mut instance = RuntimeInstance::new(&validation_info).expect("instantiation failed");

assert_eq!(12 as i64, instance.invoke_func(0, 11 as i64).unwrap());
assert_eq!(1 as i64, instance.invoke_func(0, 0 as i64).unwrap());
assert_eq!(-5 as i64, instance.invoke_func(0, -6 as i64).unwrap());
assert_eq!(
12 as i64,
instance
.invoke(&instance.get_fn_idx(0, 0).unwrap(), 11 as i64)
.unwrap()
);
assert_eq!(
1 as i64,
instance
.invoke(&instance.get_fn_idx(0, 0).unwrap(), 0 as i64)
.unwrap()
);
assert_eq!(
-5 as i64,
instance
.invoke(&instance.get_fn_idx(0, 0).unwrap(), -6 as i64)
.unwrap()
);
}
Loading

0 comments on commit a973d0f

Please sign in to comment.