feat: stop closure upvalue search at method boundaries and treat capitalized names as globals

Modify `findUpvalue` in the compiler to return -1 when the enclosing function is a method, preventing closures from capturing outer local variables. Add a new `nonlocal` variable resolution path that treats capitalized identifiers as global variables rather than method receivers. Update existing test files to use capitalized global names and add new test cases for nonlocal assignment, duplicate nonlocal declarations, block scoping, and method-local variable shadowing.
This commit is contained in:
Bob Nystrom
2015-01-14 05:36:42 +00:00
parent d031d0a77d
commit 54ef9bbc73
8 changed files with 124 additions and 61 deletions
@@ -1,12 +1,12 @@
var f = null
var F = null
class Foo {
method(param) {
f = new Fn {
F = new Fn {
IO.print(param)
}
}
}
(new Foo).method("param")
f.call // expect: param
F.call // expect: param
-6
View File
@@ -1,6 +0,0 @@
var global = "global"
// TODO: Forward reference to global declared after use.
new Fn {
IO.print(global) // expect: global
}.call
-15
View File
@@ -1,15 +0,0 @@
var global = "global"
// TODO: Forward reference to global declared after use.
class Foo {
method {
IO.print(global)
}
static classMethod {
IO.print(global)
}
}
(new Foo).method // expect: global
Foo.classMethod // expect: global