Skip to content

Commit

Permalink
Add a basic Wasm VM test (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaka authored Feb 10, 2023
1 parent a7029fa commit 727e36d
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
43 changes: 43 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ wasmtime = { version = "3.0.0", default-features = false, features = ["async", "
async-std = "1.12.0"
criterion = "0.4.0"
tempfile = "3.3.0"
wat = "1.0.58"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
Expand Down
2 changes: 1 addition & 1 deletion src/executor/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ impl TryFrom<wasmi::Signature> for Signature {
}

/// Value that a Wasm function can accept or produce.
#[derive(Debug, Copy, Clone)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum WasmValue {
/// A 32-bits integer. There is no fundamental difference between signed and unsigned
/// integer, and the signed-ness should be determined depending on the context.
Expand Down
51 changes: 51 additions & 0 deletions src/executor/vm/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#![cfg(test)]

use core::iter;

#[test]
fn is_send() {
// Makes sure that the virtual machine types implement `Send`.
Expand Down Expand Up @@ -117,3 +119,52 @@ fn unsupported_type() {
assert!(super::VirtualMachinePrototype::new(&module2, |_, _, _| Ok(0)).is_err());
}
}

#[test]
fn basic_host_function_return_value_works() {
let module_bytes = wat::parse_str(
r#"
(module
(import "host" "hello" (func $host_hello (param i32) (result i32)))
(import "env" "memory" (memory $mem 0 4096))
(func (export "hello") (result i32)
(call $host_hello (i32.const 3))
)
)
"#,
)
.unwrap();

for exec_hint in iter::once(super::ExecHint::ForceWasmi)
.chain(super::ExecHint::force_wasmtime_if_available().into_iter())
{
let module = super::Module::new(&module_bytes, exec_hint).unwrap();

let prototype = super::VirtualMachinePrototype::new(&module, |_, _, _| Ok(0)).unwrap();

let mut vm = prototype
.start(super::HeapPages::new(1024), "hello", &[])
.unwrap();

let mut resume_value = None;
loop {
match vm.run(resume_value) {
Ok(super::ExecOutcome::Finished {
return_value: Ok(value),
}) => {
assert_eq!(value, Some(super::WasmValue::I32(3)));
break;
}
Ok(super::ExecOutcome::Finished {
return_value: Err(_),
}) => panic!(),
Ok(super::ExecOutcome::Interrupted { id: 0, params }) => {
assert_eq!(params, vec![super::WasmValue::I32(3)]);
resume_value = Some(super::WasmValue::I32(3));
}
Ok(super::ExecOutcome::Interrupted { .. }) => panic!(),
Err(_) => panic!(),
}
}
}
}

0 comments on commit 727e36d

Please sign in to comment.