From f1bdc73a74caecdf63e13dad9103a2c2bc3c64bb Mon Sep 17 00:00:00 2001 From: Heejin Ahn Date: Thu, 16 Feb 2023 17:56:39 -0800 Subject: [PATCH 1/5] [js-api] Tidy up rethrow identity test This makes some cosmetic changes to the identity-preserving `rethrow` test added in #253. This is to prepare for adding the same set of tests for a Wasm-defined-and-exported tag, as requrested in https://github.com/WebAssembly/exception-handling/pull/253#issuecomment-1433703216. This also contains a one-liner drive-by fix for a variable name in `wasm-module-builder.js`. --- .../exception/identity.tentative.any.js | 53 +++++++++++-------- test/js-api/wasm-module-builder.js | 4 +- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/test/js-api/exception/identity.tentative.any.js b/test/js-api/exception/identity.tentative.any.js index 65787c10..3e688e10 100644 --- a/test/js-api/exception/identity.tentative.any.js +++ b/test/js-api/exception/identity.tentative.any.js @@ -3,27 +3,29 @@ // META: script=/wasm/jsapi/wasm-module-builder.js test(() => { - const tag = new WebAssembly.Tag({ parameters: ["i32"] }); - const exn = new WebAssembly.Exception(tag, [42]); - const exn_same_payload = new WebAssembly.Exception(tag, [42]); - const exn_diff_payload = new WebAssembly.Exception(tag, [53]); - const builder = new WasmModuleBuilder(); - const jsfuncIndex = builder.addImport("module", "jsfunc", kSig_v_v); - const tagIndex = builder.addImportedTag("module", "tag", kSig_v_i); + + // Tag defined in JavaScript and imported into Wasm + const jsTag = new WebAssembly.Tag({ parameters: ["i32"] }); + const jsTagIndex = builder.addImportedTag("module", "jsTag", kSig_v_i) + const jsTagExn = new WebAssembly.Exception(jsTag, [42]); + const jsTagExnSamePayload = new WebAssembly.Exception(jsTag, [42]); + const jsTagExnDiffPayload = new WebAssembly.Exception(jsTag, [53]); + const throwJSTagExnIndex = builder.addImport("module", "throwJSTagExn", kSig_v_v); + const imports = { module: { - jsfunc: function() { throw exn; }, - tag: tag + throwJSTagExn: function() { throw jsTagExn; }, + jsTag: jsTag } }; builder - .addFunction("catch_rethrow", kSig_v_v) + .addFunction("catch_js_tag_rethrow", kSig_v_v) .addBody([ kExprTry, kWasmStmt, - kExprCallFunction, jsfuncIndex, - kExprCatch, tagIndex, + kExprCallFunction, throwJSTagExnIndex, + kExprCatch, jsTagIndex, kExprDrop, kExprRethrow, 0x00, kExprEnd @@ -31,10 +33,10 @@ test(() => { .exportFunc(); builder - .addFunction("catch_all_rethrow", kSig_v_v) + .addFunction("catch_all_js_tag_rethrow", kSig_v_v) .addBody([ kExprTry, kWasmStmt, - kExprCallFunction, jsfuncIndex, + kExprCallFunction, throwJSTagExnIndex, kExprCatchAll, kExprRethrow, 0x00, kExprEnd @@ -42,20 +44,27 @@ test(() => { .exportFunc(); const buffer = builder.toBuffer(); + + // The exception object's identity should be preserved throw 'rethrow's in + // Wasm code. Do tests with a tag defined in JS. WebAssembly.instantiate(buffer, imports).then(result => { try { - result.instance.exports.catch_rethrow(); + result.instance.exports.catch_js_tag_rethrow(); } catch (e) { - assert_equals(e, exn); - assert_not_equals(e, exn_same_payload); - assert_not_equals(e, exn_diff_payload); + assert_equals(e, jsTagExn); + assert_equals(e.getArg(jsTag, 0), jsTagExn.getArg(jsTag, 0)); + // Even if they have the same payload, they are different objects, so they + // shouldn't compare equal. + assert_not_equals(e, jsTagExnSamePayload); + assert_not_equals(e, jsTagExnDiffPayload); } try { - result.instance.exports.catch_all_rethrow(); + result.instance.exports.catch_all_js_tag_rethrow(); } catch (e) { - assert_equals(e, exn); - assert_not_equals(e, exn_same_payload); - assert_not_equals(e, exn_diff_payload); + assert_equals(e, jsTagExn); + assert_equals(e.getArg(jsTag, 0), jsTagExn.getArg(jsTag, 0)); + assert_not_equals(e, jsTagExnSamePayload); + assert_not_equals(e, jsTagExnDiffPayload); } }); }, "Identity check"); diff --git a/test/js-api/wasm-module-builder.js b/test/js-api/wasm-module-builder.js index 86d836a5..d0f9e78b 100644 --- a/test/js-api/wasm-module-builder.js +++ b/test/js-api/wasm-module-builder.js @@ -754,9 +754,9 @@ class WasmModuleBuilder { addTag(type) { let type_index = (typeof type) == "number" ? type : this.addType(type); - let except_index = this.tags.length + this.num_imported_tags; + let tag_index = this.tags.length + this.num_imported_tags; this.tags.push(type_index); - return except_index; + return tag_index; } addFunction(name, type) { From ffd991888ecd6d897d0b2184ad8376f947c5f351 Mon Sep 17 00:00:00 2001 From: Heejin Ahn Date: Fri, 17 Feb 2023 12:00:35 -0800 Subject: [PATCH 2/5] Update test/js-api/exception/identity.tentative.any.js Co-authored-by: Derek Schuff --- test/js-api/exception/identity.tentative.any.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/js-api/exception/identity.tentative.any.js b/test/js-api/exception/identity.tentative.any.js index 3e688e10..630e9280 100644 --- a/test/js-api/exception/identity.tentative.any.js +++ b/test/js-api/exception/identity.tentative.any.js @@ -45,7 +45,7 @@ test(() => { const buffer = builder.toBuffer(); - // The exception object's identity should be preserved throw 'rethrow's in + // The exception object's identity should be preserved across 'rethrow's in // Wasm code. Do tests with a tag defined in JS. WebAssembly.instantiate(buffer, imports).then(result => { try { From c055b0179a10ea66cdcb453795e2be46be61eecb Mon Sep 17 00:00:00 2001 From: Heejin Ahn Date: Fri, 17 Feb 2023 12:31:12 -0800 Subject: [PATCH 3/5] Comments on functions --- test/js-api/exception/identity.tentative.any.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/js-api/exception/identity.tentative.any.js b/test/js-api/exception/identity.tentative.any.js index 630e9280..849f975e 100644 --- a/test/js-api/exception/identity.tentative.any.js +++ b/test/js-api/exception/identity.tentative.any.js @@ -20,6 +20,8 @@ test(() => { } }; + // Call a JS function throws an exception using a JS-defined tag, catch it + // with a 'catch' instruction, and rethrows it. builder .addFunction("catch_js_tag_rethrow", kSig_v_v) .addBody([ @@ -32,6 +34,8 @@ test(() => { ]) .exportFunc(); + // Call a JS function throws an exception using a JS-defined tag, catch it + // with a 'catch_all' instruction, and rethrows it. builder .addFunction("catch_all_js_tag_rethrow", kSig_v_v) .addBody([ From 968c69e5b944ccf48b6c76a73cddb64871d336ab Mon Sep 17 00:00:00 2001 From: Heejin Ahn Date: Fri, 17 Feb 2023 12:32:31 -0800 Subject: [PATCH 4/5] Typo --- test/js-api/exception/identity.tentative.any.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/js-api/exception/identity.tentative.any.js b/test/js-api/exception/identity.tentative.any.js index 849f975e..65e95796 100644 --- a/test/js-api/exception/identity.tentative.any.js +++ b/test/js-api/exception/identity.tentative.any.js @@ -20,8 +20,8 @@ test(() => { } }; - // Call a JS function throws an exception using a JS-defined tag, catch it - // with a 'catch' instruction, and rethrows it. + // Call a JS function that throws an exception using a JS-defined tag, catches + // it with a 'catch' instruction, and rethrows it. builder .addFunction("catch_js_tag_rethrow", kSig_v_v) .addBody([ @@ -34,8 +34,8 @@ test(() => { ]) .exportFunc(); - // Call a JS function throws an exception using a JS-defined tag, catch it - // with a 'catch_all' instruction, and rethrows it. + // Call a JS function that throws an exception using a JS-defined tag, catches + // it with a 'catch_all' instruction, and rethrows it. builder .addFunction("catch_all_js_tag_rethrow", kSig_v_v) .addBody([ From 5e866d241f1616676502328c51b4040b7482ddfd Mon Sep 17 00:00:00 2001 From: Heejin Ahn Date: Fri, 17 Feb 2023 12:44:34 -0800 Subject: [PATCH 5/5] Delete payload comparison --- test/js-api/exception/identity.tentative.any.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/js-api/exception/identity.tentative.any.js b/test/js-api/exception/identity.tentative.any.js index 65e95796..3854b014 100644 --- a/test/js-api/exception/identity.tentative.any.js +++ b/test/js-api/exception/identity.tentative.any.js @@ -56,7 +56,6 @@ test(() => { result.instance.exports.catch_js_tag_rethrow(); } catch (e) { assert_equals(e, jsTagExn); - assert_equals(e.getArg(jsTag, 0), jsTagExn.getArg(jsTag, 0)); // Even if they have the same payload, they are different objects, so they // shouldn't compare equal. assert_not_equals(e, jsTagExnSamePayload); @@ -66,7 +65,6 @@ test(() => { result.instance.exports.catch_all_js_tag_rethrow(); } catch (e) { assert_equals(e, jsTagExn); - assert_equals(e.getArg(jsTag, 0), jsTagExn.getArg(jsTag, 0)); assert_not_equals(e, jsTagExnSamePayload); assert_not_equals(e, jsTagExnDiffPayload); }