Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os, dynlib
- type
- DLHandle* = ref DLHandleObj
- DLHandleObj = object
- handle*: LibHandle
- loadFuncs: (proc ())
- proc newDL*(): DLHandle =
- new(result)
- proc loadDL*(handle: DLHandle, path: string, global_symbols: bool = false) =
- handle.handle = loadLib(path, global_symbols)
- if handle.handle != nil:
- handle.loadFuncs()
- else:
- raise newException(IOError, "dynamic library " & path & " could not be read.")
- proc unloadDL*(handle: DLHandle) =
- unloadLib(handle.handle)
- handle.handle = nil
- var foo = newDL()
- proc something(): string {.importc, dynlib: foo.}
- # when the dynlib pragma is given a DLHandle, it adds the proc to the DLHandle's loadFuncs function.
- # loadFuncs is static at runtime. besides loadFuncs, the compiler is completely unaware of
- # DLHandle's implementation.
- foo.loadDL(os.getAppDir() / "modules/plugin/libplugin.so")
- # running loadDL makes an attempt to load the library. if it's successful, it runs loadFuncs, giving
- # procs with that DLHandle their symAddrs.
- proc hello() =
- echo("Hello, ", something(), "!")
- # the dynamic library's procs can now be run without fear of SIGSEGV
- hello()
Advertisement
Add Comment
Please, Sign In to add comment