Skip to content

Commit 07b8e1b

Browse files
committed
[EH] Allow catch/delegate-less trys
This removes the restriction that `try` should have at least one `catch`/`catch_all`/`delegate`. See WebAssembly/exception-handling#157.
1 parent a4ab1c7 commit 07b8e1b

7 files changed

+68
-38
lines changed

src/wasm/wasm-binary.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -6069,10 +6069,18 @@ void WasmBinaryBuilder::visitTryOrTryInBlock(Expression*& out) {
60696069
// blocks instead.
60706070
curr->type = getType();
60716071
curr->body = getBlockOrSingleton(curr->type);
6072+
6073+
// try without catch or delegate
6074+
if (lastSeparator == BinaryConsts::End) {
6075+
curr->finalize();
6076+
out = curr;
6077+
return;
6078+
}
6079+
60726080
if (lastSeparator != BinaryConsts::Catch &&
60736081
lastSeparator != BinaryConsts::CatchAll &&
60746082
lastSeparator != BinaryConsts::Delegate) {
6075-
throwError("No catch instruction within a try scope");
6083+
throwError("try scope should contain only catch/catch_all/delegate");
60766084
}
60776085

60786086
Builder builder(wasm);
@@ -6189,7 +6197,7 @@ void WasmBinaryBuilder::visitTryOrTryInBlock(Expression*& out) {
61896197
// (catch $e
61906198
// (block ;; Now this can be deleted when writing binary
61916199
// ...
6192-
// (br $label0)
6200+
// (br $label)
61936201
// )
61946202
// )
61956203
// )

src/wasm/wasm-s-parser.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -2470,9 +2470,6 @@ Expression* SExpressionWasmBuilder::makeTry(Element& s) {
24702470
throw ParseException(
24712471
"there should be at most one catch_all block at the end", s.line, s.col);
24722472
}
2473-
if (ret->catchBodies.empty() && !ret->isDelegate()) {
2474-
throw ParseException("no catch bodies or delegate", s.line, s.col);
2475-
}
24762473

24772474
ret->finalize(type);
24782475

src/wasm/wasm-validator.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -2078,9 +2078,6 @@ void FunctionValidator::visitTry(Try* curr) {
20782078
shouldBeFalse(curr->isCatch() && curr->isDelegate(),
20792079
curr,
20802080
"try cannot have both catch and delegate at the same time");
2081-
shouldBeTrue(curr->isCatch() || curr->isDelegate(),
2082-
curr,
2083-
"try should have either catches or a delegate");
20842081

20852082
if (curr->isDelegate()) {
20862083
noteDelegate(curr->delegateTarget, curr);

test/exception-handling.wast

+7
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@
131131
)
132132
)
133133
)
134+
135+
;; try without catch or delegate
136+
(try
137+
(do
138+
(throw $e-i32 (i32.const 0))
139+
)
140+
)
134141
)
135142

136143
(func $delegate-test

test/exception-handling.wast.from-wast

+37-30
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,13 @@
170170
)
171171
)
172172
)
173+
(try $try10
174+
(do
175+
(throw $e-i32
176+
(i32.const 0)
177+
)
178+
)
179+
)
173180
)
174181
(func $delegate-test
175182
(try $l0
@@ -180,7 +187,7 @@
180187
)
181188
(delegate $l0)
182189
)
183-
(try $try10
190+
(try $try11
184191
(do
185192
(call $foo)
186193
)
@@ -191,43 +198,43 @@
191198
(nop)
192199
)
193200
)
194-
(block $l014
195-
(try $l011
201+
(block $l015
202+
(try $l012
196203
(do
197-
(try $try12
204+
(try $try13
198205
(do
199-
(br_if $l014
206+
(br_if $l015
200207
(i32.const 1)
201208
)
202209
)
203-
(delegate $l011)
210+
(delegate $l012)
204211
)
205-
(try $try13
212+
(try $try14
206213
(do
207-
(br_if $l014
214+
(br_if $l015
208215
(i32.const 1)
209216
)
210217
)
211-
(delegate $l011)
218+
(delegate $l012)
212219
)
213220
)
214221
(catch_all
215222
(nop)
216223
)
217224
)
218225
)
219-
(try $l015
226+
(try $l016
220227
(do
221-
(try $try16
228+
(try $try17
222229
(do
223230
(call $foo)
224231
)
225-
(delegate $l015)
232+
(delegate $l016)
226233
)
227234
)
228235
(delegate 0)
229236
)
230-
(try $try17
237+
(try $try18
231238
(do
232239
(nop)
233240
)
@@ -251,23 +258,23 @@
251258
(rethrow $l0)
252259
)
253260
)
254-
(block $l019
255-
(try $l018
261+
(block $l020
262+
(try $l019
256263
(do
257264
(call $foo)
258265
)
259266
(catch $e-i32
260267
(drop
261268
(pop i32)
262269
)
263-
(rethrow $l018)
270+
(rethrow $l019)
264271
)
265272
(catch_all
266-
(br $l019)
273+
(br $l020)
267274
)
268275
)
269276
)
270-
(try $l020
277+
(try $l021
271278
(do
272279
(call $foo)
273280
)
@@ -280,20 +287,20 @@
280287
(drop
281288
(pop i32)
282289
)
283-
(rethrow $l020)
290+
(rethrow $l021)
284291
)
285292
(catch_all
286-
(rethrow $l020)
293+
(rethrow $l021)
287294
)
288295
)
289296
)
290297
)
291-
(try $l021
298+
(try $l022
292299
(do
293300
(call $foo)
294301
)
295302
(catch_all
296-
(try $try22
303+
(try $try23
297304
(do
298305
(call $foo)
299306
)
@@ -302,40 +309,40 @@
302309
(pop i32)
303310
)
304311
(block $b0
305-
(rethrow $l021)
312+
(rethrow $l022)
306313
)
307314
)
308315
(catch_all
309316
(block $b1
310-
(rethrow $l021)
317+
(rethrow $l022)
311318
)
312319
)
313320
)
314321
)
315322
)
316-
(try $l023
323+
(try $l024
317324
(do
318325
(call $foo)
319326
)
320327
(catch_all
321-
(try $try24
328+
(try $try25
322329
(do
323-
(rethrow $l023)
330+
(rethrow $l024)
324331
)
325332
(catch_all
326333
(nop)
327334
)
328335
)
329336
)
330337
)
331-
(try $l025
338+
(try $l026
332339
(do
333340
(call $foo)
334341
)
335342
(catch_all
336-
(try $try26
343+
(try $try27
337344
(do
338-
(rethrow $l025)
345+
(rethrow $l026)
339346
)
340347
(catch_all
341348
(nop)

test/exception-handling.wast.fromBinary

+7
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,13 @@
195195
)
196196
)
197197
)
198+
(try
199+
(do
200+
(throw $event$0
201+
(i32.const 0)
202+
)
203+
)
204+
)
198205
)
199206
(func $delegate-test
200207
(try $label$9

test/exception-handling.wast.fromBinary.noDebugInfo

+7
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,13 @@
195195
)
196196
)
197197
)
198+
(try
199+
(do
200+
(throw $event$0
201+
(i32.const 0)
202+
)
203+
)
204+
)
198205
)
199206
(func $3
200207
(try $label$9

0 commit comments

Comments
 (0)