From e57f02d99305526c08d3649204a3f42a44541bf1 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Fri, 27 Nov 2020 09:50:04 +0100 Subject: [PATCH] feat(api) Avoid using a closure to declare host function. Closures as host functions are going to be disabled for a moment in Wasmer, see https://github.com/wasmerio/wasmer/pull/1841. In the `wasmer` Python package, we were passing a closure to `Function::new_with_env`. In order to get a regular function, this patch stores the previously captured varibles into the function environment. The only concerned variable is `result_types`. --- packages/api/src/externals/function.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/api/src/externals/function.rs b/packages/api/src/externals/function.rs index ff39b2bc..31d3d466 100644 --- a/packages/api/src/externals/function.rs +++ b/packages/api/src/externals/function.rs @@ -136,19 +136,21 @@ impl Function { struct Environment { py_function: PyObject, + result_types: Vec, } let environment = Environment { py_function: py_function.to_object(py), + result_types: result_types.clone(), }; let host_function = wasmer::Function::new_with_env( store.inner(), - &wasmer::FunctionType::new(argument_types, result_types.clone()), + &wasmer::FunctionType::new(argument_types, result_types), environment, - move |environment, - arguments: &[wasmer::Value]| - -> Result, wasmer::RuntimeError> { + |environment, + arguments: &[wasmer::Value]| + -> Result, wasmer::RuntimeError> { let gil = Python::acquire_gil(); let py = gil.python(); @@ -162,7 +164,8 @@ impl Function { wasmer::RuntimeError::new(io::Error::from(error).to_string()) })?; - let result_types = result_types.clone(); + let result_types = environment.result_types.clone(); + let has_result_types = !result_types.is_empty(); Ok(if let Ok(results) = results.cast_as::(py) { results @@ -173,7 +176,7 @@ impl Function { .map_err(|error| { wasmer::RuntimeError::new(io::Error::from(error).to_string()) })? - } else if !results.is_none(py) && !result_types.is_empty() { + } else if !results.is_none(py) && has_result_types { vec![to_wasm_value(( results .cast_as::(py)