Skip to content

Commit

Permalink
Remove with_func_from_context
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed Dec 30, 2020
1 parent d1fab6b commit b16ad45
Showing 1 changed file with 6 additions and 61 deletions.
67 changes: 6 additions & 61 deletions packages/vm/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::borrow::{Borrow, BorrowMut};
use std::ptr::NonNull;
use std::sync::{Arc, RwLock};

use wasmer::{Function, HostEnvInitError, Instance as WasmerInstance, Memory, Val, WasmerEnv};
use wasmer::{HostEnvInitError, Instance as WasmerInstance, Memory, Val, WasmerEnv};
use wasmer_middlewares::metering::{get_remaining_points, set_remaining_points, MeteringPoints};

use crate::backend::{Api, GasInfo, Querier, Storage};
Expand Down Expand Up @@ -130,24 +130,17 @@ impl<A: Api, S: Storage, Q: Querier> Environment<A, S, Q> {
self.with_context_data(|context_data| callback(&context_data.gas_state))
}

fn with_func_from_context<C, T>(&self, name: &str, callback: C) -> VmResult<T>
where
C: FnOnce(&Function) -> VmResult<T>,
{
self.with_context_data_mut(|context_data| match context_data.wasmer_instance {
pub fn call_function(&self, name: &str, args: &[Val]) -> VmResult<Box<[Val]>> {
// Clone function before calling it to avoid dead locks
let func = self.with_context_data(|context_data| match context_data.wasmer_instance {
Some(instance_ptr) => {
let func = unsafe { instance_ptr.as_ref() }
.exports
.get_function(name)?;
callback(func)
Ok(func.clone())
}
None => Err(VmError::uninitialized_context_data("wasmer_instance")),
})
}

pub fn call_function(&self, name: &str, args: &[Val]) -> VmResult<Box<[Val]>> {
// Clone function before calling it to avoid dead locks
let func = self.with_func_from_context(name, |func| Ok(func.clone()))?;
})?;
Ok(func.call(args)?)
}

Expand Down Expand Up @@ -536,54 +529,6 @@ mod tests {
assert_eq!(env.is_storage_readonly(), true);
}

#[test]
fn with_func_from_context_works() {
let (env, _instance) = make_instance();
leave_default_data(&env);

let ptr = env
.with_func_from_context::<_, _>("allocate", |alloc_func| {
let result = alloc_func.call(&[10u32.into()])?;
let ptr = ref_to_u32(&result[0])?;
Ok(ptr)
})
.unwrap();
assert!(ptr > 0);
}

#[test]
fn with_func_from_context_fails_for_missing_instance() {
let (env, _instance) = make_instance();
leave_default_data(&env);

// Clear context's wasmer_instance
env.set_wasmer_instance(None);

let res = env.with_func_from_context::<_, ()>("allocate", |_func| {
panic!("unexpected callback call");
});
match res.unwrap_err() {
VmError::UninitializedContextData { kind, .. } => assert_eq!(kind, "wasmer_instance"),
err => panic!("Unexpected error: {:?}", err),
}
}

#[test]
fn with_func_from_context_fails_for_missing_function() {
let (env, _instance) = make_instance();
leave_default_data(&env);

let res = env.with_func_from_context::<_, ()>("doesnt_exist", |_func| {
panic!("unexpected callback call");
});
match res.unwrap_err() {
VmError::ResolveErr { msg, .. } => {
assert_eq!(msg, "Could not get export: Missing export doesnt_exist");
}
err => panic!("Unexpected error: {:?}", err),
}
}

#[test]
fn call_function_works() {
let (env, _instance) = make_instance();
Expand Down

0 comments on commit b16ad45

Please sign in to comment.