1(component
2  (core module $m)
3  (core instance (instantiate $m))
4)
5
6(component
7  (core module $m
8    (func (export ""))
9  )
10  (core instance $i (instantiate $m))
11
12  (core module $m2
13    (func (import "" ""))
14  )
15  (core instance (instantiate $m2 (with "" (instance $i))))
16)
17
18(component
19  (core module $m
20    (func (export "a"))
21  )
22  (core instance $i (instantiate $m))
23
24  (core module $m2
25    (func (import "" "b"))
26  )
27  (core instance (instantiate $m2
28    (with "" (instance (export "b" (func $i "a"))))
29  ))
30)
31
32;; all kinds of imports for core wasm modules, and register a start function on
33;; one module to ensure that everything is correct
34(component
35  (core module $m
36    (func (export "a"))
37    (table (export "b") 1 funcref)
38    (memory (export "c") 1)
39    (global (export "d") i32 i32.const 1)
40  )
41  (core instance $i (instantiate $m))
42
43  (core module $m2
44    (import "" "a" (func $f))
45    (import "" "b" (table 1 funcref))
46    (import "" "c" (memory 1))
47    (import "" "d" (global $g i32))
48
49    (func $start
50      global.get $g
51      i32.const 1
52      i32.ne
53      if
54        unreachable
55      end
56
57      call $f
58    )
59
60    (start $start)
61
62    (data (i32.const 0) "hello")
63    (elem (i32.const 0) $start)
64  )
65  (core instance (instantiate $m2
66    (with "" (instance $i))
67  ))
68)
69
70;; Test to see if a component with a type export can be instantiated.
71(component
72    (type string)
73    (export "a" (type 0))
74)
75
76;; double-check the start function runs by ensuring that a trap shows up and it
77;; sees the wrong value for the global import
78(assert_trap
79  (component
80    (core module $m
81      (global (export "g") i32 i32.const 1)
82    )
83    (core instance $i (instantiate $m))
84
85    (core module $m2
86      (import "" "g" (global $g i32))
87
88      (func $start
89        global.get $g
90        i32.const 0
91        i32.ne
92        if
93          unreachable
94        end
95      )
96
97      (start $start)
98    )
99    (core instance (instantiate $m2 (with "" (instance $i))))
100  )
101  "unreachable")
102
103;; shuffle around imports to get to what the target core wasm module needs
104(component
105  (core module $m
106    (func (export "1"))
107    (table (export "2") 1 funcref)
108    (memory (export "3") 1)
109    (global (export "4") i32 i32.const 1)
110  )
111  (core instance $i (instantiate $m))
112
113  (core module $m2
114    (import "" "a" (func $f))
115    (import "" "b" (table 1 funcref))
116    (import "" "c" (memory 1))
117    (import "" "d" (global $g i32))
118  )
119  (core instance (instantiate $m2
120    (with "" (instance
121      (export "a" (func $i "1"))
122      (export "b" (table $i "2"))
123      (export "c" (memory $i "3"))
124      (export "d" (global $i "4"))
125    ))
126  ))
127)
128
129;; indirect references through a synthetic instance
130(component
131  (core module $m
132    (func (export "a"))
133    (table (export "b") 1 funcref)
134    (memory (export "c") 1)
135    (global (export "d") i32 i32.const 1)
136  )
137  (core instance $i (instantiate $m))
138  (core instance $i2
139    (export "a1" (func $i "a"))
140    (export "a2" (table $i "b"))
141    (export "a3" (memory $i "c"))
142    (export "a4" (global $i "d"))
143  )
144
145  (core module $m2
146    (import "" "1" (func $f))
147    (import "" "2" (table 1 funcref))
148    (import "" "3" (memory 1))
149    (import "" "4" (global $g i32))
150  )
151  (core instance (instantiate $m2
152    (with "" (instance
153      (export "1" (func $i2 "a1"))
154      (export "2" (table $i2 "a2"))
155      (export "3" (memory $i2 "a3"))
156      (export "4" (global $i2 "a4"))
157    ))
158  ))
159)
160
161(component
162  (import "host" (instance $i (export "return-three" (func (result u32)))))
163
164  (core module $m
165    (import "host" "return-three" (func $three (result i32)))
166    (func $start
167      call $three
168      i32.const 3
169      i32.ne
170      if unreachable end
171    )
172    (start $start)
173  )
174  (core func $three_lower
175    (canon lower (func $i "return-three"))
176  )
177  (core instance (instantiate $m
178    (with "host" (instance (export "return-three" (func $three_lower))))
179  ))
180)
181
182(component
183  (import "host" (instance $i
184    (type $x' (record (field "x" u32)))
185    (export "x" (type $x (eq $x')))
186    (type $rec' (record (field "x" $x) (field "y" string)))
187    (export "rec" (type $rec (eq $rec')))
188    (export "some-record" (type (eq $rec)))))
189)
190
191(component
192  (import "host" (instance $i
193    (export "nested" (instance
194      (export "return-four" (func (result u32)))
195    ))
196  ))
197
198  (core module $m
199    (import "host" "return-three" (func $three (result i32)))
200    (func $start
201      call $three
202      i32.const 4
203      i32.ne
204      if unreachable end
205    )
206    (start $start)
207  )
208  (core func $three_lower
209    (canon lower (func $i "nested" "return-four"))
210  )
211  (core instance (instantiate $m
212    (with "host" (instance (export "return-three" (func $three_lower))))
213  ))
214)
215
216(component
217  (import "host" (instance $i
218    (export "simple-module" (core module))
219  ))
220
221  (core instance (instantiate (module $i "simple-module")))
222)
223
224(component
225  (import "host" (instance $i
226    (export "simple-module" (core module
227      (export "f" (func (result i32)))
228      (export "g" (global i32))
229    ))
230  ))
231
232  (core instance $i (instantiate (module $i "simple-module")))
233  (core module $verify
234    (import "host" "f" (func $f (result i32)))
235    (import "host" "g" (global $g i32))
236
237    (func $start
238      call $f
239      i32.const 101
240      i32.ne
241      if unreachable end
242
243      global.get $g
244      i32.const 100
245      i32.ne
246      if unreachable end
247    )
248    (start $start)
249  )
250
251  (core instance (instantiate $verify (with "host" (instance $i))))
252)
253
254;; export an instance
255(component
256  (core module $m)
257  (instance $i (export "m" (core module $m)))
258  (export "i" (instance $i))
259)
260(component
261  (component $c)
262  (instance $i (instantiate $c))
263  (export "i" (instance $i))
264)
265(component
266  (import "host" (instance $i))
267  (export "i" (instance $i))
268)
269
270
271(component definition $C1
272  (type $r1 (resource (rep i32)))
273  (export "r" (type $r1))
274)
275(component definition $C2
276  (type $r1 (resource (rep i32)))
277  (export "r" (type $r1))
278)
279
280(component instance $I1 $C1)
281(component instance $I2 $C1)
282(component instance $I3 $C2)
283(component instance $I4 $C2)
284
285;; all instances have different resource types
286(assert_unlinkable
287  (component
288    (import "I1" (instance $i1 (export "r" (type (sub resource)))))
289    (alias export $i1 "r" (type $r))
290    (import "I2" (instance $i2 (export "r" (type (eq $r)))))
291  )
292  "mismatched resource types")
293(assert_unlinkable
294  (component
295    (import "I1" (instance $i1 (export "r" (type (sub resource)))))
296    (alias export $i1 "r" (type $r))
297    (import "I3" (instance $i2 (export "r" (type (eq $r)))))
298  )
299  "mismatched resource types")
300(assert_unlinkable
301  (component
302    (import "I1" (instance $i1 (export "r" (type (sub resource)))))
303    (alias export $i1 "r" (type $r))
304    (import "I4" (instance $i2 (export "r" (type (eq $r)))))
305  )
306  "mismatched resource types")
307(assert_unlinkable
308  (component
309    (import "I2" (instance $i1 (export "r" (type (sub resource)))))
310    (alias export $i1 "r" (type $r))
311    (import "I3" (instance $i2 (export "r" (type (eq $r)))))
312  )
313  "mismatched resource types")
314(assert_unlinkable
315  (component
316    (import "I2" (instance $i1 (export "r" (type (sub resource)))))
317    (alias export $i1 "r" (type $r))
318    (import "I4" (instance $i2 (export "r" (type (eq $r)))))
319  )
320  "mismatched resource types")
321(assert_unlinkable
322  (component
323    (import "I3" (instance $i1 (export "r" (type (sub resource)))))
324    (alias export $i1 "r" (type $r))
325    (import "I4" (instance $i2 (export "r" (type (eq $r)))))
326  )
327  "mismatched resource types")
328