mirror of
https://github.com/fluencelabs/wasmer
synced 2025-05-12 18:47:12 +00:00
test(runtime-core) Test more host functions as closures.
This commit is contained in:
parent
6035bd2d9b
commit
0e27f2fa72
@ -11,21 +11,37 @@ fn imported_functions_forms() {
|
|||||||
(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" (func $callback_fn (type $type)))
|
||||||
|
(import "env" "callback_closure" (func $callback_closure (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_closure_with_vmctx" (func $callback_closure_with_vmctx (type $type)))
|
||||||
(import "env" "callback_fn_trap" (func $callback_fn_trap (type $type)))
|
(import "env" "callback_fn_trap" (func $callback_fn_trap (type $type)))
|
||||||
|
(import "env" "callback_closure_trap" (func $callback_closure_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)))
|
||||||
|
(import "env" "callback_closure_trap_with_vmctx" (func $callback_closure_trap_with_vmctx (type $type)))
|
||||||
(func (export "function_fn") (type $type)
|
(func (export "function_fn") (type $type)
|
||||||
get_local 0
|
get_local 0
|
||||||
call $callback_fn)
|
call $callback_fn)
|
||||||
|
(func (export "function_closure") (type $type)
|
||||||
|
get_local 0
|
||||||
|
call $callback_closure)
|
||||||
(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_closure_with_vmctx") (type $type)
|
||||||
|
get_local 0
|
||||||
|
call $callback_closure_with_vmctx)
|
||||||
(func (export "function_fn_trap") (type $type)
|
(func (export "function_fn_trap") (type $type)
|
||||||
get_local 0
|
get_local 0
|
||||||
call $callback_fn_trap)
|
call $callback_fn_trap)
|
||||||
|
(func (export "function_closure_trap") (type $type)
|
||||||
|
get_local 0
|
||||||
|
call $callback_closure_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)
|
||||||
|
(func (export "function_closure_trap_with_vmctx") (type $type)
|
||||||
|
get_local 0
|
||||||
|
call $callback_closure_trap_with_vmctx))
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
let wasm_binary = wat2wasm(MODULE.as_bytes()).expect("WAST not valid or malformed");
|
let wasm_binary = wat2wasm(MODULE.as_bytes()).expect("WAST not valid or malformed");
|
||||||
@ -40,9 +56,27 @@ fn imported_functions_forms() {
|
|||||||
"env" => {
|
"env" => {
|
||||||
"memory" => memory.clone(),
|
"memory" => memory.clone(),
|
||||||
"callback_fn" => Func::new(callback_fn),
|
"callback_fn" => Func::new(callback_fn),
|
||||||
|
"callback_closure" => Func::new(|n: i32| -> Result<i32, ()> {
|
||||||
|
Ok(n + 1)
|
||||||
|
}),
|
||||||
"callback_fn_with_vmctx" => Func::new(callback_fn_with_vmctx),
|
"callback_fn_with_vmctx" => Func::new(callback_fn_with_vmctx),
|
||||||
|
"callback_closure_with_vmctx" => Func::new(|vmctx: &mut vm::Ctx, n: i32| -> Result<i32, ()> {
|
||||||
|
let memory = vmctx.memory(0);
|
||||||
|
let shift: i32 = memory.view()[0].get();
|
||||||
|
|
||||||
|
Ok(shift + n + 1)
|
||||||
|
}),
|
||||||
"callback_fn_trap" => Func::new(callback_fn_trap),
|
"callback_fn_trap" => Func::new(callback_fn_trap),
|
||||||
|
"callback_closure_trap" => Func::new(|n: i32| -> Result<i32, String> {
|
||||||
|
Err(format!("bar {}", n + 1))
|
||||||
|
}),
|
||||||
"callback_fn_trap_with_vmctx" => Func::new(callback_fn_trap_with_vmctx),
|
"callback_fn_trap_with_vmctx" => Func::new(callback_fn_trap_with_vmctx),
|
||||||
|
"callback_closure_trap_with_vmctx" => Func::new(|vmctx: &mut vm::Ctx, n: i32| -> Result<i32, String> {
|
||||||
|
let memory = vmctx.memory(0);
|
||||||
|
let shift: i32 = memory.view()[0].get();
|
||||||
|
|
||||||
|
Err(format!("qux {}", shift + n + 1))
|
||||||
|
}),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
let instance = module.instantiate(&import_object).unwrap();
|
let instance = module.instantiate(&import_object).unwrap();
|
||||||
@ -99,11 +133,19 @@ fn imported_functions_forms() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
call_and_assert!(function_fn, Ok(2));
|
call_and_assert!(function_fn, Ok(2));
|
||||||
|
call_and_assert!(function_closure, 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_closure_with_vmctx, Ok(2 + SHIFT));
|
||||||
call_and_assert!(
|
call_and_assert!(
|
||||||
function_fn_trap,
|
function_fn_trap,
|
||||||
Err(RuntimeError::Error {
|
Err(RuntimeError::Error {
|
||||||
data: Box::new(format!("foo {}", 1))
|
data: Box::new(format!("foo {}", 2))
|
||||||
|
})
|
||||||
|
);
|
||||||
|
call_and_assert!(
|
||||||
|
function_closure_trap,
|
||||||
|
Err(RuntimeError::Error {
|
||||||
|
data: Box::new(format!("bar {}", 2))
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
call_and_assert!(
|
call_and_assert!(
|
||||||
@ -112,6 +154,12 @@ fn imported_functions_forms() {
|
|||||||
data: Box::new(format!("baz {}", 2 + SHIFT))
|
data: Box::new(format!("baz {}", 2 + SHIFT))
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
call_and_assert!(
|
||||||
|
function_closure_trap_with_vmctx,
|
||||||
|
Err(RuntimeError::Error {
|
||||||
|
data: Box::new(format!("qux {}", 2 + SHIFT))
|
||||||
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn callback_fn(n: i32) -> Result<i32, ()> {
|
fn callback_fn(n: i32) -> Result<i32, ()> {
|
||||||
@ -126,7 +174,7 @@ fn callback_fn_with_vmctx(vmctx: &mut vm::Ctx, n: i32) -> Result<i32, ()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn callback_fn_trap(n: i32) -> Result<i32, String> {
|
fn callback_fn_trap(n: i32) -> Result<i32, String> {
|
||||||
Err(format!("foo {}", n))
|
Err(format!("foo {}", n + 1))
|
||||||
}
|
}
|
||||||
|
|
||||||
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> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user