Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronize between -sys and -js tests #3164

Merged
merged 17 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
477 changes: 477 additions & 0 deletions lib/api/tests/externals.rs

Large diffs are not rendered by default.

89 changes: 89 additions & 0 deletions lib/api/tests/instance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use anyhow::Result;
#[cfg(feature = "js")]
use wasm_bindgen_test::*;

use wasmer::*;


#[cfg(feature = "js")]
#[cfg_attr(feature = "js", wasm_bindgen_test)]
fn exports_work_after_multiple_instances_have_been_freed_js() {
exports_work_after_multiple_instances_have_been_freed().unwrap();
}

#[cfg_attr(feature = "sys", test)]
fn exports_work_after_multiple_instances_have_been_freed() -> Result<()> {
fschutt marked this conversation as resolved.
Show resolved Hide resolved
let mut store = Store::default();
let module = Module::new(
&store,
"
(module
(type $sum_t (func (param i32 i32) (result i32)))
(func $sum_f (type $sum_t) (param $x i32) (param $y i32) (result i32)
local.get $x
local.get $y
i32.add)
(export \"sum\" (func $sum_f)))
",
)?;

let imports = Imports::new();
let instance = Instance::new(&mut store, &module, &imports)?;
let instance2 = instance.clone();
let instance3 = instance.clone();

// The function is cloned to “break” the connection with `instance`.
let sum = instance.exports.get_function("sum")?.clone();

drop(instance);
drop(instance2);
drop(instance3);

// All instances have been dropped, but `sum` continues to work!
assert_eq!(
sum.call(&mut store, &[Value::I32(1), Value::I32(2)])?
.into_vec(),
vec![Value::I32(3)],
);

Ok(())
}


#[cfg(feature = "js")]
#[cfg_attr(feature = "js", wasm_bindgen_test)]
fn unit_native_function_env_js() {
unit_native_function_env().unwrap();
}

#[cfg_attr(feature = "sys", test)]
fn unit_native_function_env() -> Result<()> {
let mut store = Store::default();

#[derive(Clone)]
struct Env {
multiplier: u32,
}

fn imported_fn(
env: FunctionEnvMut<Env>,
args: &[Value],
) -> Result<Vec<Value>, RuntimeError> {
let value = env.data().multiplier * args[0].unwrap_i32() as u32;
Ok(vec![Value::I32(value as _)])
}

// We create the environment
let env = Env { multiplier: 3 };
// We move the environment to the store, so it can be used by the `Function`
let env = FunctionEnv::new(&mut store, env);

let imported_signature = FunctionType::new(vec![Type::I32], vec![Type::I32]);
let imported = Function::new_with_env(&mut store, &env, imported_signature, imported_fn);

let expected = vec![Value::I32(12)].into_boxed_slice();
let result = imported.call(&mut store, &[Value::I32(4)])?;
assert_eq!(result, expected);

Ok(())
}
Loading