Chromium Code Reviews| Index: pkg/kernel/lib/interpreter/interpreter.dart |
| diff --git a/pkg/kernel/lib/interpreter/interpreter.dart b/pkg/kernel/lib/interpreter/interpreter.dart |
| index b3787a671c6e2e213a633ce2ee010e808ccde172..b02709f099a355ea752c8984cd8e805320ccfe21 100644 |
| --- a/pkg/kernel/lib/interpreter/interpreter.dart |
| +++ b/pkg/kernel/lib/interpreter/interpreter.dart |
| @@ -53,6 +53,12 @@ class Environment { |
| final List<Binding> bindings = <Binding>[]; |
| final Environment parent; |
| + Value get thisInstance { |
| + return containsThis() |
| + ? lookupThis().value |
| + : throw "Invalid reference to 'this' expression"; |
| + } |
| + |
| Environment.empty() : parent = null; |
| Environment(this.parent); |
| @@ -63,6 +69,13 @@ class Environment { |
| return parent?.contains(variable) ?? false; |
| } |
| + bool containsThis() { |
| + for (Binding b in bindings.reversed) { |
|
Dmitry Stefantsov
2017/07/14 00:43:08
Is it important that [bindings] is reversed?
zhivkag
2017/07/14 07:02:31
Done.
|
| + if (identical(b.variable.name, 'this')) return true; |
| + } |
| + return parent?.containsThis() ?? false; |
| + } |
| + |
| Binding lookupBinding(VariableDeclaration variable) { |
| assert(contains(variable)); |
| for (Binding b in bindings) { |
| @@ -71,6 +84,14 @@ class Environment { |
| return parent.lookupBinding(variable); |
| } |
| + Location lookupThis() { |
| + assert(containsThis()); |
|
Dmitry Stefantsov
2017/07/14 00:43:07
If we pass this `assert`, then at some iteration o
zhivkag
2017/07/14 07:02:31
The intent of this assert is similar to "contains"
Dmitry Stefantsov
2017/07/18 10:45:15
Yep. I misinterpreted [containsThis] above. Than
|
| + for (Binding b in bindings) { |
| + if (identical(b.variable.name, 'this')) return b.location; |
| + } |
| + return parent.lookupThis(); |
| + } |
| + |
| Value lookup(VariableDeclaration variable) { |
| return lookupBinding(variable).location.value; |
| } |
| @@ -85,6 +106,11 @@ class Environment { |
| return new Environment(this) |
| ..bindings.add(new Binding(variable, new Location(value))); |
| } |
| + |
| + Environment extendWithThis(ObjectValue v) { |
| + assert(!containsThis()); |
| + return extend(new VariableDeclaration('this'), v); |
| + } |
| } |
| /// Evaluate expressions. |