mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-15 09:51:21 +00:00
test(runtime-core) Test host functions without a vm::Ctx
argument.
This commit is contained in:
@ -10,11 +10,19 @@ fn imported_functions_forms() {
|
||||
(module
|
||||
(type $type (func (param i32) (result i32)))
|
||||
(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_trap" (func $callback_fn_trap (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)
|
||||
get_local 0
|
||||
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)
|
||||
get_local 0
|
||||
call $callback_fn_trap_with_vmctx))
|
||||
@ -31,7 +39,9 @@ fn imported_functions_forms() {
|
||||
let import_object = imports! {
|
||||
"env" => {
|
||||
"memory" => memory.clone(),
|
||||
"callback_fn" => Func::new(callback_fn),
|
||||
"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),
|
||||
},
|
||||
};
|
||||
@ -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_trap,
|
||||
Err(RuntimeError::Error {
|
||||
data: Box::new(format!("foo {}", 1))
|
||||
})
|
||||
);
|
||||
call_and_assert!(
|
||||
function_fn_trap_with_vmctx,
|
||||
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, ()> {
|
||||
let memory = vmctx.memory(0);
|
||||
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)
|
||||
}
|
||||
|
||||
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> {
|
||||
let memory = vmctx.memory(0);
|
||||
let shift: i32 = memory.view()[0].get();
|
||||
|
@ -643,7 +643,12 @@ mod tests {
|
||||
vec![$($x),*].iter().sum()
|
||||
}
|
||||
|
||||
fn without_vmctx($($x: i32),*) -> i32 {
|
||||
vec![$($x),*].iter().sum()
|
||||
}
|
||||
|
||||
let _func = Func::new(with_vmctx);
|
||||
let _func = Func::new(without_vmctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -654,7 +659,12 @@ mod tests {
|
||||
0
|
||||
}
|
||||
|
||||
fn bar() -> i32 {
|
||||
0
|
||||
}
|
||||
|
||||
let _ = Func::new(foo);
|
||||
let _ = Func::new(bar);
|
||||
}
|
||||
|
||||
test_func_arity_n!(test_func_arity_1, a);
|
||||
|
Reference in New Issue
Block a user