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

Side by Side Diff: pkg/kernel/lib/interpreter/interpreter.dart

Issue 2985883002: Add support for field initialization in objects (Closed)
Patch Set: Fix too long line 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/test/interpreter/interpreter.status » ('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 652 matching lines...) Expand 10 before | Expand all | Expand 10 after
663 constructor.function, argValues); 663 constructor.function, argValues);
664 664
665 var newObject = new ObjectValue(constructor.enclosingClass); 665 var newObject = new ObjectValue(constructor.enclosingClass);
666 var cont = new InitializationEK( 666 var cont = new InitializationEK(
667 constructor, ctrEnv, new NewSK(continuation, new Location(newObject))); 667 constructor, ctrEnv, new NewSK(continuation, new Location(newObject)));
668 668
669 return new ValuePassingConfiguration(cont, newObject); 669 return new ValuePassingConfiguration(cont, newObject);
670 } 670 }
671 } 671 }
672 672
673 /// Represents the application continuation applied on the list of evaluated
674 /// field initializer expressions.
675 class InstanceFieldsA extends ApplicationContinuation {
676 final Constructor constructor;
677 final Location location;
678 final Environment environment;
679 final ConstructorBodySK continuation;
680
681 final Class _currentClass;
682
683 InstanceFieldsA(
684 this.constructor, this.location, this.environment, this.continuation)
685 : _currentClass = new Class(constructor.enclosingClass.reference);
686
687 Configuration call(List<InterpreterValue> fieldValues) {
688 for (FieldInitializerValue f in fieldValues) {
689 // Directly set the field with the corresponding implicit setter.
690 _currentClass.implicitSetters[f.field.name](location.value, f.value);
691 }
692
693 // TODO(zhivkag): Execute constructor initializer list before initializing
694 // fields in immediately enclosing class to null.
695 _initializeNullFields(_currentClass, location.value);
696 return new ForwardConfiguration(continuation, environment);
697 }
698 }
699
673 // ------------------------------------------------------------------------ 700 // ------------------------------------------------------------------------
674 // Expression Continuations 701 // Expression Continuations
675 // ------------------------------------------------------------------------ 702 // ------------------------------------------------------------------------
676 703
677 /// Represents an expression continuation. 704 /// Represents an expression continuation.
678 /// 705 ///
679 /// There are various kinds of [ExpressionContinuation]s and their names are 706 /// There are various kinds of [ExpressionContinuation]s and their names are
680 /// suffixed with "EK". 707 /// suffixed with "EK".
681 abstract class ExpressionContinuation extends Continuation { 708 abstract class ExpressionContinuation extends Continuation {
682 Configuration call(Value v); 709 Configuration call(Value v);
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after
953 980
954 InitializationEK(this.constructor, this.environment, this.continuation); 981 InitializationEK(this.constructor, this.environment, this.continuation);
955 982
956 Configuration call(Value value) { 983 Configuration call(Value value) {
957 if (constructor.enclosingClass.superclass.superclass != null) { 984 if (constructor.enclosingClass.superclass.superclass != null) {
958 throw 'Support for super constructors in not implemented.'; 985 throw 'Support for super constructors in not implemented.';
959 } 986 }
960 987
961 if (constructor.initializers.isNotEmpty && 988 if (constructor.initializers.isNotEmpty &&
962 !(constructor.initializers.last is SuperInitializer)) { 989 !(constructor.initializers.last is SuperInitializer)) {
963 throw 'Support for initializer is not implemented.'; 990 throw 'Support for initializers is not implemented.';
964 } 991 }
965 992
966 // The statement body is captured by the next statement continuation and 993 // The statement body is captured by the next statement continuation and
967 // expressions for field initialization are evaluated. 994 // expressions for field initialization are evaluated.
968 var ctrEnv = environment.extendWithThis(value); 995 var ctrEnv = environment.extendWithThis(value);
969 var bodyCont = 996 var bodyCont =
970 new ConstructorBodySK(constructor.function.body, ctrEnv, continuation); 997 new ConstructorBodySK(constructor.function.body, ctrEnv, continuation);
971 // TODO(zhivkag): Add support for initialization of fields with initializers . 998 var initializers = _getFieldInitializers(constructor.enclosingClass);
972 return new ForwardConfiguration(bodyCont, ctrEnv); 999 var fieldsCont =
1000 new InstanceFieldsA(constructor, new Location(value), ctrEnv, bodyCont);
1001 return new EvalListConfiguration(
1002 initializers, new Environment.empty(), fieldsCont);
973 } 1003 }
974 } 1004 }
975 1005
976 /// Executes statements. 1006 /// Executes statements.
977 /// 1007 ///
978 /// Execution of a statement completes in one of the following ways: 1008 /// Execution of a statement completes in one of the following ways:
979 /// - It completes normally, in which case the execution proceeds to applying 1009 /// - It completes normally, in which case the execution proceeds to applying
980 /// the next continuation. 1010 /// the next continuation.
981 /// - It breaks with a label, in which case the corresponding continuation is 1011 /// - It breaks with a label, in which case the corresponding continuation is
982 /// returned and applied. 1012 /// returned and applied.
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after
1362 var current = providedArgs.named[i]; 1392 var current = providedArgs.named[i];
1363 args.add(new NamedExpression(current.name, current.value)); 1393 args.add(new NamedExpression(current.name, current.value));
1364 namedFormals.remove(current.name); 1394 namedFormals.remove(current.name);
1365 } 1395 }
1366 1396
1367 // Add missing optional named initializers. 1397 // Add missing optional named initializers.
1368 args.addAll(namedFormals.values); 1398 args.addAll(namedFormals.values);
1369 1399
1370 return args; 1400 return args;
1371 } 1401 }
1402
1403 /// Creates a list of all field expressions to be evaluated.
1404 ///
1405 /// A field expression is an initializer expression for a given field defined
1406 /// when the field was created.
1407 List<InterpreterExpression> _getFieldInitializers(ast.Class class_) {
1408 var fieldInitializers = new List<InterpreterExpression>();
1409
1410 for (Field f in class_.fields) {
1411 if (f.initializer != null) {
1412 fieldInitializers.add(new FieldInitializerExpression(f, f.initializer));
1413 }
1414 }
1415
1416 return fieldInitializers;
1417 }
1418
1419 /// Initializes all non initialized fields from the provided class to
1420 /// `Value.nullInstance` in the provided value.
1421 void _initializeNullFields(Class class_, Value value) {
1422 int startIndex = class_.superclass?.instanceSize ?? 0;
1423 for (int i = startIndex; i < class_.instanceSize; i++) {
1424 if (value.fields[i].value == null) {
1425 value.fields[i].value = Value.nullInstance;
1426 }
1427 }
1428 }
OLDNEW
« no previous file with comments | « no previous file | pkg/kernel/test/interpreter/interpreter.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698