Skip to content

Commit

Permalink
Remove FunctionEnvMut for Function::new(…)
Browse files Browse the repository at this point in the history
  • Loading branch information
silwol committed Aug 4, 2022
1 parent 6171b1b commit 8dec3a1
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 30 deletions.
2 changes: 1 addition & 1 deletion examples/imports_exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// 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)])
});

Expand Down
2 changes: 1 addition & 1 deletion examples/imports_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

// 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;
Expand Down
1 change: 1 addition & 0 deletions lib/api/src/js/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FT, F>(store: &mut impl AsStoreMut, ty: FT, func: F) -> Self
where
FT: Into<FunctionType>,
Expand Down
10 changes: 5 additions & 5 deletions lib/api/src/sys/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ impl Function {
pub fn new<FT, F>(store: &mut impl AsStoreMut, ty: FT, func: F) -> Self
where
FT: Into<FunctionType>,
F: Fn(FunctionEnvMut<()>, &[Value]) -> Result<Vec<Value>, RuntimeError>
+ 'static
+ Send
+ Sync,
F: Fn(&[Value]) -> Result<Vec<Value>, 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<Vec<Value>, RuntimeError> { func(args) };
Self::new_with_env(store, &env, ty, wrapped_func)
}

#[cfg(feature = "compiler")]
Expand Down
2 changes: 1 addition & 1 deletion lib/api/tests/js_externals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 6 additions & 6 deletions lib/api/tests/sys_externals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,37 +277,37 @@ 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 =
FunctionType::new(vec![Type::I32, Type::I64, Type::F32, Type::F64], 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 = 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 =
FunctionType::new(vec![], vec![Type::I32, Type::I64, Type::F32, Type::F64]);
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);

Expand All @@ -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!(
Expand Down
8 changes: 4 additions & 4 deletions tests/compilers/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion tests/compilers/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 5 additions & 9 deletions tests/compilers/traps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
});

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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")
});
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tests/compilers/typed_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn long_f(
+ a as u64 * 1000000000
}

fn long_f_dynamic(_env: FunctionEnvMut<()>, values: &[Value]) -> Result<Vec<Value>, RuntimeError> {
fn long_f_dynamic(values: &[Value]) -> Result<Vec<Value>, RuntimeError> {
Ok(vec![Value::I64(
values[9].unwrap_i32() as i64
+ values[8].unwrap_i32() as i64 * 10
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 8dec3a1

Please sign in to comment.