fix read/write variable in parent's scope, fix #39

This commit is contained in:
harttle
2017-08-26 17:57:43 +08:00
parent 942a3224a1
commit 21af8c4d7a
5 changed files with 57 additions and 28 deletions
+12 -1
View File
@@ -20,7 +20,8 @@ var Scope = {
}
},
set: function (k, v) {
setPropertyByPath(this.scopes[this.scopes.length - 1], k, v)
var scope = this.findScopeFor(k)
setPropertyByPath(scope, k, v)
return this
},
push: function (ctx) {
@@ -30,6 +31,16 @@ var Scope = {
pop: function () {
return this.scopes.pop()
},
findScopeFor: function (key) {
var i = this.scopes.length - 1
while (i >= 0 && !(key in this.scopes[i])) {
i--
}
if (i < 0) {
i = this.scopes.length - 1
}
return this.scopes[i]
},
unshift: function (ctx) {
assert(ctx, `trying to push ${ctx} into scopes`)
return this.scopes.unshift(ctx)