-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
interp: improve internal handling of functions
Up to now functions could be stored as node values in frame (as for interpreter defined functions) or function values, directly callable by the Go runtime. We now always store functions in the later form, making the processing of functions, anonymous closures and methods simpler and more robust. All functions, once compiled are always directly callable, with no further wrapping necessary. Fixes #1459.
- Loading branch information
Showing
10 changed files
with
196 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
) | ||
|
||
type T struct { | ||
name string | ||
next http.Handler | ||
} | ||
|
||
func (t *T) ServeHTTP(rw http.ResponseWriter, req *http.Request) { | ||
println("in T.ServeHTTP") | ||
if t.next != nil { | ||
t.next.ServeHTTP(rw, req) | ||
} | ||
} | ||
|
||
func New(name string, next http.Handler) (http.Handler, error) { return &T{name, next}, nil } | ||
|
||
func main() { | ||
next := func(rw http.ResponseWriter, req *http.Request) { | ||
println("in next") | ||
} | ||
|
||
t, err := New("test", http.HandlerFunc(next)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
recorder := httptest.NewRecorder() | ||
req := httptest.NewRequest(http.MethodGet, "/", nil) | ||
t.ServeHTTP(recorder, req) | ||
println(recorder.Result().Status) | ||
} | ||
|
||
// Output: | ||
// in T.ServeHTTP | ||
// in next | ||
// 200 OK |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
func main() { | ||
next := func(rw http.ResponseWriter, req *http.Request) { | ||
rw.Header().Set("Cache-Control", "max-age=20") | ||
rw.WriteHeader(http.StatusOK) | ||
} | ||
f := http.HandlerFunc(next) | ||
fmt.Printf("%T\n", f.ServeHTTP) | ||
} | ||
|
||
// Output: | ||
// func(http.ResponseWriter, *http.Request) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
type funclistItem func() | ||
|
||
type funclist struct { | ||
list []funclistItem | ||
} | ||
|
||
func main() { | ||
funcs := funclist{} | ||
|
||
funcs.list = append(funcs.list, func() { fmt.Println("first") }) | ||
|
||
for _, f := range funcs.list { | ||
f() | ||
} | ||
} | ||
|
||
// Output: | ||
// first |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
// Package unsafe2 provides helpers to generate recursive struct types. | ||
package unsafe2 | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.