2019-10-30 12:28:01 +00:00
|
|
|
use wasmer_runtime_core::{
|
|
|
|
compile_with, error::RuntimeError, imports, memory::Memory, typed_func::Func,
|
|
|
|
types::MemoryDescriptor, units::Pages, vm,
|
|
|
|
};
|
|
|
|
use wasmer_runtime_core_tests::{get_compiler, wat2wasm};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn imported_functions_forms() {
|
|
|
|
const MODULE: &str = r#"
|
|
|
|
(module
|
|
|
|
(type $type (func (param i32) (result i32)))
|
|
|
|
(import "env" "memory" (memory 1 1))
|
2019-10-30 13:58:57 +00:00
|
|
|
(import "env" "callback_fn" (func $callback_fn (type $type)))
|
2019-11-04 12:02:29 +00:00
|
|
|
(import "env" "callback_closure" (func $callback_closure (type $type)))
|
2019-11-04 13:14:12 +00:00
|
|
|
(import "env" "callback_closure_with_env" (func $callback_closure_with_env (type $type)))
|
2019-10-30 12:28:01 +00:00
|
|
|
(import "env" "callback_fn_with_vmctx" (func $callback_fn_with_vmctx (type $type)))
|
2019-11-04 12:02:29 +00:00
|
|
|
(import "env" "callback_closure_with_vmctx" (func $callback_closure_with_vmctx (type $type)))
|
2019-11-04 14:21:09 +00:00
|
|
|
(import "env" "callback_closure_with_vmctx_and_env" (func $callback_closure_with_vmctx_and_env (type $type)))
|
2019-10-30 13:58:57 +00:00
|
|
|
(import "env" "callback_fn_trap" (func $callback_fn_trap (type $type)))
|
2019-11-04 12:02:29 +00:00
|
|
|
(import "env" "callback_closure_trap" (func $callback_closure_trap (type $type)))
|
2019-10-30 12:28:01 +00:00
|
|
|
(import "env" "callback_fn_trap_with_vmctx" (func $callback_fn_trap_with_vmctx (type $type)))
|
2019-11-04 12:02:29 +00:00
|
|
|
(import "env" "callback_closure_trap_with_vmctx" (func $callback_closure_trap_with_vmctx (type $type)))
|
2019-11-04 14:21:09 +00:00
|
|
|
(import "env" "callback_closure_trap_with_vmctx_and_env" (func $callback_closure_trap_with_vmctx_and_env (type $type)))
|
2019-11-04 13:14:12 +00:00
|
|
|
|
2019-10-30 13:58:57 +00:00
|
|
|
(func (export "function_fn") (type $type)
|
|
|
|
get_local 0
|
|
|
|
call $callback_fn)
|
2019-11-04 13:14:12 +00:00
|
|
|
|
2019-11-04 12:02:29 +00:00
|
|
|
(func (export "function_closure") (type $type)
|
|
|
|
get_local 0
|
|
|
|
call $callback_closure)
|
2019-11-04 13:14:12 +00:00
|
|
|
|
|
|
|
(func (export "function_closure_with_env") (type $type)
|
|
|
|
get_local 0
|
|
|
|
call $callback_closure_with_env)
|
|
|
|
|
2019-10-30 12:28:01 +00:00
|
|
|
(func (export "function_fn_with_vmctx") (type $type)
|
|
|
|
get_local 0
|
|
|
|
call $callback_fn_with_vmctx)
|
2019-11-04 13:14:12 +00:00
|
|
|
|
2019-11-04 12:02:29 +00:00
|
|
|
(func (export "function_closure_with_vmctx") (type $type)
|
|
|
|
get_local 0
|
|
|
|
call $callback_closure_with_vmctx)
|
2019-11-04 13:14:12 +00:00
|
|
|
|
2019-11-04 14:21:09 +00:00
|
|
|
(func (export "function_closure_with_vmctx_and_env") (type $type)
|
|
|
|
get_local 0
|
|
|
|
call $callback_closure_with_vmctx_and_env)
|
|
|
|
|
2019-10-30 13:58:57 +00:00
|
|
|
(func (export "function_fn_trap") (type $type)
|
|
|
|
get_local 0
|
|
|
|
call $callback_fn_trap)
|
2019-11-04 13:14:12 +00:00
|
|
|
|
2019-11-04 12:02:29 +00:00
|
|
|
(func (export "function_closure_trap") (type $type)
|
|
|
|
get_local 0
|
|
|
|
call $callback_closure_trap)
|
2019-11-04 13:14:12 +00:00
|
|
|
|
2019-10-30 12:28:01 +00:00
|
|
|
(func (export "function_fn_trap_with_vmctx") (type $type)
|
|
|
|
get_local 0
|
2019-11-04 12:02:29 +00:00
|
|
|
call $callback_fn_trap_with_vmctx)
|
2019-11-04 13:14:12 +00:00
|
|
|
|
2019-11-04 12:02:29 +00:00
|
|
|
(func (export "function_closure_trap_with_vmctx") (type $type)
|
|
|
|
get_local 0
|
2019-11-04 14:21:09 +00:00
|
|
|
call $callback_closure_trap_with_vmctx)
|
|
|
|
|
|
|
|
(func (export "function_closure_trap_with_vmctx_and_env") (type $type)
|
|
|
|
get_local 0
|
|
|
|
call $callback_closure_trap_with_vmctx_and_env))
|
2019-10-30 12:28:01 +00:00
|
|
|
"#;
|
|
|
|
|
|
|
|
let wasm_binary = wat2wasm(MODULE.as_bytes()).expect("WAST not valid or malformed");
|
|
|
|
let module = compile_with(&wasm_binary, &get_compiler()).unwrap();
|
|
|
|
let memory_descriptor = MemoryDescriptor::new(Pages(1), Some(Pages(1)), false).unwrap();
|
|
|
|
let memory = Memory::new(memory_descriptor).unwrap();
|
|
|
|
|
|
|
|
const SHIFT: i32 = 10;
|
|
|
|
memory.view()[0].set(SHIFT);
|
2019-11-04 13:14:12 +00:00
|
|
|
let shift = 100;
|
2019-10-30 12:28:01 +00:00
|
|
|
|
|
|
|
let import_object = imports! {
|
|
|
|
"env" => {
|
|
|
|
"memory" => memory.clone(),
|
2019-11-04 14:21:09 +00:00
|
|
|
|
|
|
|
// Regular function.
|
2019-10-30 13:58:57 +00:00
|
|
|
"callback_fn" => Func::new(callback_fn),
|
2019-11-04 14:21:09 +00:00
|
|
|
|
|
|
|
// Closure without a captured environment.
|
2019-11-04 12:02:29 +00:00
|
|
|
"callback_closure" => Func::new(|n: i32| -> Result<i32, ()> {
|
|
|
|
Ok(n + 1)
|
|
|
|
}),
|
2019-11-04 14:21:09 +00:00
|
|
|
|
|
|
|
// Closure with a captured environment (a single variable + an instance of `Memory`).
|
2019-11-04 13:14:12 +00:00
|
|
|
"callback_closure_with_env" => Func::new(move |n: i32| -> Result<i32, ()> {
|
2019-11-04 14:21:09 +00:00
|
|
|
let shift = shift + memory.view::<i32>()[0].get();
|
|
|
|
|
2019-11-04 13:14:12 +00:00
|
|
|
Ok(shift + n + 1)
|
|
|
|
}),
|
2019-11-04 14:21:09 +00:00
|
|
|
|
|
|
|
// Regular function with an explicit `vmctx`.
|
2019-10-30 12:28:01 +00:00
|
|
|
"callback_fn_with_vmctx" => Func::new(callback_fn_with_vmctx),
|
2019-11-04 14:21:09 +00:00
|
|
|
|
|
|
|
// Closure without a captured environment but with an explicit `vmctx`.
|
2019-11-04 12:02:29 +00:00
|
|
|
"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)
|
|
|
|
}),
|
2019-11-04 14:21:09 +00:00
|
|
|
|
|
|
|
// Closure with a captured environment (a single variable) and with an explicit `vmctx`.
|
|
|
|
"callback_closure_with_vmctx_and_env" => Func::new(move |vmctx: &mut vm::Ctx, n: i32| -> Result<i32, ()> {
|
|
|
|
let memory = vmctx.memory(0);
|
|
|
|
let shift = shift + memory.view::<i32>()[0].get();
|
|
|
|
|
|
|
|
Ok(shift + n + 1)
|
|
|
|
}),
|
|
|
|
|
|
|
|
// Trap a regular function.
|
2019-10-30 13:58:57 +00:00
|
|
|
"callback_fn_trap" => Func::new(callback_fn_trap),
|
2019-11-04 14:21:09 +00:00
|
|
|
|
|
|
|
// Trap a closure without a captured environment.
|
2019-11-04 12:02:29 +00:00
|
|
|
"callback_closure_trap" => Func::new(|n: i32| -> Result<i32, String> {
|
|
|
|
Err(format!("bar {}", n + 1))
|
|
|
|
}),
|
2019-11-04 14:21:09 +00:00
|
|
|
|
|
|
|
// Trap a regular function with an explicit `vmctx`.
|
2019-10-30 12:28:01 +00:00
|
|
|
"callback_fn_trap_with_vmctx" => Func::new(callback_fn_trap_with_vmctx),
|
2019-11-04 14:21:09 +00:00
|
|
|
|
|
|
|
// Trap a closure without a captured environment but with an explicit `vmctx`.
|
2019-11-04 12:02:29 +00:00
|
|
|
"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))
|
|
|
|
}),
|
2019-11-04 14:21:09 +00:00
|
|
|
|
|
|
|
// Trap a closure with a captured environment (a single variable) and with an explicit `vmctx`.
|
|
|
|
"callback_closure_trap_with_vmctx_and_env" => Func::new(move |vmctx: &mut vm::Ctx, n: i32| -> Result<i32, String> {
|
|
|
|
let memory = vmctx.memory(0);
|
|
|
|
let shift = shift + memory.view::<i32>()[0].get();
|
|
|
|
|
|
|
|
Err(format!("! {}", shift + n + 1))
|
|
|
|
}),
|
2019-10-30 12:28:01 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
let instance = module.instantiate(&import_object).unwrap();
|
|
|
|
|
|
|
|
macro_rules! call_and_assert {
|
|
|
|
($function:ident, $expected_value:expr) => {
|
|
|
|
let $function: Func<i32, i32> = instance.func(stringify!($function)).unwrap();
|
|
|
|
|
|
|
|
let result = $function.call(1);
|
|
|
|
|
|
|
|
match (result, $expected_value) {
|
|
|
|
(Ok(value), expected_value) => assert_eq!(
|
|
|
|
Ok(value),
|
|
|
|
expected_value,
|
|
|
|
concat!("Expected right when calling `", stringify!($function), "`.")
|
|
|
|
),
|
|
|
|
(
|
|
|
|
Err(RuntimeError::Error { data }),
|
|
|
|
Err(RuntimeError::Error {
|
|
|
|
data: expected_data,
|
|
|
|
}),
|
|
|
|
) => {
|
|
|
|
if let (Some(data), Some(expected_data)) = (
|
|
|
|
data.downcast_ref::<&str>(),
|
|
|
|
expected_data.downcast_ref::<&str>(),
|
|
|
|
) {
|
|
|
|
assert_eq!(
|
|
|
|
data, expected_data,
|
|
|
|
concat!("Expected right when calling `", stringify!($function), "`.")
|
|
|
|
)
|
|
|
|
} else if let (Some(data), Some(expected_data)) = (
|
|
|
|
data.downcast_ref::<String>(),
|
|
|
|
expected_data.downcast_ref::<String>(),
|
|
|
|
) {
|
|
|
|
assert_eq!(
|
|
|
|
data, expected_data,
|
|
|
|
concat!("Expected right when calling `", stringify!($function), "`.")
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
assert!(false, "Unexpected error, cannot compare it.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(result, expected_value) => assert!(
|
|
|
|
false,
|
|
|
|
format!(
|
|
|
|
"Unexpected assertion for `{}`: left = `{:?}`, right = `{:?}`.",
|
|
|
|
stringify!($function),
|
|
|
|
result,
|
|
|
|
expected_value
|
|
|
|
)
|
|
|
|
),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-10-30 13:58:57 +00:00
|
|
|
call_and_assert!(function_fn, Ok(2));
|
2019-11-04 12:02:29 +00:00
|
|
|
call_and_assert!(function_closure, Ok(2));
|
2019-11-04 14:21:09 +00:00
|
|
|
call_and_assert!(function_closure_with_env, Ok(2 + shift + SHIFT));
|
2019-10-30 12:28:01 +00:00
|
|
|
call_and_assert!(function_fn_with_vmctx, Ok(2 + SHIFT));
|
2019-11-04 12:02:29 +00:00
|
|
|
call_and_assert!(function_closure_with_vmctx, Ok(2 + SHIFT));
|
2019-11-04 14:21:09 +00:00
|
|
|
call_and_assert!(function_closure_with_vmctx_and_env, Ok(2 + shift + SHIFT));
|
2019-10-30 13:58:57 +00:00
|
|
|
call_and_assert!(
|
|
|
|
function_fn_trap,
|
|
|
|
Err(RuntimeError::Error {
|
2019-11-04 12:02:29 +00:00
|
|
|
data: Box::new(format!("foo {}", 2))
|
|
|
|
})
|
|
|
|
);
|
|
|
|
call_and_assert!(
|
|
|
|
function_closure_trap,
|
|
|
|
Err(RuntimeError::Error {
|
|
|
|
data: Box::new(format!("bar {}", 2))
|
2019-10-30 13:58:57 +00:00
|
|
|
})
|
|
|
|
);
|
2019-10-30 12:28:01 +00:00
|
|
|
call_and_assert!(
|
|
|
|
function_fn_trap_with_vmctx,
|
|
|
|
Err(RuntimeError::Error {
|
|
|
|
data: Box::new(format!("baz {}", 2 + SHIFT))
|
|
|
|
})
|
|
|
|
);
|
2019-11-04 12:02:29 +00:00
|
|
|
call_and_assert!(
|
|
|
|
function_closure_trap_with_vmctx,
|
|
|
|
Err(RuntimeError::Error {
|
|
|
|
data: Box::new(format!("qux {}", 2 + SHIFT))
|
|
|
|
})
|
|
|
|
);
|
2019-11-04 14:21:09 +00:00
|
|
|
call_and_assert!(
|
|
|
|
function_closure_trap_with_vmctx_and_env,
|
|
|
|
Err(RuntimeError::Error {
|
|
|
|
data: Box::new(format!("! {}", 2 + shift + SHIFT))
|
|
|
|
})
|
|
|
|
);
|
2019-10-30 12:28:01 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 13:58:57 +00:00
|
|
|
fn callback_fn(n: i32) -> Result<i32, ()> {
|
|
|
|
Ok(n + 1)
|
|
|
|
}
|
|
|
|
|
2019-10-30 12:28:01 +00:00
|
|
|
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();
|
|
|
|
|
|
|
|
Ok(shift + n + 1)
|
|
|
|
}
|
|
|
|
|
2019-10-30 13:58:57 +00:00
|
|
|
fn callback_fn_trap(n: i32) -> Result<i32, String> {
|
2019-11-04 12:02:29 +00:00
|
|
|
Err(format!("foo {}", n + 1))
|
2019-10-30 13:58:57 +00:00
|
|
|
}
|
|
|
|
|
2019-10-30 12:28:01 +00:00
|
|
|
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();
|
|
|
|
|
|
|
|
Err(format!("baz {}", shift + n + 1))
|
|
|
|
}
|