Skip to content

Commit

Permalink
classfile: auto-import statement
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Jan 31, 2024
1 parent 8d4baeb commit d899ba7
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 6 deletions.
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 @@ -366,6 +366,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 @@ -388,6 +389,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 @@ -727,6 +731,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
}
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.20240130050415-3bd8cee57731
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.20240130050415-3bd8cee57731 h1:ZjWvtPmuf15J8dmcl1ILOmxi400Bf1y6mAMsVPUbg7I=
github.com/goplus/gox v1.14.1-0.20240130050415-3bd8cee57731/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

0 comments on commit d899ba7

Please sign in to comment.