compiler: implement func value and builtin defers

Co-authored-by: Justin A. Wilson <maker.pro.game@gmail.com>
This commit is contained in:
waj334
2020-07-30 18:48:57 -05:00
committed by GitHub
parent 3650c2c739
commit 848c3e55a9
4 changed files with 167 additions and 14 deletions
+49
View File
@@ -44,6 +44,12 @@ func main() {
// defers in loop
testDeferLoop()
//defer func variable call
testDeferFuncVar()
//More complicated func variable call
testMultiFuncVar()
// Take a bound method and use it as a function pointer.
// This function pointer needs a context pointer.
testBound(thing.String)
@@ -64,6 +70,9 @@ func main() {
// regression testing
regression1033()
//Test deferred builtins
testDeferBuiltin()
}
func runFunc(f func(int), arg int) {
@@ -91,6 +100,8 @@ func testDefer() {
defer t.Print("bar")
println("deferring...")
d := dumb{}
defer d.Value(0)
}
func testDeferLoop() {
@@ -99,6 +110,30 @@ func testDeferLoop() {
}
}
func testDeferFuncVar() {
dummy, f := deferFunc()
dummy++
defer f(1)
}
func testMultiFuncVar() {
f := multiFuncDefer()
defer f(1)
}
func testDeferBuiltin() {
i := make(chan int)
defer close(i)
}
type dumb struct {
}
func (*dumb) Value(key interface{}) interface{} {
return nil
}
func deferred(msg string, i int) {
println(msg, i)
}
@@ -108,6 +143,20 @@ func exportedDefer() {
println("...exported defer")
}
func deferFunc() (int, func(int)) {
return 0, func(i int){println("...extracted defer func ", i)}
}
func multiFuncDefer() func(int) {
i := 0
if i > 0 {
return func(i int){println("Should not have gotten here. i = ", i)}
}
return func(i int){println("Called the correct function. i = ", i)}
}
func testBound(f func() string) {
println("bound method:", f())
}
+2
View File
@@ -9,6 +9,8 @@ loop 3
loop 2
loop 1
loop 0
...extracted defer func 1
Called the correct function. i = 1
bound method: foo
thing inside closure: foo
inside fp closure: foo 3