forked from traefik/yaegi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
interp: fix type assertion for wrapped empty interface
Although empty interfaces are usually not wrapped, for compatibility with the runtime, we may have to wrap them sometime into `valueInterface` type. It allows to preserve interpreter type metadata for interface values exchanged with the runtime. It is necessary to resolve methods and receivers in the absence of reflect support. During type assertions on empty interfaces, we now handle a possible valueInterface and dereference the original value to pursue the type assertion. In the same change, we have improved the format of some panic messages at runtime to give location of offending source at interpreter level. This change will allow to fix traefik/traefik#9362.
- Loading branch information
Showing
5 changed files
with
78 additions
and
14 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,56 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"log" | ||
"net/http" | ||
"net/http/httptest" | ||
) | ||
|
||
type T struct { | ||
http.ResponseWriter | ||
} | ||
|
||
type mw1 struct { | ||
} | ||
|
||
var obj = map[string]interface{}{} | ||
|
||
func (m *mw1) ServeHTTP(rw http.ResponseWriter, rq *http.Request) { | ||
t := &T{ | ||
ResponseWriter: rw, | ||
} | ||
x := t.Header() | ||
i := obj["m1"].(*mw1) | ||
fmt.Fprint(rw, "Welcome to my website!", x, i) | ||
} | ||
|
||
func main() { | ||
m1 := &mw1{} | ||
|
||
obj["m1"] = m1 | ||
|
||
mux := http.NewServeMux() | ||
mux.HandleFunc("/", m1.ServeHTTP) | ||
|
||
server := httptest.NewServer(mux) | ||
defer server.Close() | ||
|
||
client(server.URL) | ||
} | ||
|
||
func client(uri string) { | ||
resp, err := http.Get(uri) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Println(string(body)) | ||
} | ||
|
||
// Output: | ||
// Welcome to my website!map[] &{} |
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
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