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

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

Issue 2877733002: Add suport for initializer list execution in constructor invocation (Closed)
Patch Set: Created 3 years, 7 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 | no next file » | 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 525 matching lines...) Expand 10 before | Expand all | Expand 10 after
536 if (constructor.initializers.isNotEmpty && 536 if (constructor.initializers.isNotEmpty &&
537 constructor.initializers.last is RedirectingInitializer) { 537 constructor.initializers.last is RedirectingInitializer) {
538 var cont = new RedirectingConstructorInvocationApplication(newObject, 538 var cont = new RedirectingConstructorInvocationApplication(newObject,
539 constructor.initializers.last, ctrEnv, expressionContinuation); 539 constructor.initializers.last, ctrEnv, expressionContinuation);
540 var es = _createLocalInitializerExpressionList( 540 var es = _createLocalInitializerExpressionList(
541 constructor.initializers.take(constructor.initializers.length - 1)); 541 constructor.initializers.take(constructor.initializers.length - 1));
542 return new ExpressionListConfiguration(es, ctrEnv, cont); 542 return new ExpressionListConfiguration(es, ctrEnv, cont);
543 } 543 }
544 544
545 var cont = new InstanceFieldsApplication( 545 var cont = new InstanceFieldsApplication(
546 newObject, constructor, expressionContinuation); 546 newObject, constructor, ctrEnv, expressionContinuation);
547 var fieldExpressions = _createInstanceInitializers(constructor); 547 var fieldExpressions = _createInstanceInitializers(constructor);
548 548
549 return new ExpressionListConfiguration(fieldExpressions, ctrEnv, cont); 549 return new ExpressionListConfiguration(fieldExpressions, ctrEnv, cont);
550 } 550 }
551 551
552 /// Creates a list of expressions for local initializers. 552 /// Creates a list of expressions for local initializers.
553 static List<InterpreterExpression> _createLocalInitializerExpressionList( 553 static List<InterpreterExpression> _createLocalInitializerExpressionList(
554 List<LocalInitializer> initializers) { 554 List<LocalInitializer> initializers) {
555 List<InterpreterExpression> es = <InterpreterExpression>[]; 555 List<InterpreterExpression> es = <InterpreterExpression>[];
556 556
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
604 604
605 return new ExpressionListConfiguration(args, environment, cont); 605 return new ExpressionListConfiguration(args, environment, cont);
606 } 606 }
607 } 607 }
608 608
609 /// Represents the application continuation applied on the list of evaluated 609 /// Represents the application continuation applied on the list of evaluated
610 /// field initializer expressions. 610 /// field initializer expressions.
611 class InstanceFieldsApplication extends ApplicationContinuation { 611 class InstanceFieldsApplication extends ApplicationContinuation {
612 final ObjectValue newObject; 612 final ObjectValue newObject;
613 final Constructor constructor; 613 final Constructor constructor;
614 final Environment environment;
614 final ExpressionContinuation expressionContinuation; 615 final ExpressionContinuation expressionContinuation;
615 616
616 final Class _currentClass; 617 final Class _currentClass;
617 618
618 InstanceFieldsApplication( 619 InstanceFieldsApplication(this.newObject, this.constructor, this.environment,
619 this.newObject, this.constructor, this.expressionContinuation) 620 this.expressionContinuation)
620 : _currentClass = new Class(constructor.enclosingClass.reference); 621 : _currentClass = new Class(constructor.enclosingClass.reference);
621 622
622 Configuration call(List<InterpreterValue> fieldValues) { 623 Configuration call(List<InterpreterValue> fieldValues) {
623 for (FieldInitializerValue current in fieldValues.reversed) { 624 for (FieldInitializerValue current in fieldValues.reversed) {
624 _currentClass.setProperty(newObject, current.field, current.value); 625 _currentClass.setProperty(newObject, current.field, current.value);
625 } 626 }
626 627
628 var es = _createInitializerListExpressions(constructor.initializers);
629 List<Initializer> initializers = constructor.initializers.skip(es.length);
630
631 ApplicationContinuation cont = new InitializerListApplication(newObject,
632 constructor, environment, initializers, expressionContinuation);
633
634 return new ExpressionListConfiguration(es, environment, cont);
635 }
636 }
637
638 /// Represents the application continuation applied on the list of evaluated
639 /// initializer expressions preceding a super call in the list.
640 class InitializerListApplication extends ApplicationContinuation {
641 final ObjectValue newObject;
642 final Constructor constructor;
643 final Environment environment;
644 final List<Initializer> remainingInitializers;
645 final ExpressionContinuation expressionContinuation;
646
647 final Class _currentClass;
648
649 InitializerListApplication(this.newObject, this.constructor, this.environment,
650 this.remainingInitializers, this.expressionContinuation)
651 : _currentClass = new Class(constructor.enclosingClass.reference);
652
653 Configuration call(List<InterpreterValue> values) {
654 var initEnv = new Environment(environment);
655
656 // Apply values from evaluation to object or/and initializer environment.
657 for (InterpreterValue current in values.reversed) {
658 if (current is LocalInitializerValue) {
659 initEnv.expand(current.variable, current.value);
660 } else if (current is FieldInitializerValue) {
661 _currentClass.setProperty(newObject, current.field, current.value);
662 } else {
663 throw '${current.runtimeType} in InitializerListApplication';
664 }
665 }
666
667 if (remainingInitializers.isNotEmpty) {
668 assert(remainingInitializers.first is SuperInitializer);
669 // todo: Evaluate arguments for super invocation.
670 }
671
627 return new ContinuationConfiguration(expressionContinuation, newObject); 672 return new ContinuationConfiguration(expressionContinuation, newObject);
628 } 673 }
629 } 674 }
630 675
631 /// Represents the application continuation called after the evaluation of all 676 /// Represents the application continuation called after the evaluation of all
632 /// argument expressions for an invocation. 677 /// argument expressions for an invocation.
633 class ValueApplication extends ApplicationContinuation { 678 class ValueApplication extends ApplicationContinuation {
634 final InterpreterValue value; 679 final InterpreterValue value;
635 final ApplicationContinuation applicationContinuation; 680 final ApplicationContinuation applicationContinuation;
636 681
(...skipping 685 matching lines...) Expand 10 before | Expand all | Expand 10 after
1322 var current = providedArgs.named[i]; 1367 var current = providedArgs.named[i];
1323 args.add(new NamedArgumentExpression(current.name, current.value)); 1368 args.add(new NamedArgumentExpression(current.name, current.value));
1324 namedFormals.remove(current.name); 1369 namedFormals.remove(current.name);
1325 } 1370 }
1326 1371
1327 // Add missing optional named initializers. 1372 // Add missing optional named initializers.
1328 args.addAll(namedFormals.values); 1373 args.addAll(namedFormals.values);
1329 1374
1330 return args; 1375 return args;
1331 } 1376 }
1377
1378 List<InterpreterExpression> _createInitializerListExpressions(
1379 List<Initializer> initializers) {
1380 List<InterpreterExpression> es = <InterpreterExpression>[];
1381
1382 for (Initializer current in initializers) {
1383 if (current is FieldInitializer) {
1384 es.add(new FieldInitializerExpression(current.field, current.value));
1385 } else if (current is LocalInitializer) {
1386 es.add(new LocalInitializerExpression(current.variable));
1387 } else {
1388 assert(current is SuperInitializer);
1389 return es;
1390 }
1391 }
1392
1393 return es;
1394 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698