Skip to content

Commit

Permalink
Merge pull request #1648 from xushiwei/ov
Browse files Browse the repository at this point in the history
initBuiltin: echo; call gmxMainFunc only if no main func
  • Loading branch information
xushiwei authored Jan 20, 2024
2 parents 4a42bdc + 2e3b77e commit d6c099b
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 12 deletions.
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 @@ -569,10 +569,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 @@ -594,12 +603,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 @@ -809,7 +816,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

0 comments on commit d6c099b

Please sign in to comment.