summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHeejin Ahn <aheejin@gmail.com>2021-06-10 23:44:57 -0700
committerGitHub <noreply@github.com>2021-06-10 23:44:57 -0700
commitca263c00ec8ff3b7c51d066b273eeee50180091b (patch)
tree6ee8fa964b5ea8a1dc30cdc1b4e720b6d92d2056
parent83ae39af1f8ffe67856b16f7ee13de066169b48f (diff)
downloadbinaryen-ca263c00ec8ff3b7c51d066b273eeee50180091b.tar.gz
binaryen-ca263c00ec8ff3b7c51d066b273eeee50180091b.tar.bz2
binaryen-ca263c00ec8ff3b7c51d066b273eeee50180091b.zip
[EH] Allow catch/delegate-less trys (#3924)
This removes the restriction that `try` should have at least one `catch`/`catch_all`/`delegate`. See WebAssembly/exception-handling#157.
-rw-r--r--src/wasm/wasm-binary.cpp12
-rw-r--r--src/wasm/wasm-s-parser.cpp3
-rw-r--r--src/wasm/wasm-validator.cpp3
-rw-r--r--test/exception-handling.wast7
-rw-r--r--test/exception-handling.wast.from-wast67
-rw-r--r--test/exception-handling.wast.fromBinary7
-rw-r--r--test/exception-handling.wast.fromBinary.noDebugInfo7
7 files changed, 65 insertions, 41 deletions
diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp
index 84834352b..db253fa6e 100644
--- a/src/wasm/wasm-binary.cpp
+++ b/src/wasm/wasm-binary.cpp
@@ -6080,10 +6080,12 @@ void WasmBinaryBuilder::visitTryOrTryInBlock(Expression*& out) {
// blocks instead.
curr->type = getType();
curr->body = getBlockOrSingleton(curr->type);
- if (lastSeparator != BinaryConsts::Catch &&
- lastSeparator != BinaryConsts::CatchAll &&
- lastSeparator != BinaryConsts::Delegate) {
- throwError("No catch instruction within a try scope");
+
+ // try without catch or delegate
+ if (lastSeparator == BinaryConsts::End) {
+ curr->finalize();
+ out = curr;
+ return;
}
Builder builder(wasm);
@@ -6200,7 +6202,7 @@ void WasmBinaryBuilder::visitTryOrTryInBlock(Expression*& out) {
// (catch $e
// (block ;; Now this can be deleted when writing binary
// ...
- // (br $label0)
+ // (br $label)
// )
// )
// )
diff --git a/src/wasm/wasm-s-parser.cpp b/src/wasm/wasm-s-parser.cpp
index 9c2d028a4..8c0fb9b2d 100644
--- a/src/wasm/wasm-s-parser.cpp
+++ b/src/wasm/wasm-s-parser.cpp
@@ -2470,9 +2470,6 @@ Expression* SExpressionWasmBuilder::makeTry(Element& s) {
throw ParseException(
"there should be at most one catch_all block at the end", s.line, s.col);
}
- if (ret->catchBodies.empty() && !ret->isDelegate()) {
- throw ParseException("no catch bodies or delegate", s.line, s.col);
- }
ret->finalize(type);
diff --git a/src/wasm/wasm-validator.cpp b/src/wasm/wasm-validator.cpp
index 6035a38a9..15b17bf26 100644
--- a/src/wasm/wasm-validator.cpp
+++ b/src/wasm/wasm-validator.cpp
@@ -2078,9 +2078,6 @@ void FunctionValidator::visitTry(Try* curr) {
shouldBeFalse(curr->isCatch() && curr->isDelegate(),
curr,
"try cannot have both catch and delegate at the same time");
- shouldBeTrue(curr->isCatch() || curr->isDelegate(),
- curr,
- "try should have either catches or a delegate");
if (curr->isDelegate()) {
noteDelegate(curr->delegateTarget, curr);
diff --git a/test/exception-handling.wast b/test/exception-handling.wast
index 833010e73..ee5ffbf54 100644
--- a/test/exception-handling.wast
+++ b/test/exception-handling.wast
@@ -131,6 +131,13 @@
)
)
)
+
+ ;; try without catch or delegate
+ (try
+ (do
+ (throw $e-i32 (i32.const 0))
+ )
+ )
)
(func $delegate-test
diff --git a/test/exception-handling.wast.from-wast b/test/exception-handling.wast.from-wast
index 6d997c521..68034653f 100644
--- a/test/exception-handling.wast.from-wast
+++ b/test/exception-handling.wast.from-wast
@@ -170,6 +170,13 @@
)
)
)
+ (try $try10
+ (do
+ (throw $e-i32
+ (i32.const 0)
+ )
+ )
+ )
)
(func $delegate-test
(try $l0
@@ -180,7 +187,7 @@
)
(delegate $l0)
)
- (try $try10
+ (try $try11
(do
(call $foo)
)
@@ -191,24 +198,24 @@
(nop)
)
)
- (block $l014
- (try $l011
+ (block $l015
+ (try $l012
(do
- (try $try12
+ (try $try13
(do
- (br_if $l014
+ (br_if $l015
(i32.const 1)
)
)
- (delegate $l011)
+ (delegate $l012)
)
- (try $try13
+ (try $try14
(do
- (br_if $l014
+ (br_if $l015
(i32.const 1)
)
)
- (delegate $l011)
+ (delegate $l012)
)
)
(catch_all
@@ -216,18 +223,18 @@
)
)
)
- (try $l015
+ (try $l016
(do
- (try $try16
+ (try $try17
(do
(call $foo)
)
- (delegate $l015)
+ (delegate $l016)
)
)
(delegate 0)
)
- (try $try17
+ (try $try18
(do
(nop)
)
@@ -251,8 +258,8 @@
(rethrow $l0)
)
)
- (block $l019
- (try $l018
+ (block $l020
+ (try $l019
(do
(call $foo)
)
@@ -260,14 +267,14 @@
(drop
(pop i32)
)
- (rethrow $l018)
+ (rethrow $l019)
)
(catch_all
- (br $l019)
+ (br $l020)
)
)
)
- (try $l020
+ (try $l021
(do
(call $foo)
)
@@ -280,20 +287,20 @@
(drop
(pop i32)
)
- (rethrow $l020)
+ (rethrow $l021)
)
(catch_all
- (rethrow $l020)
+ (rethrow $l021)
)
)
)
)
- (try $l021
+ (try $l022
(do
(call $foo)
)
(catch_all
- (try $try22
+ (try $try23
(do
(call $foo)
)
@@ -302,25 +309,25 @@
(pop i32)
)
(block $b0
- (rethrow $l021)
+ (rethrow $l022)
)
)
(catch_all
(block $b1
- (rethrow $l021)
+ (rethrow $l022)
)
)
)
)
)
- (try $l023
+ (try $l024
(do
(call $foo)
)
(catch_all
- (try $try24
+ (try $try25
(do
- (rethrow $l023)
+ (rethrow $l024)
)
(catch_all
(nop)
@@ -328,14 +335,14 @@
)
)
)
- (try $l025
+ (try $l026
(do
(call $foo)
)
(catch_all
- (try $try26
+ (try $try27
(do
- (rethrow $l025)
+ (rethrow $l026)
)
(catch_all
(nop)
diff --git a/test/exception-handling.wast.fromBinary b/test/exception-handling.wast.fromBinary
index 851ec0eb4..ff9a71f28 100644
--- a/test/exception-handling.wast.fromBinary
+++ b/test/exception-handling.wast.fromBinary
@@ -195,6 +195,13 @@
)
)
)
+ (try
+ (do
+ (throw $event$0
+ (i32.const 0)
+ )
+ )
+ )
)
(func $delegate-test
(try $label$9
diff --git a/test/exception-handling.wast.fromBinary.noDebugInfo b/test/exception-handling.wast.fromBinary.noDebugInfo
index 3ec4a690c..5a0e3331b 100644
--- a/test/exception-handling.wast.fromBinary.noDebugInfo
+++ b/test/exception-handling.wast.fromBinary.noDebugInfo
@@ -195,6 +195,13 @@
)
)
)
+ (try
+ (do
+ (throw $event$0
+ (i32.const 0)
+ )
+ )
+ )
)
(func $3
(try $label$9