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