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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « no previous file | pkg/kernel/testcases/interpreter/throw_simple_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 library kernel.interpreter; 4 library kernel.interpreter;
5 5
6 import '../ast.dart'; 6 import '../ast.dart';
7 import '../ast.dart' as ast show Class; 7 import '../ast.dart' as ast show Class;
8 8
9 import '../log.dart'; 9 import '../log.dart';
10 export '../log.dart'; 10 export '../log.dart';
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 return new EvalListConfiguration( 249 return new EvalListConfiguration(
250 expressions, config.environment, config.exceptionComponents, cont); 250 expressions, config.environment, config.exceptionComponents, cont);
251 } 251 }
252 252
253 Configuration visitThisExpression( 253 Configuration visitThisExpression(
254 ThisExpression node, EvalConfiguration config) { 254 ThisExpression node, EvalConfiguration config) {
255 return new ValuePassingConfiguration( 255 return new ValuePassingConfiguration(
256 config.continuation, config.environment.thisInstance); 256 config.continuation, config.environment.thisInstance);
257 } 257 }
258 258
259 Configuration visitThrow(Throw node, EvalConfiguration config) {
260 var cont = new ThrowEK(config.exceptionComponents);
261
262 return new EvalConfiguration(
263 node.expression, config.environment, config.exceptionComponents, cont);
264 }
265
259 // Evaluation of BasicLiterals. 266 // Evaluation of BasicLiterals.
260 Configuration visitStringLiteral( 267 Configuration visitStringLiteral(
261 StringLiteral node, EvalConfiguration config) { 268 StringLiteral node, EvalConfiguration config) {
262 return new ValuePassingConfiguration( 269 return new ValuePassingConfiguration(
263 config.continuation, new StringValue(node.value)); 270 config.continuation, new StringValue(node.value));
264 } 271 }
265 272
266 Configuration visitIntLiteral(IntLiteral node, EvalConfiguration config) { 273 Configuration visitIntLiteral(IntLiteral node, EvalConfiguration config) {
267 return new ValuePassingConfiguration( 274 return new ValuePassingConfiguration(
268 config.continuation, new IntValue(node.value)); 275 config.continuation, new IntValue(node.value));
(...skipping 946 matching lines...) Expand 10 before | Expand all | Expand 10 after
1215 Configuration _createEvalListConfig( 1222 Configuration _createEvalListConfig(
1216 Arguments args, Constructor ctr, Environment env) { 1223 Arguments args, Constructor ctr, Environment env) {
1217 List<InterpreterExpression> exprs = 1224 List<InterpreterExpression> exprs =
1218 _getArgumentExpressions(args, ctr.function); 1225 _getArgumentExpressions(args, ctr.function);
1219 var cont = new ConstructorInitializerA( 1226 var cont = new ConstructorInitializerA(
1220 ctr, location, exceptionComponents, continuation); 1227 ctr, location, exceptionComponents, continuation);
1221 return new EvalListConfiguration(exprs, env, exceptionComponents, cont); 1228 return new EvalListConfiguration(exprs, env, exceptionComponents, cont);
1222 } 1229 }
1223 } 1230 }
1224 1231
1232 class ThrowEK extends ExpressionContinuation {
1233 final ExceptionComponents exceptionComponents;
1234
1235 ThrowEK(this.exceptionComponents);
1236
1237 Configuration call(Value value) {
1238 return new ThrowConfiguration(
1239 exceptionComponents.handler, value, exceptionComponents.stackTrace);
1240 }
1241 }
1242
1225 // ------------------------------------------------------------------------ 1243 // ------------------------------------------------------------------------
1226 // Exceptions Handlers 1244 // Exceptions Handlers
1227 // ------------------------------------------------------------------------ 1245 // ------------------------------------------------------------------------
1228 1246
1229 abstract class ExceptionHandler extends Continuation { 1247 abstract class ExceptionHandler extends Continuation {
1230 ExceptionHandler get nextHandler; 1248 ExceptionHandler get nextHandler;
1231 1249
1232 Configuration call(Value exception, StackTrace stacktrace); 1250 Configuration call(Value exception, StackTrace stacktrace);
1233 } 1251 }
1234 1252
1253 /// Handler for showing an exception to the user and returning a halting the
1254 /// execution of the program when an exception is not handled.
1255 class MainHandler extends ExceptionHandler {
1256 ExceptionHandler get nextHandler =>
1257 throw 'The current handler is the main exception handler';
1258
1259 Configuration call(Value exception, StackTrace stacktrace) {
1260 var errorMessage = 'Uncaught exception '
1261 '"${exception.value.runtimeType} : ${exception.value}"\n'
1262 '${stacktrace.toString()}';
1263 log.info(errorMessage);
1264 print(errorMessage);
1265 return null;
1266 }
1267 }
1268
1235 // ------------------------------------------------------------------------ 1269 // ------------------------------------------------------------------------
1236 // Exceptions 1270 // Exceptions
1237 // ------------------------------------------------------------------------ 1271 // ------------------------------------------------------------------------
1238 /// Represents the components for Exception handling. 1272 /// Represents the components for Exception handling.
1239 /// 1273 ///
1240 /// It contains the list of exception handlers, a stack trace and optional 1274 /// It contains the list of exception handlers, a stack trace and optional
1241 /// components, current stacktrace and exception. 1275 /// components, current stacktrace and exception.
1242 class ExceptionComponents { 1276 class ExceptionComponents {
1243 final ExceptionHandler handler; 1277 final ExceptionHandler handler;
1244 final StackTrace stackTrace; 1278 final StackTrace stackTrace;
1245 1279
1246 /// Current exception and stack trace. 1280 /// Current exception and stack trace.
1247 /// 1281 ///
1248 /// Components enabling support for `rethrow` expressions and set only in 1282 /// Components enabling support for `rethrow` expressions and set only in
1249 /// catch clauses. 1283 /// catch clauses.
1250 final StackTrace currentStackTrace; 1284 final StackTrace currentStackTrace;
1251 final Value currentException; 1285 final Value currentException;
1252 1286
1253 ExceptionComponents(this.handler, this.stackTrace, this.currentStackTrace, 1287 ExceptionComponents(this.handler, this.stackTrace, this.currentStackTrace,
1254 this.currentException); 1288 this.currentException);
1255 1289
1256 // TODO(zhivkag): Add a top level handler for initial exception state.
1257 ExceptionComponents.initial() 1290 ExceptionComponents.initial()
1258 : handler = null, 1291 : handler = new MainHandler(),
1259 stackTrace = null, 1292 stackTrace = new StackTrace(null, null),
1260 currentStackTrace = null, 1293 currentStackTrace = null,
1261 currentException = null; 1294 currentException = null;
1262 } 1295 }
1263 1296
1264 class StackTrace { 1297 class StackTrace {
1265 final Expression expression; 1298 final Expression expression;
1266 final StackTrace stackTrace; 1299 final StackTrace stackTrace;
1267 1300
1268 StackTrace(this.expression, this.stackTrace); 1301 StackTrace(this.expression, this.stackTrace);
1302
1303 String toString() {
1304 var buffer = new StringBuffer('in main()');
1305 var current = this;
1306 while (current.expression != null) {
1307 buffer.write('at ${current.expression.toString()}\n');
1308 current = current.stackTrace;
1309 }
1310 return buffer.toString();
1311 }
1269 } 1312 }
1270 1313
1271 /// Executes statements. 1314 /// Executes statements.
1272 /// 1315 ///
1273 /// Execution of a statement completes in one of the following ways: 1316 /// Execution of a statement completes in one of the following ways:
1274 /// - It completes normally, in which case the execution proceeds to applying 1317 /// - It completes normally, in which case the execution proceeds to applying
1275 /// the next continuation. 1318 /// the next continuation.
1276 /// - It breaks with a label, in which case the corresponding continuation is 1319 /// - It breaks with a label, in which case the corresponding continuation is
1277 /// returned and applied. 1320 /// returned and applied.
1278 /// - It returns with or without value, in which case the return continuation is 1321 /// - It returns with or without value, in which case the return continuation is
(...skipping 405 matching lines...) Expand 10 before | Expand all | Expand 10 after
1684 /// Initializes all non initialized fields from the provided class to 1727 /// Initializes all non initialized fields from the provided class to
1685 /// `Value.nullInstance` in the provided value. 1728 /// `Value.nullInstance` in the provided value.
1686 void _initializeNullFields(Class class_, Value value) { 1729 void _initializeNullFields(Class class_, Value value) {
1687 int startIndex = class_.superclass?.instanceSize ?? 0; 1730 int startIndex = class_.superclass?.instanceSize ?? 0;
1688 for (int i = startIndex; i < class_.instanceSize; i++) { 1731 for (int i = startIndex; i < class_.instanceSize; i++) {
1689 if (value.fields[i].value == null) { 1732 if (value.fields[i].value == null) {
1690 value.fields[i].value = Value.nullInstance; 1733 value.fields[i].value = Value.nullInstance;
1691 } 1734 }
1692 } 1735 }
1693 } 1736 }
OLDNEW
« 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