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