From f46b01c26092191a1a49fa91a695920ee3e715e9 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Mon, 12 Jul 2021 23:21:55 -0700 Subject: [PATCH] Improved tests clarity --- lib/js-api/src/externals/function.rs | 20 +-- lib/js-api/src/store.rs | 1 - lib/js-api/tests/externals.rs | 180 ++++++++++++++------------- 3 files changed, 92 insertions(+), 109 deletions(-) diff --git a/lib/js-api/src/externals/function.rs b/lib/js-api/src/externals/function.rs index c5282d83c..fbae5bf29 100644 --- a/lib/js-api/src/externals/function.rs +++ b/lib/js-api/src/externals/function.rs @@ -33,17 +33,12 @@ pub struct VMFunctionBody(u8); /// with native functions. Attempting to create a native `Function` with one will /// result in a panic. /// [Closures as host functions tracking issue](https://github.com/wasmerio/wasmer/issues/1840) +#[derive(Clone, PartialEq)] pub struct Function { pub(crate) store: Store, pub(crate) exported: VMFunction, } -impl PartialEq for Function { - fn eq(&self, other: &Self) -> bool { - self.exported == other.exported - } -} - impl wasmer_types::WasmValueType for Function { /// Write the value. 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 { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { formatter diff --git a/lib/js-api/src/store.rs b/lib/js-api/src/store.rs index 176f57e57..ed34094f0 100644 --- a/lib/js-api/src/store.rs +++ b/lib/js-api/src/store.rs @@ -1,4 +1,3 @@ -use std::any::Any; use std::fmt; /// The store represents all global state that can be manipulated by diff --git a/lib/js-api/tests/externals.rs b/lib/js-api/tests/externals.rs index 9f0e86122..62f2f589e 100644 --- a/lib/js-api/tests/externals.rs +++ b/lib/js-api/tests/externals.rs @@ -177,75 +177,73 @@ fn memory_grow() { // assert!(bad_result.is_err()); } -// #[test] -// fn function_new() -> Result<()> { -// let store = Store::default(); -// let function = Function::new_native(&store, || {}); -// assert_eq!(function.ty().clone(), FunctionType::new(vec![], vec![])); -// let function = Function::new_native(&store, |_a: i32| {}); -// assert_eq!( -// function.ty().clone(), -// FunctionType::new(vec![Type::I32], vec![]) -// ); -// let function = Function::new_native(&store, |_a: i32, _b: i64, _c: f32, _d: f64| {}); -// assert_eq!( -// function.ty().clone(), -// FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], vec![]) -// ); -// let function = Function::new_native(&store, || -> i32 { 1 }); -// assert_eq!( -// function.ty().clone(), -// FunctionType::new(vec![], vec![Type::I32]) -// ); -// let function = Function::new_native(&store, || -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) }); -// assert_eq!( -// function.ty().clone(), -// FunctionType::new(vec![], vec![Type::I32, Type::I64, Type::F32, Type::F64]) -// ); -// Ok(()) -// } +#[wasm_bindgen_test] +fn function_new() { + let store = Store::default(); + let function = Function::new_native(&store, || {}); + assert_eq!(function.ty().clone(), FunctionType::new(vec![], vec![])); + let function = Function::new_native(&store, |_a: i32| {}); + assert_eq!( + function.ty().clone(), + FunctionType::new(vec![Type::I32], vec![]) + ); + let function = Function::new_native(&store, |_a: i32, _b: i64, _c: f32, _d: f64| {}); + assert_eq!( + function.ty().clone(), + FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], vec![]) + ); + let function = Function::new_native(&store, || -> i32 { 1 }); + assert_eq!( + function.ty().clone(), + FunctionType::new(vec![], vec![Type::I32]) + ); + let function = Function::new_native(&store, || -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) }); + assert_eq!( + function.ty().clone(), + FunctionType::new(vec![], vec![Type::I32, Type::I64, Type::F32, Type::F64]) + ); +} -// #[test] -// fn function_new_env() -> Result<()> { -// let store = Store::default(); -// #[derive(Clone, WasmerEnv)] -// struct MyEnv {} +#[wasm_bindgen_test] +fn function_new_env() { + let store = Store::default(); + #[derive(Clone, WasmerEnv)] + struct MyEnv {} -// let my_env = MyEnv {}; -// let function = Function::new_native_with_env(&store, my_env.clone(), |_env: &MyEnv| {}); -// assert_eq!(function.ty().clone(), FunctionType::new(vec![], vec![])); -// let function = -// Function::new_native_with_env(&store, my_env.clone(), |_env: &MyEnv, _a: i32| {}); -// assert_eq!( -// function.ty().clone(), -// FunctionType::new(vec![Type::I32], vec![]) -// ); -// let function = Function::new_native_with_env( -// &store, -// my_env.clone(), -// |_env: &MyEnv, _a: i32, _b: i64, _c: f32, _d: f64| {}, -// ); -// assert_eq!( -// function.ty().clone(), -// FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], vec![]) -// ); -// let function = -// Function::new_native_with_env(&store, my_env.clone(), |_env: &MyEnv| -> i32 { 1 }); -// assert_eq!( -// function.ty().clone(), -// FunctionType::new(vec![], vec![Type::I32]) -// ); -// let function = Function::new_native_with_env( -// &store, -// my_env.clone(), -// |_env: &MyEnv| -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) }, -// ); -// assert_eq!( -// function.ty().clone(), -// FunctionType::new(vec![], vec![Type::I32, Type::I64, Type::F32, Type::F64]) -// ); -// Ok(()) -// } + let my_env = MyEnv {}; + let function = Function::new_native_with_env(&store, my_env.clone(), |_env: &MyEnv| {}); + assert_eq!(function.ty().clone(), FunctionType::new(vec![], vec![])); + let function = + Function::new_native_with_env(&store, my_env.clone(), |_env: &MyEnv, _a: i32| {}); + assert_eq!( + function.ty().clone(), + FunctionType::new(vec![Type::I32], vec![]) + ); + let function = Function::new_native_with_env( + &store, + my_env.clone(), + |_env: &MyEnv, _a: i32, _b: i64, _c: f32, _d: f64| {}, + ); + assert_eq!( + function.ty().clone(), + FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], vec![]) + ); + let function = + Function::new_native_with_env(&store, my_env.clone(), |_env: &MyEnv| -> i32 { 1 }); + assert_eq!( + function.ty().clone(), + FunctionType::new(vec![], vec![Type::I32]) + ); + let function = Function::new_native_with_env( + &store, + my_env.clone(), + |_env: &MyEnv| -> (i32, i64, f32, f64) { (1, 2, 3.0, 4.0) }, + ); + assert_eq!( + function.ty().clone(), + FunctionType::new(vec![], vec![Type::I32, Type::I64, Type::F32, Type::F64]) + ); +} // #[test] // fn function_new_dynamic() -> Result<()> { @@ -374,31 +372,35 @@ fn memory_grow() { // Ok(()) // } -// #[test] -// fn function_outlives_instance() -> Result<()> { -// let store = Store::default(); -// let wat = r#"(module -// (type $sum_t (func (param i32 i32) (result i32))) -// (func $sum_f (type $sum_t) (param $x i32) (param $y i32) (result i32) -// local.get $x -// local.get $y -// i32.add) -// (export "sum" (func $sum_f))) -// "#; +#[wasm_bindgen_test] +fn function_outlives_instance() { + let store = Store::default(); + let wat = r#"(module + (type $sum_t (func (param i32 i32) (result i32))) + (func $sum_f (type $sum_t) (param $x i32) (param $y i32) (result i32) + local.get $x + local.get $y + i32.add) + (export "sum" (func $sum_f))) +"#; -// let f = { -// let module = Module::new(&store, wat)?; -// let instance = Instance::new(&module, &imports! {})?; -// let f: NativeFunc<(i32, i32), i32> = instance.exports.get_native_function("sum")?; + let f = { + let module = Module::new(&store, wat).unwrap(); + let instance = Instance::new(&module, &imports! {}).unwrap(); + let f = instance.exports.get_function("sum").unwrap(); -// assert_eq!(f.call(4, 5)?, 9); -// f -// }; + assert_eq!( + 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); - -// Ok(()) -// } + assert_eq!( + f.call(&[Val::I32(4), Val::I32(5)]).unwrap(), + vec![Val::I32(9)].into_boxed_slice() + ); +} // #[test] // fn weak_instance_ref_externs_after_instance() -> Result<()> {