Skip to content

Commit 13725e5

Browse files
authored
Make try body with multiple instructions roundtrip (#2374)
Previously we didn't print an additional block when there are multiple instructions within a `try` body, so those wast files cannot be parsed correctly, because the wast parser assumes there are two bodies within a `try` scope: a try body and a catch body. We don't need to print an additional block for a `catch` body because `(catch ...)` itself serves as a scope.
1 parent d0f538e commit 13725e5

8 files changed

+77
-4
lines changed

src/passes/Print.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1768,7 +1768,7 @@ struct PrintSExpression : public OverriddenVisitor<PrintSExpression> {
17681768
o << '(';
17691769
PrintExpressionContents(currFunction, o).visit(curr);
17701770
incIndent();
1771-
maybePrintImplicitBlock(curr->body, true);
1771+
maybePrintImplicitBlock(curr->body, false);
17721772
doIndent(o, indent);
17731773
o << "(catch";
17741774
incIndent();

src/wasm/wasm-s-parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1792,7 +1792,7 @@ Expression* SExpressionWasmBuilder::makeTry(Element& s) {
17921792
ret->body = parseExpression(*s[i++]);
17931793
}
17941794
if (!elementStartsWith(*s[i], "catch")) {
1795-
throw ParseException("catch clause does not exist");
1795+
throw ParseException("catch clause does not exist", s[i]->line, s[i]->col);
17961796
}
17971797
ret->catchBody = makeCatch(*s[i++]);
17981798
ret->finalize(type);

test/exception-handling.wast

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
(local.get $0)
66
)
77

8+
(func $foo)
9+
(func $bar)
10+
811
(func $eh_test (local $exn exnref)
912
(try
1013
(throw $e0 (i32.const 0))
@@ -36,5 +39,18 @@
3639
(drop (exnref.pop))
3740
)
3841
)
42+
43+
;; Multiple instructions within try and catch bodies
44+
(try
45+
(block
46+
(call $foo)
47+
(call $bar)
48+
)
49+
(catch
50+
(drop (exnref.pop))
51+
(call $foo)
52+
(call $bar)
53+
)
54+
)
3955
)
4056
)

test/exception-handling.wast.from-wast

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
(func $exnref_test (; 0 ;) (type $FUNCSIG$ee) (param $0 exnref) (result exnref)
77
(local.get $0)
88
)
9-
(func $eh_test (; 1 ;) (type $FUNCSIG$v)
9+
(func $foo (; 1 ;) (type $FUNCSIG$v)
10+
(nop)
11+
)
12+
(func $bar (; 2 ;) (type $FUNCSIG$v)
13+
(nop)
14+
)
15+
(func $eh_test (; 3 ;) (type $FUNCSIG$v)
1016
(local $exn exnref)
1117
(try
1218
(throw $e0
@@ -43,5 +49,18 @@
4349
)
4450
)
4551
)
52+
(try
53+
(block $block
54+
(call $foo)
55+
(call $bar)
56+
)
57+
(catch
58+
(drop
59+
(exnref.pop)
60+
)
61+
(call $foo)
62+
(call $bar)
63+
)
64+
)
4665
)
4766
)

test/exception-handling.wast.fromBinary

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
(func $exnref_test (; 0 ;) (type $0) (param $0 exnref) (result exnref)
77
(local.get $0)
88
)
9-
(func $eh_test (; 1 ;) (type $1)
9+
(func $foo (; 1 ;) (type $1)
10+
(nop)
11+
)
12+
(func $bar (; 2 ;) (type $1)
13+
(nop)
14+
)
15+
(func $eh_test (; 3 ;) (type $1)
1016
(local $0 exnref)
1117
(try
1218
(throw $event$0

test/exception-handling.wast.fromBinary.noDebugInfo

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
(local.get $0)
88
)
99
(func $1 (; 1 ;) (type $1)
10+
(nop)
11+
)
12+
(func $2 (; 2 ;) (type $1)
13+
(nop)
14+
)
15+
(func $3 (; 3 ;) (type $1)
1016
(local $0 exnref)
1117
(try
1218
(throw $event$0

test/try-body-multiple-insts.wasm

49 Bytes
Binary file not shown.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
(module
2+
(type $0 (func))
3+
(func $0 (; 0 ;) (type $0)
4+
(nop)
5+
)
6+
(func $1 (; 1 ;) (type $0)
7+
(nop)
8+
)
9+
(func $2 (; 2 ;) (type $0)
10+
(local $0 exnref)
11+
(try
12+
(block
13+
(call $0)
14+
(call $1)
15+
)
16+
(catch
17+
(drop
18+
(exnref.pop)
19+
)
20+
(call $0)
21+
(call $1)
22+
)
23+
)
24+
)
25+
)
26+

0 commit comments

Comments
 (0)