mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-10 22:58:18 +00:00
Improved tests clarity
This commit is contained in:
20
lib/js-api/src/externals/function.rs
vendored
20
lib/js-api/src/externals/function.rs
vendored
@@ -33,17 +33,12 @@ pub struct VMFunctionBody(u8);
|
|||||||
/// with native functions. Attempting to create a native `Function` with one will
|
/// with native functions. Attempting to create a native `Function` with one will
|
||||||
/// result in a panic.
|
/// result in a panic.
|
||||||
/// [Closures as host functions tracking issue](https://github.com/wasmerio/wasmer/issues/1840)
|
/// [Closures as host functions tracking issue](https://github.com/wasmerio/wasmer/issues/1840)
|
||||||
|
#[derive(Clone, PartialEq)]
|
||||||
pub struct Function {
|
pub struct Function {
|
||||||
pub(crate) store: Store,
|
pub(crate) store: Store,
|
||||||
pub(crate) exported: VMFunction,
|
pub(crate) exported: VMFunction,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialEq for Function {
|
|
||||||
fn eq(&self, other: &Self) -> bool {
|
|
||||||
self.exported == other.exported
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl wasmer_types::WasmValueType for Function {
|
impl wasmer_types::WasmValueType for Function {
|
||||||
/// Write the value.
|
/// Write the value.
|
||||||
unsafe fn write_value_to(&self, p: *mut i128) {
|
unsafe fn write_value_to(&self, p: *mut i128) {
|
||||||
@@ -761,19 +756,6 @@ impl<'a> Exportable<'a> for Function {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Clone for Function {
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
unimplemented!();
|
|
||||||
// let mut exported = self.exported.clone();
|
|
||||||
// exported.vm_function.upgrade_instance_ref().unwrap();
|
|
||||||
|
|
||||||
// Self {
|
|
||||||
// store: self.store.clone(),
|
|
||||||
// exported,
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Debug for Function {
|
impl fmt::Debug for Function {
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||||
formatter
|
formatter
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
use std::any::Any;
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
/// The store represents all global state that can be manipulated by
|
/// The store represents all global state that can be manipulated by
|
||||||
|
|||||||
@@ -177,75 +177,73 @@ fn memory_grow() {
|
|||||||
// assert!(bad_result.is_err());
|
// assert!(bad_result.is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
// #[test]
|
#[wasm_bindgen_test]
|
||||||
// fn function_new() -> Result<()> {
|
fn function_new() {
|
||||||
// let store = Store::default();
|
let store = Store::default();
|
||||||
// let function = Function::new_native(&store, || {});
|
let function = Function::new_native(&store, || {});
|
||||||
// assert_eq!(function.ty().clone(), FunctionType::new(vec![], vec![]));
|
assert_eq!(function.ty().clone(), FunctionType::new(vec![], vec![]));
|
||||||
// let function = Function::new_native(&store, |_a: i32| {});
|
let function = Function::new_native(&store, |_a: i32| {});
|
||||||
// assert_eq!(
|
assert_eq!(
|
||||||
// function.ty().clone(),
|
function.ty().clone(),
|
||||||
// FunctionType::new(vec![Type::I32], vec![])
|
FunctionType::new(vec![Type::I32], vec![])
|
||||||
// );
|
);
|
||||||
// let function = Function::new_native(&store, |_a: i32, _b: i64, _c: f32, _d: f64| {});
|
let function = Function::new_native(&store, |_a: i32, _b: i64, _c: f32, _d: f64| {});
|
||||||
// assert_eq!(
|
assert_eq!(
|
||||||
// function.ty().clone(),
|
function.ty().clone(),
|
||||||
// FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], vec![])
|
FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], vec![])
|
||||||
// );
|
);
|
||||||
// let function = Function::new_native(&store, || -> i32 { 1 });
|
let function = Function::new_native(&store, || -> i32 { 1 });
|
||||||
// assert_eq!(
|
assert_eq!(
|
||||||
// function.ty().clone(),
|
function.ty().clone(),
|
||||||
// FunctionType::new(vec![], vec![Type::I32])
|
FunctionType::new(vec![], vec![Type::I32])
|
||||||
// );
|
);
|
||||||
// let function = Function::new_native(&store, || -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) });
|
let function = Function::new_native(&store, || -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) });
|
||||||
// assert_eq!(
|
assert_eq!(
|
||||||
// function.ty().clone(),
|
function.ty().clone(),
|
||||||
// FunctionType::new(vec![], vec![Type::I32, Type::I64, Type::F32, Type::F64])
|
FunctionType::new(vec![], vec![Type::I32, Type::I64, Type::F32, Type::F64])
|
||||||
// );
|
);
|
||||||
// Ok(())
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
// #[test]
|
#[wasm_bindgen_test]
|
||||||
// fn function_new_env() -> Result<()> {
|
fn function_new_env() {
|
||||||
// let store = Store::default();
|
let store = Store::default();
|
||||||
// #[derive(Clone, WasmerEnv)]
|
#[derive(Clone, WasmerEnv)]
|
||||||
// struct MyEnv {}
|
struct MyEnv {}
|
||||||
|
|
||||||
// let my_env = MyEnv {};
|
let my_env = MyEnv {};
|
||||||
// let function = Function::new_native_with_env(&store, my_env.clone(), |_env: &MyEnv| {});
|
let function = Function::new_native_with_env(&store, my_env.clone(), |_env: &MyEnv| {});
|
||||||
// assert_eq!(function.ty().clone(), FunctionType::new(vec![], vec![]));
|
assert_eq!(function.ty().clone(), FunctionType::new(vec![], vec![]));
|
||||||
// let function =
|
let function =
|
||||||
// Function::new_native_with_env(&store, my_env.clone(), |_env: &MyEnv, _a: i32| {});
|
Function::new_native_with_env(&store, my_env.clone(), |_env: &MyEnv, _a: i32| {});
|
||||||
// assert_eq!(
|
assert_eq!(
|
||||||
// function.ty().clone(),
|
function.ty().clone(),
|
||||||
// FunctionType::new(vec![Type::I32], vec![])
|
FunctionType::new(vec![Type::I32], vec![])
|
||||||
// );
|
);
|
||||||
// let function = Function::new_native_with_env(
|
let function = Function::new_native_with_env(
|
||||||
// &store,
|
&store,
|
||||||
// my_env.clone(),
|
my_env.clone(),
|
||||||
// |_env: &MyEnv, _a: i32, _b: i64, _c: f32, _d: f64| {},
|
|_env: &MyEnv, _a: i32, _b: i64, _c: f32, _d: f64| {},
|
||||||
// );
|
);
|
||||||
// assert_eq!(
|
assert_eq!(
|
||||||
// function.ty().clone(),
|
function.ty().clone(),
|
||||||
// FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], vec![])
|
FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], vec![])
|
||||||
// );
|
);
|
||||||
// let function =
|
let function =
|
||||||
// Function::new_native_with_env(&store, my_env.clone(), |_env: &MyEnv| -> i32 { 1 });
|
Function::new_native_with_env(&store, my_env.clone(), |_env: &MyEnv| -> i32 { 1 });
|
||||||
// assert_eq!(
|
assert_eq!(
|
||||||
// function.ty().clone(),
|
function.ty().clone(),
|
||||||
// FunctionType::new(vec![], vec![Type::I32])
|
FunctionType::new(vec![], vec![Type::I32])
|
||||||
// );
|
);
|
||||||
// let function = Function::new_native_with_env(
|
let function = Function::new_native_with_env(
|
||||||
// &store,
|
&store,
|
||||||
// my_env.clone(),
|
my_env.clone(),
|
||||||
// |_env: &MyEnv| -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) },
|
|_env: &MyEnv| -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) },
|
||||||
// );
|
);
|
||||||
// assert_eq!(
|
assert_eq!(
|
||||||
// function.ty().clone(),
|
function.ty().clone(),
|
||||||
// FunctionType::new(vec![], vec![Type::I32, Type::I64, Type::F32, Type::F64])
|
FunctionType::new(vec![], vec![Type::I32, Type::I64, Type::F32, Type::F64])
|
||||||
// );
|
);
|
||||||
// Ok(())
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
// #[test]
|
// #[test]
|
||||||
// fn function_new_dynamic() -> Result<()> {
|
// fn function_new_dynamic() -> Result<()> {
|
||||||
@@ -374,31 +372,35 @@ fn memory_grow() {
|
|||||||
// Ok(())
|
// Ok(())
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// #[test]
|
#[wasm_bindgen_test]
|
||||||
// fn function_outlives_instance() -> Result<()> {
|
fn function_outlives_instance() {
|
||||||
// let store = Store::default();
|
let store = Store::default();
|
||||||
// let wat = r#"(module
|
let wat = r#"(module
|
||||||
// (type $sum_t (func (param i32 i32) (result i32)))
|
(type $sum_t (func (param i32 i32) (result i32)))
|
||||||
// (func $sum_f (type $sum_t) (param $x i32) (param $y i32) (result i32)
|
(func $sum_f (type $sum_t) (param $x i32) (param $y i32) (result i32)
|
||||||
// local.get $x
|
local.get $x
|
||||||
// local.get $y
|
local.get $y
|
||||||
// i32.add)
|
i32.add)
|
||||||
// (export "sum" (func $sum_f)))
|
(export "sum" (func $sum_f)))
|
||||||
// "#;
|
"#;
|
||||||
|
|
||||||
// let f = {
|
let f = {
|
||||||
// let module = Module::new(&store, wat)?;
|
let module = Module::new(&store, wat).unwrap();
|
||||||
// let instance = Instance::new(&module, &imports! {})?;
|
let instance = Instance::new(&module, &imports! {}).unwrap();
|
||||||
// let f: NativeFunc<(i32, i32), i32> = instance.exports.get_native_function("sum")?;
|
let f = instance.exports.get_function("sum").unwrap();
|
||||||
|
|
||||||
// assert_eq!(f.call(4, 5)?, 9);
|
assert_eq!(
|
||||||
// f
|
f.call(&[Val::I32(4), Val::I32(5)]).unwrap(),
|
||||||
// };
|
vec![Val::I32(9)].into_boxed_slice()
|
||||||
|
);
|
||||||
|
f.clone()
|
||||||
|
};
|
||||||
|
|
||||||
// assert_eq!(f.call(4, 5)?, 9);
|
assert_eq!(
|
||||||
|
f.call(&[Val::I32(4), Val::I32(5)]).unwrap(),
|
||||||
// Ok(())
|
vec![Val::I32(9)].into_boxed_slice()
|
||||||
// }
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// #[test]
|
// #[test]
|
||||||
// fn weak_instance_ref_externs_after_instance() -> Result<()> {
|
// fn weak_instance_ref_externs_after_instance() -> Result<()> {
|
||||||
|
|||||||
Reference in New Issue
Block a user