1 //! Dummy implementations of things that a Wasm module can import.
2
3 use wasmtime::{
4 Extern, ExternType, Func, FuncType, Global, GlobalType, ImportType, Memory, MemoryType, Store,
5 Table, TableType, Trap, Val, ValType,
6 };
7
8 /// Create a set of dummy functions/globals/etc for the given imports.
dummy_imports<'module>( store: &Store, import_tys: impl Iterator<Item = ImportType<'module>>, ) -> Result<Vec<Extern>, Trap>9 pub fn dummy_imports<'module>(
10 store: &Store,
11 import_tys: impl Iterator<Item = ImportType<'module>>,
12 ) -> Result<Vec<Extern>, Trap> {
13 import_tys
14 .map(|imp| {
15 Ok(match imp.ty() {
16 ExternType::Func(func_ty) => Extern::Func(dummy_func(&store, func_ty)),
17 ExternType::Global(global_ty) => Extern::Global(dummy_global(&store, global_ty)?),
18 ExternType::Table(table_ty) => Extern::Table(dummy_table(&store, table_ty)?),
19 ExternType::Memory(mem_ty) => Extern::Memory(dummy_memory(&store, mem_ty)),
20 })
21 })
22 .collect()
23 }
24
25 /// Construct a dummy function for the given function type
dummy_func(store: &Store, ty: FuncType) -> Func26 pub fn dummy_func(store: &Store, ty: FuncType) -> Func {
27 Func::new(store, ty.clone(), move |_, _, results| {
28 for (ret_ty, result) in ty.results().iter().zip(results) {
29 *result = dummy_value(ret_ty)?;
30 }
31 Ok(())
32 })
33 }
34
35 /// Construct a dummy value for the given value type.
dummy_value(val_ty: &ValType) -> Result<Val, Trap>36 pub fn dummy_value(val_ty: &ValType) -> Result<Val, Trap> {
37 Ok(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 => {
43 return Err(Trap::new(
44 "dummy_value: unsupported function return type: v128".to_string(),
45 ))
46 }
47 ValType::ExternRef => {
48 return Err(Trap::new(
49 "dummy_value: unsupported function return type: externref".to_string(),
50 ))
51 }
52 ValType::FuncRef => {
53 return Err(Trap::new(
54 "dummy_value: unsupported function return type: funcref".to_string(),
55 ))
56 }
57 })
58 }
59
60 /// Construct a sequence of dummy values for the given types.
dummy_values(val_tys: &[ValType]) -> Result<Vec<Val>, Trap>61 pub fn dummy_values(val_tys: &[ValType]) -> Result<Vec<Val>, Trap> {
62 val_tys.iter().map(dummy_value).collect()
63 }
64
65 /// Construct a dummy global for the given global type.
dummy_global(store: &Store, ty: GlobalType) -> Result<Global, Trap>66 pub fn dummy_global(store: &Store, ty: GlobalType) -> Result<Global, Trap> {
67 let val = dummy_value(ty.content())?;
68 Ok(Global::new(store, ty, val).unwrap())
69 }
70
71 /// Construct a dummy table for the given table type.
dummy_table(store: &Store, ty: TableType) -> Result<Table, Trap>72 pub fn dummy_table(store: &Store, ty: TableType) -> Result<Table, Trap> {
73 let init_val = dummy_value(&ty.element())?;
74 Ok(Table::new(store, ty, init_val).unwrap())
75 }
76
77 /// Construct a dummy memory for the given memory type.
dummy_memory(store: &Store, ty: MemoryType) -> Memory78 pub fn dummy_memory(store: &Store, ty: MemoryType) -> Memory {
79 Memory::new(store, ty)
80 }
81