diff --git a/codebuild.go b/codebuild.go index d4218279..dc91dc7d 100644 --- a/codebuild.go +++ b/codebuild.go @@ -1738,12 +1738,21 @@ func (p *CodeBuilder) method( if autoprop && !methodHasAutoProperty(typ, 0) { return memberBad } + sel := selector(arg, found.Name()) - p.stk.Ret(1, &internal.Elem{ + ret := &internal.Elem{ Val: sel, Type: methodSigOf(typ, flag, arg, sel), Src: src, - }) + } + // TODO: We should take `methodSigOf` more seriously + if trm, ok := ret.Type.(*TyTemplateRecvMethod); ok { + o := trm.Func + ret.Val = toObjectExpr(p.pkg, o) + ret.Type = o.Type() + } + p.stk.Ret(1, ret) + if p.rec != nil { p.rec.Member(src, found) } @@ -1877,7 +1886,11 @@ func methodSigOf(typ types.Type, flag MemberFlag, arg *Element, sel *ast.Selecto } sig := typ.(*types.Signature) - if _, ok := CheckFuncEx(sig); ok { + if t, ok := CheckFuncEx(sig); ok { + if trm, ok := t.(*TyTemplateRecvMethod); ok { + // TODO: We should take `methodSigOf` more seriously + return trm + } return typ } diff --git a/gop_test.go b/gop_test.go index 39c759d4..b866a2a4 100644 --- a/gop_test.go +++ b/gop_test.go @@ -994,6 +994,25 @@ func main() { `) } +func TestTemplateRecvMethod2(t *testing.T) { + pkg := newMainPackage() + bar := pkg.Import("github.com/goplus/gogen/internal/bar") + tyGame := bar.Ref("Game").Type() + pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg). + NewVar(tyGame, "g"). + Typ(tyGame).MemberVal("Run").VarRef("g").UnaryOp(token.AND).Val("Hi").Call(2).EndStmt(). + End() + domTest(t, pkg, `package main + +import "github.com/goplus/gogen/internal/bar" + +func main() { + var g bar.Game + bar.Gopt_Game_Run(&g, "Hi") +} +`) +} + func TestErrTemplateRecvMethod(t *testing.T) { pkg := newMainPackage() bar := pkg.Import("github.com/goplus/gogen/internal/bar")