Guest User

proposed hotswap mechanism

a guest
Apr 13th, 2015
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Nim 1.19 KB | None | 0 0
  1. import os, dynlib
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8. type
  9.   DLHandle* = ref DLHandleObj
  10.   DLHandleObj = object
  11.     handle*: LibHandle
  12.     loadFuncs: (proc ())
  13.  
  14. proc newDL*(): DLHandle =
  15.   new(result)
  16.  
  17. proc loadDL*(handle: DLHandle, path: string, global_symbols: bool = false) =
  18.   handle.handle = loadLib(path, global_symbols)
  19.   if handle.handle != nil:
  20.     handle.loadFuncs()
  21.   else:
  22.     raise newException(IOError, "dynamic library " & path & " could not be read.")
  23.  
  24. proc unloadDL*(handle: DLHandle) =
  25.   unloadLib(handle.handle)
  26.   handle.handle = nil
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33. var foo = newDL()
  34.  
  35. proc something(): string {.importc, dynlib: foo.}
  36.   # when the dynlib pragma is given a DLHandle, it adds the proc to the DLHandle's loadFuncs function.
  37.   # loadFuncs is static at runtime. besides loadFuncs, the compiler is completely unaware of
  38.   # DLHandle's implementation.
  39.  
  40. foo.loadDL(os.getAppDir() / "modules/plugin/libplugin.so")
  41.   # running loadDL makes an attempt to load the library. if it's successful, it runs loadFuncs, giving
  42.   # procs with that DLHandle their symAddrs.
  43.  
  44. proc hello() =
  45.   echo("Hello, ", something(), "!")
  46.     # the dynamic library's procs can now be run without fear of SIGSEGV
  47.  
  48. hello()
Advertisement
Add Comment
Please, Sign In to add comment