summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/wasm/gc/ref.js
blob: 2bf76daf523b2251cc89029ffefccbcc35f6c92d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// |jit-test| skip-if: !wasmGcEnabled()

// Parsing and resolving.

var text = `(module
      (rec
        (type $cons (struct
                     (field $car i32)
                     (field $cdr (ref null $cons))))

        (type $odd (struct
                    (field $odd.x i32)
                    (field $to_even (ref null $even))))

        (type $even (struct
                     (field $even.x i32)
                     (field $to_odd (ref null $odd))))
      )

      ;; Use eqref on the API since struct types cannot be exposed outside the module yet.

      (import "m" "f" (func $imp (param eqref) (result eqref)))

      ;; The bodies do nothing since we have no operations on structs yet.
      ;; Note none of these functions are exported, as they use Ref types in their signatures.

      (func (param (ref null $cons)) (result i32)
       (i32.const 0))

      (func $cdr (param $p (ref null $cons)) (result (ref null $cons))
       (local $l (ref null $cons))
       ;; store null value of correct type
       (local.set $l (ref.null $cons))
       ;; store local of correct type
       (local.set $l (local.get $p))
       ;; store call result of correct type
       (local.set $l (call $cdr (local.get $p)))
       ;; TODO: eventually also a test with global.get
       ;; blocks and if with result type
       (block (result (ref null $cons))
       (if (result (ref null $cons)) (i32.eqz (i32.const 0))
        (then (unreachable))
        (else (ref.null $cons)))))

      (func (param (ref null $even)) (result (ref null $odd))
       (ref.null $odd))

      (func (param (ref null $odd)) (result (ref null $even))
       (ref.null $even))

      (func (param (ref null $cons))
       (call $cdr (local.get 0))
       drop
       (call $imp (local.get 0))
       drop)

      (func (param (ref null $cons))
       (drop (ref.eq (local.get 0) (ref.null $cons)))
       (drop (ref.eq (ref.null $cons) (local.get 0)))
       (drop (ref.eq (local.get 0) (ref.null $cons)))
       (drop (ref.eq (ref.null $cons) (local.get 0))))
     )`;

// Validation

wasmValidateText(text);

// ref.is_null should work on any reference type

new WebAssembly.Module(wasmTextToBinary(`
(module
 (type $s (struct))
 (func $null (param (ref null $s)) (result i32)
   (ref.is_null (local.get 0))))
`))

// Automatic upcast to eqref

new WebAssembly.Module(wasmTextToBinary(`
(module
 (type $s (struct (field i32)))
 (func $f (param (ref null $s)) (call $g (local.get 0)))
 (func $g (param eqref) (unreachable)))
`));

// Misc failure modes

assertErrorMessage(() => wasmEvalText(`
(module
 (func (param (ref null $odd)) (unreachable)))
`),
SyntaxError, /failed to find name/);

// Ref type mismatch in parameter is allowed through the prefix rule
// but not if the structs are incompatible.

wasmEvalText(`
(module
 (type $s (struct (field i32)))
 (type $t (struct (field i32)))
 (func $f (param (ref null $s)) (unreachable))
 (func $g (param (ref null $t)) (call $f (local.get 0)))
)`);

assertErrorMessage(() => wasmEvalText(`
(module
 (type $s (struct (field i32)))
 (type $t (struct (field f32))) ;; Incompatible type
 (func $f (param (ref null $s)) (unreachable))
 (func $g (param (ref null $t)) (call $f (local.get 0)))
)`),
WebAssembly.CompileError, /expression has type \(ref null.*\) but expected \(ref null.*\)/);

assertErrorMessage(() => wasmEvalText(`
(module
 (type $s (struct (field i32)))
 (type $t (struct (field (mut i32)))) ;; Incompatible mutability
 (func $f (param (ref null $s)) (unreachable))
 (func $g (param (ref null $t)) (call $f (local.get 0)))
)`),
WebAssembly.CompileError, /expression has type \(ref null.*\) but expected \(ref null.*\)/);

// Ref type mismatch in assignment to local but the prefix rule allows
// the assignment to succeed if the structs are the same.

wasmEvalText(`
(module
 (type $s (struct (field i32)))
 (type $t (struct (field i32)))
 (func $f (param (ref null $s)) (local (ref null $t)) (local.set 1 (local.get 0))))
`)

assertErrorMessage(() => wasmEvalText(`
(module
 (type $s (struct (field i32)))
 (type $t (struct (field f32)))
 (func $f (param (ref null $s)) (local (ref null $t)) (local.set 1 (local.get 0))))
`),
WebAssembly.CompileError, /expression has type \(ref null.*\) but expected \(ref null.*\)/);

assertErrorMessage(() => wasmEvalText(`
(module
 (type $s (struct (field i32)))
 (type $t (struct (field (mut i32))))
 (func $f (param (ref null $s)) (unreachable))
 (func $g (param (ref null $t)) (call $f (local.get 0)))
)`),
WebAssembly.CompileError, /expression has type \(ref null.*\) but expected \(ref null.*\)/);

// Ref type mismatch in return but the prefix rule allows the return
// to succeed if the structs are the same.

wasmEvalText(`
(module
 (type $s (struct (field i32)))
 (type $t (struct (field i32)))
 (func $f (param (ref null $s)) (result (ref null $t)) (local.get 0)))
`);

assertErrorMessage(() => wasmEvalText(`
(module
 (type $s (struct (field i32)))
 (type $t (struct (field f32)))
 (func $f (param (ref null $s)) (result (ref null $t)) (local.get 0)))
`),
WebAssembly.CompileError, /expression has type \(ref null.*\) but expected \(ref null.*\)/);

assertErrorMessage(() => wasmEvalText(`
(module
 (type $s (struct (field i32)))
 (type $t (struct (field (mut i32))))
 (func $f (param (ref null $s)) (result (ref null $t)) (local.get 0)))
`),
WebAssembly.CompileError, /expression has type \(ref null.*\) but expected \(ref null.*\)/);

if (!wasmGcEnabled()) {
  // Ref type can't reference a function type

  assertErrorMessage(() => wasmEvalText(`
(module
 (type $x (func (param i32)))
 (func $f (param (ref null $x)) (unreachable)))
`),
  WebAssembly.CompileError, /does not reference a gc type/);

  assertErrorMessage(() => wasmEvalText(`
(module
 (type (func (param i32)))
 (func $f (param (ref null 0)) (unreachable)))
`),
  WebAssembly.CompileError, /does not reference a gc type/);
}

// No automatic downcast from eqref

assertErrorMessage(() => wasmEvalText(`
(module
 (type $s (struct (field i32)))
 (func $f (param eqref) (call $g (local.get 0)))
 (func $g (param (ref null $s)) (unreachable)))
`),
WebAssembly.CompileError, /expression has type eqref but expected \(ref null.*\)/);