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 "" (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    (export "nested" (instance
185      (export "return-four" (func (result u32)))
186    ))
187  ))
188
189  (core module $m
190    (import "host" "return-three" (func $three (result i32)))
191    (func $start
192      call $three
193      i32.const 4
194      i32.ne
195      if unreachable end
196    )
197    (start $start)
198  )
199  (core func $three_lower
200    (canon lower (func $i "nested" "return-four"))
201  )
202  (core instance (instantiate $m
203    (with "host" (instance (export "return-three" (func $three_lower))))
204  ))
205)
206
207(component
208  (import "host" (instance $i
209    (export "simple-module" (core module))
210  ))
211
212  (core instance (instantiate (module $i "simple-module")))
213)
214
215(component
216  (import "host" (instance $i
217    (export "simple-module" (core module
218      (export "f" (func (result i32)))
219      (export "g" (global i32))
220    ))
221  ))
222
223  (core instance $i (instantiate (module $i "simple-module")))
224  (core module $verify
225    (import "host" "f" (func $f (result i32)))
226    (import "host" "g" (global $g i32))
227
228    (func $start
229      call $f
230      i32.const 101
231      i32.ne
232      if unreachable end
233
234      global.get $g
235      i32.const 100
236      i32.ne
237      if unreachable end
238    )
239    (start $start)
240  )
241
242  (core instance (instantiate $verify (with "host" (instance $i))))
243)
244
245;; export an instance
246(component
247  (core module $m)
248  (instance $i (export "m" (core module $m)))
249  (export "i" (instance $i))
250)
251(component
252  (component $c)
253  (instance $i (instantiate $c))
254  (export "i" (instance $i))
255)
256(component
257  (import "host" (instance $i))
258  (export "i" (instance $i))
259)
260
261