1;;! reference_types = true
2;;! exceptions = true
3
4(component $foo
5  (core module (export "a-module"))
6)
7
8;; the above instance can be imported into this component
9(component
10  (import "foo" (instance
11    (export "a-module" (core module))
12  ))
13)
14
15;; specifying extra imports is ok
16(component
17  (import "foo" (instance
18    (export "a-module" (core module
19      (import "foo" "bar" (func))
20    ))
21  ))
22)
23
24;; specifying extra exports is not ok
25(assert_unlinkable
26  (component
27    (import "foo" (instance
28      (export "a-module" (core module
29        (export "the-export" (func))
30      ))
31    ))
32  )
33  "module export `the-export` not defined")
34
35(component $foo
36  (core module (export "a-module")
37    (import "env" "something" (func))
38  )
39)
40
41;; imports must be specified
42(assert_unlinkable
43  (component
44    (import "foo" (instance
45      (export "a-module" (core module))
46    ))
47  )
48  "module import `env::something` not defined")
49
50(component
51  (import "foo" (instance
52    (export "a-module" (core module
53      (import "env" "something" (func))
54    ))
55  ))
56)
57
58;; extra imports still ok
59(component
60  (import "foo" (instance
61    (export "a-module" (core module
62      (import "env" "something" (func))
63      (import "env" "other" (global i32))
64    ))
65  ))
66)
67
68(component $foo
69  (core module (export "a-module")
70    (func (export "f"))
71  )
72)
73
74;; dropping exports is ok
75(component
76  (import "foo" (instance
77    (export "a-module" (core module))
78  ))
79)
80
81(component
82  (import "foo" (instance
83    (export "a-module" (core module
84      (export "f" (func))
85    ))
86  ))
87)
88
89(assert_unlinkable
90  (component
91    (import "foo" (instance
92      (export "a-module" (core module
93        (export "f" (func (param i32)))
94      ))
95    ))
96  )
97  "expected type `(func (param i32))`, found type `(func)`")
98
99(assert_unlinkable
100  (component
101    (import "foo" (instance
102      (export "a-module" (core module
103        (export "f" (global i32))
104      ))
105    ))
106  )
107  "expected global found func")
108
109(component $foo
110  (core module (export "m")
111    (func (export "f"))
112    (table (export "t") 1 funcref)
113    (memory (export "m") 1)
114    (global (export "g") i32 i32.const 0)
115  )
116)
117
118;; wrong class of item
119(assert_unlinkable
120  (component
121    (import "foo" (instance
122      (export "m" (core module (export "f" (global i32))))
123    ))
124  )
125  "expected global found func")
126(assert_unlinkable
127  (component
128    (import "foo" (instance
129      (export "m" (core module (export "t" (func))))
130    ))
131  )
132  "expected func found table")
133(assert_unlinkable
134  (component
135    (import "foo" (instance
136      (export "m" (core module (export "m" (func))))
137    ))
138  )
139  "expected func found memory")
140(assert_unlinkable
141  (component
142    (import "foo" (instance
143      (export "m" (core module (export "g" (func))))
144    ))
145  )
146  "expected func found global")
147
148;; wrong item type
149(assert_unlinkable
150  (component
151    (import "foo" (instance
152      (export "m" (core module (export "f" (func (param i32)))))
153    ))
154  )
155  "export `f` has the wrong type")
156(assert_unlinkable
157  (component
158    (import "foo" (instance
159      (export "m" (core module (export "t" (table 1 externref))))
160    ))
161  )
162  "export `t` has the wrong type")
163(assert_unlinkable
164  (component
165    (import "foo" (instance
166      (export "m" (core module (export "t" (table 2 funcref))))
167    ))
168  )
169  "export `t` has the wrong type")
170(assert_unlinkable
171  (component
172    (import "foo" (instance
173      (export "m" (core module (export "m" (memory 2))))
174    ))
175  )
176  "export `m` has the wrong type")
177(assert_unlinkable
178  (component
179    (import "foo" (instance
180      (export "m" (core module (export "g" (global f32))))
181    ))
182  )
183  "export `g` has the wrong type")
184(assert_unlinkable
185  (component
186    (import "foo" (instance
187      (export "m" (core module (export "g" (global (mut i32)))))
188    ))
189  )
190  "export `g` has the wrong type")
191
192;; subtyping ok
193(component
194  (import "foo" (instance
195    (export "m" (core module
196      (export "t" (table 0 funcref))
197      (export "m" (memory 0))
198    ))
199  ))
200)
201
202(component $foo
203  (core module (export "f") (func (import "" "")))
204  (core module (export "t") (table (import "" "") 1 funcref))
205  (core module (export "m") (memory (import "" "") 1))
206  (core module (export "g") (global (import "" "") i32))
207)
208
209;; wrong class of item
210(assert_unlinkable
211  (component
212    (import "foo" (instance
213      (export "f" (core module (import "" "" (global i32))))
214    ))
215  )
216  "expected func found global")
217(assert_unlinkable
218  (component
219    (import "foo" (instance
220      (export "t" (core module (import "" "" (func))))
221    ))
222  )
223  "expected table found func")
224(assert_unlinkable
225  (component
226    (import "foo" (instance
227      (export "m" (core module (import "" "" (func))))
228    ))
229  )
230  "expected memory found func")
231(assert_unlinkable
232  (component
233    (import "foo" (instance
234      (export "g" (core module (import "" "" (func))))
235    ))
236  )
237  "expected global found func")
238
239;; wrong item type
240(assert_unlinkable
241  (component
242    (import "foo" (instance
243      (export "f" (core module (import "" "" (func (param i32)))))
244    ))
245  )
246  "module import `::` has the wrong type")
247(assert_unlinkable
248  (component
249    (import "foo" (instance
250      (export "t" (core module (import "" "" (table 1 externref))))
251    ))
252  )
253  "module import `::` has the wrong type")
254(assert_unlinkable
255  (component
256    (import "foo" (instance
257      (export "t" (core module (import "" "" (table 0 funcref))))
258    ))
259  )
260  "module import `::` has the wrong type")
261(assert_unlinkable
262  (component
263    (import "foo" (instance
264      (export "m" (core module (import "" "" (memory 0))))
265    ))
266  )
267  "module import `::` has the wrong type")
268(assert_unlinkable
269  (component
270    (import "foo" (instance
271      (export "g" (core module (import "" "" (global f32))))
272    ))
273  )
274  "module import `::` has the wrong type")
275(assert_unlinkable
276  (component
277    (import "foo" (instance
278      (export "g" (core module (import "" "" (global (mut i32)))))
279    ))
280  )
281  "module import `::` has the wrong type")
282
283;; subtyping ok, but in the opposite direction of imports
284(component
285  (import "foo" (instance
286    (export "t" (core module (import "" "" (table 2 funcref))))
287    (export "m" (core module (import "" "" (memory 2))))
288  ))
289)
290
291;; An instance can reexport a module, define a module, and everything can be
292;; used by something else
293(component $src
294  (core module (export "m")
295    (global (export "g") i32 i32.const 2)
296  )
297)
298
299(component $reexport
300  (core module $m1
301    (global (export "g") i32 i32.const 1)
302  )
303  (import "src" (instance $src
304    (export "m" (core module (export "g" (global i32))))
305  ))
306
307  (core module $m3
308    (global (export "g") i32 i32.const 3)
309  )
310
311  (export "m1" (core module $m1))
312  (export "m2" (core module $src "m"))
313  (export "m3" (core module $m3))
314)
315
316(component
317  (core type $modulety (module (export "g" (global i32))))
318  (import "reexport" (instance $reexport
319    (export "m1" (core module (type $modulety)))
320    (export "m2" (core module (type $modulety)))
321    (export "m3" (core module (type $modulety)))
322  ))
323
324  (core module $assert_ok
325    (import "m1" "g" (global $m1 i32))
326    (import "m2" "g" (global $m2 i32))
327    (import "m3" "g" (global $m3 i32))
328
329    (func $assert_ok
330      block
331        global.get $m1
332        i32.const 1
333        i32.eq
334        br_if 0
335        unreachable
336      end
337      block
338        global.get $m2
339        i32.const 2
340        i32.eq
341        br_if 0
342        unreachable
343      end
344      block
345        global.get $m3
346        i32.const 3
347        i32.eq
348        br_if 0
349        unreachable
350      end
351    )
352
353    (start $assert_ok)
354  )
355
356  (core instance $m1 (instantiate (module $reexport "m1")))
357  (core instance $m2 (instantiate (module $reexport "m2")))
358  (core instance $m3 (instantiate (module $reexport "m3")))
359
360  (core instance (instantiate $assert_ok
361    (with "m1" (instance $m1))
362    (with "m2" (instance $m2))
363    (with "m3" (instance $m3))
364  ))
365)
366
367;; order of imports and exports can be shuffled between definition site and
368;; use-site
369(component $provider
370  (core module (export "m")
371    (import "" "1" (global $i1 i32))
372    (import "" "2" (global $i2 i32))
373    (import "" "3" (global $i3 i32))
374    (import "" "4" (global $i4 i32))
375
376    (global $g1 i32 i32.const 100)
377    (global $g2 i32 i32.const 101)
378    (global $g3 i32 i32.const 102)
379    (global $g4 i32 i32.const 103)
380
381    (func $assert_imports
382      (block
383        global.get $i1
384        i32.const 1
385        i32.eq
386        br_if 0
387        unreachable)
388      (block
389        global.get $i2
390        i32.const 2
391        i32.eq
392        br_if 0
393        unreachable)
394      (block
395        global.get $i3
396        i32.const 3
397        i32.eq
398        br_if 0
399        unreachable)
400      (block
401        global.get $i4
402        i32.const 4
403        i32.eq
404        br_if 0
405        unreachable)
406    )
407
408    (start $assert_imports)
409
410    (export "g1" (global $g1))
411    (export "g2" (global $g2))
412    (export "g3" (global $g3))
413    (export "g4" (global $g4))
414  )
415)
416
417(component
418  (import "provider" (instance $provider
419    (export "m" (core module
420      (import "" "4" (global i32))
421      (import "" "3" (global i32))
422      (import "" "2" (global i32))
423      (import "" "1" (global i32))
424
425      (export "g4" (global i32))
426      (export "g3" (global i32))
427      (export "g2" (global i32))
428      (export "g1" (global i32))
429    ))
430  ))
431
432  (core module $imports
433    (global (export "1") i32 (i32.const 1))
434    (global (export "3") i32 (i32.const 3))
435    (global (export "2") i32 (i32.const 2))
436    (global (export "4") i32 (i32.const 4))
437  )
438  (core instance $imports (instantiate $imports))
439  (core instance $m (instantiate (module $provider "m")
440    (with "" (instance $imports))
441  ))
442
443  (core module $import_globals
444    (import "" "g4" (global $g4 i32))
445    (import "" "g3" (global $g3 i32))
446    (import "" "g2" (global $g2 i32))
447    (import "" "g1" (global $g1 i32))
448
449    (func $assert_imports
450      (block
451        global.get $g1
452        i32.const 100
453        i32.eq
454        br_if 0
455        unreachable)
456      (block
457        global.get $g2
458        i32.const 101
459        i32.eq
460        br_if 0
461        unreachable)
462      (block
463        global.get $g3
464        i32.const 102
465        i32.eq
466        br_if 0
467        unreachable)
468      (block
469        global.get $g4
470        i32.const 103
471        i32.eq
472        br_if 0
473        unreachable)
474    )
475
476    (start $assert_imports)
477  )
478
479  (core instance (instantiate $import_globals (with "" (instance $m))))
480)
481
482(component $tag-module
483  (core module (export "m")
484        (tag (export "t") (param i32))))
485(component
486  (import "tag-module" (instance
487                         (export "m"
488                                 (core module
489                                       (export "t" (tag (param i32))))))))
490