Debian Codegraph @

tests/cli/component-model/resources.wast

raw
1 ;; RUN: wast --assert default --snapshot tests/snapshots %
2
3 (component
4 (type $x (resource (rep i32)))
5 )
6
7 (component
8 (type $x (resource (rep i32)))
9
10 (core func (canon resource.new $x))
11 (core func (canon resource.rep $x))
12 (core func (canon resource.drop $x))
13 )
14
15 (component
16 (import "x" (type $x (sub resource)))
17
18 (core func (canon resource.drop $x))
19 )
20
21 (component
22 (core module $m
23 (func (export "dtor") (param i32))
24 )
25 (core instance $m (instantiate $m))
26 (type $x (resource (rep i32) (dtor (func $m "dtor"))))
27 (core func (canon resource.new $x))
28 )
29
30 (component
31 (type $x (resource (rep i32)))
32 (core func $f1 (canon resource.new $x))
33 (core func $f2 (canon resource.rep $x))
34 (core func $f3 (canon resource.drop $x))
35
36 (core module $m
37 (import "" "f1" (func (param i32) (result i32)))
38 (import "" "f2" (func (param i32) (result i32)))
39 (import "" "f3" (func (param i32)))
40 )
41
42 (core instance (instantiate $m
43 (with "" (instance
44 (export "f1" (func $f1))
45 (export "f2" (func $f2))
46 (export "f3" (func $f3))
47 ))
48 ))
49 )
50
51 (assert_invalid
52 (component
53 (type $x (resource (rep i64)))
54 )
55 "resources can only be represented by `i32`")
56
57 (assert_invalid
58 (component
59 (type $x (own 100))
60 )
61 "type index out of bounds")
62
63 (assert_invalid
64 (component
65 (type $x (borrow 100))
66 )
67 "type index out of bounds")
68
69 (assert_invalid
70 (component
71 (type $t u8)
72 (type $x (borrow $t))
73 )
74 "not a resource type")
75
76 (assert_invalid
77 (component
78 (type $t u8)
79 (type $x (own $t))
80 )
81 "not a resource type")
82
83 (assert_invalid
84 (component
85 (import "x" (type $x (sub resource)))
86 (core func (canon resource.new $x))
87 )
88 "not a local resource")
89
90 (assert_invalid
91 (component
92 (import "x" (type $x (sub resource)))
93 (core func (canon resource.rep $x))
94 )
95 "not a local resource")
96
97 (assert_invalid
98 (component
99 (type $t (tuple u32))
100 (core func (canon resource.drop $t))
101 )
102 "not a resource type")
103
104 (assert_invalid
105 (component
106 (core func (canon resource.drop 100))
107 )
108 "type index out of bounds")
109
110 (assert_invalid
111 (component
112 (type (component))
113 (core func (canon resource.drop 0))
114 )
115 "not a resource type")
116
117 (assert_invalid
118 (component
119 (type (component))
120 (core func (canon resource.new 0))
121 )
122 "not a resource type")
123
124 (assert_invalid
125 (component
126 (core module $m
127 (func (export "dtor"))
128 )
129 (core instance $m (instantiate $m))
130 (type $x (resource (rep i32) (dtor (func $m "dtor"))))
131 (core func (canon resource.new $x))
132 )
133 "wrong signature for a destructor")
134
135 (assert_invalid
136 (component
137 (type (resource (rep i32) (dtor (func 100))))
138 )
139 "function index out of bounds")
140
141 (assert_invalid
142 (component
143 (import "x" (type $x (sub resource)))
144 (import "y" (type $y (sub resource)))
145 (import "z" (func $z (param "x" (own $x)) (param "y" (own $y))))
146
147 (component $c
148 (import "x" (type $x (sub resource)))
149 (import "z" (func (param "x" (own $x)) (param "y" (own $x))))
150 )
151
152 (instance (instantiate $c (with "x" (type $x)) (with "z" (func $z))))
153 )
154 "resource types are not the same")
155
156 (component
157 (type (component
158 (import "x" (type $x (sub resource)))
159 (export "y" (type (eq $x)))
160 (export "z" (type (sub resource)))
161 ))
162 )
163
164 (assert_invalid
165 (component
166 (type (component
167 (type $x (resource (rep i32)))
168 ))
169 )
170 "resources can only be defined within a concrete component")
171
172 (assert_invalid
173 (component
174 (type (instance
175 (type $x (resource (rep i32)))
176 ))
177 )
178 "resources can only be defined within a concrete component")
179
180 (component
181 (type (component
182 (import "x" (instance $i
183 (export "t" (type $t (sub resource)))
184 (export "f" (func (result (own $t))))
185 ))
186 (alias export $i "t" (type $t))
187 (export "f" (func (result (own $t))))
188 ))
189 )
190
191 (component
192 (import "fancy-fs" (instance $fancy-fs
193 (export "fs" (instance $fs
194 (export "file" (type (sub resource)))
195 ))
196 (alias export $fs "file" (type $file))
197 (export "fancy-op" (func (param "f" (borrow $file))))
198 ))
199 )
200
201 (component $C
202 (type $T (list (tuple string bool)))
203 (type $U (option $T))
204 (type $G (func (param "x" (list $T)) (result $U)))
205 (type $D (component
206 (alias outer $C $T (type $C_T))
207 (type $L (list $C_T))
208 (import "f" (func (param "x" $L) (result (list u8))))
209 (import "g" (func (type $G)))
210 (export "g2" (func (type $G)))
211 (export "h" (func (result $U)))
212 (import "T" (type $T (sub resource)))
213 (import "i" (func (param "x" (list (own $T)))))
214 (export "T2" (type $T' (eq $T)))
215 (export "U" (type $U' (sub resource)))
216 (export "j" (func (param "x" (borrow $T')) (result (own $U'))))
217 ))
218 )
219
220 (component
221 (import "T1" (type $T1 (sub resource)))
222 (import "T2" (type $T2 (sub resource)))
223 )
224
225 (component $C
226 (import "T1" (type $T1 (sub resource)))
227 (import "T2" (type $T2 (sub resource)))
228 (import "T3" (type $T3 (eq $T2)))
229 (type $ListT1 (list (own $T1)))
230 (type $ListT2 (list (own $T2)))
231 (type $ListT3 (list (own $T3)))
232 )
233
234 (component
235 (import "T" (type $T (sub resource)))
236 (import "U" (type $U (sub resource)))
237 (type $Own1 (own $T))
238 (type $Own2 (own $T))
239 (type $Own3 (own $U))
240 (type $ListOwn1 (list $Own1))
241 (type $ListOwn2 (list $Own2))
242 (type $ListOwn3 (list $Own3))
243 (type $Borrow1 (borrow $T))
244 (type $Borrow2 (borrow $T))
245 (type $Borrow3 (borrow $U))
246 (type $ListBorrow1 (list $Borrow1))
247 (type $ListBorrow2 (list $Borrow2))
248 (type $ListBorrow3 (list $Borrow3))
249 )
250
251 (component
252 (import "C" (component $C
253 (export "T1" (type (sub resource)))
254 (export "T2" (type $T2 (sub resource)))
255 (export "T3" (type (eq $T2)))
256 ))
257 (instance $c (instantiate $C))
258 (alias export $c "T1" (type $T1))
259 (alias export $c "T2" (type $T2))
260 (alias export $c "T3" (type $T3))
261 )
262
263 (component
264 (component $C
265 (type $r1 (export "r1") (resource (rep i32)))
266 (type $r2 (export "r2") (resource (rep i32)))
267 )
268 (instance $c1 (instantiate $C))
269 (instance $c2 (instantiate $C))
270 (alias export $c1 "r1" (type $c1r1))
271 (alias export $c1 "r2" (type $c1r2))
272 (alias export $c2 "r1" (type $c2r1))
273 (alias export $c2 "r2" (type $c2r2))
274 )
275
276 (component
277 (type $r (resource (rep i32)))
278 (export "r1" (type $r))
279 (export "r2" (type $r))
280 )
281
282 (component
283 (type (component
284 (export "r1" (type (sub resource)))
285 (export "r2" (type (sub resource)))
286 ))
287 )
288
289 (component
290 (type $r (resource (rep i32)))
291 (export $r1 "r1" (type $r))
292 (export "r2" (type $r1))
293 )
294
295 (component
296 (type (component
297 (export "r1" (type $r1 (sub resource)))
298 (export "r2" (type (eq $r1)))
299 ))
300 )
301
302 (component $P
303 (import "C1" (component $C1
304 (import "T" (type $T (sub resource)))
305 (export "foo" (func (param "t" (own $T))))
306 ))
307 (import "C2" (component $C2
308 (import "T" (type $T (sub resource)))
309 (import "foo" (func (param "t" (own $T))))
310 ))
311 (type $R (resource (rep i32)))
312 (instance $c1 (instantiate $C1 (with "T" (type $R))))
313 (instance $c2 (instantiate $C2
314 (with "T" (type $R))
315 (with "foo" (func $c1 "foo"))
316 ))
317 )
318
319 (component
320 (import "C1" (component $C1
321 (import "T1" (type $T1 (sub resource)))
322 (import "T2" (type $T2 (sub resource)))
323 (export "foo" (func (param "t" (tuple (own $T1) (own $T2)))))
324 ))
325 (import "C2" (component $C2
326 (import "T" (type $T (sub resource)))
327 (export "foo" (func (param "t" (tuple (own $T) (own $T)))))
328 ))
329 (type $R (resource (rep i32)))
330 (instance $c1 (instantiate $C1
331 (with "T1" (type $R))
332 (with "T2" (type $R))
333 ))
334 (instance $c2 (instantiate $C2
335 (with "T" (type $R))
336 (with "foo" (func $c1 "foo"))
337 ))
338 )
339
340 (assert_invalid
341 (component
342 (component $C
343 (type $R (resource (rep i32)))
344 (export "R" (type $R))
345 )
346 (instance $c (instantiate $C))
347 (alias export $c "R" (type $R))
348 (core func (canon resource.rep $R))
349 )
350 "not a local resource")
351
352 (component
353 (component $C
354 (type $R (resource (rep i32)))
355 (export "R" (type $R))
356 )
357 (instance $c (instantiate $C))
358 (alias export $c "R" (type $R))
359 (core func (canon resource.drop $R))
360 )
361
362 (component
363 (component $C1
364 (import "X" (type (sub resource)))
365 )
366 (component $C2
367 (import "C1" (component
368 (import "X" (type (sub resource)))
369 ))
370 )
371 (instance $c (instantiate $C2 (with "C1" (component $C1))))
372 )
373
374 (component
375 (component $C1
376 (import "X" (type $X (sub resource)))
377 (import "f" (func $f (result (own $X))))
378 (export "g" (func $f))
379 )
380 (component $C2
381 (import "C1" (component
382 (import "X" (type $X (sub resource)))
383 (import "f" (func (result (own $X))))
384 (export "g" (func (result (own $X))))
385 ))
386 )
387 (instance $c (instantiate $C2 (with "C1" (component $C1))))
388 )
389
390 (component
391 (component $C1
392 (type $X' (resource (rep i32)))
393 (export $X "X" (type $X'))
394
395 (core func $f (canon resource.drop $X))
396 (func (export "f") (param "X" (own $X)) (canon lift (core func $f)))
397 )
398 (instance $c1 (instantiate $C1))
399
400 (component $C2
401 (import "X" (type $X (sub resource)))
402 (import "f" (func (param "X" (own $X))))
403 )
404 (instance $c2 (instantiate $C2
405 (with "X" (type $c1 "X"))
406 (with "f" (func $c1 "f"))
407 ))
408 )
409
410 (assert_invalid
411 (component
412 (component $C1
413 (type $X' (resource (rep i32)))
414 (export $X "X" (type $X'))
415
416 (core func $f (canon resource.drop $X))
417 (func (export "f") (param "X" (own $X)) (canon lift (core func $f)))
418 )
419 (instance $c1 (instantiate $C1))
420 (instance $c2 (instantiate $C1))
421
422 (component $C2
423 (import "X" (type $X (sub resource)))
424 (import "f" (func (param "X" (own $X))))
425 )
426 (instance $c3 (instantiate $C2
427 (with "X" (type $c1 "X"))
428 (with "f" (func $c2 "f"))
429 ))
430 )
431 "resource types are not the same")
432
433 (component
434 (component $C1
435 (type $X (resource (rep i32)))
436 (export $X1 "X1" (type $X))
437 (export $X2 "X2" (type $X))
438
439 (core func $f (canon resource.drop $X))
440 (func (export "f1") (param "X" (own $X1)) (canon lift (core func $f)))
441 (func (export "f2") (param "X" (own $X2)) (canon lift (core func $f)))
442 )
443 (instance $c1 (instantiate $C1))
444
445 (component $C2
446 (import "X" (type $X (sub resource)))
447 (import "f" (func (param "X" (own $X))))
448 )
449 (instance $c2 (instantiate $C2
450 (with "X" (type $c1 "X1"))
451 (with "f" (func $c1 "f1"))
452 ))
453 (instance $c3 (instantiate $C2
454 (with "X" (type $c1 "X2"))
455 (with "f" (func $c1 "f2"))
456 ))
457 )
458
459 (component
460 (component $C1
461 (type $X (resource (rep i32)))
462 (export $X1 "X1" (type $X))
463 (export $X2 "X2" (type $X))
464
465 (core func $f (canon resource.drop $X))
466 (func (export "f1") (param "X" (own $X1)) (canon lift (core func $f)))
467 (func (export "f2") (param "X" (own $X2)) (canon lift (core func $f)))
468 )
469 (instance $c1 (instantiate $C1))
470
471 (component $C2
472 (import "X" (type $X (sub resource)))
473 (import "f" (func (param "X" (own $X))))
474 )
475 (instance $c2 (instantiate $C2
476 (with "X" (type $c1 "X1"))
477 (with "f" (func $c1 "f2"))
478 ))
479 (instance $c3 (instantiate $C2
480 (with "X" (type $c1 "X2"))
481 (with "f" (func $c1 "f1"))
482 ))
483 )
484
485 (assert_invalid
486 (component
487 (component $c
488 (import "x" (type (sub resource)))
489 )
490 (type $x u32)
491 (instance (instantiate $c (with "x" (type $x))))
492 )
493 "expected resource, found defined type")
494
495 (assert_invalid
496 (component
497 (component $c
498 (type $t u32)
499 (import "x" (type (eq $t)))
500 )
501 (type $x (resource (rep i32)))
502 (instance (instantiate $c (with "x" (type $x))))
503 )
504 "expected defined type, found resource")
505
506 (assert_invalid
507 (component
508 (component $c
509 (import "x1" (type $x1 (sub resource)))
510 (import "x2" (type $x2 (eq $x1)))
511 )
512 (type $x1 (resource (rep i32)))
513 (type $x2 (resource (rep i32)))
514 (instance (instantiate $c
515 (with "x1" (type $x1))
516 (with "x2" (type $x2))
517 ))
518 )
519 "resource types are not the same")
520
521 (component
522 (type $x (resource (rep i32)))
523 (component $c
524 (import "x" (type $t (sub resource)))
525 (export "y" (type $t))
526 )
527 (instance $c (instantiate $c (with "x" (type $x))))
528
529 (alias export $c "y" (type $x2))
530 (core func (canon resource.rep $x2))
531
532 )
533
534 (assert_invalid
535 (component
536 (type $r (resource (rep i32)))
537 (import "x" (func (result (own $r))))
538 )
539 "func not valid to be used as import")
540
541 (assert_invalid
542 (component
543 (type (component
544 (export "x" (type $x (sub resource)))
545 (import "f" (func (result (own $x))))
546 ))
547 )
548 "func not valid to be used as import")
549
550 (assert_invalid
551 (component
552 (type $r (resource (rep i32)))
553
554 (core func $f (canon resource.drop $r))
555 (func (export "f") (param "x" (own $r))
556 (canon lift (core func $f)))
557 )
558 "func not valid to be used as export")
559
560 ;; direct exports count as "explicit in" for resources
561 (component
562 (type $r' (resource (rep i32)))
563 (export $r "r" (type $r'))
564
565 (core func $f (canon resource.drop $r))
566 (func (export "f") (param "x" (own $r))
567 (canon lift (core func $f)))
568 )
569
570 ;; instances-as-a-bundle count as "explicit in" for resources
571 (component
572 (type $r' (resource (rep i32)))
573 (instance $i'
574 (export "r" (type $r'))
575 )
576 (export $i "i" (instance $i'))
577 (alias export $i "r" (type $r))
578
579 (core func $f (canon resource.drop $r))
580 (func (export "f") (param "x" (own $r))
581 (canon lift (core func $f)))
582 )
583
584 ;; Transitive bundles count for "explicit in"
585 (component
586 (type $r' (resource (rep i32)))
587 (instance $i'
588 (export "r" (type $r'))
589 )
590 (instance $i2'
591 (export "i" (instance $i'))
592 )
593 (export $i2 "i2" (instance $i2'))
594 (alias export $i2 "i" (instance $i))
595 (alias export $i "r" (type $r))
596
597 (core func $f (canon resource.drop $r))
598 (func (export "f") (param "x" (own $r))
599 (canon lift (core func $f)))
600 )
601
602 ;; Component instantiations count for "explicit in"
603 (component
604 (type $r' (resource (rep i32)))
605 (component $C
606 (import "x" (type $x (sub resource)))
607 (export "y" (type $x))
608 )
609 (instance $c' (instantiate $C (with "x" (type $r'))))
610 (export $c "c" (instance $c'))
611 (alias export $c "y" (type $r))
612
613 (core func $f (canon resource.drop $r))
614 (func (export "f") (param "x" (own $r))
615 (canon lift (core func $f)))
616 )
617
618 ;; Make sure threading things around is valid for "explicit in"
619 (component
620 (type $r' (resource (rep i32)))
621 (component $C
622 (import "x" (type $x (sub resource)))
623 (export "y" (type $x))
624 )
625 (instance $c (instantiate $C (with "x" (type $r'))))
626 (instance $i (export "x" (type $c "y")))
627
628 (component $C2
629 (import "x" (instance $i
630 (export "i1" (instance
631 (export "i2" (type (sub resource)))
632 ))
633 ))
634 (export "y" (type $i "i1" "i2"))
635 )
636
637 (instance $i2 (export "i2" (type $i "x")))
638 (instance $i1 (export "i1" (instance $i2)))
639 (instance $c2 (instantiate $C2
640 (with "x" (instance $i1))
641 ))
642 (export $r "x" (type $c2 "y"))
643
644 (core func $f (canon resource.drop $r))
645 (func (export "f") (param "x" (own $r))
646 (canon lift (core func $f)))
647 )
648
649 ;; Importing-and-exporting instances through instantiation counts for "explicit
650 ;; in"
651 (component
652 (type $r' (resource (rep i32)))
653 (component $C
654 (import "x" (instance $x (export "t" (type (sub resource)))))
655 (export "y" (instance $x))
656 )
657 (instance $c' (instantiate $C
658 (with "x" (instance
659 (export "t" (type $r'))
660 ))
661 ))
662 (export $c "c" (instance $c'))
663 (alias export $c "y" (instance $y))
664 (alias export $y "t" (type $r))
665
666 (core func $f (canon resource.drop $r))
667 (func (export "f") (param "x" (own $r))
668 (canon lift (core func $f)))
669 )
670
671 (component
672 (type $i (instance
673 (export "r" (type $r (sub resource)))
674 (export "f" (func (result (own $r))))
675 ))
676 (import "i1" (instance $i1 (type $i)))
677 (import "i2" (instance $i2 (type $i)))
678
679 (component $c
680 (import "r" (type $t (sub resource)))
681 (import "f" (func (result (own $t))))
682 )
683 (instance (instantiate $c
684 (with "r" (type $i1 "r"))
685 (with "f" (func $i1 "f"))
686 ))
687 (instance (instantiate $c
688 (with "r" (type $i2 "r"))
689 (with "f" (func $i2 "f"))
690 ))
691 )
692
693
694 (assert_invalid
695 (component
696 (type $i (instance
697 (export "r" (type $r (sub resource)))
698 (export "f" (func (result (own $r))))
699 ))
700 (import "i1" (instance $i1 (type $i)))
701 (import "i2" (instance $i2 (type $i)))
702
703 (component $c
704 (import "r" (type $t (sub resource)))
705 (import "f" (func (result (own $t))))
706 )
707 (instance (instantiate $c
708 (with "r" (type $i1 "r"))
709 (with "f" (func $i2 "f"))
710 ))
711 )
712 "resource types are not the same")
713
714 ;; substitution works
715 (component
716 (type $t (resource (rep i32)))
717 (component $c
718 (import "x" (type $t (sub resource)))
719 (export "y" (type $t))
720 )
721 (instance $c1 (instantiate $c (with "x" (type $t))))
722 (instance $c2 (instantiate $c (with "x" (type $t))))
723
724 (component $c2
725 (import "x1" (type $t (sub resource)))
726 (import "x2" (type (eq $t)))
727 (import "x3" (type (eq $t)))
728 )
729 (instance (instantiate $c2
730 (with "x1" (type $t))
731 (with "x2" (type $c1 "y"))
732 (with "x3" (type $c2 "y"))
733 ))
734 )
735
736 ;; must supply a resource to instantiation
737 (assert_invalid
738 (component
739 (component $c
740 (import "x" (type (sub resource)))
741 )
742 (instance (instantiate $c))
743 )
744 "missing import named `x`")
745 (assert_invalid
746 (component
747 (type $x (resource (rep i32)))
748 (component $c
749 (import "x" (type (sub resource)))
750 (import "y" (type (sub resource)))
751 )
752 (instance (instantiate $c (with "x" (type $x))))
753 )
754 "missing import named `y`")
755
756 ;; supply the wrong resource
757 (assert_invalid
758 (component
759 (type $x (resource (rep i32)))
760 (type $y (resource (rep i32)))
761 (component $c
762 (import "x" (type $t (sub resource)))
763 (import "y" (type (eq $t)))
764 )
765 (instance (instantiate $c
766 (with "x" (type $x))
767 (with "y" (type $y))
768 ))
769 )
770 "resource types are not the same")
771
772 ;; aliasing outer resources is ok
773 (component $A
774 (type $C (component
775 (import "x" (type $x (sub resource)))
776
777 (type $y (component
778 (alias outer $C $x (type $my-x))
779 (import "x" (type (eq $my-x)))
780 ))
781
782 (import "y" (component (type $y)))
783 (export "z" (component (type $y)))
784 ))
785
786 (type $t (resource (rep i32)))
787
788 (alias outer $A $t (type $other-t))
789
790 (type (instance (export "t" (type (eq $t)))))
791 (type (component (export "t" (type (eq $t)))))
792 (type (component (import "t" (type (eq $t)))))
793 )
794
795 ;; aliasing beyond components, however, is not ok
796 (assert_invalid
797 (component $A
798 (type $t (resource (rep i32)))
799 (component (alias outer $A $t (type $foo)))
800 )
801 "refers to resources not defined in the current component")
802 (assert_invalid
803 (component $A
804 (type $t (resource (rep i32)))
805 (type $u (record (field "x" (own $t))))
806 (component (alias outer $A $u (type $foo)))
807 )
808 "refers to resources not defined in the current component")
809 (assert_invalid
810 (component $A
811 (type $t (resource (rep i32)))
812 (type $u (borrow $t))
813 (component (alias outer $A $u (type $foo)))
814 )
815 "refers to resources not defined in the current component")
816 (assert_invalid
817 (component $A
818 (type $t (resource (rep i32)))
819 (type $u (component (export "a" (type (eq $t)))))
820 (component (alias outer $A $u (type $foo)))
821 )
822 "refers to resources not defined in the current component")
823 (assert_invalid
824 (component $A
825 (type $t (resource (rep i32)))
826 (type $u (component (import "a" (type (eq $t)))))
827 (component (alias outer $A $u (type $foo)))
828 )
829 "refers to resources not defined in the current component")
830
831 (assert_invalid
832 (component
833 (component $X
834 (type $t (resource (rep i32)))
835 (export "t" (type $t))
836 )
837 (component $F
838 (import "x" (component (export "t" (type $t (sub resource)))))
839 )
840 (instance $x1 (instantiate $X))
841 (instance $f1 (instantiate $F (with "x" (instance $x1))))
842 )
843 "expected component, found instance")
844
845 ;; Show that two instantiations of the same component produce unique exported
846 ;; resource types.
847 (assert_invalid
848 (component
849 (component $F
850 (type $t1 (resource (rep i32)))
851 (export "t1" (type $t1))
852 )
853 (instance $f1 (instantiate $F))
854 (instance $f2 (instantiate $F))
855 (alias export $f1 "t1" (type $t1))
856 (alias export $f2 "t1" (type $t2))
857 (component $T
858 (import "x" (type $x (sub resource)))
859 (import "y" (type (eq $x)))
860 )
861 (instance $test
862 (instantiate $T (with "x" (type $t1)) (with "y" (type $t2))))
863 )
864 "type mismatch for import `y`")
865
866 ;; Show that re-exporting imported resources from an imported component doesn't
867 ;; change the identity of that resource.
868 (component
869 (component $X
870 (type $t (resource (rep i32)))
871 (export "t" (type $t))
872 )
873 (component $F
874 (import "x" (instance $i (export "t" (type $t (sub resource)))))
875 (alias export $i "t" (type $t))
876 (export "t" (type $t))
877 )
878 (instance $x1 (instantiate $X))
879 (instance $f1 (instantiate $F (with "x" (instance $x1))))
880 (instance $f2 (instantiate $F (with "x" (instance $x1))))
881 (alias export $f1 "t" (type $t1))
882 (alias export $f2 "t" (type $t2))
883 (component $T
884 (import "x" (type $x (sub resource)))
885 (import "y" (type (eq $x)))
886 )
887 (instance $test
888 (instantiate $T (with "x" (type $t1)) (with "y" (type $t2))))
889 )
890
891 (assert_invalid
892 (component (import "[static]" (func)))
893 "failed to find `.` character")
894
895 ;; validation of `[constructor]foo`
896 (assert_invalid
897 (component (import "[constructor]" (func)))
898 "not in kebab case")
899 (assert_invalid
900 (component (import "[constructor]a" (func)))
901 "should return one value")
902 (assert_invalid
903 (component (import "[constructor]a" (func (result u32))))
904 "should return `(own $T)`")
905 (assert_invalid
906 (component
907 (import "b" (type $a (sub resource)))
908 (import "[constructor]a" (func (result (own $a)))))
909 "import name `[constructor]a` is not valid")
910 (assert_invalid
911 (component
912 (import "b" (type $a (sub resource)))
913 (import "[constructor]a" (func (result (own $a)))))
914 "function does not match expected resource name `b`")
915 (assert_invalid
916 (component
917 (import "b" (type $a (sub resource)))
918 (import "[constructor]a" (func (result (result(own $a))))))
919 "function does not match expected resource name `b`")
920 (component
921 (import "a" (type $a (sub resource)))
922 (import "[constructor]a" (func (result (own $a)))))
923 (component
924 (import "a" (type $a (sub resource)))
925 (import "[constructor]a" (func (result (result (own $a))))))
926 (component
927 (import "a" (type $a (sub resource)))
928 (import "[constructor]a" (func (result (result (own $a) (error string))))))
929 (component
930 (import "a" (type $a (sub resource)))
931 (import "[constructor]a" (func (param "x" u32) (result (own $a)))))
932 (assert_invalid
933 (component
934 (import "a" (type $a (sub resource)))
935 (import "[constructor]a" (func (result string))))
936 "function should return `(own $T)` or `(result (own $T))`")
937 (assert_invalid
938 (component
939 (import "a" (type $a (sub resource)))
940 (import "[constructor]a" (func (result (result string)))))
941 "function should return `(own $T)` or `(result (own $T))`")
942
943 ;; validation of `[method]a.b`
944 (assert_invalid
945 (component (import "[method]" (func)))
946 "failed to find `.` character")
947 (assert_invalid
948 (component (import "[method]a" (func)))
949 "failed to find `.` character")
950 (assert_invalid
951 (component (import "[method]a." (func)))
952 "not in kebab case")
953 (assert_invalid
954 (component (import "[method].a" (func)))
955 "not in kebab case")
956 (assert_invalid
957 (component (import "[method]a.b.c" (func)))
958 "not in kebab case")
959 (assert_invalid
960 (component (import "[method]a.b" (instance)))
961 "is not a func")
962 (assert_invalid
963 (component (import "[method]a.b" (func)))
964 "should have at least one argument")
965 (assert_invalid
966 (component (import "[method]a.b" (func (param "x" u32))))
967 "should have a first argument called `self`")
968 (assert_invalid
969 (component (import "[method]a.b" (func (param "self" u32))))
970 "should take a first argument of `(borrow $T)`")
971 (assert_invalid
972 (component
973 (import "b" (type $T (sub resource)))
974 (import "[method]a.b" (func (param "self" (borrow $T)))))
975 "does not match expected resource name")
976 (component
977 (import "a" (type $T (sub resource)))
978 (import "[method]a.b" (func (param "self" (borrow $T)))))
979
980 ;; validation of `[static]a.b`
981 (assert_invalid
982 (component (import "[static]" (func)))
983 "failed to find `.` character")
984 (assert_invalid
985 (component (import "[static]a" (func)))
986 "failed to find `.` character")
987 (assert_invalid
988 (component (import "[static]a." (func)))
989 "not in kebab case")
990 (assert_invalid
991 (component (import "[static].a" (func)))
992 "not in kebab case")
993 (assert_invalid
994 (component (import "[static]a.b.c" (func)))
995 "not in kebab case")
996 (assert_invalid
997 (component (import "[static]a.b" (instance)))
998 "is not a func")
999 (assert_invalid
1000 (component (import "[static]a.b" (func)))
1001 "static resource name is not known in this context")
1002
1003 (component
1004 (import "a" (type (sub resource)))
1005 (import "[static]a.b" (func)))
1006
1007 ;; exports/imports are disjoint
1008 (assert_invalid
1009 (component
1010 (import "b" (type $T (sub resource)))
1011 (import "f" (func $f (param "self" (borrow $T))))
1012 (export "[method]b.foo" (func $f))
1013 )
1014 "resource used in function does not have a name in this context")
1015
1016 (component
1017 (import "b" (type $T (sub resource)))
1018 (import "f" (func $f (param "self" (borrow $T))))
1019 (export $c "c" (type $T))
1020 (export "[method]c.foo" (func $f) (func (param "self" (borrow $c))))
1021 )
1022
1023 ;; imports aren't transitive
1024 (assert_invalid
1025 (component
1026 (import "i" (instance $i
1027 (export "t" (type (sub resource)))
1028 ))
1029 (alias export $i "t" (type $t))
1030 (import "[method]t.foo" (func (param "self" (borrow $t))))
1031 )
1032 "resource used in function does not have a name in this context")
1033
1034 ;; validation happens in a type context
1035 (assert_invalid
1036 (component
1037 (type (component
1038 (import "b" (type $T (sub resource)))
1039 (import "[constructor]a" (func (result (own $T))))
1040 ))
1041 )
1042 "function does not match expected resource name `b`")
1043
1044 ;; bag-of-exports validation
1045 (assert_invalid
1046 (component
1047 (type $T (resource (rep i32)))
1048 (core module $m (func (export "a") (result i32) unreachable))
1049 (core instance $i (instantiate $m))
1050 (func $f (result (own $T)) (canon lift (core func $i "a")))
1051 (instance
1052 (export "a" (type $T))
1053 (export "[constructor]a" (func $f))
1054 )
1055 )
1056 "resource used in function does not have a name in this context")
1057
1058 (component
1059 (component $C)
1060 (instance (instantiate $C (with "this is not kebab case" (component $C))))
1061 )
1062
1063 ;; Test that unused arguments to instantiation are not validated to have
1064 ;; appropriate types with respect to kebab naming conventions which require
1065 ;; functions/interfaces/etc.
1066 (component
1067 (component $C)
1068 (instance (instantiate $C (with "[method]foo.bar" (component $C))))
1069 )
1070
1071 ;; thread a resource through a few layers
1072 (component
1073 (component $C
1074 (import "in" (type $r (sub resource)))
1075 (export "out" (type $r))
1076 )
1077
1078 (type $r (resource (rep i32)))
1079
1080 (instance $c1 (instantiate $C (with "in" (type $r))))
1081 (instance $c2 (instantiate $C (with "in" (type $c1 "out"))))
1082 (instance $c3 (instantiate $C (with "in" (type $c2 "out"))))
1083 (instance $c4 (instantiate $C (with "in" (type $c3 "out"))))
1084 (instance $c5 (instantiate $C (with "in" (type $c4 "out"))))
1085
1086 (component $C2
1087 (import "in1" (type $r (sub resource)))
1088 (import "in2" (type (eq $r)))
1089 (import "in3" (type (eq $r)))
1090 (import "in4" (type (eq $r)))
1091 (import "in5" (type (eq $r)))
1092 (import "in6" (type (eq $r)))
1093 )
1094
1095 (instance (instantiate $C2
1096 (with "in1" (type $r))
1097 (with "in2" (type $c1 "out"))
1098 (with "in3" (type $c2 "out"))
1099 (with "in4" (type $c3 "out"))
1100 (with "in5" (type $c4 "out"))
1101 (with "in6" (type $c5 "out"))
1102 ))
1103 )
1104
1105 ;; exporting an instance type "freshens" resources
1106 (assert_invalid
1107 (component
1108 (import "x" (instance $i
1109 (type $i (instance
1110 (export "r" (type (sub resource)))
1111 ))
1112 (export "a" (instance (type $i)))
1113 (export "b" (instance (type $i)))
1114 ))
1115
1116 (component $C
1117 (import "x" (type $x (sub resource)))
1118 (import "y" (type (eq $x)))
1119 )
1120 (instance (instantiate $C
1121 (with "x" (type $i "a" "r"))
1122 (with "y" (type $i "b" "r"))
1123 ))
1124 )
1125 "resource types are not the same")
1126
1127 (component
1128 (type (export "x") (component
1129 (type $t' (instance
1130 (export "r" (type (sub resource)))
1131 ))
1132 (export "t" (instance $t (type $t')))
1133 (alias export $t "r" (type $r))
1134 (type $t2' (instance
1135 (export "r2" (type (eq $r)))
1136 (export "r" (type (sub resource)))
1137 ))
1138 (export "t2" (instance (type $t2')))
1139 ))
1140 )
1141
1142 (component
1143 (type (component
1144 (type (instance
1145 (export "bar" (type (sub resource)))
1146 (export "[static]bar.a" (func))
1147 ))
1148 (export "x" (instance (type 0)))
1149 ))
1150 )
1151
1152 (assert_invalid
1153 (component
1154 (type $r (resource (rep i32)))
1155 (type (func (result (borrow $r))))
1156 )
1157 "function result cannot contain a `borrow` type")
1158 (assert_invalid
1159 (component
1160 (type $r (resource (rep i32)))
1161 (type (func (result (list (borrow $r)))))
1162 )
1163 "function result cannot contain a `borrow` type")
1164 (assert_invalid
1165 (component
1166 (type $r (resource (rep i32)))
1167 (type (func (result (option (borrow $r)))))
1168 )
1169 "function result cannot contain a `borrow` type")
1170 (assert_invalid
1171 (component
1172 (type $r (resource (rep i32)))
1173 (type $t (record (field "f" (borrow $r))))
1174 (type (func (result (option (list $t)))))
1175 )
1176 "function result cannot contain a `borrow` type")
1177
1178 ;; forms of canon builtins
1179 (component
1180 (type $r (resource (rep i32)))
1181 (core func (canon resource.new $r))
1182 (canon resource.new $r (core func))
1183 (core func (canon resource.drop $r))
1184 (canon resource.drop $r (core func))
1185 (core func (canon resource.rep $r))
1186 (canon resource.rep $r (core func))
1187 )