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

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

Issue 2987153002: Add support for 'throw' expression (Closed)
Patch Set: Apply comment 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 1e35f21c01145f06d989e57fb819514af26bada4..d31d5d154aaa6d99a45aa92e7b6804a1fd64a2bb 100644
--- a/pkg/kernel/lib/interpreter/interpreter.dart
+++ b/pkg/kernel/lib/interpreter/interpreter.dart
@@ -256,6 +256,13 @@ class Evaluator extends ExpressionVisitor1<Configuration, EvalConfiguration> {
config.continuation, config.environment.thisInstance);
}
+ Configuration visitThrow(Throw node, EvalConfiguration config) {
+ var cont = new ThrowEK(config.exceptionComponents);
+
+ return new EvalConfiguration(
+ node.expression, config.environment, config.exceptionComponents, cont);
+ }
+
// Evaluation of BasicLiterals.
Configuration visitStringLiteral(
StringLiteral node, EvalConfiguration config) {
@@ -1222,6 +1229,17 @@ class InitializerListEK extends ExpressionContinuation {
}
}
+class ThrowEK extends ExpressionContinuation {
+ final ExceptionComponents exceptionComponents;
+
+ ThrowEK(this.exceptionComponents);
+
+ Configuration call(Value value) {
+ return new ThrowConfiguration(
+ exceptionComponents.handler, value, exceptionComponents.stackTrace);
+ }
+}
+
// ------------------------------------------------------------------------
// Exceptions Handlers
// ------------------------------------------------------------------------
@@ -1232,6 +1250,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
// ------------------------------------------------------------------------
@@ -1253,10 +1287,9 @@ class ExceptionComponents {
ExceptionComponents(this.handler, this.stackTrace, this.currentStackTrace,
this.currentException);
- // TODO(zhivkag): Add a top level handler for initial exception state.
ExceptionComponents.initial()
- : handler = null,
- stackTrace = null,
+ : handler = new MainHandler(),
+ stackTrace = new StackTrace(null, null),
currentStackTrace = null,
currentException = null;
}
@@ -1266,6 +1299,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()}\n');
+ 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