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

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

Merged
merged 3 commits into from
Jan 31, 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
15 changes: 15 additions & 0 deletions cl/classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions cl/compile_spx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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 {
Expand All @@ -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")
}
Expand Down
21 changes: 21 additions & 0 deletions cl/internal/spx3/jwt/jwt.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 1 addition & 5 deletions cmd/internal/gopget/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
Loading