Debian Codegraph @

tests/cli/component-model-gc/variant-types.wast

raw
1 ;; RUN: wast --assert default --snapshot tests/snapshots % -f gc,cm-gc
2
3 ;; Basic.
4 (component
5 (type $variant (variant (case "a" bool)
6 (case "b" bool)
7 (case "c" u8)))
8
9 (core type $variant (struct))
10 (core type $ty (func (param (ref $variant))))
11
12 (import "i" (instance $i
13 (export "ty" (type $variant' (eq $variant)))
14 (export "f" (func (param "x" $variant')))))
15 (core func (canon lower (func $i "f") gc (core-type $ty)))
16 )
17
18 ;; With a nullable reference.
19 (component
20 (type $variant (variant (case "a" bool)
21 (case "b" bool)
22 (case "c" u8)))
23
24 (core type $variant (struct))
25 (core type $ty (func (param (ref null $variant))))
26
27 (import "i" (instance $i
28 (export "ty" (type $variant' (eq $variant)))
29 (export "f" (func (param "x" $variant')))))
30 (core func (canon lower (func $i "f") gc (core-type $ty)))
31 )
32
33 ;; With a custom rec group.
34 (component
35 (type $variant (variant (case "a" bool)
36 (case "b" bool)
37 (case "c" u8)))
38
39 (core rec
40 (type $variant (struct))
41 (type $ty (func (param (ref null $variant)))))
42
43 (import "i" (instance $i
44 (export "ty" (type $variant' (eq $variant)))
45 (export "f" (func (param "x" $variant')))))
46 (core func (canon lower (func $i "f") gc (core-type $ty)))
47 )
48
49 ;; With a custom subtype.
50 (component
51 (type $variant (variant (case "a" bool)
52 (case "b" bool)
53 (case "c" u8)))
54
55 (core type $base (sub (struct)))
56 (core type $variant (sub $base (struct)))
57 (core type $ty (func (param (ref null $variant))))
58
59 (import "i" (instance $i
60 (export "ty" (type $variant' (eq $variant)))
61 (export "f" (func (param "x" $variant')))))
62 (core func (canon lower (func $i "f") gc (core-type $ty)))
63 )
64
65 ;; Unexpected field in struct.
66 (assert_invalid
67 (component
68 (type $variant (variant (case "a" bool)
69 (case "b" bool)
70 (case "c" u8)))
71
72 (core type $variant (struct (field i8)))
73 (core type $ty (func (param (ref $variant))))
74
75 (import "i" (instance $i
76 (export "ty" (type $variant' (eq $variant)))
77 (export "f" (func (param "x" $variant')))))
78 (core func (canon lower (func $i "f") gc (core-type $ty)))
79 )
80 "expected to lower component `variant` type to core `(ref null? (struct))`"
81 )
82
83 ;; Lowering into a non-struct.
84 (assert_invalid
85 (component
86 (type $variant (variant (case "a" bool)
87 (case "b" bool)
88 (case "c" u8)))
89
90 (core type $ty (func (param externref)))
91
92 (import "i" (instance $i
93 (export "ty" (type $variant' (eq $variant)))
94 (export "f" (func (param "x" $variant')))))
95 (core func (canon lower (func $i "f") gc (core-type $ty)))
96 )
97 "expected to lower component `variant` type to core `(ref null? (struct))`"
98 )