| 1 |
let InvalidI31Values = [ |
| 2 |
null, |
| 3 |
Number.EPSILON, |
| 4 |
Number.MAX_SAFE_INTEGER, |
| 5 |
Number.MIN_SAFE_INTEGER, |
| 6 |
Number.MIN_VALUE, |
| 7 |
Number.MAX_VALUE, |
| 8 |
Number.NaN, |
| 9 |
-0, |
| 10 |
// Number objects are not coerced |
| 11 |
...WasmI31refValues.map(n => new Number(n)), |
| 12 |
// Non-integers are not valid |
| 13 |
...WasmI31refValues.map(n => n + 0.1), |
| 14 |
...WasmI31refValues.map(n => n + 0.5), |
| 15 |
...WasmI31refValues.map(n => n + 0.9) |
| 16 |
]; |
| 17 |
|
| 18 |
// Return an equivalent JS number for if a JS number is converted to i31ref |
| 19 |
// and then zero extended back to 32-bits. |
| 20 |
function valueAsI31GetU(value) { |
| 21 |
// Zero extending will drop the sign bit, if any |
| 22 |
return value & 0x7fffffff; |
| 23 |
} |
| 24 |
|
| 25 |
let identity = (n) => n; |
| 26 |
|
| 27 |
let { |
| 28 |
castFromAnyref, |
| 29 |
castFromExternref, |
| 30 |
refI31, |
| 31 |
refI31Identity, |
| 32 |
i31GetU, |
| 33 |
i31GetS, |
| 34 |
i31EqualsI31, |
| 35 |
i31EqualsEq |
| 36 |
} = wasmEvalText(`(module |
| 37 |
(func $identity (import "" "identity") (param anyref) (result anyref)) |
| 38 |
|
| 39 |
(func (export "castFromAnyref") (param anyref) (result i32) |
| 40 |
local.get 0 |
| 41 |
ref.test (ref i31) |
| 42 |
) |
| 43 |
(func (export "castFromExternref") (param externref) (result i32) |
| 44 |
local.get 0 |
| 45 |
any.convert_extern |
| 46 |
ref.test (ref i31) |
| 47 |
) |
| 48 |
(func (export "refI31") (param i32) (result anyref) |
| 49 |
local.get 0 |
| 50 |
ref.i31 |
| 51 |
) |
| 52 |
(func (export "refI31Identity") (param i32) (result anyref) |
| 53 |
local.get 0 |
| 54 |
ref.i31 |
| 55 |
call $identity |
| 56 |
) |
| 57 |
(func (export "i31GetU") (param i32) (result i32) |
| 58 |
local.get 0 |
| 59 |
ref.i31 |
| 60 |
i31.get_u |
| 61 |
) |
| 62 |
(func (export "i31GetS") (param i32) (result i32) |
| 63 |
local.get 0 |
| 64 |
ref.i31 |
| 65 |
i31.get_s |
| 66 |
) |
| 67 |
(func (export "i31EqualsI31") (param i32) (param i32) (result i32) |
| 68 |
(ref.eq |
| 69 |
(ref.i31 local.get 0) |
| 70 |
(ref.i31 local.get 1) |
| 71 |
) |
| 72 |
) |
| 73 |
(func (export "i31EqualsEq") (param i32) (param eqref) (result i32) |
| 74 |
(ref.eq |
| 75 |
(ref.i31 local.get 0) |
| 76 |
local.get 1 |
| 77 |
) |
| 78 |
) |
| 79 |
)`, {"": {identity}}).exports; |
| 80 |
|
| 81 |
// Test that wasm will represent JS number values that are 31-bit integers as |
| 82 |
// an i31ref |
| 83 |
for (let i of WasmI31refValues) { |
| 84 |
assertEq(castFromAnyref(i), 1); |
| 85 |
assertEq(castFromExternref(i), 1); |
| 86 |
} |
| 87 |
|
| 88 |
// Test that wasm will not represent a JS value that is not a 31-bit number as |
| 89 |
// an i31ref |
| 90 |
for (let i of InvalidI31Values) { |
| 91 |
assertEq(castFromAnyref(i), 0); |
| 92 |
assertEq(castFromExternref(i), 0); |
| 93 |
} |
| 94 |
|
| 95 |
// Test that we can roundtrip 31-bit integers through the i31ref type |
| 96 |
// faithfully. |
| 97 |
for (let i of WasmI31refValues) { |
| 98 |
assertEq(refI31(i), i); |
| 99 |
assertEq(refI31Identity(i), i); |
| 100 |
assertEq(i31GetU(i), valueAsI31GetU(i)); |
| 101 |
assertEq(i31GetS(i), i); |
| 102 |
} |
| 103 |
|
| 104 |
// Test that i31ref values are truncated when given a 32-bit value |
| 105 |
for (let i of WasmI31refValues) { |
| 106 |
let adjusted = i | 0x80000000; |
| 107 |
assertEq(refI31(adjusted), i); |
| 108 |
} |
| 109 |
|
| 110 |
// Test that comparing identical i31 values works |
| 111 |
for (let a of WasmI31refValues) { |
| 112 |
for (let b of WasmI31refValues) { |
| 113 |
assertEq(!!i31EqualsI31(a, b), a === b); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
// Test that an i31ref is never mistaken for a different kind of reference |
| 118 |
for (let a of WasmI31refValues) { |
| 119 |
for (let b of WasmEqrefValues) { |
| 120 |
assertEq(!!i31EqualsEq(a, b), a === b); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
// Test that i32 values get wrapped correctly |
| 125 |
const bigI32Tests = [ |
| 126 |
{ |
| 127 |
input: MaxI31refValue + 1, |
| 128 |
expected: MinI31refValue, |
| 129 |
}, |
| 130 |
{ |
| 131 |
input: MinI31refValue - 1, |
| 132 |
expected: MaxI31refValue, |
| 133 |
}, |
| 134 |
] |
| 135 |
for (const {input, expected} of bigI32Tests) { |
| 136 |
const { get, getElem } = wasmEvalText(`(module |
| 137 |
(func (export "get") (param i32) (result i32) |
| 138 |
(i31.get_s (ref.i31 (local.get 0))) |
| 139 |
) |
| 140 |
|
| 141 |
(table i31ref (elem (item (ref.i31 (i32.const ${input}))))) |
| 142 |
(func (export "getElem") (result i32) |
| 143 |
(i31.get_s (table.get 0 (i32.const 0))) |
| 144 |
) |
| 145 |
)`).exports; |
| 146 |
assertEq(get(input), expected); |
| 147 |
assertEq(getElem(), expected); |
| 148 |
} |
| 149 |
|
| 150 |
// Test that (ref.i31 (i32 const value)) optimization is correct |
| 151 |
for (let value of WasmI31refValues) { |
| 152 |
let {compare} = wasmEvalText(`(module |
| 153 |
(func $innerCompare (param i32) (param i31ref) (result i32) |
| 154 |
(ref.eq |
| 155 |
(ref.i31 local.get 0) |
| 156 |
local.get 1 |
| 157 |
) |
| 158 |
) |
| 159 |
(func (export "compare") (result i32) |
| 160 |
i32.const ${value} |
| 161 |
(ref.i31 i32.const ${value}) |
| 162 |
call $innerCompare |
| 163 |
) |
| 164 |
)`).exports; |
| 165 |
assertEq(compare(value), 1); |
| 166 |
} |
| 167 |
|
| 168 |
const { i31GetU_null, i31GetS_null } = wasmEvalText(`(module |
| 169 |
(func (export "i31GetU_null") (result i32) |
| 170 |
ref.null i31 |
| 171 |
i31.get_u |
| 172 |
) |
| 173 |
(func (export "i31GetS_null") (result i32) |
| 174 |
ref.null i31 |
| 175 |
i31.get_s |
| 176 |
) |
| 177 |
)`).exports; |
| 178 |
|
| 179 |
assertErrorMessage(() => i31GetU_null(), WebAssembly.RuntimeError, /dereferencing null pointer/); |
| 180 |
assertErrorMessage(() => i31GetS_null(), WebAssembly.RuntimeError, /dereferencing null pointer/); |
| 181 |
|
| 182 |
// Test i31.get_s and i31.get_u operations with some cornercases. |
| 183 |
let { f } = new WebAssembly.Instance( |
| 184 |
new WebAssembly.Module(wasmTextToBinary(` |
| 185 |
(module |
| 186 |
(func (export "f") (param i32) (result i32) |
| 187 |
(local i31ref) |
| 188 |
local.get 0 |
| 189 |
ref.i31 |
| 190 |
local.tee 1 |
| 191 |
i31.get_s |
| 192 |
local.get 1 |
| 193 |
i31.get_u |
| 194 |
i32.eq |
| 195 |
) |
| 196 |
)`))).exports; |
| 197 |
|
| 198 |
assertEq(f(-1), 0); |
| 199 |
assertEq(f(-2), 0); |
| 200 |
assertEq(f(-0x40000000), 0); |
| 201 |
assertEq(f(0), 1); |
| 202 |
assertEq(f(1), 1); |
| 203 |
assertEq(f(0x3FFFFFFF), 1); |