Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(interp): convert bin interface to src interface. #1562

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

laushunyu
Copy link
Contributor

@laushunyu laushunyu commented Jun 16, 2023

fix: #1558

test case:

package main

import "fmt"

type A struct {
}

func (a A) String() string {
	return "String"
}

func (a A) GoString() string {
	return "GoString"
}

type Stringer interface {
	String() string
}

type GoStringer interface {
	GoString() string
}

func Fuck(in fmt.Stringer) {
	var a Stringer = A{}

	{
		t, ok := a.(GoStringer)
		fmt.Printf("src to src: %v\n", ok)
		if ok {
			fmt.Println(t.GoString())
		}
	}

	{
		t, ok := a.(fmt.GoStringer)
		fmt.Printf("src to src: %v\n", ok)
		if ok {
			fmt.Println(t.GoString())
		}
	}

	{
		t, ok := in.(GoStringer)
		fmt.Printf("bin to src: %v\n", ok)
		if ok {
			fmt.Println(t.GoString())
		}
	}

	{
		t, ok := in.(fmt.GoStringer)
		fmt.Printf("bin to bin: %v\n", ok)
		if ok {
			fmt.Println(t.GoString())
		}
	}
}

runner:

package main

import (
	"fmt"
	"github.com/traefik/yaegi/interp"
	"github.com/traefik/yaegi/stdlib"
	"os"
)

type B struct {
}

func (a B) String() string {
	return "String"
}

func (a B) GoString() string {
	return "GoString"
}

func main() {
	interpreter := interp.New(interp.Options{
		SourcecodeFilesystem: os.DirFS("_lib"),
	})
	interpreter.Use(stdlib.Symbols)

	_, err := interpreter.CompilePath("fuck")
	if err != nil {
		panic(err)
	}

	mainSymbols := interpreter.Symbols("fuck")["fuck"]
	fn := mainSymbols["Fuck"].Interface().(func(stringer fmt.Stringer))
	fn(B{})
}

output:

src to src: true
GoString
src to src: true
GoString
bin to src: true
GoString
bin to bin: true
GoString

it works, but i dont know whether the fix will cause more bugs....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

interface assertion not work
1 participant