Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions cl/classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,12 +626,17 @@ func genMainFunc(pkg *gogen.Package, gameClass string) {

func findMethod(o types.Object, name string) *types.Func {
if obj, ok := o.(*types.TypeName); ok {
if t, ok := obj.Type().(*types.Named); ok {
for i, n := 0, t.NumMethods(); i < n; i++ {
f := t.Method(i)
if f.Name() == name {
return f
}
return findMethodByType(obj.Type(), name)
}
return nil
}

func findMethodByType(typ types.Type, name string) *types.Func {
if t, ok := typ.(*types.Named); ok {
for i, n := 0, t.NumMethods(); i < n; i++ {
f := t.Method(i)
if f.Name() == name {
return f
}
}
}
Expand Down
25 changes: 10 additions & 15 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -1552,33 +1552,28 @@ var unaryXGoNames = map[string]string{
"<-": "XGo_Recv",
}

// shouldCallXGoInit checks if XGo_Init is directly defined on the receiver type (not through embedding)
func shouldCallXGoInit(fn *gogen.Func) bool {
recv := fn.Func.Type().(*types.Signature).Recv()
if recv == nil {
return false
}
// shouldCallXGoInit checks if XGo_Init is directly defined on the receiver
// type (not through embedding).
func shouldCallXGoInit(recv *types.Var) bool {
typ := recv.Type()
if pt, ok := typ.(*types.Pointer); ok {
typ = pt.Elem()
}
if named, ok := typ.(*types.Named); ok {
return findMethod(named.Obj(), "XGo_Init") != nil
}
return false
return findMethodByType(typ, "XGo_Init") != nil
}

func loadFuncBody(ctx *blockCtx, fn *gogen.Func, body *ast.BlockStmt, sigBase *types.Signature, src ast.Node, initClass bool) {
cb := fn.BodyStart(ctx.pkg, body)
cb.SetComments(nil, false)
if initClass && shouldCallXGoInit(fn) {
// this.XGo_Init()
cb.VarVal("this").MemberVal("XGo_Init", 0).Call(0).EndStmt()
}
if initClass {
recv := fn.Type().(*types.Signature).Recv()
if shouldCallXGoInit(recv) {
// this.XGo_Init()
cb.Val(recv).MemberVal("XGo_Init", 0).Call(0).EndStmt()
}
if sigBase != nil {
// this.Sprite.Main(...) or this.Game.MainEntry(...)
cb.VarVal("this").MemberVal(ctx.baseClass.Name(), 0).MemberVal(fn.Name(), 0)
cb.Val(recv).MemberVal(ctx.baseClass.Name(), 0).MemberVal(fn.Name(), 0)
params := sigBase.Params()
n := params.Len()
for i := 0; i < n; i++ {
Expand Down