@@ -13,9 +13,19 @@ test(() => {
13
13
const jsTagExnDiffPayload = new WebAssembly . Exception ( jsTag , [ 53 ] ) ;
14
14
const throwJSTagExnIndex = builder . addImport ( "module" , "throwJSTagExn" , kSig_v_v ) ;
15
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
+
16
25
const imports = {
17
26
module : {
18
27
throwJSTagExn : function ( ) { throw jsTagExn ; } ,
28
+ throwWasmTagExn : function ( ) { throw wasmTagExn ; } ,
19
29
jsTag : jsTag
20
30
}
21
31
} ;
@@ -34,6 +44,20 @@ test(() => {
34
44
] )
35
45
. exportFunc ( ) ;
36
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 , kWasmStmt ,
53
+ kExprCallFunction , throwWasmTagExnIndex ,
54
+ kExprCatch , wasmTagIndex ,
55
+ kExprDrop ,
56
+ kExprRethrow , 0x00 ,
57
+ kExprEnd
58
+ ] )
59
+ . exportFunc ( ) ;
60
+
37
61
// Call a JS function that throws an exception using a JS-defined tag, catches
38
62
// it with a 'catch_all' instruction, and rethrows it.
39
63
builder
@@ -47,11 +71,24 @@ test(() => {
47
71
] )
48
72
. exportFunc ( ) ;
49
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 , kWasmStmt ,
80
+ kExprCallFunction , throwWasmTagExnIndex ,
81
+ kExprCatchAll ,
82
+ kExprRethrow , 0x00 ,
83
+ kExprEnd
84
+ ] )
85
+ . exportFunc ( ) ;
86
+
50
87
const buffer = builder . toBuffer ( ) ;
51
88
52
- // The exception object's identity should be preserved across 'rethrow's in
53
- // Wasm code. Do tests with a tag defined in JS.
54
89
WebAssembly . instantiate ( buffer , imports ) . then ( result => {
90
+ // The exception object's identity should be preserved across 'rethrow's in
91
+ // Wasm code. Do tests with a tag defined in JS.
55
92
try {
56
93
result . instance . exports . catch_js_tag_rethrow ( ) ;
57
94
} catch ( e ) {
@@ -68,5 +105,25 @@ test(() => {
68
105
assert_not_equals ( e , jsTagExnSamePayload ) ;
69
106
assert_not_equals ( e , jsTagExnDiffPayload ) ;
70
107
}
108
+
109
+ // Do the same tests with a tag defined in Wasm.
110
+ const wasmTag = result . instance . exports . wasmTag ;
111
+ wasmTagExn = new WebAssembly . Exception ( wasmTag , [ 42 ] ) ;
112
+ wasmTagExnSamePayload = new WebAssembly . Exception ( wasmTag , [ 42 ] ) ;
113
+ wasmTagExnDiffPayload = new WebAssembly . Exception ( wasmTag , [ 53 ] ) ;
114
+ try {
115
+ result . instance . exports . catch_wasm_tag_rethrow ( ) ;
116
+ } catch ( e ) {
117
+ assert_equals ( e , wasmTagExn ) ;
118
+ assert_not_equals ( e , wasmTagExnSamePayload ) ;
119
+ assert_not_equals ( e , wasmTagExnDiffPayload ) ;
120
+ }
121
+ try {
122
+ result . instance . exports . catch_all_wasm_tag_rethrow ( ) ;
123
+ } catch ( e ) {
124
+ assert_equals ( e , wasmTagExn ) ;
125
+ assert_not_equals ( e , wasmTagExnSamePayload ) ;
126
+ assert_not_equals ( e , wasmTagExnDiffPayload ) ;
127
+ }
71
128
} ) ;
72
129
} , "Identity check" ) ;
0 commit comments