nimhcr.nim 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2018 Nim Contributors
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # This is the Nim hot code reloading run-time for the native targets.
  10. #
  11. # This minimal dynamic library is not subject to reloading when the
  12. # `hotCodeReloading` build mode is enabled. It's responsible for providing
  13. # a permanent memory location for all globals and procs within a program
  14. # and orchestrating the reloading. For globals, this is easily achieved
  15. # by storing them on the heap. For procs, we produce on the fly simple
  16. # trampolines that can be dynamically overwritten to jump to a different
  17. # target. In the host program, all globals and procs are first registered
  18. # here with ``hcrRegisterGlobal`` and ``hcrRegisterProc`` and then the
  19. # returned permanent locations are used in every reference to these symbols
  20. # onwards.
  21. #
  22. # Detailed description:
  23. #
  24. # When code is compiled with the hotCodeReloading option for native targets
  25. # a couple of things happen for all modules in a project:
  26. # - the useNimRtl option is forced (including when building the HCR runtime too)
  27. # - all modules of a target get built into separate shared libraries
  28. # - the smallest granularity of reloads is modules
  29. # - for each .c (or .cpp) in the corresponding nimcache folder of the project
  30. # a shared object is built with the name of the source file + DLL extension
  31. # - only the main module produces whatever the original project type intends
  32. # (again in nimcache) and is then copied to its original destination
  33. # - linking is done in parallel - just like compilation
  34. # - function calls to functions from the same project go through function pointers:
  35. # - with a few exceptions - see the nonReloadable pragma
  36. # - the forward declarations of the original functions become function
  37. # pointers as static globals with the same names
  38. # - the original function definitions get suffixed with <name>_actual
  39. # - the function pointers get initialized with the address of the corresponding
  40. # function in the DatInit of their module through a call to either hcrRegisterProc
  41. # or hcrGetProc. When being registered, the <name>_actual address is passed to
  42. # hcrRegisterProc and a permanent location is returned and assigned to the pointer.
  43. # This way the implementation (<name>_actual) can change but the address for it
  44. # will be the same - this works by just updating a jump instruction (trampoline).
  45. # For functions from other modules hcrGetProc is used (after they are registered).
  46. # - globals are initialized only once and their state is preserved
  47. # - including locals with the {.global.} pragma
  48. # - their definitions are changed into pointer definitions which are initialized
  49. # in the DatInit() of their module with calls to hcrRegisterGlobal (supplying the
  50. # size of the type that this HCR runtime should allocate) and a bool is returned
  51. # which when true triggers the initialization code for the global (only once).
  52. # Globals from other modules: a global pointer coupled with a hcrGetGlobal call.
  53. # - globals which have already been initialized cannot have their values changed
  54. # by changing their initialization - use a handler or some other mechanism
  55. # - new globals can be introduced when reloading
  56. # - top-level code (global scope) is executed only once - at the first module load
  57. # - the runtime knows every symbol's module owner (globals and procs)
  58. # - both the RTL and HCR shared libraries need to be near the program for execution
  59. # - same folder, in the PATH or LD_LIBRARY_PATH env var, etc (depending on OS)
  60. # - the main module is responsible for initializing the HCR runtime
  61. # - the main module loads the RTL and HCR shared objects
  62. # - after that a call to hcrInit() is done in the main module which triggers
  63. # the loading of all modules the main one imports, and doing that for the
  64. # dependencies of each module recursively. Basically a DFS traversal.
  65. # - then initialization takes place with several passes over all modules:
  66. # - HcrInit - initializes the pointers for HCR procs such as hcrRegisterProc
  67. # - HcrCreateTypeInfos - creates globals which will be referenced in the next pass
  68. # - DatInit - usual dat init + register/get procs and get globals
  69. # - Init - it does the following multiplexed operations:
  70. # - register globals (if already registered - then just retrieve pointer)
  71. # - execute top level scope (only if loaded for the first time)
  72. # - when modules are loaded the originally built shared libraries get copied in
  73. # the same folder and the copies are loaded instead of the original files
  74. # - a module import tree is built in the runtime (and maintained when reloading)
  75. # - hcrPerformCodeReload
  76. # - named `performCodeReload`, requires the hotcodereloading module
  77. # - explicitly called by the user - the current active callstack shouldn't contain
  78. # any functions which are defined in modules that will be reloaded (or crash!).
  79. # The reason is that old dynalic libraries get unloaded.
  80. # Example:
  81. # if A is the main module and it imports B, then only B is reloadable and only
  82. # if when calling hcrPerformCodeReload there is no function defined in B in the
  83. # current active callstack at the point of the call (it has to be done from A)
  84. # - for reloading to take place the user has to have rebuilt parts of the application
  85. # without changes affecting the main module in any way - it shouldn't be rebuilt.
  86. # - to determine what needs to be reloaded the runtime starts traversing the import
  87. # tree from the root and checks the timestamps of the loaded shared objects
  88. # - modules that are no longer referenced are unloaded and cleaned up properly
  89. # - symbols (procs/globals) that have been removed in the code are also cleaned up
  90. # - so changing the init of a global does nothing, but removing it, reloading,
  91. # and then re-introducing it with a new initializer works
  92. # - new modules can be imported, and imports can also be reodereded/removed
  93. # - hcrReloadNeeded() can be used to determine if any module needs reloading
  94. # - named `hasAnyModuleChanged`, requires the hotcodereloading module
  95. # - code in the beforeCodeReload/afterCodeReload handlers is executed on each reload
  96. # - require the hotcodereloading module
  97. # - such handlers can be added and removed
  98. # - before each reload all "beforeCodeReload" handlers are executed and after
  99. # that all handlers (including "after") from the particular module are deleted
  100. # - the order of execution is the same as the order of top-level code execution.
  101. # Example: if A imports B which imports C, then all handlers in C will be executed
  102. # first (from top to bottom) followed by all from B and lastly all from A
  103. # - after the reload all "after" handlers are executed the same way as "before"
  104. # - the handlers for a reloaded module are always removed when reloading and then
  105. # registered when the top-level scope is executed (thanks to `executeOnReload`)
  106. #
  107. # TODO next:
  108. #
  109. # - implement the before/after handlers and hasModuleChanged for the javascript target
  110. # - ARM support for the trampolines
  111. # - investigate:
  112. # - soon the system module might be importing other modules - the init order...?
  113. # - rethink the closure iterators
  114. # - ability to keep old versions of dynamic libraries alive
  115. # - because of async server code
  116. # - perhaps with refcounting of .dlls for unfinished closures
  117. # - linking with static libs
  118. # - all shared objects for each module will (probably) have to link to them
  119. # - state in static libs gets duplicated
  120. # - linking is slow and therefore iteration time suffers
  121. # - have just a single .dll for all .nim files and bulk reload?
  122. # - think about the compile/link/passc/passl/emit/injectStmt pragmas
  123. # - if a passc pragma is introduced (either written or dragged in by a new
  124. # import) the whole command line for compilation changes - for example:
  125. # winlean.nim: {.passc: "-DWIN32_LEAN_AND_MEAN".}
  126. # - play with plugins/dlls/lfIndirect/lfDynamicLib/lfExportLib - shouldn't add an extra '*'
  127. # - everything thread-local related
  128. # - tests
  129. # - add a new travis build matrix entry which builds everything with HCR enabled
  130. # - currently building with useNimRtl is problematic - lots of problems...
  131. # - how to supply the nimrtl/nimhcr shared objects to all test binaries...?
  132. # - think about building to C++ instead of only to C - added type safety
  133. # - run tests through valgrind and the sanitizers!
  134. #
  135. # TODO - nice to have cool stuff:
  136. #
  137. # - separate handling of global state for much faster reloading and manipulation
  138. # - imagine sliders in an IDE for tweaking variables
  139. # - perhaps using shared memory
  140. # - multi-dll projects - how everything can be reloaded..?
  141. # - a single HCR instance shared across multiple .dlls
  142. # - instead of having to call hcrPerformCodeReload from a function in each dll
  143. # - which currently renders the main module of each dll not reloadable
  144. # - ability to check with the current callstack if a reload is "legal"
  145. # - if it is in any function which is in a module about to be reloaded ==> error
  146. # - pragma annotations for files - to be excluded from dll shenanigans
  147. # - for such file-global pragmas look at codeReordering or injectStmt
  148. # - how would the initialization order be kept? messy...
  149. # - C code calling stable exportc interface of nim code (for bindings)
  150. # - generate proxy functions with the stable names
  151. # - in a non-reloadable part (the main binary) that call the function pointers
  152. # - parameter passing/forwarding - how? use the same trampoline jumping?
  153. # - extracting the dependencies for these stubs/proxies will be hard...
  154. # - changing memory layout of types - detecting this..?
  155. # - implement with registerType() call to HCR runtime...?
  156. # - and checking if a previously registered type matches
  157. # - issue an error
  158. # - or let the user handle this by transferring the state properly
  159. # - perhaps in the before/afterCodeReload handlers
  160. # - implement executeOnReload for global vars too - not just statements (and document!)
  161. # - cleanup at shutdown - freeing all globals
  162. # - fallback mechanism if the program crashes (the program should detect crashes
  163. # by itself using SEH/signals on Windows/Unix) - should be able to revert to
  164. # previous versions of the .dlls by calling some function from HCR
  165. # - improve runtime performance - possibilities
  166. # - implement a way for multiple .nim files to be bundled into the same dll
  167. # and have all calls within that domain to use the "_actual" versions of
  168. # procs so there are no indirections (or the ability to just bundle everything
  169. # except for a few unreloadable modules into a single mega reloadable dll)
  170. # - try to load the .dlls at specific addresses of memory (close to each other)
  171. # allocated with execution flags - check this: https://github.com/fancycode/MemoryModule
  172. #
  173. # TODO - unimportant:
  174. #
  175. # - have a "bad call" trampoline that all no-longer-present functions are routed to call there
  176. # - so the user gets some error msg if he calls a dangling pointer instead of a crash
  177. # - before/afterCodeReload and hasModuleChanged should be accessible only where appropriate
  178. # - nim_program_result is inaccessible in HCR mode from external C code (see nimbase.h)
  179. # - proper .json build file - but the format is different... multiple link commands...
  180. # - avoid registering globals on each loop when using an iterator in global scope
  181. #
  182. # TODO - REPL:
  183. # - proper way (as proposed by Zahary):
  184. # - parse the input code and put everything in global scope except for
  185. # statements with side effects only - those go in afterCodeReload blocks
  186. # - my very hacky idea: just append to a closure iterator the new statements
  187. # followed by a yield statement. So far I can think of 2 problems:
  188. # - import and some other code cannot be written inside of a proc -
  189. # has to be parsed and extracted in the outer scope
  190. # - when new variables are created they are actually locals to the closure
  191. # so the struct for the closure state grows in memory, but it has already
  192. # been allocated when the closure was created with the previous smaller size.
  193. # That would lead to working with memory outside of the initially allocated
  194. # block. Perhaps something can be done about this - some way of re-allocating
  195. # the state and transferring the old...
  196. when not defined(JS) and (defined(hotcodereloading) or
  197. defined(createNimHcr) or
  198. defined(testNimHcr)):
  199. const
  200. dllExt = when defined(windows): "dll"
  201. elif defined(macosx): "dylib"
  202. else: "so"
  203. type
  204. HcrProcGetter* = proc (libHandle: pointer, procName: cstring): pointer {.nimcall.}
  205. HcrGcMarkerProc = proc () {.nimcall.}
  206. HcrModuleInitializer* = proc () {.nimcall.}
  207. when defined(createNimHcr):
  208. when system.appType != "lib":
  209. {.error: "This file has to be compiled as a library!".}
  210. import os, tables, sets, times, strutils, reservedmem, dynlib
  211. template trace(args: varargs[untyped]) =
  212. when defined(testNimHcr) or defined(traceHcr):
  213. echo args
  214. proc sanitize(arg: Time): string =
  215. when defined(testNimHcr): return "<time>"
  216. else: return $arg
  217. proc sanitize(arg: string|cstring): string =
  218. when defined(testNimHcr): return ($arg).splitFile.name.splitFile.name
  219. else: return $arg
  220. {.pragma: nimhcr, compilerProc, exportc, dynlib.}
  221. when hostCPU in ["i386", "amd64"]:
  222. type
  223. ShortJumpInstruction {.packed.} = object
  224. opcode: byte
  225. offset: int32
  226. LongJumpInstruction {.packed.} = object
  227. opcode1: byte
  228. opcode2: byte
  229. offset: int32
  230. absoluteAddr: pointer
  231. proc writeJump(jumpTableEntry: ptr LongJumpInstruction, targetFn: pointer) =
  232. let
  233. jumpFrom = jumpTableEntry.shift(sizeof(ShortJumpInstruction))
  234. jumpDistance = distance(jumpFrom, targetFn)
  235. if abs(jumpDistance) < 0x7fff0000:
  236. let shortJump = cast[ptr ShortJumpInstruction](jumpTableEntry)
  237. shortJump.opcode = 0xE9 # relative jump
  238. shortJump.offset = int32(jumpDistance)
  239. else:
  240. jumpTableEntry.opcode1 = 0xff # indirect absolute jump
  241. jumpTableEntry.opcode2 = 0x25
  242. when hostCPU == "i386":
  243. # on x86 we write the absolute address of the following pointer
  244. jumpTableEntry.offset = cast[int32](addr jumpTableEntry.absoluteAddr)
  245. else:
  246. # on x64, we use a relative address for the same location
  247. jumpTableEntry.offset = 0
  248. jumpTableEntry.absoluteAddr = targetFn
  249. elif hostCPU == "arm":
  250. const jumpSize = 8
  251. elif hostCPU == "arm64":
  252. const jumpSize = 16
  253. const defaultJumpTableSize = case hostCPU
  254. of "i386": 50
  255. of "amd64": 500
  256. else: 50
  257. let jumpTableSizeStr = getEnv("HOT_CODE_RELOADING_JUMP_TABLE_SIZE")
  258. let jumpTableSize = if jumpTableSizeStr.len > 0: parseInt(jumpTableSizeStr)
  259. else: defaultJumpTableSize
  260. # TODO: perhaps keep track of free slots due to removed procs using a free list
  261. var jumpTable = ReservedMemSeq[LongJumpInstruction].init(
  262. memStart = cast[pointer](0x10000000),
  263. maxLen = jumpTableSize * 1024 * 1024 div sizeof(LongJumpInstruction),
  264. accessFlags = memExecReadWrite)
  265. type
  266. ProcSym = object
  267. jump: ptr LongJumpInstruction
  268. gen: int
  269. GlobalVarSym = object
  270. p: pointer
  271. markerProc: HcrGcMarkerProc
  272. gen: int
  273. ModuleDesc = object
  274. procs: Table[string, ProcSym]
  275. globals: Table[string, GlobalVarSym]
  276. imports: seq[string]
  277. handle: LibHandle
  278. hash: string
  279. gen: int
  280. lastModification: Time
  281. handlers: seq[tuple[isBefore: bool, cb: proc ()]]
  282. proc newModuleDesc(): ModuleDesc =
  283. result.procs = initTable[string, ProcSym]()
  284. result.globals = initTable[string, GlobalVarSym]()
  285. result.handle = nil
  286. result.gen = -1
  287. result.lastModification = low(Time)
  288. # the global state necessary for traversing and reloading the module import tree
  289. var modules = initTable[string, ModuleDesc]()
  290. var root: string
  291. var system: string
  292. var mainDatInit: HcrModuleInitializer
  293. var generation = 0
  294. # necessary for queries such as "has module X changed" - contains all but the main module
  295. var hashToModuleMap = initTable[string, string]()
  296. # necessary for registering handlers and keeping them up-to-date
  297. var currentModule: string
  298. # supplied from the main module - used by others to initialize pointers to this runtime
  299. var hcrDynlibHandle: pointer
  300. var getProcAddr: HcrProcGetter
  301. proc hcrRegisterProc*(module: cstring, name: cstring, fn: pointer): pointer {.nimhcr.} =
  302. trace " register proc: ", module.sanitize, " ", name
  303. # Please note: We must allocate a local copy of the strings, because the supplied
  304. # `cstring` will reside in the data segment of a DLL that will be later unloaded.
  305. let name = $name
  306. let module = $module
  307. var jumpTableEntryAddr: ptr LongJumpInstruction
  308. modules[module].procs.withValue(name, p):
  309. trace " update proc: ", name
  310. jumpTableEntryAddr = p.jump
  311. p.gen = generation
  312. do:
  313. let len = jumpTable.len
  314. jumpTable.setLen(len + 1)
  315. jumpTableEntryAddr = addr jumpTable[len]
  316. modules[module].procs[name] = ProcSym(jump: jumpTableEntryAddr, gen: generation)
  317. writeJump jumpTableEntryAddr, fn
  318. return jumpTableEntryAddr
  319. proc hcrGetProc*(module: cstring, name: cstring): pointer {.nimhcr.} =
  320. trace " get proc: ", module.sanitize, " ", name
  321. return modules[$module].procs[$name].jump
  322. proc hcrRegisterGlobal*(module: cstring,
  323. name: cstring,
  324. size: Natural,
  325. gcMarker: HcrGcMarkerProc,
  326. outPtr: ptr pointer): bool {.nimhcr.} =
  327. trace " register global: ", module.sanitize, " ", name
  328. # Please note: We must allocate local copies of the strings, because the supplied
  329. # `cstring` will reside in the data segment of a DLL that will be later unloaded.
  330. # Also using a ptr pointer instead of a var pointer (an output parameter)
  331. # because for the C++ backend var parameters use references and in this use case
  332. # it is not possible to cast an int* (for example) to a void* and then pass it
  333. # to void*& since the casting yields an rvalue and references bind only to lvalues.
  334. let name = $name
  335. let module = $module
  336. modules[module].globals.withValue(name, global):
  337. trace " update global: ", name
  338. outPtr[] = global.p
  339. global.gen = generation
  340. global.markerProc = gcMarker
  341. return false
  342. do:
  343. outPtr[] = alloc0(size)
  344. modules[module].globals[name] = GlobalVarSym(p: outPtr[],
  345. gen: generation,
  346. markerProc: gcMarker)
  347. return true
  348. proc hcrGetGlobal*(module: cstring, name: cstring): pointer {.nimhcr.} =
  349. trace " get global: ", module.sanitize, " ", name
  350. return modules[$module].globals[$name].p
  351. proc getListOfModules(cstringArray: ptr pointer): seq[string] =
  352. var curr = cast[ptr cstring](cstringArray)
  353. while len(curr[]) > 0:
  354. result.add($curr[])
  355. curr = cast[ptr cstring](cast[int64](curr) + sizeof(ptr cstring))
  356. template cleanup(collection, body) =
  357. var toDelete: seq[string]
  358. for name, data in collection.pairs:
  359. if data.gen < generation:
  360. toDelete.add(name)
  361. trace "HCR Cleaning ", astToStr(collection), " :: ", name, " ", data.gen
  362. for name {.inject.} in toDelete:
  363. body
  364. proc cleanupGlobal(module: string, name: string) =
  365. var g: GlobalVarSym
  366. if modules[module].globals.take(name, g):
  367. dealloc g.p
  368. proc cleanupSymbols(module: string) =
  369. cleanup modules[module].globals:
  370. cleanupGlobal(module, name)
  371. cleanup modules[module].procs:
  372. modules[module].procs.del(name)
  373. proc unloadDll(name: string) =
  374. if modules[name].handle != nil:
  375. unloadLib(modules[name].handle)
  376. proc loadDll(name: cstring) {.nimhcr.} =
  377. let name = $name
  378. trace "HCR LOADING: ", name.sanitize
  379. if modules.contains(name):
  380. unloadDll(name)
  381. else:
  382. modules.add(name, newModuleDesc())
  383. let copiedName = name & ".copy." & dllExt
  384. copyFile(name, copiedName)
  385. let lib = loadLib(copiedName)
  386. assert lib != nil
  387. modules[name].handle = lib
  388. modules[name].gen = generation
  389. modules[name].lastModification = getLastModificationTime(name)
  390. # update the list of imports by the module
  391. let getImportsProc = cast[proc (): ptr pointer {.nimcall.}](
  392. checkedSymAddr(lib, "HcrGetImportedModules"))
  393. modules[name].imports = getListOfModules(getImportsProc())
  394. # get the hash of the module
  395. let getHashProc = cast[proc (): cstring {.nimcall.}](
  396. checkedSymAddr(lib, "HcrGetSigHash"))
  397. modules[name].hash = $getHashProc()
  398. hashToModuleMap[modules[name].hash] = name
  399. # Remove handlers for this module if reloading - they will be re-registered.
  400. # In order for them to be re-registered we need to de-register all globals
  401. # that trigger the registering of handlers through calls to hcrAddEventHandler
  402. modules[name].handlers.setLen(0)
  403. proc initHcrData(name: cstring) {.nimhcr.} =
  404. trace "HCR Hcr init: ", name.sanitize
  405. cast[proc (h: pointer, gpa: HcrProcGetter) {.nimcall.}](
  406. checkedSymAddr(modules[$name].handle, "HcrInit000"))(hcrDynlibHandle, getProcAddr)
  407. proc initTypeInfoGlobals(name: cstring) {.nimhcr.} =
  408. trace "HCR TypeInfo globals init: ", name.sanitize
  409. cast[HcrModuleInitializer](checkedSymAddr(modules[$name].handle, "HcrCreateTypeInfos"))()
  410. proc initPointerData(name: cstring) {.nimhcr.} =
  411. trace "HCR Dat init: ", name.sanitize
  412. cast[HcrModuleInitializer](checkedSymAddr(modules[$name].handle, "DatInit000"))()
  413. proc initGlobalScope(name: cstring) {.nimhcr.} =
  414. trace "HCR Init000: ", name.sanitize
  415. # set the currently inited module - necessary for registering the before/after HCR handlers
  416. currentModule = $name
  417. cast[HcrModuleInitializer](checkedSymAddr(modules[$name].handle, "Init000"))()
  418. var modulesToInit: seq[string] = @[]
  419. var allModulesOrderedByDFS: seq[string] = @[]
  420. proc recursiveDiscovery(dlls: seq[string]) =
  421. for curr in dlls:
  422. if modules.contains(curr):
  423. # skip updating modules that have already been updated to the latest generation
  424. if modules[curr].gen >= generation:
  425. trace "HCR SKIP: ", curr.sanitize, " gen is already: ", modules[curr].gen
  426. continue
  427. # skip updating an unmodified module but continue traversing its dependencies
  428. if modules[curr].lastModification >= getLastModificationTime(curr):
  429. trace "HCR SKIP (not modified): ", curr.sanitize, " ", modules[curr].lastModification.sanitize
  430. # update generation so module doesn't get collected
  431. modules[curr].gen = generation
  432. # recurse to imported modules - they might be changed
  433. recursiveDiscovery(modules[curr].imports)
  434. allModulesOrderedByDFS.add(curr)
  435. continue
  436. loadDll(curr)
  437. # first load all dependencies of the current module and init it after that
  438. recursiveDiscovery(modules[curr].imports)
  439. allModulesOrderedByDFS.add(curr)
  440. modulesToInit.add(curr)
  441. proc initModules() =
  442. # first init the pointers to hcr functions and also do the registering of typeinfo globals
  443. for curr in modulesToInit:
  444. initHcrData(curr)
  445. initTypeInfoGlobals(curr)
  446. # for now system always gets fully inited before any other module (including when reloading)
  447. initPointerData(system)
  448. initGlobalScope(system)
  449. # proceed with the DatInit calls - for all modules - including the main one!
  450. for curr in allModulesOrderedByDFS:
  451. if curr != system:
  452. initPointerData(curr)
  453. mainDatInit()
  454. # execute top-level code (in global scope)
  455. for curr in modulesToInit:
  456. if curr != system:
  457. initGlobalScope(curr)
  458. # cleanup old symbols which are gone now
  459. for curr in modulesToInit:
  460. cleanupSymbols(curr)
  461. proc hcrInit*(moduleList: ptr pointer, main, sys: cstring,
  462. datInit: HcrModuleInitializer, handle: pointer, gpa: HcrProcGetter) {.nimhcr.} =
  463. trace "HCR INITING: ", main.sanitize, " gen: ", generation
  464. # initialize globals
  465. root = $main
  466. system = $sys
  467. mainDatInit = datInit
  468. hcrDynlibHandle = handle
  469. getProcAddr = gpa
  470. # the root is already added and we need it because symbols from it will also be registered in the HCR system
  471. modules[root].imports = getListOfModules(moduleList)
  472. modules[root].gen = high(int) # something huge so it doesn't get collected
  473. # recursively initialize all modules
  474. recursiveDiscovery(modules[root].imports)
  475. initModules()
  476. # the next module to be inited will be the root
  477. currentModule = root
  478. proc hcrHasModuleChanged*(moduleHash: string): bool {.nimhcr.} =
  479. let module = hashToModuleMap[moduleHash]
  480. return modules[module].lastModification < getLastModificationTime(module)
  481. proc hcrReloadNeeded*(): bool {.nimhcr.} =
  482. for hash, _ in hashToModuleMap:
  483. if hcrHasModuleChanged(hash):
  484. return true
  485. return false
  486. proc hcrPerformCodeReload*() {.nimhcr.} =
  487. if not hcrReloadNeeded():
  488. trace "HCR - no changes"
  489. return
  490. # We disable the GC during the reload, because the reloading procedures
  491. # will replace type info objects and GC marker procs. This seems to create
  492. # problems when the GC is executed while the reload is underway.
  493. # Future versions of NIMHCR won't use the GC, because all globals and the
  494. # metadata needed to access them will be placed in shared memory, so they
  495. # can be manipulted from external programs without reloading.
  496. GC_disable()
  497. defer: GC_enable()
  498. inc(generation)
  499. trace "HCR RELOADING: ", generation
  500. var traversedHandlerModules = initSet[string]()
  501. proc recursiveExecuteHandlers(isBefore: bool, module: string) =
  502. # do not process an already traversed module
  503. if traversedHandlerModules.containsOrIncl(module): return
  504. traversedHandlerModules.incl module
  505. # first recurse to do a DFS traversal
  506. for curr in modules[module].imports:
  507. recursiveExecuteHandlers(isBefore, curr)
  508. # and then execute the handlers - from leaf modules all the way up to the root module
  509. for curr in modules[module].handlers:
  510. if curr.isBefore == isBefore:
  511. curr.cb()
  512. # first execute the before reload handlers
  513. traversedHandlerModules.clear()
  514. recursiveExecuteHandlers(true, root)
  515. # do the reloading
  516. modulesToInit = @[]
  517. allModulesOrderedByDFS = @[]
  518. recursiveDiscovery(modules[root].imports)
  519. initModules()
  520. # execute the after reload handlers
  521. traversedHandlerModules.clear()
  522. recursiveExecuteHandlers(false, root)
  523. # collecting no longer referenced modules - based on their generation
  524. cleanup modules:
  525. cleanupSymbols(name)
  526. unloadDll(name)
  527. hashToModuleMap.del(modules[name].hash)
  528. modules.del(name)
  529. proc hcrAddEventHandler*(isBefore: bool, cb: proc ()) {.nimhcr.} =
  530. modules[currentModule].handlers.add(
  531. (isBefore: isBefore, cb: cb))
  532. proc hcrAddModule*(module: cstring) {.nimhcr.} =
  533. if not modules.contains($module):
  534. modules.add($module, newModuleDesc())
  535. proc hcrGeneration*(): int {.nimhcr.} =
  536. generation
  537. proc hcrMarkGlobals*() {.nimhcr, nimcall, gcsafe.} =
  538. # This is gcsafe, because it will be registered
  539. # only in the GC of the main thread.
  540. {.gcsafe.}:
  541. for _, module in modules:
  542. for _, global in module.globals:
  543. if global.markerProc != nil:
  544. global.markerProc()
  545. elif defined(hotcodereloading) or defined(testNimHcr):
  546. when not defined(JS):
  547. const
  548. nimhcrLibname = when defined(windows): "nimhcr." & dllExt
  549. elif defined(macosx): "libnimhcr." & dllExt
  550. else: "libnimhcr." & dllExt
  551. {.pragma: nimhcr, compilerProc, importc, dynlib: nimhcrLibname.}
  552. proc hcrRegisterProc*(module: cstring, name: cstring, fn: pointer): pointer {.nimhcr.}
  553. proc hcrGetProc*(module: cstring, name: cstring): pointer {.nimhcr.}
  554. proc hcrRegisterGlobal*(module: cstring, name: cstring, size: Natural,
  555. gcMarker: HcrGcMarkerProc, outPtr: ptr pointer): bool {.nimhcr.}
  556. proc hcrGetGlobal*(module: cstring, name: cstring): pointer {.nimhcr.}
  557. proc hcrInit*(moduleList: ptr pointer,
  558. main, sys: cstring,
  559. datInit: HcrModuleInitializer,
  560. handle: pointer,
  561. gpa: HcrProcGetter) {.nimhcr.}
  562. proc hcrAddModule*(module: cstring) {.nimhcr.}
  563. proc hcrHasModuleChanged*(moduleHash: string): bool {.nimhcr.}
  564. proc hcrReloadNeeded*(): bool {.nimhcr.}
  565. proc hcrPerformCodeReload*() {.nimhcr.}
  566. proc hcrAddEventHandler*(isBefore: bool, cb: proc ()) {.nimhcr.}
  567. proc hcrMarkGlobals*() {.nimhcr, nimcall, gcsafe.}
  568. when declared(nimRegisterGlobalMarker):
  569. nimRegisterGlobalMarker(hcrMarkGlobals)
  570. else:
  571. proc hcrHasModuleChanged*(moduleHash: string): bool =
  572. # TODO
  573. false
  574. proc hcrAddEventHandler*(isBefore: bool, cb: proc ()) =
  575. # TODO
  576. discard