Repository: goplus/gop
Pull request #1170 (2022-05-16 03:48:55)

Revert "Revert "gox v1.11.3""
Reverts goplus/gop#1169


Patch:
diff --git a/cl/compile.go b/cl/compile.go
index b74fa9d5..f017a22f 100644
--- a/cl/compile.go
+++ b/cl/compile.go
@@ -350,6 +350,11 @@ func (p *pkgCtx) handleRecover(e interface{}) {
 	p.handleErr(err)
 }
 
+const (
+	defaultGoFile = ""
+	testingGoFile = "_test"
+)
+
 // NewPackage creates a Go+ package instance.
 func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package, err error) {
 	workingDir := conf.WorkingDir
@@ -369,6 +374,7 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package,
 		HandleErr:       ctx.handleErr,
 		NodeInterpreter: interp,
 		NewBuiltin:      newBuiltinDefault,
+		DefaultGoFile:   defaultGoFile,
 	}
 	p = gox.NewPackage(pkgPath, pkg.Name, confGox)
 	for file, gmx := range pkg.Files {
@@ -402,9 +408,9 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package,
 
 	if !conf.NoAutoGenMain && pkg.Name == "main" {
 		if obj := p.Types.Scope().Lookup("main"); obj == nil {
-			old := p.SetInTestingFile(false)
+			old, _ := p.SetCurFile(defaultGoFile, false)
 			p.NewFunc(nil, "main", nil, nil, false).BodyStart(p).End()
-			p.SetInTestingFile(old)
+			p.RestoreCurFile(old)
 		}
 	}
 
@@ -468,10 +474,17 @@ func loadFile(ctx *pkgCtx, f *ast.File) {
 	}
 }
 
