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..f75d0777c5ab0e99e41e4b34b4317e738f473455 100644 |
--- a/pkg/kernel/lib/interpreter/interpreter.dart |
+++ b/pkg/kernel/lib/interpreter/interpreter.dart |
@@ -53,16 +53,29 @@ 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); |
bool contains(VariableDeclaration variable) { |
- for (Binding b in bindings.reversed) { |
+ for (Binding b in bindings) { |
if (identical(b.variable, variable)) return true; |
} |
return parent?.contains(variable) ?? false; |
} |
+ bool containsThis() { |
+ for (Binding b in bindings) { |
+ 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()); |
+ 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. |