Closing over "this".

This commit is contained in:
Bob Nystrom
2013-12-21 15:55:08 -08:00
parent 76ac818eaf
commit 24a6f4cd8c
6 changed files with 75 additions and 18 deletions

View File

@ -14,6 +14,5 @@
}
}
// TODO: Closing over this.
// TODO: Close over fn/method parameter.
// TODO: Maximum number of closed-over variables (directly and/or indirect).
// TODO: Shadow variable used in closure.

12
test/this/closure.wren Normal file
View File

@ -0,0 +1,12 @@
class Foo {
getClosure {
return fn {
return this.toString
}
}
toString { return "Foo" }
}
var closure = (new Foo).getClosure
io.write(closure.call) // expect: Foo

View File

@ -0,0 +1,22 @@
class Outer {
method {
io.write(this.toString) // expect: Outer
fn {
io.write(this.toString) // expect: Outer
class Inner {
method {
io.write(this.toString) // expect: Inner
}
toString { return "Inner" }
}
(new Inner).method
}.call
}
toString { return "Outer" }
}
(new Outer).method

View File

@ -0,0 +1,16 @@
class Foo {
getClosure {
return fn {
return fn {
return fn {
return this.toString
}
}
}
}
toString { return "Foo" }
}
var closure = (new Foo).getClosure
io.write(closure.call.call.call) // expect: Foo