1 #![cfg(not(miri))]
2
3 use super::REALLOC_AND_FREE;
4 use wasmtime::Result;
5 use wasmtime::component::*;
6 use wasmtime::{Module, Store, StoreContextMut};
7
8 #[test]
top_level_instance_two_level() -> Result<()>9 fn top_level_instance_two_level() -> Result<()> {
10 let component = r#"
11 (component
12 (import "c" (instance $i
13 (export "c" (instance
14 (export "m" (core module
15 (export "g" (global i32))
16 ))
17 ))
18 ))
19 (component $c1
20 (import "c" (instance $i
21 (export "c" (instance
22 (export "m" (core module
23 (export "g" (global i32))
24 ))
25 ))
26 ))
27 (core module $verify
28 (import "" "g" (global i32))
29 (func $start
30 global.get 0
31 i32.const 101
32 i32.ne
33 if unreachable end
34 )
35
36 (start $start)
37 )
38 (core instance $m (instantiate (module $i "c" "m")))
39 (core instance (instantiate $verify (with "" (instance $m))))
40 )
41 (instance (instantiate $c1 (with "c" (instance $i))))
42 )
43 "#;
44 let module = r#"
45 (module
46 (global (export "g") i32 i32.const 101)
47 )
48 "#;
49
50 let engine = super::engine();
51 let module = Module::new(&engine, module)?;
52 let component = Component::new(&engine, component)?;
53 let mut store = Store::new(&engine, ());
54 let mut linker = Linker::new(&engine);
55 linker.instance("c")?.instance("c")?.module("m", &module)?;
56 linker.instantiate(&mut store, &component)?;
57 Ok(())
58 }
59
60 #[test]
nested_many_instantiations() -> Result<()>61 fn nested_many_instantiations() -> Result<()> {
62 let component = r#"
63 (component
64 (import "count" (func $count))
65 (component $c1
66 (import "count" (func $count))
67 (core func $count_lower (canon lower (func $count)))
68 (core module $m
69 (import "" "" (func $count))
70 (start $count)
71 )
72 (core instance (instantiate $m (with "" (instance (export "" (func $count_lower))))))
73 (core instance (instantiate $m (with "" (instance (export "" (func $count_lower))))))
74 )
75 (component $c2
76 (import "count" (func $count))
77 (instance (instantiate $c1 (with "count" (func $count))))
78 (instance (instantiate $c1 (with "count" (func $count))))
79 )
80 (component $c3
81 (import "count" (func $count))
82 (instance (instantiate $c2 (with "count" (func $count))))
83 (instance (instantiate $c2 (with "count" (func $count))))
84 )
85 (component $c4
86 (import "count" (func $count))
87 (instance (instantiate $c3 (with "count" (func $count))))
88 (instance (instantiate $c3 (with "count" (func $count))))
89 )
90
91 (instance (instantiate $c4 (with "count" (func $count))))
92 )
93 "#;
94 let engine = super::engine();
95 let component = Component::new(&engine, component)?;
96 let mut store = Store::new(&engine, 0);
97 let mut linker = Linker::new(&engine);
98 linker
99 .root()
100 .func_wrap("count", |mut store: StoreContextMut<'_, u32>, _: ()| {
101 *store.data_mut() += 1;
102 Ok(())
103 })?;
104 linker.instantiate(&mut store, &component)?;
105 assert_eq!(*store.data(), 16);
106 Ok(())
107 }
108
109 #[test]
thread_options_through_inner() -> Result<()>110 fn thread_options_through_inner() -> Result<()> {
111 let component = format!(
112 r#"
113 (component
114 (import "hostfn" (func $host (param "a" u32) (result string)))
115
116 (component $c
117 (import "hostfn" (func $host (param "a" u32) (result string)))
118
119 (core module $libc
120 (memory (export "memory") 1)
121 {REALLOC_AND_FREE}
122 )
123 (core instance $libc (instantiate $libc))
124
125 (core func $host_lower
126 (canon lower
127 (func $host)
128 (memory $libc "memory")
129 (realloc (func $libc "realloc"))
130 )
131 )
132
133 (core module $m
134 (import "" "host" (func $host (param i32 i32)))
135 (import "libc" "memory" (memory 1))
136 (func (export "run") (param i32) (result i32)
137 i32.const 42
138 i32.const 100
139 call $host
140 i32.const 100
141 )
142 (export "memory" (memory 0))
143 )
144 (core instance $m (instantiate $m
145 (with "" (instance (export "host" (func $host_lower))))
146 (with "libc" (instance $libc))
147 ))
148
149 (func (export "run") (param "a" u32) (result string)
150 (canon lift
151 (core func $m "run")
152 (memory $m "memory")
153 )
154 )
155 )
156 (instance $c (instantiate $c (with "hostfn" (func $host))))
157 (export "run" (func $c "run"))
158 )
159 "#
160 );
161 let engine = super::engine();
162 let component = Component::new(&engine, component)?;
163 let mut store = Store::new(&engine, 0);
164 let mut linker = Linker::new(&engine);
165 linker
166 .root()
167 .func_wrap("hostfn", |_, (param,): (u32,)| Ok((param.to_string(),)))?;
168 let instance = linker.instantiate(&mut store, &component)?;
169 let result = instance
170 .get_typed_func::<(u32,), (WasmStr,)>(&mut store, "run")?
171 .call(&mut store, (43,))?
172 .0;
173 assert_eq!(result.to_str(&store)?, "42");
174 Ok(())
175 }
176