github.com/gotranspile/cxgo@v0.3.8-0.20240118201721-29871598a6a2/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  const int RTLD_LAZY = 1;
    35  const int RTLD_NOW = 2;
    36  const int RTLD_GLOBAL = 4;
    37  const int RTLD_LOCAL = 8;
    38  
    39  typedef struct _cxgo_dllib {
    40  	void* (*Sym) (_cxgo_go_string);
    41  	_cxgo_go_int (*Close) ();
    42  } _cxgo_dllib;
    43  
    44  #define dlsym(l, s) ((_cxgo_dllib*)l)->Sym(s)
    45  #define dlclose(l) ((_cxgo_dllib*)b)->Close(v)
    46  `,
    47  		}
    48  		l.Declare(
    49  			c.NewIdent("dlopen", "dlopen.Open", dlopen.Open, c.FuncTT(c.PtrT(libT), c.Go().String(), gint)),
    50  			c.NewIdent("dlerror", "dlopen.Error", dlopen.Error, c.FuncTT(c.C().String())),
    51  		)
    52  		return l
    53  	})
    54  }