Wasmer 3 upgrade: Testing import implementations but FunctionEnvMut is gone #3824
-
SummaryI'm in the process of upgrading to Wasmer 3 but the new FunctionEnv design is causing some trouble. Additional detailsI would like to test my import implementation env_imports.insert(
"db_read",
Function::new_typed_with_env(&mut store, &fe, do_db_read),
); The signature is: pub fn do_db_read<A: BackendApi + 'static, S: Storage + 'static, Q: Querier + 'static>(
mut env: FunctionEnvMut<Environment<A, S, Q>>,
key_ptr: u32,
) -> VmResult<u32> { I have this test (slightly simplified version): #[test]
fn do_db_read_works() {
// returns an owned fe: FunctionEnv and store: Strore
let (fe, mut store, _instance) = make_instance();
let mut fe_mut = fe.into_mut(&mut store);
// Populate env with some data before the test
leave_default_data(&mut fe_mut);
let key_ptr = write_data(&mut fe_mut, KEY1);
// The actual test consumed the fe_mut: FunctionEnvMut
let result = do_db_read(fe_mut, key_ptr);
// Aftermath has no function env available anymore 😢
let value_ptr = result.unwrap();
assert!(value_ptr > 0);
assert_eq!(force_read(fe.into_mut(&mut store), value_ptr), VALUE1);
} I do understand that FunctionEnv and FunctionEnvMut are not supposed to be clonable. And my import implementation must take an owned |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
Use |
Beta Was this translation helpful? Give feedback.
-
Also, converting this to a discussion. |
Beta Was this translation helpful? Give feedback.
-
One solution seems to be downgrading the owned FunctionEnvMut to a reference in a wrapper function like this: fn do_db_read_wrapper<
A: BackendApi + 'static,
S: Storage + 'static,
Q: Querier + 'static,
>(
mut ctx: FunctionEnvMut<Environment<A, S, Q>>,
a: u32,
) -> VmResult<u32> {
do_db_read(&mut ctx, a)
}
env_imports.insert(
"db_read",
Function::new_typed_with_env(&mut store, &fe, do_db_read_wrapper),
); But this is a lot of overhead for all the functions. I tried using closures to but it seems like closures are not possible in |
Beta Was this translation helpful? Give feedback.
But again,
as_mut()
should act like aclone()
and allow you to duplicatedFunctionEnvMut