Repository: goplus/gop
File: cl/classfile.go (Go)

Code before:
	sp *spxObj, iobj, ilst int, callMain func()) (lstName string) {
	const (
		indexGame     = 1
		objNamePrefix = "_gop_obj"
		lstNamePrefix = "_gop_lst"
	)
	embedded := (sp.feats&spriteEmbedded != 0)
	sptypes := sp.types
	for i, spt := range sptypes {
		spto := pkg.Ref(spt)
		objName := objNamePrefix + strconv.Itoa(iobj+i)
		cb.DefineVarStart(token.NoPos, objName).
			Val(indexGame).Val(recv).StructLit(spto.Type(), 2, true).
			UnaryOp(gotoken.AND).EndInit(1)
		if embedded {
			cb.Val(recv).MemberRef(spt).VarVal(objName).Assign(1)
		}
	}
	if ilst > 0 {
		lstName = lstNamePrefix + strconv.Itoa(ilst-1)
		cb.DefineVarStart(token.NoPos, lstName)
	} else {
		callMain()
	}
	for i, spt := range sptypes {
		src := parent.node(filepath.Join(parent.nodeInterp.relBaseDir, spt+sp.ext))
		objName := objNamePrefix + strconv.Itoa(iobj+i)
		cb.VarVal(objName, src)
	}
	return
}

func genMainFunc(pkg *gogen.Package, gameClass string) {
	if o := pkg.TryRef(gameClass); o != nil {
		// force remove //line comments for main func
		pkg.CB().SetComments(nil, false)
		// new(gameClass).Main()
		new := pkg.Builtin().Ref("new")
		pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
			Val(new).Val(o).Call(1).MemberVal("Main").Call(0).EndStmt().
			End()
	}
}

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

Review comment (suggestion):
可以从 parnet.classes 中获取 *ast.File
```
for f, cls := range parent.classes {
	if cls.name == spt {
		// f.Name
		break
	}
}
```

Code after:
	sp *spxObj, iobj, ilst int, callMain func()) (lstName string) {
	const (
		indexGame     = 1
		objNamePrefix = "_gop_obj"
		lstNamePrefix = "_gop_lst"
	)
	embedded := (sp.feats&spriteEmbedded != 0)
	sptypes := sp.types
	for i, spt := range sptypes {
		spto := pkg.Ref(spt)
		objName := objNamePrefix + strconv.Itoa(iobj+i)
		cb.DefineVarStart(token.NoPos, objName).
			Val(indexGame).Val(recv).StructLit(spto.Type(), 2, true).
			UnaryOp(gotoken.AND).EndInit(1)
		if embedded {
			cb.Val(recv).MemberRef(spt).VarVal(objName).Assign(1)
		}
	}
	if ilst > 0 {
		lstName = lstNamePrefix + strconv.Itoa(ilst-1)
		cb.DefineVarStart(token.NoPos, lstName)
	} else {
		callMain()
	}
	for i, spt := range sptypes {
		src := parent.lookupClassNode(spt)
		objName := objNamePrefix + strconv.Itoa(iobj+i)
		cb.VarVal(objName, src)
	}
	return
}

func genMainFunc(pkg *gogen.Package, gameClass string) {
	if o := pkg.TryRef(gameClass); o != nil {
		// force remove //line comments for main func
		pkg.CB().SetComments(nil, false)
		// new(gameClass).Main()
		new := pkg.Builtin().Ref("new")
		pkg.NewFunc(nil, "main", nil, nil, false).BodyStart(pkg).
			Val(new).Val(o).Call(1).MemberVal("Main").Call(0).EndStmt().
			End()
	}
}

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