Hot code reloading hasn't worked for me so far. Has anyone managed to get it to work?
The instructions to set it up in code are easy enough, but one thing it doesn't mention is that the nimhcr and nimrtl libraries are not currently prebuilt in nim. I tried building them according to this and copied both DLLs into nim's bin folder (so they are on the path) but right now it just crashes. I'm on windows 10 and tried with nim 1.0.4 and 1.0.6.
I tested this with Nim v1.0.6 on Windows 8.1.
Build nimhcr.dll and nimrtl.dll:
cd c:\my\path\to\nim
nim c -d:release lib/nimrtl.nim
nim c -d:release lib/nimhcr.nim
Put them in the parent directory of mymain.nim or an directory PATH env var contain. As sample code in the instructions failed to compile, I tested following code.
#logic.nim
#*** import the hotcodereloading stdlib module ***
import hotcodereloading
import os, times
var lastMod = getLastModificationTime(currentSourcePath())
proc update*() =
echo "bar"
let t = getLastModificationTime(currentSourcePath())
if t > lastMod:
if execShellCmd("nim c --hotcodereloading:on mymain.nim") == 0:
lastMod = t
#*** reload this logic.nim module on this file updated.***
performCodeReload()
sleep 1000
#mymain.nim
import logic
proc main() =
while true:
update()
main()
Build mymain.nim and run mymain.exe:
nim c --hotcodereloading:on mymain.nim
mymain.exe
Change code in logic.nim from
echo "bar"
to
echo "foo"
When I save it to the file, it is rebuilt and reloaded automatically.I wrote a sample Hot code reloading with GLFW. I placed it on gist because I think the source code is too long to put in this forum. https://gist.github.com/demotomohiro/64050496bb615c50fa608d7105509a53
It seems there are bugs related to hot code reloading.
So I used -d:glfwDLL option to avoid that pragma and use glfw3.dll.
Program crash when loadExtensions() is called. This module use undocumented 'dynlib' feature and I think that doesn't work with hotcodereloading.
So I used nimgl/opengl in the sample code. It doesn't use that feature and load OpenGL functions only using documented feature. (But it loads all available OpenGL functions even if it is not used in the program.)
For me a very simple example worked on windows after compiling nimhcr.nim and nimrtl.nim but after I uncommented a variable inside an update function, that was not uncommented on main build, it crashes with sigsegv:
main.nim
import hcr
import os
const ms = 1
const sec = 1000 * ms
proc main() =
while true:
os.sleep(5 * sec)
hcr.update()
echo repl.x
echo repl.y
when isMainModule:
main()
hcr.nim
import hotcodereloading
var x* = 82
var y* = @[1,2,3]
var z* = 171771
proc update*() =
# x = 84
performCodeReload()