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

initBuiltin: echo; call gmxMainFunc only if no main func #1648

Merged
merged 3 commits into from
Jan 20, 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
2 changes: 1 addition & 1 deletion ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -1135,7 +1135,7 @@ type File struct {
}

// There is no entrypoint func to indicate the module entry point.
func (f *File) NoEntrypoint() bool {
func (f *File) HasShadowEntry() bool {
return f.ShadowEntry != nil
}

Expand Down
1 change: 1 addition & 0 deletions cl/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func initBuiltin(pkg *gox.Package, builtin *types.Package, os, fmt, ng, iox, bui
scope.Insert(types.NewTypeName(token.NoPos, builtin, "int128", ng.Ref("Int128").Type()))
}
if fmt != nil {
scope.Insert(gox.NewOverloadFunc(token.NoPos, builtin, "echo", fmt.Ref("Println")))
initBuiltinFns(builtin, scope, fmt, []string{
"print", "println", "printf", "errorf",
"fprint", "fprintln", "fprintf",
Expand Down
4 changes: 3 additions & 1 deletion cl/classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,16 @@ func setBodyHandler(ctx *blockCtx) {
}
}

func gmxMainFunc(p *gox.Package, ctx *pkgCtx) {
func gmxMainFunc(p *gox.Package, ctx *pkgCtx) bool {
if o := p.Types.Scope().Lookup(ctx.gameClass); o != nil && hasMethod(o, "MainEntry") {
// new(Game).Main()
p.NewFunc(nil, "main", nil, nil, false).BodyStart(p).
Val(p.Builtin().Ref("new")).Val(o).Call(1).
MemberVal("Main").Call(0).EndStmt().
End()
return true
}
return false
}

// -----------------------------------------------------------------------------
23 changes: 15 additions & 8 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,10 +561,19 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package,
return sfiles[i].path < sfiles[j].path
})

// genMain = true if it is main package and no main func
var genMain bool
if pkg.Name == "main" {
_, hasMain := ctx.syms["main"]
genMain = !hasMain
}

for _, f := range sfiles {
if f.IsProj {
loadFile(ctx, f.File)
gmxMainFunc(p, ctx)
if genMain { // generate main func if need
genMain = !gmxMainFunc(p, ctx)
}
break
}
}
Expand All @@ -586,12 +595,10 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package,
}
err = ctx.complete()

if !conf.NoAutoGenMain && pkg.Name == "main" {
if obj := p.Types.Scope().Lookup("main"); obj == nil {
old, _ := p.SetCurFile(defaultGoFile, false)
p.NewFunc(nil, "main", nil, nil, false).BodyStart(p).End()
p.RestoreCurFile(old)
}
if genMain && !conf.NoAutoGenMain { // generate empty main func
old, _ := p.SetCurFile(defaultGoFile, false)
p.NewFunc(nil, "main", nil, nil, false).BodyStart(p).End()
p.RestoreCurFile(old)
}
return
}
Expand Down Expand Up @@ -801,7 +808,7 @@ func preloadGopFile(p *gox.Package, ctx *blockCtx, file string, f *ast.File, con
}}}
}
// check class project no MainEntry and auto added
if f.IsProj && !conf.NoAutoGenMain && !f.NoEntrypoint() && f.Name.Name == "main" {
if f.IsProj && !conf.NoAutoGenMain && !f.HasShadowEntry() && f.Name.Name == "main" {
entry := getEntrypoint(f)
var hasEntry bool
for _, decl := range f.Decls {
Expand Down
2 changes: 1 addition & 1 deletion cl/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func gopClTestFS(t *testing.T, conf *cl.Config, fs parser.FileSystem, pkgname, e
}

func TestStringLitBasic(t *testing.T) {
gopClTest(t, `println "$$"`, `package main
gopClTest(t, `echo "$$"`, `package main

import "fmt"

Expand Down
2 changes: 1 addition & 1 deletion parser/parsertest/parsertest.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func Fprint(w io.Writer, pkg *ast.Package) {
for _, fpath := range paths {
fmt.Fprintf(w, "\nfile %s\n", filepath.Base(fpath))
file := pkg.Files[fpath]
if file.NoEntrypoint() {
if file.HasShadowEntry() {
fmt.Fprintf(w, "noEntrypoint\n")
}
FprintNode(w, "", file.Decls, "", " ")
Expand Down