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

Side by Side 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 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 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 return new EvalListConfiguration( 245 return new EvalListConfiguration(
246 expressions, config.environment, config.handlers, cont); 246 expressions, config.environment, config.handlers, cont);
247 } 247 }
248 248
249 Configuration visitThisExpression( 249 Configuration visitThisExpression(
250 ThisExpression node, EvalConfiguration config) { 250 ThisExpression node, EvalConfiguration config) {
251 return new ValuePassingConfiguration( 251 return new ValuePassingConfiguration(
252 config.continuation, config.environment.thisInstance); 252 config.continuation, config.environment.thisInstance);
253 } 253 }
254 254
255 Configuration visitThrow(Throw node, EvalConfiguration config) {
256 var cont = new ThrowEK(config.handlers);
257
258 return new EvalConfiguration(
259 node.expression, config.environment, config.handlers, cont);
260 }
261
255 // Evaluation of BasicLiterals. 262 // Evaluation of BasicLiterals.
256 Configuration visitStringLiteral( 263 Configuration visitStringLiteral(
257 StringLiteral node, EvalConfiguration config) { 264 StringLiteral node, EvalConfiguration config) {
258 return new ValuePassingConfiguration( 265 return new ValuePassingConfiguration(
259 config.continuation, new StringValue(node.value)); 266 config.continuation, new StringValue(node.value));
260 } 267 }
261 268
262 Configuration visitIntLiteral(IntLiteral node, EvalConfiguration config) { 269 Configuration visitIntLiteral(IntLiteral node, EvalConfiguration config) {
263 return new ValuePassingConfiguration( 270 return new ValuePassingConfiguration(
264 config.continuation, new IntValue(node.value)); 271 config.continuation, new IntValue(node.value));
(...skipping 929 matching lines...) Expand 10 before | Expand all | Expand 10 after
1194 Configuration _createEvalListConfig( 1201 Configuration _createEvalListConfig(
1195 Arguments args, Constructor ctr, Environment env) { 1202 Arguments args, Constructor ctr, Environment env) {
1196 List<InterpreterExpression> exprs = 1203 List<InterpreterExpression> exprs =
1197 _getArgumentExpressions(args, ctr.function); 1204 _getArgumentExpressions(args, ctr.function);
1198 var cont = 1205 var cont =
1199 new ConstructorInitializerA(ctr, location, handlers, continuation); 1206 new ConstructorInitializerA(ctr, location, handlers, continuation);
1200 return new EvalListConfiguration(exprs, env, handlers, cont); 1207 return new EvalListConfiguration(exprs, env, handlers, cont);
1201 } 1208 }
1202 } 1209 }
1203 1210
1211 class ThrowEK extends ExpressionContinuation {
1212 final Handlers handlers;
1213
1214 ThrowEK(this.handlers);
1215
1216 Configuration call(Value value) {
1217 return new ThrowConfiguration(handlers.handler, value, handlers.stackTrace);
1218 }
1219 }
1220
1204 // ------------------------------------------------------------------------ 1221 // ------------------------------------------------------------------------
1205 // Exceptions Handlers 1222 // Exceptions Handlers
1206 // ------------------------------------------------------------------------ 1223 // ------------------------------------------------------------------------
1207 1224
1208 abstract class ExceptionHandler extends Continuation { 1225 abstract class ExceptionHandler extends Continuation {
1209 ExceptionHandler get nextHandler; 1226 ExceptionHandler get nextHandler;
1210 1227
1211 Configuration call(Value exception, StackTrace stacktrace); 1228 Configuration call(Value exception, StackTrace stacktrace);
1212 } 1229 }
1213 1230
1231 /// Handler for showing an exception to the user and returning a halting the
1232 /// execution of the program when an exception is not handled.
1233 class MainHandler extends ExceptionHandler {
1234 ExceptionHandler get nextHandler =>
1235 throw 'The current handler is the main exception handler';
1236
1237 Configuration call(Value exception, StackTrace stacktrace) {
1238 var errorMessage = 'Uncaught exception '
1239 '"${exception.value.runtimeType} : ${exception.value}"\n'
1240 '${stacktrace.toString()}';
1241 log.info(errorMessage);
1242 print(errorMessage);
1243 return null;
1244 }
1245 }
1246
1214 // ------------------------------------------------------------------------ 1247 // ------------------------------------------------------------------------
1215 // Exceptions 1248 // Exceptions
1216 // ------------------------------------------------------------------------ 1249 // ------------------------------------------------------------------------
1217 /// Represents the components for Exception handling. 1250 /// Represents the components for Exception handling.
1218 /// 1251 ///
1219 /// It contains the list of exception handlers, a stack trace and optional 1252 /// It contains the list of exception handlers, a stack trace and optional
1220 /// components, current stacktrace and exception. 1253 /// components, current stacktrace and exception.
1221 class Handlers { 1254 class Handlers {
1222 final ExceptionHandler handler; 1255 final ExceptionHandler handler;
1223 final StackTrace stackTrace; 1256 final StackTrace stackTrace;
1224 1257
1225 /// Current exception and stack trace. 1258 /// Current exception and stack trace.
1226 /// 1259 ///
1227 /// Components enabling support for `rethrow` expressions and set only in 1260 /// Components enabling support for `rethrow` expressions and set only in
1228 /// catch clauses. 1261 /// catch clauses.
1229 final StackTrace currentStackTrace; 1262 final StackTrace currentStackTrace;
1230 final Value currentException; 1263 final Value currentException;
1231 1264
1232 Handlers(this.handler, this.stackTrace, this.currentStackTrace, 1265 Handlers(this.handler, this.stackTrace, this.currentStackTrace,
1233 this.currentException); 1266 this.currentException);
1234 1267
1235 // TODO(zhivkag): Add a top level handler for initial exception state.
1236 Handlers.initial() 1268 Handlers.initial()
1237 : handler = null, 1269 : handler = new MainHandler(),
1238 stackTrace = null, 1270 stackTrace = new StackTrace(null, null),
1239 currentStackTrace = null, 1271 currentStackTrace = null,
1240 currentException = null; 1272 currentException = null;
1241 } 1273 }
1242 1274
1243 class StackTrace { 1275 class StackTrace {
1244 final Expression expression; 1276 final Expression expression;
1245 final StackTrace stackTrace; 1277 final StackTrace stackTrace;
1246 1278
1247 StackTrace(this.expression, this.stackTrace); 1279 StackTrace(this.expression, this.stackTrace);
1280
1281 String toString() {
1282 var buffer = new StringBuffer('in main()');
1283 var current = this;
1284 while (current.expression != null) {
1285 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.
1286 current = current.stackTrace;
1287 }
1288 return buffer.toString();
1289 }
1248 } 1290 }
1249 1291
1250 /// Executes statements. 1292 /// Executes statements.
1251 /// 1293 ///
1252 /// Execution of a statement completes in one of the following ways: 1294 /// Execution of a statement completes in one of the following ways:
1253 /// - It completes normally, in which case the execution proceeds to applying 1295 /// - It completes normally, in which case the execution proceeds to applying
1254 /// the next continuation. 1296 /// the next continuation.
1255 /// - It breaks with a label, in which case the corresponding continuation is 1297 /// - It breaks with a label, in which case the corresponding continuation is
1256 /// returned and applied. 1298 /// returned and applied.
1257 /// - It returns with or without value, in which case the return continuation is 1299 /// - 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
1663 /// Initializes all non initialized fields from the provided class to 1705 /// Initializes all non initialized fields from the provided class to
1664 /// `Value.nullInstance` in the provided value. 1706 /// `Value.nullInstance` in the provided value.
1665 void _initializeNullFields(Class class_, Value value) { 1707 void _initializeNullFields(Class class_, Value value) {
1666 int startIndex = class_.superclass?.instanceSize ?? 0; 1708 int startIndex = class_.superclass?.instanceSize ?? 0;
1667 for (int i = startIndex; i < class_.instanceSize; i++) { 1709 for (int i = startIndex; i < class_.instanceSize; i++) {
1668 if (value.fields[i].value == null) { 1710 if (value.fields[i].value == null) {
1669 value.fields[i].value = Value.nullInstance; 1711 value.fields[i].value = Value.nullInstance;
1670 } 1712 }
1671 } 1713 }
1672 } 1714 }
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