github.com/gotranspile/cxgo@v0.3.7/libs/config.go (about)

     1  package libs
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/gotranspile/cxgo/types"
     7  )
     8  
     9  // NewEnv creates a new environment. It uses GOARCH env to set sensible defaults.
    10  func NewEnv(conf types.Config) *Env {
    11  	return &Env{
    12  		Env:  types.NewEnv(conf),
    13  		libs: make(map[string]*Library),
    14  		imports: map[string]string{
    15  			"unsafe": "unsafe",
    16  			"math":   "math",
    17  			"libc":   RuntimeLibc,
    18  		},
    19  		macros: make(map[string]bool),
    20  	}
    21  }
    22  
    23  type Env struct {
    24  	*types.Env
    25  	libs    map[string]*Library
    26  	imports map[string]string
    27  	macros  map[string]bool
    28  }
    29  
    30  func (c *Env) Clone() *Env {
    31  	c2 := &Env{Env: c.Env}
    32  	c2.libs = make(map[string]*Library)
    33  	for k, v := range c.libs {
    34  		c2.libs[k] = v
    35  	}
    36  	c2.imports = make(map[string]string)
    37  	for k, v := range c.imports {
    38  		c2.imports[k] = v
    39  	}
    40  	c2.macros = make(map[string]bool)
    41  	for k, v := range c.macros {
    42  		c2.macros[k] = v
    43  	}
    44  	return c2
    45  }
    46  
    47  func (c *Env) ResolveImport(name string) string {
    48  	path := c.imports[name]
    49  	if path == "" {
    50  		path = name
    51  	}
    52  	return path
    53  }
    54  
    55  // LookupLibrary finds an already loaded Library. It is useful to prevent import loops.
    56  //
    57  // Typically, the GetLibrary function should be used instead, because it will load the library automatically, if needed.
    58  func (c *Env) LookupLibrary(name string) *Library {
    59  	l, ok := c.libs[name]
    60  	if !ok {
    61  		panic(errors.New("cannot find library: " + name))
    62  	}
    63  	return l
    64  }