github.com/goplus/gossa@v0.3.25/package.go (about) 1 package gossa 2 3 import ( 4 "go/constant" 5 "reflect" 6 ) 7 8 var ( 9 registerPkgs = make(map[string]*Package) 10 ) 11 12 // LookupPackage lookup register pkgs 13 func LookupPackage(name string) (pkg *Package, ok bool) { 14 pkg, ok = registerPkgs[name] 15 return 16 } 17 18 // RegisterPackage register pkg 19 func RegisterPackage(pkg *Package) { 20 if p, ok := registerPkgs[pkg.Path]; ok { 21 p.merge(pkg) 22 return 23 } 24 registerPkgs[pkg.Path] = pkg 25 // externPackages[pkg.Path] = true 26 } 27 28 type TypedConst struct { 29 Typ reflect.Type 30 Value constant.Value 31 } 32 33 type UntypedConst struct { 34 Typ string 35 Value constant.Value 36 } 37 38 type NamedType struct { 39 Typ reflect.Type 40 Methods string 41 PtrMethods string 42 } 43 44 type Package struct { 45 Name string 46 Path string 47 Interfaces map[string]reflect.Type 48 NamedTypes map[string]NamedType 49 AliasTypes map[string]reflect.Type 50 Vars map[string]reflect.Value 51 Funcs map[string]reflect.Value 52 TypedConsts map[string]TypedConst 53 UntypedConsts map[string]UntypedConst 54 Deps map[string]string 55 methods map[string]reflect.Value // methods cached 56 } 57 58 // merge same package 59 func (p *Package) merge(same *Package) { 60 for k, v := range same.Interfaces { 61 p.Interfaces[k] = v 62 } 63 for k, v := range same.NamedTypes { 64 p.NamedTypes[k] = v 65 } 66 for k, v := range same.Vars { 67 p.Vars[k] = v 68 } 69 for k, v := range same.Funcs { 70 p.Funcs[k] = v 71 } 72 for k, v := range same.UntypedConsts { 73 p.UntypedConsts[k] = v 74 } 75 } 76 77 var ( 78 externValues = make(map[string]reflect.Value) 79 ) 80 81 // register external function for no function body 82 func RegisterExternal(key string, i interface{}) { 83 externValues[key] = reflect.ValueOf(i) 84 }