github.com/goplus/igop@v0.25.0/package.go (about)

     1  /*
     2   * Copyright (c) 2022 The GoPlus Authors (goplus.org). All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package igop
    18  
    19  import (
    20  	"go/constant"
    21  	"log"
    22  	"reflect"
    23  	"sort"
    24  )
    25  
    26  var (
    27  	registerPkgs = make(map[string]*Package)
    28  )
    29  
    30  // PackageList return all register packages
    31  func PackageList() (list []string) {
    32  	for pkg := range registerPkgs {
    33  		list = append(list, pkg)
    34  	}
    35  	sort.Strings(list)
    36  	return
    37  }
    38  
    39  // LookupPackage lookup register pkgs
    40  func LookupPackage(name string) (pkg *Package, ok bool) {
    41  	pkg, ok = registerPkgs[name]
    42  	return
    43  }
    44  
    45  // RegisterPackage register pkg
    46  func RegisterPackage(pkg *Package) {
    47  	if p, ok := registerPkgs[pkg.Path]; ok {
    48  		p.merge(pkg)
    49  		return
    50  	}
    51  	registerPkgs[pkg.Path] = pkg
    52  	//	externPackages[pkg.Path] = true
    53  }
    54  
    55  type TypedConst struct {
    56  	Typ   reflect.Type
    57  	Value constant.Value
    58  }
    59  
    60  type UntypedConst struct {
    61  	Typ   string
    62  	Value constant.Value
    63  }
    64  
    65  type Package struct {
    66  	Interfaces    map[string]reflect.Type
    67  	NamedTypes    map[string]reflect.Type
    68  	AliasTypes    map[string]reflect.Type
    69  	Vars          map[string]reflect.Value
    70  	Funcs         map[string]reflect.Value
    71  	TypedConsts   map[string]TypedConst
    72  	UntypedConsts map[string]UntypedConst
    73  	Deps          map[string]string // path -> name
    74  	Name          string
    75  	Path          string
    76  	Source        string
    77  }
    78  
    79  // merge same package
    80  func (p *Package) merge(same *Package) {
    81  	for k, v := range same.Interfaces {
    82  		p.Interfaces[k] = v
    83  	}
    84  	for k, v := range same.NamedTypes {
    85  		p.NamedTypes[k] = v
    86  	}
    87  	for k, v := range same.Vars {
    88  		p.Vars[k] = v
    89  	}
    90  	for k, v := range same.Funcs {
    91  		p.Funcs[k] = v
    92  	}
    93  	for k, v := range same.UntypedConsts {
    94  		p.UntypedConsts[k] = v
    95  	}
    96  }
    97  
    98  var (
    99  	externValues = make(map[string]reflect.Value)
   100  )
   101  
   102  // RegisterExternal is register external variable address or func
   103  func RegisterExternal(key string, i interface{}) {
   104  	if i == nil {
   105  		delete(externValues, key)
   106  		return
   107  	}
   108  	v := reflect.ValueOf(i)
   109  	switch v.Kind() {
   110  	case reflect.Func, reflect.Ptr:
   111  		externValues[key] = v
   112  	default:
   113  		log.Printf("register external must variable address or func. not %v\n", v.Kind())
   114  	}
   115  }