Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Unified Diff: pkg/kernel/lib/interpreter/interpreter.dart

Issue 2987153002: Add support for 'throw' expression (Closed)
Patch Set: Merge Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pkg/kernel/testcases/interpreter/throw_simple_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/kernel/lib/interpreter/interpreter.dart
diff --git a/pkg/kernel/lib/interpreter/interpreter.dart b/pkg/kernel/lib/interpreter/interpreter.dart
index 710d106bac7bbac513fbc35d0ba67d3c8cb610a9..e85045273c3f3b5bca3a90088d0da94fd2157f56 100644
--- a/pkg/kernel/lib/interpreter/interpreter.dart
+++ b/pkg/kernel/lib/interpreter/interpreter.dart
@@ -252,6 +252,13 @@ class Evaluator extends ExpressionVisitor1<Configuration, EvalConfiguration> {
config.continuation, config.environment.thisInstance);
}
+ Configuration visitThrow(Throw node, EvalConfiguration config) {
+ var cont = new ThrowEK(config.handlers);
+
+ return new EvalConfiguration(
+ node.expression, config.environment, config.handlers, cont);
+ }
+
// Evaluation of BasicLiterals.
Configuration visitStringLiteral(
StringLiteral node, EvalConfiguration config) {
@@ -1201,6 +1208,16 @@ class InitializerListEK extends ExpressionContinuation {
}
}
+class ThrowEK extends ExpressionContinuation {
+ final Handlers handlers;
+
+ ThrowEK(this.handlers);
+
+ Configuration call(Value value) {
+ return new ThrowConfiguration(handlers.handler, value, handlers.stackTrace);
+ }
+}
+
// ------------------------------------------------------------------------
// Exceptions Handlers
// ------------------------------------------------------------------------
@@ -1211,6 +1228,22 @@ abstract class ExceptionHandler extends Continuation {
Configuration call(Value exception, StackTrace stacktrace);
}
+/// Handler for showing an exception to the user and returning a halting the
+/// execution of the program when an exception is not handled.
+class MainHandler extends ExceptionHandler {
+ ExceptionHandler get nextHandler =>
+ throw 'The current handler is the main exception handler';
+
+ Configuration call(Value exception, StackTrace stacktrace) {
+ var errorMessage = 'Uncaught exception '
+ '"${exception.value.runtimeType} : ${exception.value}"\n'
+ '${stacktrace.toString()}';
+ log.info(errorMessage);
+ print(errorMessage);
+ return null;
+ }
+}
+
// ------------------------------------------------------------------------
// Exceptions
// ------------------------------------------------------------------------
@@ -1232,10 +1265,9 @@ class Handlers {
Handlers(this.handler, this.stackTrace, this.currentStackTrace,
this.currentException);
- // TODO(zhivkag): Add a top level handler for initial exception state.
Handlers.initial()
- : handler = null,
- stackTrace = null,
+ : handler = new MainHandler(),
+ stackTrace = new StackTrace(null, null),
currentStackTrace = null,
currentException = null;
}
@@ -1245,6 +1277,16 @@ class StackTrace {
final StackTrace stackTrace;
StackTrace(this.expression, this.stackTrace);
+
+ String toString() {
+ var buffer = new StringBuffer('in main()');
+ var current = this;
+ while (current.expression != null) {
+ buffer.write('at ${current.expression.toString()}');
Dmitry Stefantsov 2017/08/03 09:01:36 I think we need a newline at the end of this strin
zhivkag 2017/08/03 10:35:50 Done.
+ current = current.stackTrace;
+ }
+ return buffer.toString();
+ }
}
/// Executes statements.
« no previous file with comments | « no previous file | pkg/kernel/testcases/interpreter/throw_simple_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698