Skip to content

Commit

Permalink
Add test for dynamic fn using WasmerEnv
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark McCaskey committed Dec 2, 2020
1 parent 46567a4 commit 2d74536
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/engine/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ pub fn resolve_imports(
let env_ptr = if f.import_init_function_ptr.is_some() {
// Our function env looks like:
// Box<VMDynamicFunctionContext<VMDynamicFunctionWithEnv<Env>>>
// Which we can interpret as `*const <field offset> *const Env` (due to precise
// layout of these types via `repr(C)`)
// Which we can interpret as `*const <field offset> *const Env` (due to
// the precise layout of these types via `repr(C)`)
// We extract the `*const Env`:
unsafe {
// Box<VMDynamicFunctionContext<...>>
Expand Down
57 changes: 54 additions & 3 deletions tests/compilers/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ fn get_module(store: &Store) -> Result<Module> {
(import "host" "1" (func (param i32) (result i32)))
(import "host" "2" (func (param i32) (param i64)))
(import "host" "3" (func (param i32 i64 i32 f32 f64)))
(memory $mem 1)
(export "memory" (memory $mem))
(func $foo
call 0
Expand Down Expand Up @@ -87,16 +89,20 @@ fn dynamic_function_with_env() -> Result<()> {
let module = get_module(&store)?;

#[derive(WasmerEnv, Clone)]
struct Env(Arc<AtomicUsize>);
struct Env {
counter: Arc<AtomicUsize>,
};

impl std::ops::Deref for Env {
type Target = Arc<AtomicUsize>;
fn deref(&self) -> &Self::Target {
&self.0
&self.counter
}
}

let env: Env = Env(Arc::new(AtomicUsize::new(0)));
let env: Env = Env {
counter: Arc::new(AtomicUsize::new(0)),
};
Instance::new(
&module,
&imports! {
Expand Down Expand Up @@ -292,3 +298,48 @@ fn static_function_that_fails() -> Result<()> {

Ok(())
}

fn get_module2(store: &Store) -> Result<Module> {
let wat = r#"
(import "host" "fn" (func))
(memory $mem 1)
(export "memory" (memory $mem))
(export "main" (func $main))
(func $main (param) (result)
(call 0))
"#;

let module = Module::new(&store, &wat)?;
Ok(module)
}

#[test]
fn dynamic_function_with_env_wasmer_env_init_works() -> Result<()> {
let store = get_store(false);
let module = get_module2(&store)?;

#[allow(dead_code)]
#[derive(WasmerEnv, Clone)]
struct Env {
#[wasmer(export)]
memory: LazyInit<Memory>,
};

let env: Env = Env {
memory: LazyInit::default(),
};
let instance = Instance::new(
&module,
&imports! {
"host" => {
"fn" => Function::new_with_env(&store, &FunctionType::new(vec![], vec![]), env.clone(), |env, _values| {
assert!(env.memory_ref().is_some());
Ok(vec![])
}),
},
},
)?;
let f: NativeFunc<(), ()> = instance.exports.get_native_function("main")?;
f.call()?;
Ok(())
}

0 comments on commit 2d74536

Please sign in to comment.