test(runtime-core) Test host functions without a vm::Ctx argument.

This commit is contained in:
Ivan Enderlin
2019-10-30 14:58:57 +01:00
parent 95e1b85c56
commit 7eef49be12
2 changed files with 35 additions and 0 deletions

View File

@ -10,11 +10,19 @@ fn imported_functions_forms() {
(module (module
(type $type (func (param i32) (result i32))) (type $type (func (param i32) (result i32)))
(import "env" "memory" (memory 1 1)) (import "env" "memory" (memory 1 1))
(import "env" "callback_fn" (func $callback_fn (type $type)))
(import "env" "callback_fn_with_vmctx" (func $callback_fn_with_vmctx (type $type))) (import "env" "callback_fn_with_vmctx" (func $callback_fn_with_vmctx (type $type)))
(import "env" "callback_fn_trap" (func $callback_fn_trap (type $type)))
(import "env" "callback_fn_trap_with_vmctx" (func $callback_fn_trap_with_vmctx (type $type))) (import "env" "callback_fn_trap_with_vmctx" (func $callback_fn_trap_with_vmctx (type $type)))
(func (export "function_fn") (type $type)
get_local 0
call $callback_fn)
(func (export "function_fn_with_vmctx") (type $type) (func (export "function_fn_with_vmctx") (type $type)
get_local 0 get_local 0
call $callback_fn_with_vmctx) call $callback_fn_with_vmctx)
(func (export "function_fn_trap") (type $type)
get_local 0
call $callback_fn_trap)
(func (export "function_fn_trap_with_vmctx") (type $type) (func (export "function_fn_trap_with_vmctx") (type $type)
get_local 0 get_local 0
call $callback_fn_trap_with_vmctx)) call $callback_fn_trap_with_vmctx))
@ -31,7 +39,9 @@ fn imported_functions_forms() {
let import_object = imports! { let import_object = imports! {
"env" => { "env" => {
"memory" => memory.clone(), "memory" => memory.clone(),
"callback_fn" => Func::new(callback_fn),
"callback_fn_with_vmctx" => Func::new(callback_fn_with_vmctx), "callback_fn_with_vmctx" => Func::new(callback_fn_with_vmctx),
"callback_fn_trap" => Func::new(callback_fn_trap),
"callback_fn_trap_with_vmctx" => Func::new(callback_fn_trap_with_vmctx), "callback_fn_trap_with_vmctx" => Func::new(callback_fn_trap_with_vmctx),
}, },
}; };
@ -88,7 +98,14 @@ fn imported_functions_forms() {
}; };
} }
call_and_assert!(function_fn, Ok(2));
call_and_assert!(function_fn_with_vmctx, Ok(2 + SHIFT)); call_and_assert!(function_fn_with_vmctx, Ok(2 + SHIFT));
call_and_assert!(
function_fn_trap,
Err(RuntimeError::Error {
data: Box::new(format!("foo {}", 1))
})
);
call_and_assert!( call_and_assert!(
function_fn_trap_with_vmctx, function_fn_trap_with_vmctx,
Err(RuntimeError::Error { Err(RuntimeError::Error {
@ -97,6 +114,10 @@ fn imported_functions_forms() {
); );
} }
fn callback_fn(n: i32) -> Result<i32, ()> {
Ok(n + 1)
}
fn callback_fn_with_vmctx(vmctx: &mut vm::Ctx, n: i32) -> Result<i32, ()> { fn callback_fn_with_vmctx(vmctx: &mut vm::Ctx, n: i32) -> Result<i32, ()> {
let memory = vmctx.memory(0); let memory = vmctx.memory(0);
let shift: i32 = memory.view()[0].get(); let shift: i32 = memory.view()[0].get();
@ -104,6 +125,10 @@ fn callback_fn_with_vmctx(vmctx: &mut vm::Ctx, n: i32) -> Result<i32, ()> {
Ok(shift + n + 1) Ok(shift + n + 1)
} }
fn callback_fn_trap(n: i32) -> Result<i32, String> {
Err(format!("foo {}", n))
}
fn callback_fn_trap_with_vmctx(vmctx: &mut vm::Ctx, n: i32) -> Result<i32, String> { fn callback_fn_trap_with_vmctx(vmctx: &mut vm::Ctx, n: i32) -> Result<i32, String> {
let memory = vmctx.memory(0); let memory = vmctx.memory(0);
let shift: i32 = memory.view()[0].get(); let shift: i32 = memory.view()[0].get();

View File

@ -643,7 +643,12 @@ mod tests {
vec![$($x),*].iter().sum() vec![$($x),*].iter().sum()
} }
fn without_vmctx($($x: i32),*) -> i32 {
vec![$($x),*].iter().sum()
}
let _func = Func::new(with_vmctx); let _func = Func::new(with_vmctx);
let _func = Func::new(without_vmctx);
} }
} }
} }
@ -654,7 +659,12 @@ mod tests {
0 0
} }
fn bar() -> i32 {
0
}
let _ = Func::new(foo); let _ = Func::new(foo);
let _ = Func::new(bar);
} }
test_func_arity_n!(test_func_arity_1, a); test_func_arity_n!(test_func_arity_1, a);