-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
interp: improve handling of composed interfaces wrappers
This change implements a workaround to better support composed interfaces in yaegi and let the interpreter define objects which implement multiple interfaces at once. We use the existing MapTypes to store what possible composed interface wrapper could be used for some interfaces. When generating an interface wrapper, the wrapper with the highest number of implemented methods is chosen. This is still an imperfect solution but it improves the accuracy of interpreter in some critical cases. This workaround could be removed in future if/when golang/go#15924 is resolved. Fixes #1425.
- Loading branch information
Showing
9 changed files
with
395 additions
and
229 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,44 @@ | ||
package main | ||
|
||
import ( | ||
"io" | ||
"log" | ||
"os" | ||
"strings" | ||
) | ||
|
||
type WrappedReader struct { | ||
reader io.Reader | ||
} | ||
|
||
func (wr WrappedReader) Read(p []byte) (n int, err error) { | ||
return wr.reader.Read(p) | ||
} | ||
|
||
// Of course, this implementation is completely stupid because it does not write | ||
// to the intended writer, as any honest WriteTo implementation should. its | ||
// implemtion is just to make obvious the divergence of behaviour with yaegi. | ||
func (wr WrappedReader) WriteTo(w io.Writer) (n int64, err error) { | ||
// Ignore w, send to Stdout to prove whether this WriteTo is used. | ||
data, err := io.ReadAll(wr) | ||
if err != nil { | ||
return 0, err | ||
} | ||
nn, err := os.Stdout.Write(data) | ||
return int64(nn), err | ||
} | ||
|
||
func main() { | ||
f := strings.NewReader("hello world") | ||
wr := WrappedReader{reader: f} | ||
|
||
// behind the scenes, io.Copy is supposed to use wr.WriteTo if the implementation exists. | ||
// With Go, it works as expected, i.e. the output is sent to os.Stdout. | ||
// With Yaegi, it doesn't, i.e. the output is sent to io.Discard. | ||
if _, err := io.Copy(io.Discard, wr); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
// Output: | ||
// hello world |
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
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
Oops, something went wrong.