1;; simple nested component
2(component
3  (component)
4)
5
6;; simple nested component with a nested module
7(component
8  (component
9    (core module)
10  )
11)
12
13;; simple instantiation of a nested component
14(component
15  (component $c)
16  (instance (instantiate $c))
17  (instance (instantiate $c
18    (with "x" (component $c))
19  ))
20)
21
22;; instantiate a module during a nested component, and also instantiate it
23;; as an export of the nested component
24(component
25  (component $c
26    (core module $m)
27    (core instance (instantiate $m))
28    (export "m" (core module $m))
29  )
30  (instance $i (instantiate $c))
31  (core instance $i (instantiate (module $i "m")))
32)
33
34;; instantiate an inner exported module with two different modules and
35;; verify imports match
36(component
37  (component $c
38    (core module $m
39      (import "" "g" (global $g i32))
40      (import "" "f" (func $f (result i32)))
41
42      (func $start
43        call $f
44        global.get $g
45        i32.ne
46        if unreachable end)
47
48      (start $start)
49    )
50
51    (core module $m2
52      (global (export "g") i32 i32.const 1)
53      (func (export "f") (result i32) i32.const 1)
54    )
55    (core instance $i2 (instantiate $m2))
56    (core instance (instantiate $m (with "" (instance $i2))))
57
58    (export "m" (core module $m))
59  )
60  (instance $i (instantiate $c))
61  (core module $m2
62    (global (export "g") i32 i32.const 5)
63    (func (export "f") (result i32) i32.const 5)
64  )
65  (core instance $i2 (instantiate $m2))
66  (core instance (instantiate (module $i "m") (with "" (instance $i2))))
67)
68
69;; instantiate an inner component with a module import
70(component
71  (component $c
72    (import "m" (core module $m
73      (export "g" (global i32))
74    ))
75
76    (core instance $i (instantiate $m))
77
78    (core module $verify
79      (import "" "g" (global $g i32))
80
81      (func $start
82        global.get $g
83        i32.const 2
84        i32.ne
85        if unreachable end
86      )
87
88      (start $start)
89    )
90    (core instance (instantiate $verify (with "" (instance $i))))
91  )
92
93  (core module $m
94    (global (export "g") i32 (i32.const 2))
95  )
96  (instance (instantiate $c (with "m" (core module $m))))
97)
98
99;; instantiate an inner component with a module import that itself has imports
100(component
101  (component $c
102    (import "m" (core module $m
103      (import "" "g" (global i32))
104    ))
105    (core module $m2
106      (global (export "g") i32 i32.const 2100)
107    )
108    (core instance $m2 (instantiate $m2))
109    (core instance (instantiate $m (with "" (instance $m2))))
110  )
111
112  (core module $verify
113    (import "" "g" (global $g i32))
114
115    (func $start
116      global.get $g
117      i32.const 2100
118      i32.ne
119      if unreachable end
120    )
121
122    (start $start)
123  )
124  (instance (instantiate $c (with "m" (core module $verify))))
125)
126
127;; instantiate an inner component with an export from the outer component
128(component $c
129  (core module (export "m")
130    (import "" "g1" (global $g1 i32))
131    (import "" "g2" (global $g2 i32))
132
133    (func $start
134      global.get $g1
135      i32.const 10000
136      i32.ne
137      if unreachable end
138
139      global.get $g2
140      i32.const 20000
141      i32.ne
142      if unreachable end
143    )
144
145    (start $start)
146  )
147)
148
149(component
150  (import "c" (instance $i
151    (export "m" (core module
152      (import "" "g2" (global i32))
153      (import "" "g1" (global i32))
154    ))
155  ))
156
157  (component $c
158    (import "m" (core module $verify
159      (import "" "g2" (global i32))
160      (import "" "g1" (global i32))
161    ))
162
163    (core module $m
164      (global (export "g1") i32 i32.const 10000)
165      (global (export "g2") i32 i32.const 20000)
166    )
167    (core instance $m (instantiate $m))
168    (core instance (instantiate $verify (with "" (instance $m))))
169  )
170
171  (instance (instantiate $c (with "m" (core module $i "m"))))
172)
173
174;; instantiate a reexported module
175(component
176  (core module $m
177    (global (export "g") i32 i32.const 7)
178  )
179  (component $c
180    (import "i" (instance $i
181      (export "m" (core module
182        (import "" "" (func))
183        (export "g" (global i32))
184      ))
185    ))
186
187    (export "m" (core module $i "m"))
188  )
189
190  (instance $c (instantiate $c (with "i" (instance (export "m" (core module $m))))))
191  (core module $dummy
192    (func (export ""))
193  )
194  (core instance $dummy (instantiate $dummy))
195
196  (core instance $m (instantiate (module $c "m") (with "" (instance $dummy))))
197
198  (core module $verify
199    (import "" "g" (global i32))
200    (func $start
201      global.get 0
202      i32.const 7
203      i32.ne
204      if unreachable end
205    )
206
207    (start $start)
208  )
209  (core instance (instantiate $verify (with "" (instance $m))))
210)
211
212;; module must be found through a few layers of imports
213(component $c
214  (core module (export "m")
215    (global (export "g") i32 i32.const 101)
216  )
217)
218
219(component
220  (import "c" (instance $i
221    (export "m" (core module
222      (export "g" (global i32))
223    ))
224  ))
225  (component $c1
226    (import "c" (instance $i
227      (export "m" (core module
228        (export "g" (global i32))
229      ))
230    ))
231    (core module $verify
232      (import "" "g" (global i32))
233      (func $start
234        global.get 0
235        i32.const 101
236        i32.ne
237        if unreachable end
238      )
239
240      (start $start)
241    )
242    (core instance $m (instantiate (module $i "m")))
243    (core instance (instantiate $verify (with "" (instance $m))))
244  )
245  (instance (instantiate $c1 (with "c" (instance $i))))
246)
247
248;; instantiate outer alias to self
249(component $C
250  (core module $m)
251  (alias outer $C $m (core module $other_m))
252  (core instance (instantiate $other_m))
253)
254
255(component $C
256  (component $m)
257  (alias outer $C $m (component $other_m))
258  (instance (instantiate $other_m))
259)
260
261
262;; closing over an outer alias which is actually an argument to some
263;; instantiation
264(component
265  (component $c
266    (import "c" (core module $c
267      (export "a" (global i32))
268    ))
269
270    (component (export "c2")
271      (export "m" (core module $c))
272    )
273  )
274
275  (core module $m1 (global (export "a") i32 i32.const 1))
276  (core module $m2 (global (export "a") i32 i32.const 2))
277
278  (instance $c1 (instantiate $c (with "c" (core module $m1))))
279  (instance $c2 (instantiate $c (with "c" (core module $m2))))
280
281  (instance $m1_container (instantiate (component $c1 "c2")))
282  (instance $m2_container (instantiate (component $c2 "c2")))
283
284  (core instance $core1 (instantiate (module $m1_container "m")))
285  (core instance $core2 (instantiate (module $m2_container "m")))
286
287  (core module $verify
288    (import "core1" "a" (global $a i32))
289    (import "core2" "a" (global $b i32))
290
291    (func $start
292      global.get $a
293      i32.const 1
294      i32.ne
295      if unreachable end
296
297      global.get $b
298      i32.const 2
299      i32.ne
300      if unreachable end
301    )
302
303    (start $start)
304  )
305  (core instance (instantiate $verify
306    (with "core1" (instance $core1))
307    (with "core2" (instance $core2))
308  ))
309)
310
311;; simple importing of a component
312(component
313  (component $C)
314  (component $other
315    (import "x" (component $c))
316    (instance (instantiate $c))
317  )
318  (instance (instantiate $other (with "x" (component $C))))
319)
320
321;; deep nesting
322(component $C
323  (core module $m
324    (global (export "g") i32 (i32.const 1))
325  )
326  (component $c
327    (core module (export "m")
328      (global (export "g") i32 (i32.const 2))
329    )
330  )
331
332  (component $c1
333    (component $c2 (export "a")
334      (component $c3 (export "a")
335        (alias outer $C $m (core module $my_module))
336        (alias outer $C $c (component $my_component))
337
338        (export "m" (core module $my_module))
339        (export "c" (component $my_component))
340      )
341    )
342  )
343
344  (instance $i1 (instantiate $c1))
345  (instance $i2 (instantiate (component $i1 "a")))
346  (instance $i3 (instantiate (component $i2 "a")))
347
348  (core instance $m1 (instantiate (module $i3 "m")))
349  (instance $c (instantiate (component $i3 "c")))
350  (core instance $m2 (instantiate (module $c "m")))
351
352  (core module $verify
353    (import "m1" "g" (global $m1 i32))
354    (import "m2" "g" (global $m2 i32))
355
356    (func $start
357      global.get $m1
358      i32.const 1
359      i32.ne
360      if unreachable end
361
362      global.get $m2
363      i32.const 2
364      i32.ne
365      if unreachable end
366    )
367    (start $start)
368  )
369  (core instance (instantiate $verify (with "m1" (instance $m1)) (with "m2" (instance $m2))))
370)
371
372;; Try threading through component instantiation arguments as various forms of
373;; instances.
374(component
375  (component $c
376    (core module $m (export "m"))
377    (component $c (export "c")
378      (core module (export "m"))
379    )
380    (instance $i (instantiate $c))
381    (instance $i2
382      (export "m" (core module $m))
383      (export "c" (component $c))
384      (export "i" (instance $i))
385    )
386    (export "i" (instance $i))
387    (export "i2" (instance $i2))
388  )
389  (instance $i (instantiate $c))
390
391  (component $another
392    (import "host" (instance
393      (export "m" (core module))
394      (export "c" (component))
395      (export "i" (instance))
396    ))
397  )
398  (instance (instantiate $another (with "host" (instance $i))))
399  (instance (instantiate $another (with "host" (instance $i "i2"))))
400
401  (instance $reexport
402    (export "c" (component $i "c"))
403    (export "m" (core module $i "m"))
404    (export "i" (instance $i "i"))
405  )
406  (instance (instantiate $another (with "host" (instance $reexport))))
407)
408
409;; thread host functions around
410(component
411  (import "host-return-two" (func $import (result u32)))
412
413  ;; thread the host function through an instance
414  (component $c
415    (import "a" (func $f (result u32)))
416    (export "f" (func $f))
417  )
418  (instance $c (instantiate $c (with "a" (func $import))))
419  (alias export $c "f" (func $import2))
420
421  ;; thread the host function into a nested component
422  (component $c2
423    (import "host" (instance $i (export "return-two" (func (result u32)))))
424
425    (core module $m
426      (import "host" "return-two" (func $host (result i32)))
427      (func $start
428        call $host
429        i32.const 2
430        i32.ne
431        if unreachable end
432      )
433      (start $start)
434    )
435
436    (core func $return_two
437      (canon lower (func $i "return-two"))
438    )
439    (core instance (instantiate $m
440      (with "host" (instance
441        (export "return-two" (func $return_two))
442      ))
443    ))
444  )
445
446  (instance (instantiate $c2
447    (with "host" (instance
448      (export "return-two" (func $import2))
449    ))
450  ))
451)
452