Repository: goplus/gop
Pull request #1169 (2022-05-16 03:41:18)

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


Patch:
diff --git a/cl/compile.go b/cl/compile.go
index f017a22f..b74fa9d5 100644
--- a/cl/compile.go
+++ b/cl/compile.go
@@ -350,11 +350,6 @@ 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
@@ -374,7 +369,6 @@ 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 {
@@ -408,9 +402,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.SetCurFile(defaultGoFile, false)
+			old := p.SetInTestingFile(false)
 			p.NewFunc(nil, "main", nil, nil, false).BodyStart(p).End()
-			p.RestoreCurFile(old)
+			p.SetInTestingFile(old)
 		}
 	}
 
@@ -474,17 +468,10 @@ 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
-	goFile := getGoFile(file)
+	testingFile := strings.HasSuffix(file, "_test.gop")
 	ctx := &blockCtx{
 		pkg: p, pkgCtx: parent, cb: p.CB(), fset: p.Fset, targetDir: targetDir,
 		fileLine: fileLine, relativePath: conf.RelativePath, isClass: f.IsClass,
@@ -601,8 +588,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.SetCurFile(goFile, true)
-					defer p.RestoreCurFile(old)
+					old := p.SetInTestingFile(testingFile)
+					defer p.SetInTestingFile(old)
 					loadFunc(ctx, nil, d)
 				}
 				if name.Name == "init" {
@@ -623,8 +610,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.SetCurFile(goFile, true)
-						defer p.RestoreCurFile(old)
+						old := p.SetInTestingFile(testingFile)
+						defer p.SetInTestingFile(old)
 						doInitType(ld)
 						recv := toRecv(ctx, d.Recv)
 						loadFunc(ctx, recv, d)
@@ -634,7 +621,7 @@ func preloadFile(p *gox.Package, parent *pkgCtx, file string, f *ast.File, targe
 		case *ast.GenDecl:
 			switch d.Tok {
 			case token.IMPORT:
-				p.SetCurFile(goFile, true)
+				p.SetInTestingFile(testingFile)
 				for _, item := range d.Specs {
 					loadImport(ctx, item.(*ast.ImportSpec))
 				}
@@ -647,8 +634,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.SetCurFile(goFile, true)
-						defer p.RestoreCurFile(old)
+						old := p.SetInTestingFile(testingFile)
+						defer p.SetInTestingFile(old)
 						if t.Assign != token.NoPos { // alias type
 							if debugLoad {
 								log.Println("==> Load > AliasType", name)
@@ -683,8 +670,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.SetCurFile(goFile, true)
-							defer p.RestoreCurFile(old)
+							old := p.SetInTestingFile(testingFile)
+							defer p.SetInTestingFile(old)
 							cdecl = nil
 							loadConstSpecs(ctx, c, d.Specs)
 							for _, s := range d.Specs {
@@ -702,8 +689,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.SetCurFile(goFile, true)
-							defer p.RestoreCurFile(old)
+							old := p.SetInTestingFile(testingFile)
+							defer p.SetInTestingFile(old)
 							vSpec = nil
 							loadVars(ctx, v, true)
 							removeNames(syms, v.Names)
diff --git a/go.mod b/go.mod
index f72ba29c..e76d9315 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.11.3
+	github.com/goplus/gox v1.10.10
 	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 50cf9951..97f51bb5 100644
--- a/go.sum
+++ b/go.sum
@@ -1,5 +1,5 @@
-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/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/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 234140bd..def17b5a 100644
--- a/x/gengo/gengo.go
+++ b/x/gengo/gengo.go
@@ -212,7 +212,6 @@ func getKeys(v map[string]none) []string {
 }
 
 const (
-	testingGoFile    = "_test"
 	autoGenFile      = "gop_autogen.go"
 	autoGenTestFile  = "gop_autogen_test.go"
 	autoGen2TestFile = "gop_autogen2_test.go"
@@ -296,7 +295,7 @@ func (p *Runner) doGenGoPkg(pkgi *pkgInfo, imp types.Importer, conf *Config) (er
 			conf.OnErr("compile", e)
 			return e
 		}
-		err = out.WriteFile(filepath.Join(pkgDir, autoGen2TestFile), testingGoFile)
+		err = gox.WriteFile(filepath.Join(pkgDir, autoGen2TestFile), out, true)
 		if err != nil {
 			conf.OnErr("compile", err)
 		}
@@ -323,13 +322,13 @@ func saveGoFile(dir string, pkg *gox.Package, pkgi *pkgInfo) error {
 		if err != nil {
 			return
 		}
-		return pkg.WriteTo(f)
+		return gox.WriteTo(f, pkg, false)
 	}()
 	if err != nil {
 		return err
 	}
-	if _, ok := pkg.File(testingGoFile); ok {
-		return pkg.WriteFile(filepath.Join(dir, autoGenTestFile), testingGoFile)
+	if pkg.HasTestingFile() {
+		return gox.WriteFile(filepath.Join(dir, autoGenTestFile), pkg, true)
 	}
 	return nil
 }
diff --git a/x/gopproj/gopfiles.go b/x/gopproj/gopfiles.go
index 2d677227..dcbccde4 100644
--- a/x/gopproj/gopfiles.go
+++ b/x/gopproj/gopfiles.go
@@ -27,6 +27,7 @@ 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"
 )
 
@@ -131,7 +132,7 @@ func (p *gopFiles) GenGo(outFile, modFile string) error {
 	if err != nil {
 		return err
 	}
-	return out.WriteFile(outFile)
+	return gox.WriteFile(outFile, out, false)
 }
 
 // -----------------------------------------------------------------------------


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