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

     1  package libs
     2  
     3  import (
     4  	"github.com/gotranspile/cxgo/runtime/dlopen"
     5  	"github.com/gotranspile/cxgo/types"
     6  )
     7  
     8  const (
     9  	dlfcnH = "dlfcn.h"
    10  )
    11  
    12  func init() {
    13  	RegisterLibrary(dlfcnH, func(c *Env) *Library {
    14  		gint := c.Go().Int()
    15  		libT := types.NamedTGo("_cxgo_dllib", "dlopen.Library", c.MethStructT(map[string]*types.FuncType{
    16  			"Sym":   c.FuncTT(c.Go().UnsafePtr(), c.Go().String()),
    17  			"Close": c.FuncTT(gint),
    18  		}))
    19  		l := &Library{
    20  			Idents: map[string]*types.Ident{
    21  				"RTLD_LAZY":   types.NewIdentGo("RTLD_LAZY", "dlopen.RTLD_LAZY", gint),
    22  				"RTLD_NOW":    types.NewIdentGo("RTLD_NOW", "dlopen.RTLD_NOW", gint),
    23  				"RTLD_GLOBAL": types.NewIdentGo("RTLD_GLOBAL", "dlopen.RTLD_GLOBAL", gint),
    24  				"RTLD_LOCAL":  types.NewIdentGo("RTLD_LOCAL", "dlopen.RTLD_LOCAL", gint),
    25  			},
    26  			Types: map[string]types.Type{
    27  				"_cxgo_dllib": libT,
    28  			},
    29  			Imports: map[string]string{
    30  				"libc":   RuntimeLibc,
    31  				"dlopen": RuntimePrefix + "dlopen",
    32  			},
    33  			Header: `
    34  #include <` + BuiltinH + `>
    35  
    36  const int RTLD_LAZY = 1;
    37  const int RTLD_NOW = 2;
    38  const int RTLD_GLOBAL = 4;
    39  const int RTLD_LOCAL = 8;
    40  
    41  typedef struct _cxgo_dllib {
    42  	void* (*Sym) (_cxgo_go_string);
    43  	_cxgo_go_int (*Close) ();
    44  } _cxgo_dllib;
    45  
    46  #define dlsym(l, s) ((_cxgo_dllib*)l)->Sym(s)
    47  #define dlclose(l) ((_cxgo_dllib*)b)->Close(v)
    48  `,
    49  		}
    50  		l.Declare(
    51  			c.NewIdent("dlopen", "dlopen.Open", dlopen.Open, c.FuncTT(c.PtrT(libT), c.Go().String(), gint)),
    52  			c.NewIdent("dlerror", "dlopen.Error", dlopen.Error, c.FuncTT(c.C().String())),
    53  		)
    54  		return l
    55  	})
    56  }