| 1 |
//! Dummy implementations of things that a Wasm module can import. |
| 2 |
|
| 3 |
use wasmtime::*; |
| 4 |
|
| 5 |
/// Create a set of dummy functions/globals/etc for the given imports. |
| 6 |
pub fn dummy_imports<T>(store: &mut Store<T>, module: &Module) -> Result<Vec<Extern>> { |
| 7 |
let mut result = Vec::new(); |
| 8 |
for import in module.imports() { |
| 9 |
result.push(dummy_extern(store, import.ty())?); |
| 10 |
} |
| 11 |
Ok(result) |
| 12 |
} |
| 13 |
|
| 14 |
/// Construct a dummy `Extern` from its type signature |
| 15 |
pub fn dummy_extern<T>(store: &mut Store<T>, ty: ExternType) -> Result<Extern> { |
| 16 |
Ok(match ty { |
| 17 |
ExternType::Func(func_ty) => Extern::Func(dummy_func(store, func_ty)), |
| 18 |
ExternType::Global(global_ty) => Extern::Global(dummy_global(store, global_ty)), |
| 19 |
ExternType::Table(table_ty) => Extern::Table(dummy_table(store, table_ty)?), |
| 20 |
ExternType::Memory(mem_ty) => Extern::Memory(dummy_memory(store, mem_ty)?), |
| 21 |
ExternType::Tag(_) => todo!(), |
| 22 |
}) |
| 23 |
} |
| 24 |
|
| 25 |
/// Construct a dummy function for the given function type |
| 26 |
pub fn dummy_func<T>(store: &mut Store<T>, ty: FuncType) -> Func { |
| 27 |
Func::new(store, ty.clone(), move |_, _, results| { |
| 28 |
for (ret_ty, result) in ty.results().zip(results) { |
| 29 |
*result = dummy_value(ret_ty); |
| 30 |
} |
| 31 |
Ok(()) |
| 32 |
}) |
| 33 |
} |
| 34 |
|
| 35 |
/// Construct a dummy value for the given value type. |
| 36 |
pub fn dummy_value(val_ty: ValType) -> Val { |
| 37 |
match val_ty { |
| 38 |
ValType::I32 => Val::I32(0), |
| 39 |
ValType::I64 => Val::I64(0), |
| 40 |
ValType::F32 => Val::F32(0), |
| 41 |
ValType::F64 => Val::F64(0), |
| 42 |
ValType::V128 => Val::V128(0.into()), |
| 43 |
ValType::Ref(r) => dummy_ref(&r).into(), |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/// Construct a dummy value for the given value type. |
| 48 |
pub fn dummy_ref(ty: &RefType) -> Ref { |
| 49 |
assert!(ty.is_nullable()); |
| 50 |
match ty.heap_type() { |
| 51 |
HeapType::Extern | HeapType::NoExtern => Ref::Extern(None), |
| 52 |
HeapType::NoFunc | HeapType::Func | HeapType::ConcreteFunc(_) => Ref::Func(None), |
| 53 |
HeapType::Any |
| 54 |
| HeapType::I31 |
| 55 |
| HeapType::None |
| 56 |
| HeapType::Eq |
| 57 |
| HeapType::Array |
| 58 |
| HeapType::Struct |
| 59 |
| HeapType::ConcreteArray(_) |
| 60 |
| HeapType::ConcreteStruct(_) => Ref::Any(None), |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/// Construct a sequence of dummy values for the given types. |
| 65 |
pub fn dummy_values(val_tys: impl IntoIterator<Item = ValType>) -> Vec<Val> { |
| 66 |
val_tys.into_iter().map(dummy_value).collect() |
| 67 |
} |
| 68 |
|
| 69 |
/// Construct a dummy global for the given global type. |
| 70 |
pub fn dummy_global<T>(store: &mut Store<T>, ty: GlobalType) -> Global { |
| 71 |
let val = dummy_value(ty.content().clone()); |
| 72 |
Global::new(store, ty, val).unwrap() |
| 73 |
} |
| 74 |
|
| 75 |
/// Construct a dummy table for the given table type. |
| 76 |
pub fn dummy_table<T>(store: &mut Store<T>, ty: TableType) -> Result<Table> { |
| 77 |
let init_val = dummy_ref(ty.element()); |
| 78 |
Table::new(store, ty, init_val) |
| 79 |
} |
| 80 |
|
| 81 |
/// Construct a dummy memory for the given memory type. |
| 82 |
pub fn dummy_memory<T>(store: &mut Store<T>, ty: MemoryType) -> Result<Memory> { |
| 83 |
Memory::new(store, ty) |
| 84 |
} |