Skip to content

Commit 984382b

Browse files
committed
Add support for catch statements.
BUG= [email protected], [email protected] Review-Url: https://codereview.chromium.org/3002003002 .
1 parent 76a0050 commit 984382b

File tree

3 files changed

+61
-12
lines changed

3 files changed

+61
-12
lines changed

pkg/kernel/lib/interpreter/interpreter.dart

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ class NotImplemented {
1818
}
1919

2020
class Interpreter {
21-
Program program;
22-
StatementExecuter visitor = new StatementExecuter();
23-
2421
// The execution of the program starts with empty main environment.
2522
static MainEnvironment mainEnvironment =
2623
new MainEnvironment(<Member, Location>{});
2724

25+
final Program program;
26+
final StatementExecuter visitor = new StatementExecuter();
27+
2828
Interpreter(this.program);
2929

3030
void run() {
@@ -1577,9 +1577,36 @@ class CatchHandler extends ExceptionHandler {
15771577
CatchHandler(this.catches, this.environment, this.state);
15781578

15791579
Configuration call(Value exception, StackTrace stackTrace) {
1580+
if (catches.isEmpty) {
1581+
return new ThrowConfiguration(
1582+
state.exceptionComponents.handler, exception, stackTrace);
1583+
}
1584+
15801585
// TODO(zhivkag): Check if there is a matching catch clause instead.
1581-
return new ThrowConfiguration(
1582-
state.exceptionComponents.handler, exception, stackTrace);
1586+
var currentCatch = catches.first;
1587+
if (currentCatch.guard is DynamicType) {
1588+
// Exception is caught, execute the body.
1589+
1590+
var env = environment;
1591+
if (currentCatch.exception != null) {
1592+
env = env.extend(currentCatch.exception, exception);
1593+
}
1594+
if (currentCatch.stackTrace != null) {
1595+
env = env.extend(
1596+
currentCatch.stackTrace, new StringValue(stackTrace.toString()));
1597+
}
1598+
var ecs = new ExceptionComponents(state.exceptionComponents.handler,
1599+
state.exceptionComponents.stackTrace, stackTrace, exception);
1600+
var newState = new State.initial()
1601+
.withContinuation(state.continuation)
1602+
.withReturnContinuation(state.returnContinuation)
1603+
.withException(ecs);
1604+
return new ExecConfiguration(currentCatch.body, env, newState);
1605+
}
1606+
1607+
var catchHandler =
1608+
new CatchHandler(catches.skip(1).toList(), environment, state);
1609+
return new ThrowConfiguration(catchHandler, exception, stackTrace);
15831610
}
15841611
}
15851612

@@ -1661,7 +1688,7 @@ class StackTrace {
16611688
/// - It throws, in which case the handler is returned and applied accordingly.
16621689
class StatementExecuter
16631690
extends StatementVisitor1<Configuration, ExecConfiguration> {
1664-
Evaluator evaluator = new Evaluator();
1691+
final Evaluator evaluator = new Evaluator();
16651692

16661693
void trampolinedExecution(Configuration configuration) {
16671694
while (configuration != null) {

pkg/kernel/testcases/interpreter/try_catch_finally_test.dart

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ main() {
99
print('hello2');
1010
}
1111
print('hello3');
12-
print(foo());
13-
print(bar());
12+
print(fReturns());
13+
print(fFinalizes());
14+
print(fThrows());
1415
}
1516

16-
int foo() {
17+
/// Tests that the return in finally is executed.
18+
int fReturns() {
1719
try {
1820
print('foo 1');
1921
return 1;
@@ -23,7 +25,8 @@ int foo() {
2325
}
2426
}
2527

26-
int bar() {
28+
/// Tests that finally is executed before returning.
29+
int fFinalizes() {
2730
try {
2831
print('bar 1');
2932
return 1;
@@ -32,3 +35,16 @@ int bar() {
3235
}
3336
return 0;
3437
}
38+
39+
/// Tests that the exception is caught.
40+
int fThrows() {
41+
try {
42+
print(37);
43+
throw 'Error';
44+
} catch (e, _) {
45+
print('Caught $e');
46+
} finally {
47+
print("Finalizer");
48+
}
49+
return 34;
50+
}
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
print(String: hello1)
22
print(String: hello2)
33
print(String: hello3)
4-
static-invocation-foo
4+
static-invocation-fReturns
55
print(String: foo 1)
66
return
77
print(String: foo 2)
88
return
99
print(int: 2)
10-
static-invocation-bar
10+
static-invocation-fFinalizes
1111
print(String: bar 1)
1212
return
1313
print(String: bar 2)
1414
print(int: 1)
15+
static-invocation-fThrows
16+
print(int: 37)
17+
print(String: Caught Error)
18+
print(String: Finalizer)
19+
return
20+
print(int: 34)

0 commit comments

Comments
 (0)