Hi,

I was looking at purego <https://github.com/ebitengine/purego> dynamic
loading library (without cgo) and loved the idea.
It seems to be working quite well across many OS/ARCH combinations.
You first Dlopen a library, then RegisterLibFunc functions from it.

In sdl3 <https://github.com/JupiterRider/purego-sdl3> binding library, it
is effectively used like this:

var sdlInit func(InitFlags) bool
func init() {
    libSDL, _ := purego.Dlopen(libraryFileName, purego.RTLD_LAZY)
    purego.RegisterLibFunc(&sdlInit, libSDL, "SDL_Init")
}
func Init(flags InitFlags) bool {
    return sdlInit(flags)
}

So
- sdlInit is an unexported, mutable variable set at runtime (application
start)
- Init is bound at link-time, exported and immutable (let's ignore function
inlining)
- they have the exact same signature, you can do: sdlInit = Init

Go already allows implementing a function outside the language (in
Assembly) like
func Init(flags InitFlags) bool
// implementation in Assembly

But this is still bound at link-time. What I am wondering is the
possibility to have just the following and get rid of writable variables
like sdlInit:
func Init(InitFlags) bool
func init() {
    libSDL, _ := purego.Dlopen(libraryFileName, purego.RTLD_LAZY)
    purego.RegisterLibFunc(&Init, libSDL, "SDL_Init")
}

So this is like filling a function pointer table at runtime (application
startup). This would be doable only from inside init(). What do you think?

Regards..

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/golang-nuts/CAPqC6xSbeqkg%3DnzwwpgDrs5OkXjaXNzpFE8nBvh8V3tw0Rmcrg%40mail.gmail.com.

Reply via email to