Repository: goplus/gop
Pull request #1693 (2024-01-31 14:06:03)

classfile: auto-import statement; gop get: use modload.AddRequire with hasProj


Patch:
diff --git a/cl/classfile.go b/cl/classfile.go
index d019783e..725811e0 100644
--- a/cl/classfile.go
+++ b/cl/classfile.go
@@ -47,6 +47,7 @@ type gmxProject struct {
 	schedStmts []goast.Stmt // nil or len(scheds) == 2 (delayload)
 	pkgImps    []gox.PkgRef
 	pkgPaths   []string
+	autoimps   map[string]pkgImp // auto-import statement in gop.mod
 	hasScheds  bool
 	gameIsPtr  bool
 	isTest     bool
@@ -99,6 +100,20 @@ func loadClass(ctx *pkgCtx, pkg *gox.Package, file string, f *ast.File, conf *Co
 			p.pkgImps[i] = pkg.Import(pkgPath)
 		}
 
+		if len(gt.Import) > 0 {
+			autoimps := make(map[string]pkgImp)
+			for _, imp := range gt.Import {
+				pkgi := pkg.Import(imp.Path)
+				name := imp.Name
+				if name == "" {
+					name = pkgi.Types.Name()
+				}
+				pkgName := types.NewPkgName(token.NoPos, pkg.Types, name, pkgi.Types)
+				autoimps[name] = pkgImp{pkgi, pkgName}
+			}
+			p.autoimps = autoimps
+		}
+
 		spx := p.pkgImps[0]
 		if gt.Class != "" {
 			p.game, p.gameIsPtr = spxRef(spx, gt.Class)
diff --git a/cl/compile.go b/cl/compile.go
index dc44f3c2..00b515fe 100644
--- a/cl/compile.go
+++ b/cl/compile.go
@@ -365,6 +365,7 @@ type blockCtx struct {
 	pkg        *gox.Package
 	cb         *gox.CodeBuilder
 	imports    map[string]pkgImp
+	autoimps   map[string]pkgImp
 	lookups    []gox.PkgRef
 	clookups   []cpackages.PkgRef
 	tlookup    *typeParamLookup
@@ -387,6 +388,9 @@ func (bc *blockCtx) recorder() *typesRecorder {
 
 func (bc *blockCtx) findImport(name string) (pi pkgImp, ok bool) {
 	pi, ok = bc.imports[name]
+	if !ok && bc.autoimps != nil {
+		pi, ok = bc.autoimps[name]
+	}
 	return
 }
 
@@ -726,6 +730,7 @@ func preloadGopFile(p *gox.Package, ctx *blockCtx, file string, f *ast.File, con
 		} else {
 			c := parent.classes[f]
 			proj, ctx.proj = c.proj, c.proj
+			ctx.autoimps = proj.autoimps
 			classType = c.tname
 			if isGoxTestFile(c.ext) { // test classfile
 				testType = c.tname
diff --git a/cl/internal/spx3/jwt/jwt.go b/cl/internal/spx3/jwt/jwt.go
new file mode 100644
index 00000000..4e56fe6c
--- /dev/null
+++ b/cl/internal/spx3/jwt/jwt.go
@@ -0,0 +1,21 @@
+/*
+ * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package jwt
+
+func Token(v string) string {
+	return "token: " + v
+}
diff --git a/cmd/internal/gopget/get.go b/cmd/internal/gopget/get.go
index e3a909cf..b9b6756a 100644
--- a/cmd/internal/gopget/get.go
+++ b/cmd/internal/gopget/get.go
@@ -79,12 +79,8 @@ func get(pkgPath string) {
 
 	pkgMod, err := modload.Load(pkgModRoot)
 	check(err)
-	if pkgMod.HasProject() {
-		mod.Opt.AddImport(pkgModVer.Path)
-		fmt.Fprintf(os.Stderr, "gop get: import %s\n", pkgModVer.Path)
-	}
 
-	check(mod.AddRequire(pkgModVer.Path, pkgModVer.Version))
+	check(mod.AddRequire(pkgModVer.Path, pkgModVer.Version, pkgMod.HasProject()))
 	fmt.Fprintf(os.Stderr, "gop get: added %s %s\n", pkgModVer.Path, pkgModVer.Version)
 
 	check(mod.Save())
diff --git a/go.mod b/go.mod
index b375e151..fd5e0c4e 100644
--- a/go.mod
+++ b/go.mod
@@ -6,7 +6,7 @@ require (
 	github.com/fsnotify/fsnotify v1.7.0
 	github.com/goplus/c2go v0.7.20
 	github.com/goplus/gox v1.14.1-0.20240130100601-34b84d88ef29
-	github.com/goplus/mod v0.12.3
+	github.com/goplus/mod v0.12.4-0.20240131130222-5a7b7e160d0a
 	github.com/qiniu/x v1.13.2
 	golang.org/x/tools v0.17.0
 )
diff --git a/go.sum b/go.sum
index 9fef9bc4..e98d660c 100644
--- a/go.sum
+++ b/go.sum
@@ -10,8 +10,8 @@ github.com/goplus/gox v1.14.0/go.mod h1:G7Hz+cAOUyJyN9pPHrpqhfQPDUtiJNmoRVTwoaQ9
 github.com/goplus/gox v1.14.1-0.20240130100601-34b84d88ef29 h1:Sv0zFjy7HzYQMShOY8dZfL/5O+MrKboQamRouM2w6l0=
 github.com/goplus/gox v1.14.1-0.20240130100601-34b84d88ef29/go.mod h1:G7Hz+cAOUyJyN9pPHrpqhfQPDUtiJNmoRVTwoaQ9nw0=
 github.com/goplus/mod v0.12.2/go.mod h1:ZtlS9wHOcAVxZ/zq7WLdKVes1HG/8Yn3KNuWZGcpeTs=
-github.com/goplus/mod v0.12.3 h1:qLU5/F27CzUTQhCQRN1WruiCepUn5GdLKQTB41OsYfk=
-github.com/goplus/mod v0.12.3/go.mod h1:ZtlS9wHOcAVxZ/zq7WLdKVes1HG/8Yn3KNuWZGcpeTs=
+github.com/goplus/mod v0.12.4-0.20240131130222-5a7b7e160d0a h1:zsEBAx8oHpb1m3nCKYTIbKQGBqec0N28MlO8iC68Hho=
+github.com/goplus/mod v0.12.4-0.20240131130222-5a7b7e160d0a/go.mod h1:ZtlS9wHOcAVxZ/zq7WLdKVes1HG/8Yn3KNuWZGcpeTs=
 github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
 github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
 github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=


Test patch:
diff --git a/cl/compile_spx_test.go b/cl/compile_spx_test.go
index 398c9d89..32041a5f 100644
--- a/cl/compile_spx_test.go
+++ b/cl/compile_spx_test.go
@@ -50,7 +50,8 @@ func lookupClass(ext string) (c *modfile.Project, ok bool) {
 		return &modfile.Project{
 			Ext: "_spx.gox", Class: "Game",
 			Works:    []*modfile.Class{{Ext: "_spx.gox", Class: "Sprite"}},
-			PkgPaths: []string{"github.com/goplus/gop/cl/internal/spx3", "math"}}, true
+			PkgPaths: []string{"github.com/goplus/gop/cl/internal/spx3", "math"},
+			Import:   []*modfile.Import{{Path: "github.com/goplus/gop/cl/internal/spx3/jwt"}}}, true
 	case "_xtest.gox":
 		return &modfile.Project{
 			Ext: "_xtest.gox", Class: "App",
@@ -397,12 +398,13 @@ var (
 
 run
 `, `
-echo "Hi"
+echo jwt.token("Hi")
 `, `package main
 
 import (
 	"fmt"
 	"github.com/goplus/gop/cl/internal/spx3"
+	"github.com/goplus/gop/cl/internal/spx3/jwt"
 )
 
 type Kai struct {
@@ -421,7 +423,7 @@ func main() {
 	spx3.Gopt_Game_Main(new(Game), new(Kai))
 }
 func (this *Kai) Main() {
-	fmt.Println("Hi")
+	fmt.Println(jwt.Token("Hi"))
 }
 `, "main_spx.gox", "Kai_spx.gox")
 }