|
| 1 | +// META: global=window,dedicatedworker,jsshell,shadowrealm |
| 2 | +// META: script=/wasm/jsapi/assertions.js |
| 3 | +// META: script=/wasm/jsapi/wasm-module-builder.js |
| 4 | + |
| 5 | +test(() => { |
| 6 | + const builder = new WasmModuleBuilder(); |
| 7 | + |
| 8 | + // Tag defined in JavaScript and imported into Wasm |
| 9 | + const jsTag = new WebAssembly.Tag({ parameters: ["i32"] }); |
| 10 | + const jsTagIndex = builder.addImportedTag("module", "jsTag", kSig_v_i); |
| 11 | + const jsTagExn = new WebAssembly.Exception(jsTag, [42]); |
| 12 | + const jsTagExnSamePayload = new WebAssembly.Exception(jsTag, [42]); |
| 13 | + const jsTagExnDiffPayload = new WebAssembly.Exception(jsTag, [53]); |
| 14 | + const throwJSTagExnIndex = builder.addImport("module", "throwJSTagExn", kSig_v_v); |
| 15 | + |
| 16 | + // Tag defined in Wasm and exported to JS |
| 17 | + const wasmTagIndex = builder.addTag(kSig_v_i); |
| 18 | + builder.addExportOfKind("wasmTag", kExternalTag, wasmTagIndex); |
| 19 | + const throwWasmTagExnIndex = builder.addImport("module", "throwWasmTagExn", kSig_v_v); |
| 20 | + // Will be assigned after an instance is created |
| 21 | + let wasmTagExn = null; |
| 22 | + let wasmTagExnSamePayload = null; |
| 23 | + let wasmTagExnDiffPayload = null; |
| 24 | + |
| 25 | + const imports = { |
| 26 | + module: { |
| 27 | + throwJSTagExn: function() { throw jsTagExn; }, |
| 28 | + throwWasmTagExn: function() { throw wasmTagExn; }, |
| 29 | + jsTag: jsTag |
| 30 | + } |
| 31 | + }; |
| 32 | + |
| 33 | + // Call a JS function that throws an exception using a JS-defined tag, catches |
| 34 | + // it with a 'catch' instruction, and rethrows it. |
| 35 | + builder |
| 36 | + .addFunction("catch_js_tag_rethrow", kSig_v_v) |
| 37 | + .addBody([ |
| 38 | + kExprTry, kWasmVoid, |
| 39 | + kExprCallFunction, throwJSTagExnIndex, |
| 40 | + kExprCatch, jsTagIndex, |
| 41 | + kExprDrop, |
| 42 | + kExprRethrow, 0x00, |
| 43 | + kExprEnd |
| 44 | + ]) |
| 45 | + .exportFunc(); |
| 46 | + |
| 47 | + // Call a JS function that throws an exception using a Wasm-defined tag, |
| 48 | + // catches it with a 'catch' instruction, and rethrows it. |
| 49 | + builder |
| 50 | + .addFunction("catch_wasm_tag_rethrow", kSig_v_v) |
| 51 | + .addBody([ |
| 52 | + kExprTry, kWasmVoid, |
| 53 | + kExprCallFunction, throwWasmTagExnIndex, |
| 54 | + kExprCatch, wasmTagIndex, |
| 55 | + kExprDrop, |
| 56 | + kExprRethrow, 0x00, |
| 57 | + kExprEnd |
| 58 | + ]) |
| 59 | + .exportFunc(); |
| 60 | + |
| 61 | + // Call a JS function that throws an exception using a JS-defined tag, catches |
| 62 | + // it with a 'catch_all' instruction, and rethrows it. |
| 63 | + builder |
| 64 | + .addFunction("catch_all_js_tag_rethrow", kSig_v_v) |
| 65 | + .addBody([ |
| 66 | + kExprTry, kWasmVoid, |
| 67 | + kExprCallFunction, throwJSTagExnIndex, |
| 68 | + kExprCatchAll, |
| 69 | + kExprRethrow, 0x00, |
| 70 | + kExprEnd |
| 71 | + ]) |
| 72 | + .exportFunc(); |
| 73 | + |
| 74 | + // Call a JS function that throws an exception using a Wasm-defined tag, |
| 75 | + // catches it with a 'catch_all' instruction, and rethrows it. |
| 76 | + builder |
| 77 | + .addFunction("catch_all_wasm_tag_rethrow", kSig_v_v) |
| 78 | + .addBody([ |
| 79 | + kExprTry, kWasmVoid, |
| 80 | + kExprCallFunction, throwWasmTagExnIndex, |
| 81 | + kExprCatchAll, |
| 82 | + kExprRethrow, 0x00, |
| 83 | + kExprEnd |
| 84 | + ]) |
| 85 | + .exportFunc(); |
| 86 | + |
| 87 | + // Call a JS function that throws an exception, catches it with a 'catch' |
| 88 | + // instruction, and returns its i32 payload. |
| 89 | + builder |
| 90 | + .addFunction("catch_js_tag_return_payload", kSig_i_v) |
| 91 | + .addBody([ |
| 92 | + kExprTry, kWasmI32, |
| 93 | + kExprCallFunction, throwJSTagExnIndex, |
| 94 | + kExprI32Const, 0x00, |
| 95 | + kExprCatch, jsTagIndex, |
| 96 | + kExprReturn, |
| 97 | + kExprEnd |
| 98 | + ]) |
| 99 | + .exportFunc(); |
| 100 | + |
| 101 | + // Call a JS function that throws an exception, catches it with a 'catch' |
| 102 | + // instruction, and throws a new exception using that payload. |
| 103 | + builder |
| 104 | + .addFunction("catch_js_tag_throw_payload", kSig_v_v) |
| 105 | + .addBody([ |
| 106 | + kExprTry, kWasmVoid, |
| 107 | + kExprCallFunction, throwJSTagExnIndex, |
| 108 | + kExprCatch, jsTagIndex, |
| 109 | + kExprThrow, jsTagIndex, |
| 110 | + kExprEnd |
| 111 | + ]) |
| 112 | + .exportFunc(); |
| 113 | + |
| 114 | + const buffer = builder.toBuffer(); |
| 115 | + |
| 116 | + WebAssembly.instantiate(buffer, imports).then(result => { |
| 117 | + // The exception object's identity should be preserved across 'rethrow's in |
| 118 | + // Wasm code. Do tests with a tag defined in JS. |
| 119 | + try { |
| 120 | + result.instance.exports.catch_js_tag_rethrow(); |
| 121 | + } catch (e) { |
| 122 | + assert_equals(e, jsTagExn); |
| 123 | + // Even if they have the same payload, they are different objects, so they |
| 124 | + // shouldn't compare equal. |
| 125 | + assert_not_equals(e, jsTagExnSamePayload); |
| 126 | + assert_not_equals(e, jsTagExnDiffPayload); |
| 127 | + } |
| 128 | + try { |
| 129 | + result.instance.exports.catch_all_js_tag_rethrow(); |
| 130 | + } catch (e) { |
| 131 | + assert_equals(e, jsTagExn); |
| 132 | + assert_not_equals(e, jsTagExnSamePayload); |
| 133 | + assert_not_equals(e, jsTagExnDiffPayload); |
| 134 | + } |
| 135 | + |
| 136 | + // Do the same tests with a tag defined in Wasm. |
| 137 | + const wasmTag = result.instance.exports.wasmTag; |
| 138 | + wasmTagExn = new WebAssembly.Exception(wasmTag, [42]); |
| 139 | + wasmTagExnSamePayload = new WebAssembly.Exception(wasmTag, [42]); |
| 140 | + wasmTagExnDiffPayload = new WebAssembly.Exception(wasmTag, [53]); |
| 141 | + try { |
| 142 | + result.instance.exports.catch_wasm_tag_rethrow(); |
| 143 | + } catch (e) { |
| 144 | + assert_equals(e, wasmTagExn); |
| 145 | + assert_not_equals(e, wasmTagExnSamePayload); |
| 146 | + assert_not_equals(e, wasmTagExnDiffPayload); |
| 147 | + } |
| 148 | + try { |
| 149 | + result.instance.exports.catch_all_wasm_tag_rethrow(); |
| 150 | + } catch (e) { |
| 151 | + assert_equals(e, wasmTagExn); |
| 152 | + assert_not_equals(e, wasmTagExnSamePayload); |
| 153 | + assert_not_equals(e, wasmTagExnDiffPayload); |
| 154 | + } |
| 155 | + |
| 156 | + // This function catches the exception and returns its i32 payload, which |
| 157 | + // should match the original payload. |
| 158 | + assert_equals(result.instance.exports.catch_js_tag_return_payload(), 42); |
| 159 | + |
| 160 | + // This function catches the exception and throws a new exception using the |
| 161 | + // its payload. Even if the payload is reused, the exception objects should |
| 162 | + // not compare equal. |
| 163 | + try { |
| 164 | + result.instance.exports.catch_js_tag_throw_payload(); |
| 165 | + } catch (e) { |
| 166 | + assert_equals(e.getArg(jsTag, 0), 42); |
| 167 | + assert_not_equals(e, jsTagExn); |
| 168 | + } |
| 169 | + }); |
| 170 | +}, "Identity check"); |
0 commit comments