use wasmtime::*;
#[test]
fn bad_globals() {
let mut store = Store::<()>::default();
let ty = GlobalType::new(ValType::I32, Mutability::Var);
assert!(Global::new(&mut store, ty.clone(), Val::I64(0)).is_err());
assert!(Global::new(&mut store, ty.clone(), Val::F32(0)).is_err());
assert!(Global::new(&mut store, ty.clone(), Val::F64(0)).is_err());
let ty = GlobalType::new(ValType::I32, Mutability::Const);
let g = Global::new(&mut store, ty.clone(), Val::I32(0)).unwrap();
assert!(g.set(&mut store, Val::I32(1)).is_err());
let ty = GlobalType::new(ValType::I32, Mutability::Var);
let g = Global::new(&mut store, ty.clone(), Val::I32(0)).unwrap();
assert!(g.set(&mut store, Val::I64(0)).is_err());
}
#[test]
fn bad_tables() {
let mut store = Store::<()>::default();
// mismatched initializer
let ty = TableType::new(RefType::FUNCREF, 0, Some(1));
assert!(Table::new(&mut store, ty.clone(), Ref::Extern(None)).is_err());
// get out of bounds
let ty = TableType::new(RefType::FUNCREF, 0, Some(1));
let t = Table::new(&mut store, ty.clone(), Ref::Func(None)).unwrap();
assert!(t.get(&mut store, 0).is_none());
assert!(t.get(&mut store, u32::max_value()).is_none());
// set out of bounds or wrong type
let ty = TableType::new(RefType::FUNCREF, 1, Some(1));
let t = Table::new(&mut store, ty.clone(), Ref::Func(None)).unwrap();
assert!(t.set(&mut store, 0, Ref::Extern(None)).is_err());
assert!(t.set(&mut store, 0, Ref::Func(None)).is_ok());
assert!(t.set(&mut store, 1, Ref::Func(None)).is_err());
// grow beyond max
let ty = TableType::new(RefType::FUNCREF, 1, Some(1));
let t = Table::new(&mut store, ty.clone(), Ref::Func(None)).unwrap();
assert!(t.grow(&mut store, 0, Ref::Func(None)).is_ok());
assert!(t.grow(&mut store, 1, Ref::Func(None)).is_err());
assert_eq!(t.size(&store), 1);
// grow wrong type
let ty = TableType::new(RefType::FUNCREF, 1, Some(2));
let t = Table::new(&mut store, ty.clone(), Ref::Func(None)).unwrap();
assert!(t.grow(&mut store, 1, Ref::Extern(None)).is_err());
assert_eq!(t.size(&store), 1);
}
#[test]
#[cfg_attr(miri, ignore)]
fn cross_store() -> anyhow::Result<()> {
let mut cfg = Config::new();
cfg.wasm_reference_types(true);
let engine = Engine::new(&cfg)?;
let mut store1 = Store::new(&engine, ());
let mut store2 = Store::new(&engine, ());
eprintln!("============ Cross-store instantiation ==============");
let func = Func::wrap(&mut store2, || {});
let ty = GlobalType::new(ValType::I32, Mutability::Const);
let global = Global::new(&mut store2, ty, Val::I32(0))?;
let ty = MemoryType::new(1, None);
let memory = Memory::new(&mut store2, ty)?;
let ty = TableType::new(RefType::FUNCREF, 1, None);
let table = Table::new(&mut store2, ty, Ref::Func(None))?;
let need_func = Module::new(&engine, r#"(module (import "" "" (func)))"#)?;
assert!(Instance::new(&mut store1, &need_func, &[func.into()]).is_err());
let need_global = Module::new(&engine, r#"(module (import "" "" (global i32)))"#)?;
assert!(Instance::new(&mut store1, &need_global, &[global.into()]).is_err());
let need_table = Module::new(&engine, r#"(module (import "" "" (table 1 funcref)))"#)?;
assert!(Instance::new(&mut store1, &need_table, &[table.into()]).is_err());
let need_memory = Module::new(&engine, r#"(module (import "" "" (memory 1)))"#)?;
assert!(Instance::new(&mut store1, &need_memory, &[memory.into()]).is_err());
eprintln!("============ Cross-store globals ==============");
let store1val = Val::FuncRef(Some(Func::wrap(&mut store1, || {})));
let store1ref = store1val.clone().ref_().unwrap();
let store2val = Val::FuncRef(Some(Func::wrap(&mut store2, || {})));
let store2ref = store2val.clone().ref_().unwrap();
let ty = GlobalType::new(ValType::FUNCREF, Mutability::Var);
assert!(Global::new(&mut store2, ty.clone(), store1val.clone()).is_err());
if let Ok(g) = Global::new(&mut store2, ty.clone(), store2val.clone()) {
assert!(g.set(&mut store2, store1val.clone()).is_err());
}
eprintln!("============ Cross-store tables ==============");
let ty = TableType::new(RefType::FUNCREF, 1, None);
assert!(Table::new(&mut store2, ty.clone(), store1ref.clone()).is_err());
let t1 = Table::new(&mut store2, ty.clone(), store2ref.clone())?;
assert!(t1.set(&mut store2, 0, store1ref.clone()).is_err());
assert!(t1.grow(&mut store2, 0, store1ref.clone()).is_err());
assert!(t1.fill(&mut store2, 0, store1ref.clone(), 1).is_err());
eprintln!("============ Cross-store funcs ==============");
let module = Module::new(&engine, r#"(module (func (export "f") (param funcref)))"#)?;
let s1_inst = Instance::new(&mut store1, &module, &[])?;
let s2_inst = Instance::new(&mut store2, &module, &[])?;
let s1_f = s1_inst.get_func(&mut store1, "f").unwrap();
let s2_f = s2_inst.get_func(&mut store2, "f").unwrap();
assert!(s1_f
.call(&mut store1, &[Val::FuncRef(None)], &mut [])
.is_ok());
assert!(s2_f
.call(&mut store2, &[Val::FuncRef(None)], &mut [])
.is_ok());
assert!(s1_f
.call(&mut store1, &[Some(s1_f.clone()).into()], &mut [])
.is_ok());
assert!(s1_f
.call(&mut store1, &[Some(s2_f.clone()).into()], &mut [])
.is_err());
assert!(s2_f
.call(&mut store2, &[Some(s1_f.clone()).into()], &mut [])
.is_err());
assert!(s2_f
.call(&mut store2, &[Some(s2_f.clone()).into()], &mut [])
.is_ok());
let s1_f_t = s1_f.typed::