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

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

Issue 2978893002: Add support for binding 'this' (Closed)
Patch Set: Remove unnecessary reversed Created 3 years, 5 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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 final VariableDeclaration variable; 46 final VariableDeclaration variable;
47 final Location location; 47 final Location location;
48 48
49 Binding(this.variable, this.location); 49 Binding(this.variable, this.location);
50 } 50 }
51 51
52 class Environment { 52 class Environment {
53 final List<Binding> bindings = <Binding>[]; 53 final List<Binding> bindings = <Binding>[];
54 final Environment parent; 54 final Environment parent;
55 55
56 Value get thisInstance {
57 return containsThis()
58 ? lookupThis().value
59 : throw "Invalid reference to 'this' expression";
60 }
61
56 Environment.empty() : parent = null; 62 Environment.empty() : parent = null;
57 Environment(this.parent); 63 Environment(this.parent);
58 64
59 bool contains(VariableDeclaration variable) { 65 bool contains(VariableDeclaration variable) {
60 for (Binding b in bindings.reversed) { 66 for (Binding b in bindings) {
61 if (identical(b.variable, variable)) return true; 67 if (identical(b.variable, variable)) return true;
62 } 68 }
63 return parent?.contains(variable) ?? false; 69 return parent?.contains(variable) ?? false;
64 } 70 }
65 71
72 bool containsThis() {
73 for (Binding b in bindings) {
74 if (identical(b.variable.name, 'this')) return true;
75 }
76 return parent?.containsThis() ?? false;
77 }
78
66 Binding lookupBinding(VariableDeclaration variable) { 79 Binding lookupBinding(VariableDeclaration variable) {
67 assert(contains(variable)); 80 assert(contains(variable));
68 for (Binding b in bindings) { 81 for (Binding b in bindings) {
69 if (identical(b.variable, variable)) return b; 82 if (identical(b.variable, variable)) return b;
70 } 83 }
71 return parent.lookupBinding(variable); 84 return parent.lookupBinding(variable);
72 } 85 }
73 86
87 Location lookupThis() {
88 assert(containsThis());
89 for (Binding b in bindings) {
90 if (identical(b.variable.name, 'this')) return b.location;
91 }
92 return parent.lookupThis();
93 }
94
74 Value lookup(VariableDeclaration variable) { 95 Value lookup(VariableDeclaration variable) {
75 return lookupBinding(variable).location.value; 96 return lookupBinding(variable).location.value;
76 } 97 }
77 98
78 void assign(VariableDeclaration variable, Value value) { 99 void assign(VariableDeclaration variable, Value value) {
79 assert(contains(variable)); 100 assert(contains(variable));
80 lookupBinding(variable).location.value = value; 101 lookupBinding(variable).location.value = value;
81 } 102 }
82 103
83 Environment extend(VariableDeclaration variable, Value value) { 104 Environment extend(VariableDeclaration variable, Value value) {
84 assert(!contains(variable)); 105 assert(!contains(variable));
85 return new Environment(this) 106 return new Environment(this)
86 ..bindings.add(new Binding(variable, new Location(value))); 107 ..bindings.add(new Binding(variable, new Location(value)));
87 } 108 }
109
110 Environment extendWithThis(ObjectValue v) {
111 assert(!containsThis());
112 return extend(new VariableDeclaration('this'), v);
113 }
88 } 114 }
89 115
90 /// Evaluate expressions. 116 /// Evaluate expressions.
91 class Evaluator extends ExpressionVisitor1<Configuration, EvalConfiguration> { 117 class Evaluator extends ExpressionVisitor1<Configuration, EvalConfiguration> {
92 Configuration eval(Expression expr, EvalConfiguration config) => 118 Configuration eval(Expression expr, EvalConfiguration config) =>
93 expr.accept1(this, config); 119 expr.accept1(this, config);
94 120
95 Configuration evalList(List<InterpreterExpression> list, Environment env, 121 Configuration evalList(List<InterpreterExpression> list, Environment env,
96 ApplicationContinuation cont) { 122 ApplicationContinuation cont) {
97 if (list.isNotEmpty) { 123 if (list.isNotEmpty) {
(...skipping 1184 matching lines...) Expand 10 before | Expand all | Expand 10 after
1282 void _initializeNullFields(Class class_, ObjectValue newObject) { 1308 void _initializeNullFields(Class class_, ObjectValue newObject) {
1283 int superClassSize = class_.superclass?.instanceSize ?? 0; 1309 int superClassSize = class_.superclass?.instanceSize ?? 0;
1284 for (int i = superClassSize; i < class_.instanceSize; i++) { 1310 for (int i = superClassSize; i < class_.instanceSize; i++) {
1285 Field field = class_.instanceFields[i]; 1311 Field field = class_.instanceFields[i];
1286 if (class_.getProperty(newObject, field) == null) { 1312 if (class_.getProperty(newObject, field) == null) {
1287 assert(field.initializer == null); 1313 assert(field.initializer == null);
1288 class_.setProperty(newObject, field, Value.nullInstance); 1314 class_.setProperty(newObject, field, Value.nullInstance);
1289 } 1315 }
1290 } 1316 }
1291 } 1317 }
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