1
1
//! Dummy implementations of things that a Wasm module can import.
2
2
3
- use anyhow:: ensure;
4
3
use wasmtime:: * ;
5
4
6
5
/// Create a set of dummy functions/globals/etc for the given imports.
7
6
pub fn dummy_linker < T > ( store : & mut Store < T > , module : & Module ) -> Result < Linker < T > > {
8
7
let mut linker = Linker :: new ( store. engine ( ) ) ;
9
8
linker. allow_shadowing ( true ) ;
10
9
for import in module. imports ( ) {
11
- let extern_ = dummy_extern ( store, import. ty ( ) ) ?;
10
+ let extern_ = import
11
+ . ty ( )
12
+ . default_value ( & mut * store)
13
+ . ok_or ( anyhow:: anyhow!( "ERROR" ) ) ?;
12
14
linker
13
15
. define ( & store, import. module ( ) , import. name ( ) , extern_)
14
16
. unwrap ( ) ;
15
17
}
16
18
Ok ( linker)
17
19
}
18
20
19
- /// Construct a dummy `Extern` from its type signature
20
- pub fn dummy_extern < T > ( store : & mut Store < T > , ty : ExternType ) -> Result < Extern > {
21
- Ok ( match ty {
22
- ExternType :: Func ( func_ty) => Extern :: Func ( dummy_func ( store, func_ty) ?) ,
23
- ExternType :: Global ( global_ty) => Extern :: Global ( dummy_global ( store, global_ty) ?) ,
24
- ExternType :: Table ( table_ty) => Extern :: Table ( dummy_table ( store, table_ty) ?) ,
25
- ExternType :: Memory ( mem_ty) => Extern :: Memory ( dummy_memory ( store, mem_ty) ?) ,
26
- ExternType :: Tag ( _tag_ty) => todo ! ( ) , // FIXME: #10252
27
- } )
28
- }
29
-
30
- /// Construct a dummy function for the given function type
31
- pub fn dummy_func < T > ( store : & mut Store < T > , ty : FuncType ) -> Result < Func > {
32
- let dummy_results = ty. results ( ) . map ( dummy_value) . collect :: < Result < Vec < _ > > > ( ) ?;
33
- Ok ( Func :: new ( store, ty. clone ( ) , move |_, _, results| {
34
- for ( slot, dummy) in results. iter_mut ( ) . zip ( & dummy_results) {
35
- * slot = * dummy;
36
- }
37
- Ok ( ( ) )
38
- } ) )
39
- }
40
-
41
- /// Construct a dummy value for the given value type.
42
- pub fn dummy_value ( val_ty : ValType ) -> Result < Val > {
43
- Ok ( match val_ty {
44
- ValType :: I32 => Val :: I32 ( 0 ) ,
45
- ValType :: I64 => Val :: I64 ( 0 ) ,
46
- ValType :: F32 => Val :: F32 ( 0 ) ,
47
- ValType :: F64 => Val :: F64 ( 0 ) ,
48
- ValType :: V128 => Val :: V128 ( 0 . into ( ) ) ,
49
- ValType :: Ref ( r) => {
50
- ensure ! (
51
- r. is_nullable( ) ,
52
- "cannot construct a dummy value of type `{r}`"
53
- ) ;
54
- Val :: null_ref ( r. heap_type ( ) )
55
- }
56
- } )
57
- }
58
-
59
- /// Construct a sequence of dummy values for the given types.
60
- pub fn dummy_values ( val_tys : impl IntoIterator < Item = ValType > ) -> Result < Vec < Val > > {
61
- val_tys. into_iter ( ) . map ( dummy_value) . collect ( )
62
- }
63
-
64
- /// Construct a dummy global for the given global type.
65
- pub fn dummy_global < T > ( store : & mut Store < T > , ty : GlobalType ) -> Result < Global > {
66
- let val = dummy_value ( ty. content ( ) . clone ( ) ) ?;
67
- Global :: new ( store, ty, val)
68
- }
69
-
70
- /// Construct a dummy table for the given table type.
71
- pub fn dummy_table < T > ( store : & mut Store < T > , ty : TableType ) -> Result < Table > {
72
- let init_val = dummy_value ( ty. element ( ) . clone ( ) . into ( ) ) ?;
73
- Table :: new ( store, ty, init_val. ref_ ( ) . unwrap ( ) )
74
- }
75
-
76
- /// Construct a dummy memory for the given memory type.
77
- pub fn dummy_memory < T > ( store : & mut Store < T > , ty : MemoryType ) -> Result < Memory > {
78
- Memory :: new ( store, ty)
79
- }
80
-
81
21
#[ cfg( test) ]
82
22
mod tests {
83
23
@@ -93,7 +33,8 @@ mod tests {
93
33
#[ test]
94
34
fn dummy_table_import ( ) {
95
35
let mut store = store ( ) ;
96
- let table = dummy_table ( & mut store, TableType :: new ( RefType :: EXTERNREF , 10 , None ) ) . unwrap ( ) ;
36
+ let table_type = TableType :: new ( RefType :: EXTERNREF , 10 , None ) ;
37
+ let table = table_type. default_value ( & mut store) . unwrap ( ) ;
97
38
assert_eq ! ( table. size( & store) , 10 ) ;
98
39
for i in 0 ..10 {
99
40
assert ! ( table. get( & mut store, i) . unwrap( ) . unwrap_extern( ) . is_none( ) ) ;
@@ -103,24 +44,25 @@ mod tests {
103
44
#[ test]
104
45
fn dummy_global_import ( ) {
105
46
let mut store = store ( ) ;
106
- let global =
107
- dummy_global ( & mut store, GlobalType :: new ( ValType :: I32 , Mutability :: Const ) ) . unwrap ( ) ;
47
+ let global_type = GlobalType :: new ( ValType :: I32 , Mutability :: Const ) ;
48
+ let global = global_type . default_value ( & mut store) . unwrap ( ) ;
108
49
assert ! ( global. ty( & store) . content( ) . is_i32( ) ) ;
109
50
assert_eq ! ( global. ty( & store) . mutability( ) , Mutability :: Const ) ;
110
51
}
111
52
112
53
#[ test]
113
54
fn dummy_memory_import ( ) {
114
55
let mut store = store ( ) ;
115
- let memory = dummy_memory ( & mut store, MemoryType :: new ( 1 , None ) ) . unwrap ( ) ;
56
+ let memory_type = MemoryType :: new ( 1 , None ) ;
57
+ let memory = memory_type. default_value ( & mut store) . unwrap ( ) ;
116
58
assert_eq ! ( memory. size( & store) , 1 ) ;
117
59
}
118
60
119
61
#[ test]
120
62
fn dummy_function_import ( ) {
121
63
let mut store = store ( ) ;
122
64
let func_ty = FuncType :: new ( store. engine ( ) , vec ! [ ValType :: I32 ] , vec ! [ ValType :: I64 ] ) ;
123
- let func = dummy_func ( & mut store, func_ty . clone ( ) ) . unwrap ( ) ;
65
+ let func = func_ty . default_value ( & mut store) . unwrap ( ) ;
124
66
let actual_ty = func. ty ( & store) ;
125
67
assert ! ( FuncType :: eq( & actual_ty, & func_ty) ) ;
126
68
}
0 commit comments