Debian Codegraph @

tests/cli/component-model/type-export-restrictions.wast

raw
1 ;; RUN: wast --assert default --snapshot tests/snapshots %
2
3 ;; Test that unnamed types in various types are all detected
4
5 (assert_invalid
6 (component
7 (type $t (record (field "f" u32)))
8 (type $f (record (field "f" $t)))
9 (export "f" (type $f))
10 )
11 "type not valid to be used as export")
12
13 (assert_invalid
14 (component
15 (type $t (record (field "f" u32)))
16 (type $f (list $t))
17 (export "f" (type $f))
18 )
19 "type not valid to be used as export")
20
21 (assert_invalid
22 (component
23 (type $t (record (field "f" u32)))
24 (type $f (tuple $t))
25 (export "f" (type $f))
26 )
27 "type not valid to be used as export")
28
29 (assert_invalid
30 (component
31 (type $t (record (field "f" u32)))
32 (type $f (variant (case "c" $t)))
33 (export "f" (type $f))
34 )
35 "type not valid to be used as export")
36
37 (assert_invalid
38 (component
39 (type $t (record (field "f" u32)))
40 (type $f (option $t))
41 (export "f" (type $f))
42 )
43 "type not valid to be used as export")
44
45 (assert_invalid
46 (component
47 (type $t (record (field "f" u32)))
48 (type $f (result $t))
49 (export "f" (type $f))
50 )
51 "type not valid to be used as export")
52
53 ;; Test that various types are all flagged as "requires a name"
54
55 (assert_invalid
56 (component
57 (type $t (record (field "f" u32)))
58 (type $f (list $t))
59 (export "f" (type $f))
60 )
61 "type not valid to be used as export")
62
63 (assert_invalid
64 (component
65 (type $t (enum "a"))
66 (type $f (list $t))
67 (export "f" (type $f))
68 )
69 "type not valid to be used as export")
70
71 (assert_invalid
72 (component
73 (type $t (flags "a"))
74 (type $f (list $t))
75 (export "f" (type $f))
76 )
77 "type not valid to be used as export")
78
79 (assert_invalid
80 (component
81 (type $t (variant (case "a")))
82 (type $f (list $t))
83 (export "f" (type $f))
84 )
85 "type not valid to be used as export")
86
87 (assert_invalid
88 (component
89 (type $t (resource (rep i32)))
90 (type $f (list (own $t)))
91 (export "f" (type $f))
92 )
93 "type not valid to be used as export")
94
95 ;; Some types don't need names
96 (component
97 (type $t1 (tuple (tuple u32)))
98 (export "t1" (type $t1))
99
100 (type $t2 (option (tuple (list u8) (result (list u32) (error (option string))))))
101 (export "t2" (type $t2))
102
103 (type $t3 u32)
104 (export "t3" (type $t3))
105 )
106
107 (component
108 (type $t' (record (field "f" u32)))
109 (export $t "t" (type $t'))
110 (type $t2 (record (field "x" $t)))
111 (export "t2" (type $t2))
112 )
113
114 ;; imports are validated as well
115 (assert_invalid
116 (component
117 (type $t (record (field "f" u32)))
118 (type $t2 (record (field "f" $t)))
119 (import "x" (type (eq $t2)))
120 )
121 "type not valid to be used as import")
122 (component
123 (type $t (record (field "f" u32)))
124 (import "t" (type $t' (eq $t)))
125 (type $t2 (record (field "f" $t')))
126 (import "x" (type (eq $t2)))
127 )
128 (assert_invalid
129 (component
130 (type $t (record (field "f" u32)))
131 (type $t2 (record (field "f" $t)))
132 (import "x" (func (param "x" $t2)))
133 )
134 "func not valid to be used as import")
135
136 (assert_invalid
137 (component
138 (type $t (resource (rep i32)))
139 (export "t" (type $t))
140 (type $f (list (own $t)))
141 (export "f" (type $f))
142 )
143 "type not valid to be used as export")
144
145 ;; validate within the type context
146 (assert_invalid
147 (component
148 (type (component
149 (type $t (record (field "f" u32)))
150 (export "f" (func (param "x" $t)))
151 ))
152 )
153 "func not valid to be used as export")
154 (assert_invalid
155 (component
156 (type (component
157 (type $t (record (field "f" u32)))
158 (type $f (record (field "t" $t)))
159 (export "f" (type (eq $f)))
160 ))
161 )
162 "type not valid to be used as export")
163
164 ;; instances of unexported types is ok
165 (component
166 (type $t (record (field "f" u32)))
167 (type $f (record (field "t" $t)))
168 (instance
169 (export "f" (type $f))
170 )
171 )
172 ;; .. but exporting them is not
173 (assert_invalid
174 (component
175 (type $t (record (field "f" u32)))
176 (type $f (record (field "t" $t)))
177 (instance $i
178 (export "f" (type $f))
179 )
180 (export "i" (instance $i))
181 )
182 "instance not valid to be used as export")
183
184 ;; Can't export a lifted function with unexported types
185 (assert_invalid
186 (component
187 (type $t (record (field "f" u32)))
188 (type $f (record (field "t" $t)))
189
190 (core module $m (func $f (export "f") (param i32)))
191 (core instance $i (instantiate $m))
192 (func $f (param "f" $f) (canon lift (core func $i "f")))
193 (export "f" (func $f))
194 )
195 "func not valid to be used as export")
196
197 ;; Unexported instances don't work
198 (assert_invalid
199 (component
200 (type $t' (record (field "f" u32)))
201 (instance $i
202 (export "t" (type $t'))
203 )
204 (alias export $i "t" (type $t))
205
206 (core module $m (func $f (export "f") (param i32)))
207 (core instance $i (instantiate $m))
208 (func $f (param "f" $t) (canon lift (core func $i "f")))
209 (export "f" (func $f))
210 )
211 "func not valid to be used as export")
212
213 ;; Even through a component it doesn't work
214 (assert_invalid
215 (component
216 (component $C
217 (type $t (record (field "f" u32)))
218 (export "t" (type $t))
219 )
220 (instance $i (instantiate $C))
221 (alias export $i "t" (type $t))
222
223 (core module $m (func $f (export "f") (param i32)))
224 (core instance $i (instantiate $m))
225 (func $f (param "f" $t) (canon lift (core func $i "f")))
226 (export "f" (func $f))
227 )
228 "func not valid to be used as export")
229
230 ;; through exported instances is ok though
231 (component
232 (type $t' (record (field "f" u32)))
233 (instance $i'
234 (export "t" (type $t'))
235 )
236 (export $i "i" (instance $i'))
237 (alias export $i "t" (type $t))
238
239 (core module $m (func $f (export "f") (param i32)))
240 (core instance $i (instantiate $m))
241 (func $f (param "f" $t) (canon lift (core func $i "f")))
242 (export "f" (func $f))
243 )
244 (component
245 (component $C
246 (type $t (record (field "f" u32)))
247 (export "t" (type $t))
248 )
249 (instance $i' (instantiate $C))
250 (export $i "i" (instance $i'))
251 (alias export $i "t" (type $t))
252
253 (core module $m (func $f (export "f") (param i32)))
254 (core instance $i (instantiate $m))
255 (func $f (param "f" $t) (canon lift (core func $i "f")))
256 (export "f" (func $f))
257 )
258
259 ;; a type-ascribed export which is otherwise invalid can become valid
260 (component
261 (type $t (record (field "f" u32)))
262
263 (core module $m (func (export "f") (param i32)))
264 (core instance $i (instantiate $m))
265 (func $f (param "x" $t) (canon lift (core func $i "f")))
266
267 (export $t' "t" (type $t))
268 (export "f" (func $f) (func (param "x" $t')))
269 )
270
271 ;; imports can't reference exports
272 (assert_invalid
273 (component
274 (type $t1 (record (field "f" u32)))
275 (export $t2 "t1" (type $t1))
276 (import "i" (func (result $t2)))
277 )
278 "func not valid to be used as import")
279
280 ;; exports can reference imports
281 (component
282 (type $t1 (record (field "f" u32)))
283 (import "t1" (type $t2 (eq $t1)))
284 (export "e-t1" (type $t2))
285 )
286 (component
287 (type $t1 (record (field "f" u32)))
288 (import "t1" (type $t2 (eq $t1)))
289 (import "i" (func $f (result $t2)))
290
291 (export "e-i" (func $f))
292 )
293
294 ;; outer aliases don't work for imports/exports
295 (assert_invalid
296 (component
297 (type $t1 (record (field "f" u32)))
298 (import "t1" (type $t2 (eq $t1)))
299 (component
300 (import "i" (func $f (result $t2)))
301 )
302 )
303 "func not valid to be used as import")
304 (assert_invalid
305 (component
306 (type $t1 (record (field "f" u32)))
307 (export $t2 "t1" (type $t1))
308 (component
309 (core module $m (func (export "f") (result i32) unreachable))
310 (core instance $i (instantiate $m))
311 (func $f (export "i") (result $t2) (canon lift (core func $i "f")))
312 )
313 )
314 "func not valid to be used as export")
315
316 ;; outer aliases work for components, modules, and resources
317 (component
318 (type $c (component))
319 (type (component
320 (import "c" (component (type $c)))
321 ))
322 (component
323 (import "c" (component (type $c)))
324 )
325 (type $i (instance))
326 (type (component
327 (import "c" (instance (type $i)))
328 ))
329
330 (type $r (resource (rep i32)))
331 (type (component
332 (import "r" (type (eq $r)))
333 ))
334 )
335
336 ;; reexport of an import is fine
337 (component
338 (import "r" (func $r))
339 (export "r2" (func $r))
340 )
341 (component
342 (type $t (record (field "f" u32)))
343 (import "r" (type $r (eq $t)))
344 (export "r2" (type $r))
345 )
346 (component
347 (import "r" (instance $r))
348 (export "r2" (instance $r))
349 )
350 (component
351 (import "r" (type $r (sub resource)))
352 (export "r2" (type $r))
353 )
354
355 ;; bag of exports cannot be exported by carrying through context that's not
356 ;; otherwise exported
357 (assert_invalid
358 (component
359 (component $A
360 (type $t (record (field "f" u32)))
361 (export $t2 "t" (type $t))
362 (core module $m (func (export "f") (result i32) unreachable))
363 (core instance $i (instantiate $m))
364 (func $f (result $t2) (canon lift (core func $i "f")))
365
366 (instance (export "i")
367 (export "f" (func $f))
368 )
369 )
370
371 (instance $a (instantiate $A))
372 ;; this component only exports `f`, not the record type that is the result
373 ;; of `f`, so it should be invalid.
374 (export "a" (instance $a "i"))
375 )
376 "instance not valid to be used as export")
377
378 ;; instance types can be "temporarily invalid", but not if they're attached
379 ;; to a concrete component
380 (component
381 (type (instance
382 (type $t (record (field "f" u32)))
383 (export "f" (func (param "x" $t)))
384 ))
385 )
386 (assert_invalid
387 (component
388 (type $i (instance
389 (type $t (record (field "f" u32)))
390 (type $f (record (field "t" $t)))
391 (export "f" (type (eq $f)))
392 ))
393 (import "f" (instance (type $i)))
394 )
395 "instance not valid to be used as import")
396
397 ;; allow for one import to refer to another
398 (component $C
399 (import "foo" (instance $i
400 (type $baz' (record (field "f" u32)))
401 (export "baz" (type $baz (eq $baz')))
402 (type $bar' (record (field "baz" $baz)))
403 (export "bar" (type $bar (eq $bar')))
404 ))
405 (alias export $i "bar" (type $bar))
406 (import "bar" (instance
407 (alias outer $C $bar (type $bar'))
408 (export "bar" (type $bar (eq $bar')))
409 (export "a" (func $f (result $bar)))
410 ))
411 )
412
413 ;; allow for one import to refer to another
414 (component
415 (type $r' (record (field "f" u32)))
416 (import "r" (type $r (eq $r')))
417 (component $C
418 (type $r' (record (field "f" u32)))
419 (import "r" (type $r (eq $r')))
420 (type $r2' (record (field "r" $r)))
421 (export "r2" (type $r2'))
422 )
423 (instance $c (instantiate $C (with "r" (type $r))))
424 (export "r2" (type $c "r2"))
425 )
426
427 ;; types are validated when they are exported
428 (assert_invalid
429 (component
430 (type $i (instance
431 (type $t (record (field "f" u32)))
432 (type $f (record (field "t" $t)))
433 (export "f" (type (eq $f)))
434 ))
435 (import "f" (type (eq $i)))
436 )
437 "type not valid to be used as import")
438 (assert_invalid
439 (component
440 (type $i (instance
441 (type $t (record (field "f" u32)))
442 (type $f (record (field "t" $t)))
443 (export "f" (type (eq $f)))
444 ))
445 (export "f" (type $i))
446 )
447 "type not valid to be used as export")
448
449 (assert_invalid
450 (component
451 (type $t (record (field "f" u32)))
452 (type $f (func (result $t)))
453 (import "f" (type (eq $f)))
454 )
455 "type not valid to be used as import")
456 (assert_invalid
457 (component
458 (type $t (record (field "f" u32)))
459 (type $f (func (result $t)))
460 (export "f" (type $f))
461 )
462 "type not valid to be used as export")
463
464 (component
465 (type (;0;)
466 (instance
467 (type (;0;) (enum "qux"))
468 (export (;1;) "baz" (type (eq 0)))
469 (type (;2;) (record (field "bar" 1) ))
470 (export (;3;) "foo" (type (eq 2)))
471 )
472 )
473 (import (interface "demo:component/types") (instance (;0;) (type 0)))
474 (component
475 (type (;0;)
476 (instance
477 (type (;0;) (enum "qux"))
478 (export (;1;) "baz" (type (eq 0)))
479 (type (;2;) (record (field "bar" 1) ))
480 (export (;3;) "foo" (type (eq 2)))
481 )
482 )
483 (import (interface "demo:component/types") (instance (;0;) (type 0)))
484 (component (;0;)
485 (type (;0;) (enum "qux"))
486 (import "import-type-baz" (type (;1;) (eq 0)))
487 (type (;2;) (record (field "bar" 1) ))
488 (import "import-type-bar" (type (;3;) (eq 2)))
489 (export (;4;) "foo" (type 3))
490 )
491 (instance (;1;) (instantiate 0
492 (with "import-type-baz" (type 0 "baz"))
493 (with "import-type-bar" (type 0 "foo"))
494 )
495 )
496 (export (;0;) (interface "demo:component/types") (instance 1))
497 )
498 (instance (instantiate 0 (with "demo:component/types" (instance 0))))
499 (export (interface "demo:component/types") (instance 1 "demo:component/types"))
500 )