diff --git a/examples/imports_exports.rs b/examples/imports_exports.rs index 93ac580da19..193fae4790a 100644 --- a/examples/imports_exports.rs +++ b/examples/imports_exports.rs @@ -58,7 +58,7 @@ fn main() -> Result<(), Box> { // covered in more detail in other examples. println!("Creating the imported function..."); let host_function_signature = FunctionType::new(vec![], vec![Type::I32]); - let host_function = Function::new(&mut store, &host_function_signature, |_env, _args| { + let host_function = Function::new(&mut store, &host_function_signature, |_args| { Ok(vec![Value::I32(42)]) }); diff --git a/examples/imports_function.rs b/examples/imports_function.rs index a93d936af88..61612b21f85 100644 --- a/examples/imports_function.rs +++ b/examples/imports_function.rs @@ -54,7 +54,7 @@ fn main() -> Result<(), Box> { // Create the functions let multiply_dynamic_signature = FunctionType::new(vec![Type::I32], vec![Type::I32]); - let multiply_dynamic = Function::new(&mut store, &multiply_dynamic_signature, |_env, args| { + let multiply_dynamic = Function::new(&mut store, &multiply_dynamic_signature, |args| { println!("Calling `multiply_dynamic`..."); let result = args[0].unwrap_i32() * 2; diff --git a/lib/api/src/js/externals/function.rs b/lib/api/src/js/externals/function.rs index 0b0938df75a..b8bbba359ee 100644 --- a/lib/api/src/js/externals/function.rs +++ b/lib/api/src/js/externals/function.rs @@ -65,6 +65,7 @@ impl Function { /// /// If you know the signature of the host function at compile time, /// consider using [`Function::new_typed`] for less runtime overhead. + #[cfg(feature = "compiler")] pub fn new(store: &mut impl AsStoreMut, ty: FT, func: F) -> Self where FT: Into, diff --git a/lib/api/src/sys/externals/function.rs b/lib/api/src/sys/externals/function.rs index 6e43817520a..f5419cc2f05 100644 --- a/lib/api/src/sys/externals/function.rs +++ b/lib/api/src/sys/externals/function.rs @@ -50,13 +50,13 @@ impl Function { pub fn new(store: &mut impl AsStoreMut, ty: FT, func: F) -> Self where FT: Into, - F: Fn(FunctionEnvMut<()>, &[Value]) -> Result, RuntimeError> - + 'static - + Send - + Sync, + F: Fn(&[Value]) -> Result, RuntimeError> + 'static + Send + Sync, { let env = FunctionEnv::new(&mut store.as_store_mut(), ()); - Self::new_with_env(store, &env, ty, func) + let wrapped_func = move |_env: FunctionEnvMut<()>, + args: &[Value]| + -> Result, RuntimeError> { func(args) }; + Self::new_with_env(store, &env, ty, wrapped_func) } #[cfg(feature = "compiler")] diff --git a/lib/api/tests/js_externals.rs b/lib/api/tests/js_externals.rs index 980e85aad50..71b81cff0c1 100644 --- a/lib/api/tests/js_externals.rs +++ b/lib/api/tests/js_externals.rs @@ -93,7 +93,7 @@ mod js { // minimum: 0, // maximum: Some(1), // }; - // let f = Function::new(&mut store, &env, |num: i32| num + 1); + // let f = Function::new(&mut store, |num: i32| num + 1); // let table = Table::new(&store, table_type, Value::FuncRef(Some(f.clone())))?; // assert_eq!(*table.ty(&store), table_type); // let _elem = table.get(0).unwrap(); diff --git a/lib/api/tests/sys_externals.rs b/lib/api/tests/sys_externals.rs index 710c1bb882e..9bb6630b8bf 100644 --- a/lib/api/tests/sys_externals.rs +++ b/lib/api/tests/sys_externals.rs @@ -277,14 +277,14 @@ mod sys { let function = Function::new( &mut store, &function_type, - |_env: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), + |_values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&mut store).clone(), function_type); let function_type = FunctionType::new(vec![Type::I32], vec![]); let function = Function::new( &mut store, &function_type, - |_env: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), + |_values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&mut store).clone(), function_type); let function_type = @@ -292,14 +292,14 @@ mod sys { let function = Function::new( &mut store, &function_type, - |_env: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), + |_values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&mut store).clone(), function_type); let function_type = FunctionType::new(vec![], vec![Type::I32]); let function = Function::new( &mut store, &function_type, - |_env: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), + |_values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&mut store).clone(), function_type); let function_type = @@ -307,7 +307,7 @@ mod sys { let function = Function::new( &mut store, &function_type, - |_env: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), + |_values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&mut store).clone(), function_type); @@ -316,7 +316,7 @@ mod sys { let function = Function::new( &mut store, function_type, - |_env: FunctionEnvMut<()>, _values: &[Value]| unimplemented!(), + |_values: &[Value]| unimplemented!(), ); assert_eq!(function.ty(&mut store).params(), [Type::V128]); assert_eq!( diff --git a/tests/compilers/imports.rs b/tests/compilers/imports.rs index fafcaa6f10d..696a86b8594 100644 --- a/tests/compilers/imports.rs +++ b/tests/compilers/imports.rs @@ -52,22 +52,22 @@ fn dynamic_function(config: crate::Config) -> Result<()> { static HITS: AtomicUsize = AtomicUsize::new(0); let imports = imports! { "host" => { - "0" => Function::new(&mut store, FunctionType::new(vec![], vec![]), |_env, _values| { + "0" => Function::new(&mut store, FunctionType::new(vec![], vec![]), |_values| { assert_eq!(HITS.fetch_add(1, SeqCst), 0); Ok(vec![]) }), - "1" => Function::new(&mut store, FunctionType::new(vec![ValueType::I32], vec![ValueType::I32]), |_env, values| { + "1" => Function::new(&mut store, FunctionType::new(vec![ValueType::I32], vec![ValueType::I32]), |values| { assert_eq!(values[0], Value::I32(0)); assert_eq!(HITS.fetch_add(1, SeqCst), 1); Ok(vec![Value::I32(1)]) }), - "2" => Function::new(&mut store, FunctionType::new(vec![ValueType::I32, ValueType::I64], vec![]), |_env, values| { + "2" => Function::new(&mut store, FunctionType::new(vec![ValueType::I32, ValueType::I64], vec![]), |values| { assert_eq!(values[0], Value::I32(2)); assert_eq!(values[1], Value::I64(3)); assert_eq!(HITS.fetch_add(1, SeqCst), 2); Ok(vec![]) }), - "3" => Function::new(&mut store, FunctionType::new(vec![ValueType::I32, ValueType::I64, ValueType::I32, ValueType::F32, ValueType::F64], vec![]), |_env, values| { + "3" => Function::new(&mut store, FunctionType::new(vec![ValueType::I32, ValueType::I64, ValueType::I32, ValueType::F32, ValueType::F64], vec![]), |values| { assert_eq!(values[0], Value::I32(100)); assert_eq!(values[1], Value::I64(200)); assert_eq!(values[2], Value::I32(300)); diff --git a/tests/compilers/serialize.rs b/tests/compilers/serialize.rs index 23b62716efe..2610f83da1e 100644 --- a/tests/compilers/serialize.rs +++ b/tests/compilers/serialize.rs @@ -53,7 +53,7 @@ fn test_deserialize(config: crate::Config) -> Result<()> { vec![Type::I32, Type::I64, Type::I32, Type::F32, Type::F64], vec![Type::I64], ); - let f0 = Function::new(&mut store, &func_type, |_ctx, params| { + let f0 = Function::new(&mut store, &func_type, |params| { let param_0: i64 = params[0].unwrap_i32() as i64; let param_1: i64 = params[1].unwrap_i64() as i64; let param_2: i64 = params[2].unwrap_i32() as i64; diff --git a/tests/compilers/traps.rs b/tests/compilers/traps.rs index ace95080e76..7f7dbc2bd6d 100644 --- a/tests/compilers/traps.rs +++ b/tests/compilers/traps.rs @@ -14,7 +14,7 @@ fn test_trap_return(config: crate::Config) -> Result<()> { let module = Module::new(&store, wat)?; let hello_type = FunctionType::new(vec![], vec![]); - let hello_func = Function::new(&mut store, &hello_type, |_ctx, _| { + let hello_func = Function::new(&mut store, &hello_type, |_| { Err(RuntimeError::new("test 123")) }); @@ -94,9 +94,7 @@ fn test_trap_trace_cb(config: crate::Config) -> Result<()> { "#; let fn_type = FunctionType::new(vec![], vec![]); - let fn_func = Function::new(&mut store, &fn_type, |_ctx, _| { - Err(RuntimeError::new("cb throw")) - }); + let fn_func = Function::new(&mut store, &fn_type, |_| Err(RuntimeError::new("cb throw"))); let module = Module::new(&store, wat)?; let instance = Instance::new( @@ -266,9 +264,7 @@ fn trap_start_function_import(config: crate::Config) -> Result<()> { let module = Module::new(&store, &binary)?; let sig = FunctionType::new(vec![], vec![]); - let func = Function::new(&mut store, &sig, |_env, _| { - Err(RuntimeError::new("user trap")) - }); + let func = Function::new(&mut store, &sig, |_| Err(RuntimeError::new("user trap"))); let err = Instance::new( &mut store, &module, @@ -308,7 +304,7 @@ fn rust_panic_import(config: crate::Config) -> Result<()> { let module = Module::new(&store, &binary)?; let sig = FunctionType::new(vec![], vec![]); - let func = Function::new(&mut store, &sig, |_env, _| panic!("this is a panic")); + let func = Function::new(&mut store, &sig, |_| panic!("this is a panic")); let f0 = Function::new_typed(&mut store, |_env: FunctionEnvMut<_>| { panic!("this is another panic") }); @@ -355,7 +351,7 @@ fn rust_panic_start_function(config: crate::Config) -> Result<()> { let module = Module::new(&store, &binary)?; let sig = FunctionType::new(vec![], vec![]); - let func = Function::new(&mut store, &sig, |_env, _| panic!("this is a panic")); + let func = Function::new(&mut store, &sig, |_| panic!("this is a panic")); let err = panic::catch_unwind(AssertUnwindSafe(|| { drop(Instance::new( &mut store, diff --git a/tests/compilers/typed_functions.rs b/tests/compilers/typed_functions.rs index a1a8068f1e9..6cee8ad9110 100644 --- a/tests/compilers/typed_functions.rs +++ b/tests/compilers/typed_functions.rs @@ -31,7 +31,7 @@ fn long_f( + a as u64 * 1000000000 } -fn long_f_dynamic(_env: FunctionEnvMut<()>, values: &[Value]) -> Result, RuntimeError> { +fn long_f_dynamic(values: &[Value]) -> Result, RuntimeError> { Ok(vec![Value::I64( values[9].unwrap_i32() as i64 + values[8].unwrap_i32() as i64 * 10 @@ -413,7 +413,7 @@ fn dynamic_host_function_without_env(config: crate::Config) -> anyhow::Result<() ValueType::I32, ], ), - |_env: FunctionEnvMut<_>, values| { + |values| { Ok(vec![ Value::F64(values[3].unwrap_f64() * 4.0), Value::F32(values[2].unwrap_f32() * 3.0),