Skip to content

Commit

Permalink
Merge pull request #290 from xushiwei/q
Browse files Browse the repository at this point in the history
pkg.TryImport
  • Loading branch information
xushiwei authored Oct 20, 2023
2 parents 1ef4852 + 5477712 commit dc69970
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 32 deletions.
81 changes: 50 additions & 31 deletions builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -1275,53 +1275,61 @@ var (
)

func initBuiltinTIs(pkg *Package) {
strconv := pkg.Import("strconv")
strings := pkg.Import("strings")
os := pkg.Import("os")
ioxTI := (*builtinTI)(nil)
ioxPkg := pkg.conf.PkgPathIox
if debugImportIox && ioxPkg == "" {
ioxPkg = "github.com/goplus/gox/internal/iox"
}
if ioxPkg != "" {
iox := pkg.Import(ioxPkg)
ioxTI = &builtinTI{
typ: os.Ref("File").Type(),
methods: []*builtinMethod{
{"Gop_Enum", iox.Ref("EnumLines"), nil},
},
}
}
var (
float64TI, intTI, int64TI, uint64TI *builtinTI
ioxTI, stringTI, stringSliceTI *builtinTI
)
btiMap := new(typeutil.Map)
strconv := pkg.TryImport("strconv")
strings := pkg.TryImport("strings")
btoLen := types.Universe.Lookup("len")
btoCap := types.Universe.Lookup("cap")
tis := []*builtinTI{
ioxTI,
{
{
ioxPkg := pkg.conf.PkgPathIox
if debugImportIox && ioxPkg == "" {
ioxPkg = "github.com/goplus/gox/internal/iox"
}
if ioxPkg != "" {
if os := pkg.TryImport("os"); os != nil {
if iox := pkg.TryImport(ioxPkg); iox != nil {
ioxTI = &builtinTI{
typ: os.Ref("File").Type(),
methods: []*builtinMethod{
{"Gop_Enum", iox.Ref("EnumLines"), nil},
},
}
}
}
}
}
if strconv != nil {
float64TI = &builtinTI{
typ: types.Typ[types.Float64],
methods: []*builtinMethod{
{"String", strconv.Ref("FormatFloat"), bmExargs{'g', -1, 64}},
},
},
{
}
intTI = &builtinTI{
typ: types.Typ[types.Int],
methods: []*builtinMethod{
{"String", strconv.Ref("Itoa"), nil},
},
},
{
}
int64TI = &builtinTI{
typ: types.Typ[types.Int64],
methods: []*builtinMethod{
{"String", strconv.Ref("FormatInt"), bmExargs{10}},
},
},
{
}
uint64TI = &builtinTI{
typ: types.Typ[types.Uint64],
methods: []*builtinMethod{
{"String", strconv.Ref("FormatUint"), bmExargs{10}},
},
},
{
}
}
if strings != nil && strconv != nil {
stringTI = &builtinTI{
typ: types.Typ[types.String],
methods: []*builtinMethod{
{"Len", btoLen, nil},
Expand Down Expand Up @@ -1364,15 +1372,26 @@ func initBuiltinTIs(pkg *Package) {
{"TrimPrefix", strings.Ref("TrimPrefix"), nil},
{"TrimSuffix", strings.Ref("TrimSuffix"), nil},
},
},
{
}
}
if strings != nil {
stringSliceTI = &builtinTI{
typ: types.NewSlice(types.Typ[types.String]),
methods: []*builtinMethod{
{"Len", btoLen, nil},
{"Cap", btoCap, nil},
{"Join", strings.Ref("Join"), nil},
},
},
}
}
tis := []*builtinTI{
ioxTI,
float64TI,
intTI,
int64TI,
uint64TI,
stringTI,
stringSliceTI,
{
typ: tySlice,
methods: []*builtinMethod{
Expand Down
12 changes: 12 additions & 0 deletions builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,18 @@ func TestUnsafe(t *testing.T) {
}
}

func TestTryImport(t *testing.T) {
defer func() {
if e := recover(); e != nil {
t.Fatal("TestTryImport: panic?")
}
}()
pkg := NewPackage("foo", "foo", gblConf)
if pkg.TryImport("not/exist") != nil {
t.Fatal("TryImport: exist?")
}
}

func TestUntypeBig(t *testing.T) {
pkg := NewPackage("foo", "foo", gblConf)
big := pkg.Import("github.com/goplus/gox/internal/builtin")
Expand Down
10 changes: 9 additions & 1 deletion import.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,19 @@ func toIndex(c byte) int {

// ----------------------------------------------------------------------------

// Import func
// Import imports a package by pkgPath. It will panic if pkgPath not found.
func (p *Package) Import(pkgPath string, src ...ast.Node) *PkgRef {
return p.file.importPkg(p, pkgPath, getSrc(src))
}

// TryImport imports a package by pkgPath. It returns nil if pkgPath not found.
func (p *Package) TryImport(pkgPath string) *PkgRef {
defer func() {
recover()
}()
return p.file.importPkg(p, pkgPath, nil)
}

func (p *Package) big() *PkgRef {
return p.file.big(p)
}
Expand Down

0 comments on commit dc69970

Please sign in to comment.