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

gmxMainFunc: check conf.NoAutoGenMain #1658

Merged
merged 3 commits into from
Jan 22, 2024
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
26 changes: 24 additions & 2 deletions cl/classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,24 @@ func setBodyHandler(ctx *blockCtx) {
}
}

func gmxMainFunc(p *gox.Package, ctx *pkgCtx) func() {
func gmxMainFunc(p *gox.Package, ctx *pkgCtx, noAutoGenMain bool) func() {
if len(ctx.projs) == 1 { // only one project file
var proj *gmxProject
for _, v := range ctx.projs {
proj = v
break
}
scope := p.Types.Scope()
if o := scope.Lookup(proj.gameClass); o != nil && hasMethod(o, "MainEntry") {
var o types.Object
if proj.gameClass != "" {
o = scope.Lookup(proj.gameClass)
if noAutoGenMain && o != nil && hasMethod(o, "MainEntry") {
noAutoGenMain = false
}
} else {
o = proj.game
}
if !noAutoGenMain && o != nil {
// new(Game).Main()
// new(Game).Main(workers...)
fn := p.NewFunc(nil, "main", nil, nil, false)
Expand Down Expand Up @@ -228,4 +237,17 @@ func gmxMainNarg(sig *types.Signature) int {
return sig.Params().Len()
}

func hasMethod(o types.Object, name string) bool {
if obj, ok := o.(*types.TypeName); ok {
if t, ok := obj.Type().(*types.Named); ok {
for i, n := 0, t.NumMethods(); i < n; i++ {
if t.Method(i).Name() == name {
return true
}
}
}
}
return false
}

// -----------------------------------------------------------------------------
15 changes: 1 addition & 14 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package,
}
}
if genMain { // make classfile main func if need
gen = gmxMainFunc(p, ctx)
gen = gmxMainFunc(p, ctx, conf.NoAutoGenMain)
}

for _, f := range sfiles {
Expand Down Expand Up @@ -648,19 +648,6 @@ func initGopPkg(ctx *pkgCtx, pkg *gox.Package, gopSyms map[string]bool) {
initThisGopPkg(pkg.Types)
}

func hasMethod(o types.Object, name string) bool {
if obj, ok := o.(*types.TypeName); ok {
if t, ok := obj.Type().(*types.Named); ok {
for i, n := 0, t.NumMethods(); i < n; i++ {
if t.Method(i).Name() == name {
return true
}
}
}
}
return false
}

func getEntrypoint(f *ast.File) string {
switch {
case f.IsProj:
Expand Down
3 changes: 0 additions & 3 deletions cl/typeparams_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//go:build go1.18
// +build go1.18

package cl_test

import (
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.18
require (
github.com/fsnotify/fsnotify v1.7.0
github.com/goplus/c2go v0.7.20-0.20240115161159-762f74054b17
github.com/goplus/gox v1.13.1-0.20240122040255-a954f8e0b9c1
github.com/goplus/gox v1.13.1-0.20240122055409-d0400bc230e8
github.com/goplus/mod v0.12.2-0.20240122042636-7db361b76b12
github.com/qiniu/x v1.13.2
golang.org/x/tools v0.17.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/
github.com/goplus/c2go v0.7.20-0.20240115161159-762f74054b17 h1:2+AWW9KjzTwaM8pfvRZqhI3PCXs7cMxzsn98AqDpgH0=
github.com/goplus/c2go v0.7.20-0.20240115161159-762f74054b17/go.mod h1:WFbERV86IVV4wJ2/mLa8756Fy6iSXtlwGQbEqkYoIR4=
github.com/goplus/gox v1.13.1-0.20240115155941-e657d899cb2e/go.mod h1:G7Hz+cAOUyJyN9pPHrpqhfQPDUtiJNmoRVTwoaQ9nw0=
github.com/goplus/gox v1.13.1-0.20240122040255-a954f8e0b9c1 h1:14sIjKTXsqx2jfwKxDw+damXZ5lk3bTF27AsOVm18tk=
github.com/goplus/gox v1.13.1-0.20240122040255-a954f8e0b9c1/go.mod h1:G7Hz+cAOUyJyN9pPHrpqhfQPDUtiJNmoRVTwoaQ9nw0=
github.com/goplus/gox v1.13.1-0.20240122055409-d0400bc230e8 h1:k3ZTzEEU1ct6ljE1gqCg5LH2iyOs9W69lCS5COYS9As=
github.com/goplus/gox v1.13.1-0.20240122055409-d0400bc230e8/go.mod h1:G7Hz+cAOUyJyN9pPHrpqhfQPDUtiJNmoRVTwoaQ9nw0=
github.com/goplus/mod v0.12.2-0.20240107203906-5044606d0c51/go.mod h1:ZtlS9wHOcAVxZ/zq7WLdKVes1HG/8Yn3KNuWZGcpeTs=
github.com/goplus/mod v0.12.2-0.20240122042636-7db361b76b12 h1:oQX3E8MWcXznTsQ6mzXYXGbniwyfKW4GtPl5vSdOr9k=
github.com/goplus/mod v0.12.2-0.20240122042636-7db361b76b12/go.mod h1:ZtlS9wHOcAVxZ/zq7WLdKVes1HG/8Yn3KNuWZGcpeTs=
Expand Down
6 changes: 4 additions & 2 deletions x/build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,15 +389,17 @@ import (
"github.com/goplus/gop/cl/internal/spx"
)

func main() {
spx.Gopt_MyGame_Main(new(spx.MyGame))
}

type Cat struct {
spx.Sprite
}

func (this *Cat) Main() {
fmt.Println("hi")
}
func main() {
}
`)
}

Expand Down