Fix invocation of closure from runtime

This commit is contained in:
Marc Vertes
2018-07-10 16:46:03 +02:00
parent c753054159
commit 119b5bc40c
3 changed files with 29 additions and 2 deletions

28
_test/server6.go Normal file
View File

@@ -0,0 +1,28 @@
package main
import (
"fmt"
"net/http"
)
var myHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("hello world"))
})
type T1 struct {
Name string
}
func (t *T1) Handler(h http.Handler) http.Handler {
fmt.Println("#1", t.Name)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println("#2", t.Name)
h.ServeHTTP(w, r)
})
}
func main() {
t := &T1{"myName"}
handler := t.Handler(myHandler)
http.ListenAndServe(":8080", handler)
}

View File

@@ -340,7 +340,6 @@ func (interp *Interpreter) Cfg(root *Node, sdef *NodeMap) []*Node {
for i, c := range n.child[1:] {
// Wrap function defintion so it can be called from runtime
if c.kind == FuncLit {
log.Println(n.index, "FuncLit")
n.child[1+i].rval = reflect.MakeFunc(rtype.In(i), c.wrapNode)
n.child[1+i].kind = Rvalue
} else if c.ident == "nil" {

View File

@@ -498,7 +498,7 @@ func getFunc(n *Node, f *Frame) {
frame := *f
node.frame = &frame
f.data[n.findex] = &node
n.interp.Frame = &frame // Temporary, store context for futur call from runtime
}
func getMap(n *Node, f *Frame) {