i31.tentative.any.js (3460B)
1 // META: global=window,dedicatedworker,jsshell 2 // META: script=/wasm/jsapi/wasm-module-builder.js 3 4 let exports = {}; 5 setup(() => { 6 const builder = new WasmModuleBuilder(); 7 const i31Ref = wasmRefType(kWasmI31Ref); 8 const i31NullableRef = wasmRefNullType(kWasmI31Ref); 9 const anyRef = wasmRefType(kWasmAnyRef); 10 11 builder 12 .addFunction("makeI31", makeSig_r_x(i31Ref, kWasmI32)) 13 .addBody([kExprLocalGet, 0, 14 ...GCInstr(kExprI31New)]) 15 .exportFunc(); 16 17 builder 18 .addFunction("castI31", makeSig_r_x(kWasmI32, anyRef)) 19 .addBody([kExprLocalGet, 0, 20 ...GCInstr(kExprRefCast), kI31RefCode, 21 ...GCInstr(kExprI31GetU)]) 22 .exportFunc(); 23 24 builder 25 .addFunction("getI31", makeSig_r_x(kWasmI32, i31Ref)) 26 .addBody([kExprLocalGet, 0, 27 ...GCInstr(kExprI31GetS)]) 28 .exportFunc(); 29 30 builder 31 .addFunction("argI31", makeSig_v_x(i31NullableRef)) 32 .addBody([]) 33 .exportFunc(); 34 35 builder 36 .addGlobal(i31NullableRef, true, [...wasmI32Const(0), ...GCInstr(kExprI31New)]) 37 builder 38 .addExportOfKind("i31Global", kExternalGlobal, 0); 39 40 builder 41 .addTable(i31NullableRef, 10) 42 builder 43 .addExportOfKind("i31Table", kExternalTable, 0); 44 45 const buffer = builder.toBuffer(); 46 const module = new WebAssembly.Module(buffer); 47 const instance = new WebAssembly.Instance(module, {}); 48 exports = instance.exports; 49 }); 50 51 test(() => { 52 assert_equals(exports.makeI31(42), 42); 53 assert_equals(exports.makeI31(2 ** 30 - 1), 2 ** 30 - 1); 54 assert_equals(exports.makeI31(2 ** 30), -(2 ** 30)); 55 assert_equals(exports.makeI31(-(2 ** 30)), -(2 ** 30)); 56 assert_equals(exports.makeI31(2 ** 31 - 1), -1); 57 assert_equals(exports.makeI31(2 ** 31), 0); 58 }, "i31ref conversion to Number"); 59 60 test(() => { 61 assert_equals(exports.getI31(exports.makeI31(42)), 42); 62 assert_equals(exports.getI31(42), 42); 63 assert_equals(exports.getI31(2.0 ** 30 - 1), 2 ** 30 - 1); 64 assert_equals(exports.getI31(-(2 ** 30)), -(2 ** 30)); 65 }, "Number conversion to i31ref"); 66 67 test(() => { 68 exports.argI31(null); 69 assert_throws_js(TypeError, () => exports.argI31(2 ** 30)); 70 assert_throws_js(TypeError, () => exports.argI31(-(2 ** 30) - 1)); 71 assert_throws_js(TypeError, () => exports.argI31(2n)); 72 assert_throws_js(TypeError, () => exports.argI31(() => 3)); 73 assert_throws_js(TypeError, () => exports.argI31(exports.getI31)); 74 }, "Check i31ref argument type"); 75 76 test(() => { 77 assert_equals(exports.castI31(42), 42); 78 assert_equals(exports.castI31(2 ** 30 - 1), 2 ** 30 - 1); 79 assert_throws_js(WebAssembly.RuntimeError, () => { exports.castI31(2 ** 30); }); 80 assert_throws_js(WebAssembly.RuntimeError, () => { exports.castI31(-(2 ** 30) - 1); }); 81 assert_throws_js(WebAssembly.RuntimeError, () => { exports.castI31(2 ** 32); }); 82 }, "Numbers in i31 range are i31ref, not hostref"); 83 84 test(() => { 85 assert_equals(exports.i31Global.value, 0); 86 exports.i31Global.value = 42; 87 assert_throws_js(TypeError, () => exports.i31Global.value = 2 ** 30); 88 assert_throws_js(TypeError, () => exports.i31Global.value = -(2 ** 30) - 1); 89 assert_equals(exports.i31Global.value, 42); 90 }, "i31ref global"); 91 92 test(() => { 93 assert_equals(exports.i31Table.get(0), null); 94 exports.i31Table.set(0, 42); 95 assert_throws_js(TypeError, () => exports.i31Table.set(0, 2 ** 30)); 96 assert_throws_js(TypeError, () => exports.i31Table.set(0, -(2 ** 30) - 1)); 97 assert_equals(exports.i31Table.get(0), 42); 98 }, "i31ref table");