+func getGoFile(file string) string {
+	if strings.HasSuffix(file, "_test.gop") {
+		return testingGoFile
+	}
+	return defaultGoFile
+}
+
 func preloadFile(p *gox.Package, parent *pkgCtx, file string, f *ast.File, targetDir string, conf *Config) {
 	syms := parent.syms
 	fileLine := !conf.NoFileLine
-	testingFile := strings.HasSuffix(file, "_test.gop")
+	goFile := getGoFile(file)
 	ctx := &blockCtx{
 		pkg: p, pkgCtx: parent, cb: p.CB(), fset: p.Fset, targetDir: targetDir,
 		fileLine: fileLine, relativePath: conf.RelativePath, isClass: f.IsClass,
@@ -588,8 +601,8 @@ func preloadFile(p *gox.Package, parent *pkgCtx, file string, f *ast.File, targe
 			if d.Recv == nil {
 				name := d.Name
 				fn := func() {
-					old := p.SetInTestingFile(testingFile)
-					defer p.SetInTestingFile(old)
+					old, _ := p.SetCurFile(goFile, true)
+					defer p.RestoreCurFile(old)
 					loadFunc(ctx, nil, d)
 				}
 				if name.Name == "init" {
@@ -610,8 +623,8 @@ func preloadFile(p *gox.Package, parent *pkgCtx, file string, f *ast.File, targe
 					}
 					ld := getTypeLoader(parent, syms, token.NoPos, name)
 					ld.methods = append(ld.methods, func() {
-						old := p.SetInTestingFile(testingFile)
-						defer p.SetInTestingFile(old)
+						old, _ := p.SetCurFile(goFile, true)
+						defer p.RestoreCurFile(old)
 						doInitType(ld)
 						recv := toRecv(ctx, d.Recv)
 						loadFunc(ctx, recv, d)
@@ -621,7 +634,7 @@ func preloadFile(p *gox.Package, parent *pkgCtx, file string, f *ast.File, targe
 		case *ast.GenDecl:
 			switch d.Tok {
 			case token.IMPORT:
-				p.SetInTestingFile(testingFile)
+				p.SetCurFile(goFile, true)
 				for _, item := range d.Specs {
 					loadImport(ctx, item.(*ast.ImportSpec))
 				}
@@ -634,8 +647,8 @@ func preloadFile(p *gox.Package, parent *pkgCtx, file string, f *ast.File, targe
 					}
 					ld := getTypeLoader(parent, syms, t.Name.Pos(), name)
 					ld.typ = func() {
-						old := p.SetInTestingFile(testingFile)
-						defer p.SetInTestingFile(old)
+						old, _ := p.SetCurFile(goFile, true)
+						defer p.RestoreCurFile(old)
 						if t.Assign != token.NoPos { // alias type
 							if debugLoad {
 								log.Println("==> Load > AliasType", name)
@@ -670,8 +683,8 @@ func preloadFile(p *gox.Package, parent *pkgCtx, file string, f *ast.File, targe
 					}
 					setNamesLoader(parent, syms, vSpec.Names, func() {
 						if c := cdecl; c != nil {
-							old := p.SetInTestingFile(testingFile)
-							defer p.SetInTestingFile(old)
+							old, _ := p.SetCurFile(goFile, true)
+							defer p.RestoreCurFile(old)
 							cdecl = nil
 							loadConstSpecs(ctx, c, d.Specs)
 							for _, s := range d.Specs {
@@ -689,8 +702,8 @@ func preloadFile(p *gox.Package, parent *pkgCtx, file string, f *ast.File, targe
 					}
 					setNamesLoader(parent, syms, vSpec.Names, func() {
 						if v := vSpec; v != nil { // only init once
-							old := p.SetInTestingFile(testingFile)
-							defer p.SetInTestingFile(old)
+							old, _ := p.SetCurFile(goFile, true)
+							defer p.RestoreCurFile(old)
 							vSpec = nil
 							loadVars(ctx, v, true)
 							removeNames(syms, v.Names)
diff --git a/go.mod b/go.mod
index e76d9315..f72ba29c 100644
--- a/go.mod
+++ b/go.mod
@@ -3,7 +3,7 @@ module github.com/goplus/gop
 go 1.16
 
 require (
-	github.com/goplus/gox v1.10.10
+	github.com/goplus/gox v1.11.3
 	github.com/qiniu/x v1.11.5
 	golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3
 	golang.org/x/tools v0.1.10
diff --git a/go.sum b/go.sum
index 97f51bb5..50cf9951 100644
--- a/go.sum
+++ b/go.sum
@@ -1,5 +1,5 @@
-github.com/goplus/gox v1.10.10 h1:0CzfuLeg1Mmdvz/ZOL7PW57+p2yCl0Prj+lfo5NuWyg=
-github.com/goplus/gox v1.10.10/go.mod h1:gu7fuQF8RmWPZUjd+tEJGuV3m/vOEv0bHrct0x/KatM=
+github.com/goplus/gox v1.11.3 h1:CU4AhLkC2bs5gMgperqluZk6hc8oR03OHdCZVXJABdQ=
+github.com/goplus/gox v1.11.3/go.mod h1:gu7fuQF8RmWPZUjd+tEJGuV3m/vOEv0bHrct0x/KatM=
 github.com/qiniu/x v1.11.5 h1:TYr5cl4g2yoHAZeDK4MTjKF6CMoG+IHlCDvvM5qym6U=
 github.com/qiniu/x v1.11.5/go.mod h1:03Ni9tj+N2h2aKnAz+6N0Xfl8FwMEDRC2PAlxekASDs=
 github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
diff --git a/x/gengo/gengo.go b/x/gengo/gengo.go
index def17b5a..234140bd 100644
--- a/x/gengo/gengo.go
+++ b/x/gengo/gengo.go
@@ -212,6 +212,7 @@ func getKeys(v map[string]none) []string {
 }
 
 const (
+	testingGoFile    = "_test"
 	autoGenFile      = "gop_autogen.go"
 	autoGenTestFile  = "gop_autogen_test.go"
 	autoGen2TestFile = "gop_autogen2_test.go"
@@ -295,7 +296,7 @@ func (p *Runner) doGenGoPkg(pkgi *pkgInfo, imp types.Importer, conf *Config) (er
 			conf.OnErr("compile", e)
 			return e
 		}
-		err = gox.WriteFile(filepath.Join(pkgDir, autoGen2TestFile), out, true)
+		err = out.WriteFile(filepath.Join(pkgDir, autoGen2TestFile), testingGoFile)
 		if err != nil {
 			conf.OnErr("compile", err)
 		}
@@ -322,13 +323,13 @@ func saveGoFile(dir string, pkg *gox.Package, pkgi *pkgInfo) error {
 		if err != nil {
 			return
 		}
-		return gox.WriteTo(f, pkg, false)
+		return pkg.WriteTo(f)
 	}()
 	if err != nil {
 		return err
 	}
-	if pkg.HasTestingFile() {
-		return gox.WriteFile(filepath.Join(dir, autoGenTestFile), pkg, true)
+	if _, ok := pkg.File(testingGoFile); ok {
+		return pkg.WriteFile(filepath.Join(dir, autoGenTestFile), testingGoFile)
 	}
 	return nil
 }
diff --git a/x/gopproj/gopfiles.go b/x/gopproj/gopfiles.go
index dcbccde4..2d677227 100644
--- a/x/gopproj/gopfiles.go
+++ b/x/gopproj/gopfiles.go
@@ -27,7 +27,6 @@ import (
 	"github.com/goplus/gop/cl"
 	"github.com/goplus/gop/parser"
 	"github.com/goplus/gop/token"
-	"github.com/goplus/gox"
 	"github.com/goplus/gox/packages"
 )
 
@@ -132,7 +131,7 @@ func (p *gopFiles) GenGo(outFile, modFile string) error {
 	if err != nil {
 		return err
 	}
-	return gox.WriteFile(outFile, out, false)
+	return out.WriteFile(outFile)
 }
 
 // -----------------------------------------------------------------------------


Test patch:
diff --git a/cl/compile_spx_test.go b/cl/compile_spx_test.go
index f0dbfc04..18b88632 100644
--- a/cl/compile_spx_test.go
+++ b/cl/compile_spx_test.go
@@ -27,7 +27,6 @@ import (
 	"github.com/goplus/gop/parser/parsertest"
 	"github.com/goplus/gop/scanner"
 	"github.com/goplus/gop/x/gopmod"
-	"github.com/goplus/gox"
 )
 
 func newTwoFileFS(dir string, fname, data string, fname2 string, data2 string) *parsertest.MemFS {
@@ -90,7 +89,7 @@ func gopSpxTestExConf(t *testing.T, name string, conf *cl.Config, gmx, spxcode,
 			t.Fatal("NewPackage:", err)
 		}
 		var b bytes.Buffer
-		err = gox.WriteTo(&b, pkg, false)
+		err = pkg.WriteTo(&b)
 		if err != nil {
 			t.Fatal("gox.WriteTo failed:", err)
 		}
diff --git a/cl/compile_test.go b/cl/compile_test.go
index cadc4fe8..e2567228 100644
--- a/cl/compile_test.go
+++ b/cl/compile_test.go
@@ -77,7 +77,7 @@ func gopClTestEx(t *testing.T, conf *cl.Config, pkgname, gopcode, expected strin
 		t.Fatal("NewPackage:", err)
 	}
 	var b bytes.Buffer
-	err = gox.WriteTo(&b, pkg, false)
+	err = pkg.WriteTo(&b)
 	if err != nil {
 		t.Fatal("gox.WriteTo failed:", err)
 	}
diff --git a/cl/run_test.go b/cl/run_test.go
index 4687d724..e05b025d 100644
--- a/cl/run_test.go
+++ b/cl/run_test.go
@@ -15,7 +15,6 @@ import (
 	"github.com/goplus/gop/parser"
 	"github.com/goplus/gop/parser/parsertest"
 	"github.com/goplus/gop/scanner"
-	"github.com/goplus/gox"
 )
 
 var (
@@ -95,7 +94,7 @@ func genGo(t *testing.T, conf *cl.Config, gopcode string) []byte {
 		t.Fatal("NewPackage:", err)
 	}
 	var b bytes.Buffer
-	err = gox.WriteTo(&b, pkg, false)
+	err = pkg.WriteTo(&b)
 	if err != nil {
 		t.Fatal("gox.WriteTo failed:", err)
 	}