Skip to content

Commit

Permalink
Merge pull request #404 from xushiwei/gogen
Browse files Browse the repository at this point in the history
TestTemplateRecvMethod2: Typ.Member for TyTemplateRecvMethod
  • Loading branch information
xushiwei authored Mar 10, 2024
2 parents 984bf7d + 18cd65e commit b070611
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
19 changes: 16 additions & 3 deletions codebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}

Expand Down
19 changes: 19 additions & 0 deletions gop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit b070611

Please sign in to comment.