Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os, dynlib
- type
- dlHandle* = ref dlHandleObj
- dlHandleObj = object
- handle*: LibHandle
- path*: string
- loadFuncs: (proc ())
- proc newDL*(): dlHandle =
- new(result)
- proc loadDL*(self: dlHandle, path: string, global_symbols: bool = false) =
- self.handle = loadLib(path, global_symbols)
- if self.handle != nil:
- self.path = path
- self.loadFuncs()
- else:
- raise newException(IOError, "dynamic library " & path & " could not be read.")
- proc unloadDL*(self: dlHandle) =
- unloadLib(self.handle)
- self.handle = nil
- self.path = 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