| 1 |
// Test wasm::InitExpr GC specific constant expressions |
| 2 |
|
| 3 |
// struct.new and struct.new_default |
| 4 |
|
| 5 |
const { structNew, structNewDefault, structLarge } = wasmEvalText(`(module |
| 6 |
(type $r (struct (field i32) (field f32))) |
| 7 |
(type $xxl (struct |
| 8 |
(field i64) (field f64) (field i64) (field f64) |
| 9 |
(field i64) (field f64) (field i64) (field f64) |
| 10 |
(field i64) (field f64) (field i64) (field f64) |
| 11 |
(field i64) (field f64) (field i64) (field f64) |
| 12 |
(field i64) (field f64) (field i64) (field f64))) |
| 13 |
|
| 14 |
(global $g1 (ref null $r) (struct.new_default $r)) |
| 15 |
|
| 16 |
(global $g2 (ref null $r) (struct.new $r |
| 17 |
(i32.const 2) |
| 18 |
(f32.const 3.14))) |
| 19 |
|
| 20 |
(global $gx (ref null $xxl) (struct.new $xxl |
| 21 |
(i64.const 1) (f64.const 2.) (i64.const 3) (f64.const 4.) |
| 22 |
(i64.const 5) (f64.const 2.) (i64.const 3) (f64.const 4.) |
| 23 |
(i64.const 1) (f64.const 8.) (i64.const 9) (f64.const 4.) |
| 24 |
(i64.const 1) (f64.const 2.) (i64.const 12) (f64.const 3.14) |
| 25 |
(i64.const 16) (f64.const 17.) (i64.const 18) (f64.const 19.))) |
| 26 |
|
| 27 |
(func (export "structNewDefault") (result eqref) global.get $g1) |
| 28 |
(func (export "structNew") (result eqref) global.get $g2) |
| 29 |
(func (export "structLarge") (result eqref) global.get $gx) |
| 30 |
)`).exports; |
| 31 |
|
| 32 |
let result; |
| 33 |
result = structNew(); |
| 34 |
assertEq(wasmGcReadField(result, 0), 2); |
| 35 |
assertEq(wasmGcReadField(result, 1), new Float32Array([3.140000104904175])[0]); |
| 36 |
result = structNewDefault(); |
| 37 |
assertEq(wasmGcReadField(result, 0), 0); |
| 38 |
assertEq(wasmGcReadField(result, 1), 0); |
| 39 |
result = structLarge(); |
| 40 |
assertEq(wasmGcReadField(result, 2), 3n); |
| 41 |
assertEq(wasmGcReadField(result, 19), 19); |
| 42 |
|
| 43 |
// array.new, array.new_default, array.new_fixed, and array.new_elem |
| 44 |
|
| 45 |
const { arrayNew, arrayNewDefault, arrayNewFixed, arrayNewElem } = wasmEvalText(`(module |
| 46 |
(type $r (struct (field i32) (field f32))) |
| 47 |
(type $a1 (array f64)) |
| 48 |
(type $a2 (array i32)) |
| 49 |
(type $a3 (array (ref null $r))) |
| 50 |
|
| 51 |
(global $g1 (ref null $a1) (array.new $a1 (f64.const 3.14) (i32.const 3))) |
| 52 |
(global $g2 (ref null $a2) (array.new_default $a2 (i32.const 2))) |
| 53 |
(global $g3 (ref null $a3) (array.new_fixed $a3 2 |
| 54 |
(struct.new $r (i32.const 10) (f32.const 16.0)) |
| 55 |
(ref.null $r))) |
| 56 |
|
| 57 |
(elem $a3e (ref null $r) |
| 58 |
(item (struct.new $r (i32.const 1) (f32.const 2.0))) |
| 59 |
(item (struct.new $r (i32.const 3) (f32.const 4.0))) |
| 60 |
(item (struct.new $r (i32.const 5) (f32.const 6.0))) |
| 61 |
) |
| 62 |
|
| 63 |
(func (export "arrayNew") (result eqref) global.get $g1) |
| 64 |
(func (export "arrayNewDefault") (result eqref) global.get $g2) |
| 65 |
(func (export "arrayNewFixed") (result eqref) global.get $g3) |
| 66 |
(func (export "arrayNewElem") (result eqref) |
| 67 |
(array.new_elem $a3 $a3e (i32.const 0) (i32.const 3)) |
| 68 |
) |
| 69 |
)`).exports; |
| 70 |
|
| 71 |
result = arrayNew(); |
| 72 |
assertEq(wasmGcArrayLength(result), 3); |
| 73 |
assertEq(wasmGcReadField(result, 0), 3.14); |
| 74 |
assertEq(wasmGcReadField(result, 2), 3.14); |
| 75 |
|
| 76 |
result = arrayNewDefault(); |
| 77 |
assertEq(wasmGcArrayLength(result), 2); |
| 78 |
assertEq(wasmGcReadField(result, 1), 0); |
| 79 |
|
| 80 |
result = arrayNewFixed(); |
| 81 |
assertEq(wasmGcArrayLength(result), 2); |
| 82 |
assertEq(wasmGcReadField(wasmGcReadField(result, 0), 0), 10); |
| 83 |
assertEq(wasmGcReadField(wasmGcReadField(result, 0), 1), 16); |
| 84 |
assertEq(wasmGcReadField(result, 1), null); |
| 85 |
|
| 86 |
result = arrayNewElem(); |
| 87 |
assertEq(wasmGcArrayLength(result), 3); |
| 88 |
assertEq(wasmGcReadField(wasmGcReadField(result, 0), 0), 1); |
| 89 |
assertEq(wasmGcReadField(wasmGcReadField(result, 0), 1), 2); |
| 90 |
assertEq(wasmGcReadField(wasmGcReadField(result, 1), 0), 3); |
| 91 |
assertEq(wasmGcReadField(wasmGcReadField(result, 1), 1), 4); |
| 92 |
assertEq(wasmGcReadField(wasmGcReadField(result, 2), 0), 5); |
| 93 |
assertEq(wasmGcReadField(wasmGcReadField(result, 2), 1), 6); |
| 94 |
|
| 95 |
// any.convert_extern and extern.convert_any |
| 96 |
|
| 97 |
let {testString, testArray} = wasmEvalText(`(module |
| 98 |
(type $array (array i32)) |
| 99 |
(import "env" "s" (global $s (ref extern))) |
| 100 |
(global $s' (ref extern) (extern.convert_any (any.convert_extern (global.get $s)))) |
| 101 |
(func (export "testString") (result (ref extern)) |
| 102 |
(global.get $s')) |
| 103 |
(global $a (ref $array) (array.new_fixed $array 1 (i32.const 0))) |
| 104 |
(global $a' (ref any) (any.convert_extern (extern.convert_any (global.get $a)))) |
| 105 |
(func (export "testArray") (result i32) |
| 106 |
(ref.eq (global.get $a) (ref.cast (ref eq) (global.get $a')))) |
| 107 |
)`, {env:{s:"abc"}}).exports; |
| 108 |
|
| 109 |
assertEq(testString(), 'abc'); |
| 110 |
assertEq(testArray(), 1); |
| 111 |
|
| 112 |
wasmFailValidateText(`(module |
| 113 |
(global $g (ref extern) (extern.convert_any (any.convert_extern (ref.null extern)))) |
| 114 |
)`, /expected/); |
| 115 |
wasmFailValidateText(`(module |
| 116 |
(global $g (ref extern) (any.convert_extern (extern.convert_any (ref.null any)))) |
| 117 |
)`, /expected/); |
| 118 |
|
| 119 |
// Simple table initialization |
| 120 |
{ |
| 121 |
const { t1, t2, t1init } = wasmEvalText(`(module |
| 122 |
(type $s (struct (field i32))) |
| 123 |
(type $a1 (array f64)) |
| 124 |
(type $a2 (array (ref null $s))) |
| 125 |
|
| 126 |
;; passive segment |
| 127 |
(elem $e1 anyref |
| 128 |
(item (struct.new $s (i32.const 123))) |
| 129 |
(item (array.new $a1 (f64.const 234) (i32.const 3))) |
| 130 |
(item (array.new_default $a2 (i32.const 3))) |
| 131 |
(item (ref.i31 (i32.const 345))) |
| 132 |
) |
| 133 |
(table $t1 (export "t1") 4 4 anyref) |
| 134 |
|
| 135 |
;; active segment |
| 136 |
(table $t2 (export "t2") anyref (elem |
| 137 |
(item (struct.new $s (i32.const 321))) |
| 138 |
(item (array.new $a1 (f64.const 432) (i32.const 3))) |
| 139 |
(item (array.new_default $a2 (i32.const 3))) |
| 140 |
(item (ref.i31 (i32.const 543))) |
| 141 |
)) |
| 142 |
|
| 143 |
(func (export "t1init") (table.init $t1 $e1 (i32.const 0) (i32.const 0) (i32.const 4))) |
| 144 |
)`).exports; |
| 145 |
|
| 146 |
assertEq(t1.get(0), null); |
| 147 |
assertEq(t1.get(1), null); |
| 148 |
assertEq(t1.get(2), null); |
| 149 |
assertEq(t1.get(3), null); |
| 150 |
|
| 151 |
assertEq(wasmGcReadField(t2.get(0), 0), 321); |
| 152 |
assertEq(wasmGcReadField(t2.get(1), 0), 432); |
| 153 |
assertEq(wasmGcReadField(t2.get(1), 1), 432); |
| 154 |
assertEq(wasmGcReadField(t2.get(1), 2), 432); |
| 155 |
assertEq(wasmGcReadField(t2.get(2), 0), null); |
| 156 |
assertEq(wasmGcReadField(t2.get(2), 1), null); |
| 157 |
assertEq(wasmGcReadField(t2.get(2), 2), null); |
| 158 |
assertEq(t2.get(3), 543); |
| 159 |
|
| 160 |
t1init(); |
| 161 |
assertEq(wasmGcReadField(t1.get(0), 0), 123); |
| 162 |
assertEq(wasmGcReadField(t1.get(1), 0), 234); |
| 163 |
assertEq(wasmGcReadField(t1.get(1), 1), 234); |
| 164 |
assertEq(wasmGcReadField(t1.get(1), 2), 234); |
| 165 |
assertEq(wasmGcReadField(t1.get(2), 0), null); |
| 166 |
assertEq(wasmGcReadField(t1.get(2), 1), null); |
| 167 |
assertEq(wasmGcReadField(t1.get(2), 2), null); |
| 168 |
assertEq(t1.get(3), 345); |
| 169 |
} |
| 170 |
|
| 171 |
// The contents of passive segments are unique per instance and evaluated at |
| 172 |
// instantiation time. |
| 173 |
{ |
| 174 |
const mod = new WebAssembly.Module(wasmTextToBinary(`(module |
| 175 |
(type $s (struct (field (mut i32)))) |
| 176 |
(type $a (array (mut f64))) |
| 177 |
|
| 178 |
(elem $e anyref |
| 179 |
(item (struct.new $s (i32.const 123))) |
| 180 |
(item (array.new $a (f64.const 234) (i32.const 1))) |
| 181 |
) |
| 182 |
|
| 183 |
(table $t1 (export "t1") 2 2 anyref) |
| 184 |
(table $t2 (export "t2") 2 2 anyref) |
| 185 |
|
| 186 |
(start $init1) |
| 187 |
|
| 188 |
(func $init1 |
| 189 |
(table.init $t1 $e (i32.const 0) (i32.const 0) (i32.const 2)) |
| 190 |
) |
| 191 |
(func (export "init2") |
| 192 |
(table.init $t2 $e (i32.const 0) (i32.const 0) (i32.const 2)) |
| 193 |
) |
| 194 |
(func (export "update1") |
| 195 |
(ref.cast (ref $s) (table.get $t1 (i32.const 0))) |
| 196 |
(struct.set $s 0 (i32.const 321)) |
| 197 |
(ref.cast (ref $a) (table.get $t1 (i32.const 1))) |
| 198 |
(array.set $a (i32.const 0) (f64.const 432)) |
| 199 |
) |
| 200 |
(func (export "update2") |
| 201 |
(ref.cast (ref $s) (table.get $t1 (i32.const 0))) |
| 202 |
(struct.set $s 0 (i32.const -321)) |
| 203 |
(ref.cast (ref $a) (table.get $t1 (i32.const 1))) |
| 204 |
(array.set $a (i32.const 0) (f64.const -432)) |
| 205 |
) |
| 206 |
)`)); |
| 207 |
|
| 208 |
const { t1: t1_1, t2: t2_1, init2: init2_1, update1: update1_1, update2: update2_1 } = new WebAssembly.Instance(mod, {}).exports; |
| 209 |
const { t1: t1_2, t2: t2_2, init2: init2_2, update1: update1_2, update2: update2_2 } = new WebAssembly.Instance(mod, {}).exports; |
| 210 |
|
| 211 |
assertEq(wasmGcReadField(t1_1.get(0), 0), 123); |
| 212 |
assertEq(wasmGcReadField(t1_1.get(1), 0), 234); |
| 213 |
assertEq(t2_1.get(0), null); |
| 214 |
assertEq(t2_1.get(1), null); |
| 215 |
assertEq(wasmGcReadField(t1_2.get(0), 0), 123); |
| 216 |
assertEq(wasmGcReadField(t1_2.get(1), 0), 234); |
| 217 |
assertEq(t2_2.get(0), null); |
| 218 |
assertEq(t2_2.get(1), null); |
| 219 |
|
| 220 |
update1_1(); |
| 221 |
assertEq(wasmGcReadField(t1_1.get(0), 0), 321); |
| 222 |
assertEq(wasmGcReadField(t1_1.get(1), 0), 432); |
| 223 |
assertEq(t2_1.get(0), null); |
| 224 |
assertEq(t2_1.get(1), null); |
| 225 |
assertEq(wasmGcReadField(t1_2.get(0), 0), 123); |
| 226 |
assertEq(wasmGcReadField(t1_2.get(1), 0), 234); |
| 227 |
assertEq(t2_2.get(0), null); |
| 228 |
assertEq(t2_2.get(1), null); |
| 229 |
|
| 230 |
init2_1(); |
| 231 |
assertEq(wasmGcReadField(t1_1.get(0), 0), 321); |
| 232 |
assertEq(wasmGcReadField(t1_1.get(1), 0), 432); |
| 233 |
assertEq(wasmGcReadField(t2_1.get(0), 0), 321); |
| 234 |
assertEq(wasmGcReadField(t2_1.get(1), 0), 432); |
| 235 |
assertEq(wasmGcReadField(t1_2.get(0), 0), 123); |
| 236 |
assertEq(wasmGcReadField(t1_2.get(1), 0), 234); |
| 237 |
assertEq(t2_2.get(0), null); |
| 238 |
assertEq(t2_2.get(1), null); |
| 239 |
|
| 240 |
init2_2(); |
| 241 |
assertEq(wasmGcReadField(t1_1.get(0), 0), 321); |
| 242 |
assertEq(wasmGcReadField(t1_1.get(1), 0), 432); |
| 243 |
assertEq(wasmGcReadField(t2_1.get(0), 0), 321); |
| 244 |
assertEq(wasmGcReadField(t2_1.get(1), 0), 432); |
| 245 |
assertEq(wasmGcReadField(t1_2.get(0), 0), 123); |
| 246 |
assertEq(wasmGcReadField(t1_2.get(1), 0), 234); |
| 247 |
assertEq(wasmGcReadField(t2_2.get(0), 0), 123); |
| 248 |
assertEq(wasmGcReadField(t2_2.get(1), 0), 234); |
| 249 |
|
| 250 |
update2_1(); |
| 251 |
assertEq(wasmGcReadField(t1_1.get(0), 0), -321); |
| 252 |
assertEq(wasmGcReadField(t1_1.get(1), 0), -432); |
| 253 |
assertEq(wasmGcReadField(t2_1.get(0), 0), -321); |
| 254 |
assertEq(wasmGcReadField(t2_1.get(1), 0), -432); |
| 255 |
assertEq(wasmGcReadField(t1_2.get(0), 0), 123); |
| 256 |
assertEq(wasmGcReadField(t1_2.get(1), 0), 234); |
| 257 |
assertEq(wasmGcReadField(t2_2.get(0), 0), 123); |
| 258 |
assertEq(wasmGcReadField(t2_2.get(1), 0), 234); |
| 259 |
|
| 260 |
update1_2(); |
| 261 |
assertEq(wasmGcReadField(t1_1.get(0), 0), -321); |
| 262 |
assertEq(wasmGcReadField(t1_1.get(1), 0), -432); |
| 263 |
assertEq(wasmGcReadField(t2_1.get(0), 0), -321); |
| 264 |
assertEq(wasmGcReadField(t2_1.get(1), 0), -432); |
| 265 |
assertEq(wasmGcReadField(t1_2.get(0), 0), 321); |
| 266 |
assertEq(wasmGcReadField(t1_2.get(1), 0), 432); |
| 267 |
assertEq(wasmGcReadField(t2_2.get(0), 0), 321); |
| 268 |
assertEq(wasmGcReadField(t2_2.get(1), 0), 432); |
| 269 |
|
| 270 |
update2_2(); |
| 271 |
assertEq(wasmGcReadField(t1_1.get(0), 0), -321); |
| 272 |
assertEq(wasmGcReadField(t1_1.get(1), 0), -432); |
| 273 |
assertEq(wasmGcReadField(t2_1.get(0), 0), -321); |
| 274 |
assertEq(wasmGcReadField(t2_1.get(1), 0), -432); |
| 275 |
assertEq(wasmGcReadField(t1_2.get(0), 0), -321); |
| 276 |
assertEq(wasmGcReadField(t1_2.get(1), 0), -432); |
| 277 |
assertEq(wasmGcReadField(t2_2.get(0), 0), -321); |
| 278 |
assertEq(wasmGcReadField(t2_2.get(1), 0), -432); |
| 279 |
} |