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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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.
« 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