mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-28 17:04:41 +00:00
169 lines
27 KiB
HTML
169 lines
27 KiB
HTML
<?xml version="1.0" encoding="utf-8" ?>
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<!-- This file is generated by Nim. -->
|
|
<html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en" data-theme="auto">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Hot code reloading</title>
|
|
|
|
<!-- Google fonts -->
|
|
<link href='https://fonts.googleapis.com/css?family=Lato:400,600,900' rel='stylesheet' type='text/css'/>
|
|
<link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/>
|
|
|
|
<!-- Favicon -->
|
|
<link rel="shortcut icon" href="data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAUAAAAF////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAIAAABbAAAAlQAAAKIAAACbAAAAmwAAAKIAAACVAAAAWwAAAAL///8A////AP///wD///8A////AAAAABQAAADAAAAAYwAAAA3///8A////AP///wD///8AAAAADQAAAGMAAADAAAAAFP///wD///8A////AP///wAAAACdAAAAOv///wD///8A////AP///wD///8A////AP///wD///8AAAAAOgAAAJ3///8A////AP///wAAAAAnAAAAcP///wAAAAAoAAAASv///wD///8A////AP///wAAAABKAAAAKP///wAAAABwAAAAJ////wD///8AAAAAgQAAABwAAACIAAAAkAAAAJMAAACtAAAAFQAAABUAAACtAAAAkwAAAJAAAACIAAAAHAAAAIH///8A////AAAAAKQAAACrAAAAaP///wD///8AAAAARQAAANIAAADSAAAARf///wD///8AAAAAaAAAAKsAAACk////AAAAADMAAACcAAAAnQAAABj///8A////AP///wAAAAAYAAAAGP///wD///8A////AAAAABgAAACdAAAAnAAAADMAAAB1AAAAwwAAAP8AAADpAAAAsQAAAE4AAAAb////AP///wAAAAAbAAAATgAAALEAAADpAAAA/wAAAMMAAAB1AAAAtwAAAOkAAAD/AAAA/wAAAP8AAADvAAAA3gAAAN4AAADeAAAA3gAAAO8AAAD/AAAA/wAAAP8AAADpAAAAtwAAAGUAAAA/AAAA3wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAADfAAAAPwAAAGX///8A////AAAAAEgAAADtAAAAvwAAAL0AAADGAAAA7wAAAO8AAADGAAAAvQAAAL8AAADtAAAASP///wD///8A////AP///wD///8AAAAAO////wD///8A////AAAAAIcAAACH////AP///wD///8AAAAAO////wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A//8AAP//AAD4HwAA7/cAAN/7AAD//wAAoYUAAJ55AACf+QAAh+EAAAAAAADAAwAA4AcAAP5/AAD//wAA//8AAA=="/>
|
|
<link rel="icon" type="image/png" sizes="32x32" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA3XAAAN1wFCKJt4AAAAB3RJTUUH4QQQEwksSS9ZWwAAAk1JREFUWMPtll2ITVEUx39nn/O7Y5qR8f05wtCUUr6ZIS++8pEnkZInPImneaCQ5METNdOkeFBKUhMPRIkHKfEuUZSUlGlKPN2TrgfncpvmnntnmlEyq1Z7t89/rf9a6+y99oZxGZf/XeIq61EdtgKXgdXA0xrYAvBjOIF1AI9zvjcC74BSpndrJPkBWDScTF8Aa4E3wDlgHbASaANmVqlcCnwHvgDvgVfAJ+AikAAvgfVZwLnSVZHZaOuKoQi3ZOMi4NkYkpe1p4J7A8BpYAD49hfIy/oqG0+hLomiKP2L5L+1ubn5115S+3OAn4EnwBlgMzCjyt6ZAnQCJ4A7wOs88iRJHvw50HoujuPBoCKwHWiosy8MdfZnAdcHk8dxXFJ3VQbQlCTJvRBCGdRbD4M6uc5glpY3eAihpN5S5w12diSEcCCEcKUO4ljdr15T76ur1FDDLIQQ3qv71EdDOe3Kxj3leRXyk+pxdWnFWod6Wt2bY3de3aSuUHcPBVimHs7mK9WrmeOF6lR1o9qnzskh2ar2qm1qizpfXaPeVGdlmGN5pb09qMxz1Xb1kLqgzn1RyH7JUXW52lr5e/Kqi9qpto7V1atuUzfnARrV7jEib1T76gG2qxdGmXyiekkt1GswPTtek0aBfJp6YySGBfWg2tPQ0FAYgf1stUfdmdcjarbYJEniKIq6gY/Aw+zWHAC+p2labGpqiorFYgGYCEzN7oQdQClN07O1/EfDyGgC0ALMBdYAi4FyK+4H3gLPsxfR1zRNi+NP7nH5J+QntnXe5B5mpfQAAAAASUVORK5CYII=">
|
|
|
|
<!-- CSS -->
|
|
<link rel="stylesheet" type="text/css" href="nimdoc.out.css?v=2.3.1">
|
|
|
|
<!-- JS -->
|
|
<script type="text/javascript" src="dochack.js?v=2.3.1"></script>
|
|
</head>
|
|
<body>
|
|
<div class="document" id="documentId">
|
|
<div class="container">
|
|
<h1 class="title">Hot code reloading</h1>
|
|
<p>The <span id="hotcodereloading_1">hotCodeReloading</span> option enables special compilation mode where changes in the code can be applied automatically to a running program. The code reloading happens at the granularity of an individual module. When a module is reloaded, any newly added global variables will be initialized, but all other top-level code appearing in the module won't be re-executed and the state of all existing global variables will be preserved.</p>
|
|
|
|
<h1 id="basic-workflow">Basic workflow</h1><p>Currently, hot code reloading does not work for the main module itself, so we have to use a helper module where the major logic we want to change during development resides.</p>
|
|
<p>In this example, we use SDL2 to create a window and we reload the logic code when <tt class="docutils literal"><span class="pre"><span class="Identifier">F9</span></span></tt> is pressed. The important lines are marked with <tt class="docutils literal"><span class="pre"><span class="Comment">#***</span></span></tt>. To install SDL2 you can use <tt class="docutils literal"><span class="pre"><span class="program">nimble</span> <span class="option">install</span> <span class="option">sdl2</span></span></tt>.</p>
|
|
<p><pre class="listing"><span class="Comment"># logic.nim</span>
|
|
<span class="Keyword">import</span> <span class="Identifier">sdl2</span>
|
|
|
|
<span class="Comment">#*** import the hotcodereloading stdlib module ***</span>
|
|
<span class="Keyword">import</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">hotcodereloading</span>
|
|
|
|
<span class="Keyword">var</span> <span class="Identifier">runGame</span><span class="Operator">*:</span> <span class="Identifier">bool</span> <span class="Operator">=</span> <span class="Identifier">true</span>
|
|
<span class="Keyword">var</span> <span class="Identifier">window</span><span class="Punctuation">:</span> <span class="Identifier">WindowPtr</span>
|
|
<span class="Keyword">var</span> <span class="Identifier">renderer</span><span class="Punctuation">:</span> <span class="Identifier">RendererPtr</span>
|
|
<span class="Keyword">var</span> <span class="Identifier">evt</span> <span class="Operator">=</span> <span class="Identifier">sdl2</span><span class="Operator">.</span><span class="Identifier">defaultEvent</span>
|
|
|
|
<span class="Keyword">proc</span> <span class="Identifier">init</span><span class="Operator">*</span><span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Operator">=</span>
|
|
<span class="Keyword">discard</span> <span class="Identifier">sdl2</span><span class="Operator">.</span><span class="Identifier">init</span><span class="Punctuation">(</span><span class="Identifier">INIT_EVERYTHING</span><span class="Punctuation">)</span>
|
|
<span class="Identifier">window</span> <span class="Operator">=</span> <span class="Identifier">createWindow</span><span class="Punctuation">(</span><span class="StringLit">"testing"</span><span class="Punctuation">,</span> <span class="Identifier">SDL_WINDOWPOS_UNDEFINED</span><span class="Operator">.</span><span class="Identifier">cint</span><span class="Punctuation">,</span> <span class="Identifier">SDL_WINDOWPOS_UNDEFINED</span><span class="Operator">.</span><span class="Identifier">cint</span><span class="Punctuation">,</span> <span class="DecNumber">640</span><span class="Punctuation">,</span> <span class="DecNumber">480</span><span class="Punctuation">,</span> <span class="DecNumber">0'</span><span class="Identifier">u32</span><span class="Punctuation">)</span>
|
|
<span class="Identifier">assert</span><span class="Punctuation">(</span><span class="Identifier">window</span> <span class="Operator">!=</span> <span class="Keyword">nil</span><span class="Punctuation">,</span> <span class="Operator">$</span><span class="Identifier">sdl2</span><span class="Operator">.</span><span class="Identifier">getError</span><span class="Punctuation">(</span><span class="Punctuation">)</span><span class="Punctuation">)</span>
|
|
<span class="Identifier">renderer</span> <span class="Operator">=</span> <span class="Identifier">createRenderer</span><span class="Punctuation">(</span><span class="Identifier">window</span><span class="Punctuation">,</span> <span class="Operator">-</span><span class="DecNumber">1</span><span class="Punctuation">,</span> <span class="Identifier">RENDERER_SOFTWARE</span><span class="Punctuation">)</span>
|
|
<span class="Identifier">assert</span><span class="Punctuation">(</span><span class="Identifier">renderer</span> <span class="Operator">!=</span> <span class="Keyword">nil</span><span class="Punctuation">,</span> <span class="Operator">$</span><span class="Identifier">sdl2</span><span class="Operator">.</span><span class="Identifier">getError</span><span class="Punctuation">(</span><span class="Punctuation">)</span><span class="Punctuation">)</span>
|
|
|
|
<span class="Keyword">proc</span> <span class="Identifier">destroy</span><span class="Operator">*</span><span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Operator">=</span>
|
|
<span class="Identifier">destroyRenderer</span><span class="Punctuation">(</span><span class="Identifier">renderer</span><span class="Punctuation">)</span>
|
|
<span class="Identifier">destroyWindow</span><span class="Punctuation">(</span><span class="Identifier">window</span><span class="Punctuation">)</span>
|
|
|
|
<span class="Keyword">var</span> <span class="Identifier">posX</span><span class="Punctuation">:</span> <span class="Identifier">cint</span> <span class="Operator">=</span> <span class="DecNumber">1</span>
|
|
<span class="Keyword">var</span> <span class="Identifier">posY</span><span class="Punctuation">:</span> <span class="Identifier">cint</span> <span class="Operator">=</span> <span class="DecNumber">0</span>
|
|
<span class="Keyword">var</span> <span class="Identifier">dX</span><span class="Punctuation">:</span> <span class="Identifier">cint</span> <span class="Operator">=</span> <span class="DecNumber">1</span>
|
|
<span class="Keyword">var</span> <span class="Identifier">dY</span><span class="Punctuation">:</span> <span class="Identifier">cint</span> <span class="Operator">=</span> <span class="DecNumber">1</span>
|
|
|
|
<span class="Keyword">proc</span> <span class="Identifier">update</span><span class="Operator">*</span><span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Operator">=</span>
|
|
<span class="Keyword">while</span> <span class="Identifier">pollEvent</span><span class="Punctuation">(</span><span class="Identifier">evt</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
|
|
<span class="Keyword">if</span> <span class="Identifier">evt</span><span class="Operator">.</span><span class="Identifier">kind</span> <span class="Operator">==</span> <span class="Identifier">QuitEvent</span><span class="Punctuation">:</span>
|
|
<span class="Identifier">runGame</span> <span class="Operator">=</span> <span class="Identifier">false</span>
|
|
<span class="Keyword">break</span>
|
|
<span class="Keyword">if</span> <span class="Identifier">evt</span><span class="Operator">.</span><span class="Identifier">kind</span> <span class="Operator">==</span> <span class="Identifier">KeyDown</span><span class="Punctuation">:</span>
|
|
<span class="Keyword">if</span> <span class="Identifier">evt</span><span class="Operator">.</span><span class="Identifier">key</span><span class="Operator">.</span><span class="Identifier">keysym</span><span class="Operator">.</span><span class="Identifier">scancode</span> <span class="Operator">==</span> <span class="Identifier">SDL_SCANCODE_ESCAPE</span><span class="Punctuation">:</span> <span class="Identifier">runGame</span> <span class="Operator">=</span> <span class="Identifier">false</span>
|
|
<span class="Keyword">elif</span> <span class="Identifier">evt</span><span class="Operator">.</span><span class="Identifier">key</span><span class="Operator">.</span><span class="Identifier">keysym</span><span class="Operator">.</span><span class="Identifier">scancode</span> <span class="Operator">==</span> <span class="Identifier">SDL_SCANCODE_F9</span><span class="Punctuation">:</span>
|
|
<span class="Comment">#*** reload this logic.nim module on the F9 keypress ***</span>
|
|
<span class="Identifier">performCodeReload</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
|
|
|
|
<span class="Comment"># draw a bouncing rectangle:</span>
|
|
<span class="Identifier">posX</span> <span class="Operator">+=</span> <span class="Identifier">dX</span>
|
|
<span class="Identifier">posY</span> <span class="Operator">+=</span> <span class="Identifier">dY</span>
|
|
|
|
<span class="Keyword">if</span> <span class="Identifier">posX</span> <span class="Operator">>=</span> <span class="DecNumber">640</span><span class="Punctuation">:</span> <span class="Identifier">dX</span> <span class="Operator">=</span> <span class="Operator">-</span><span class="DecNumber">2</span>
|
|
<span class="Keyword">if</span> <span class="Identifier">posX</span> <span class="Operator"><=</span> <span class="DecNumber">0</span><span class="Punctuation">:</span> <span class="Identifier">dX</span> <span class="Operator">=</span> <span class="Operator">+</span><span class="DecNumber">2</span>
|
|
<span class="Keyword">if</span> <span class="Identifier">posY</span> <span class="Operator">>=</span> <span class="DecNumber">480</span><span class="Punctuation">:</span> <span class="Identifier">dY</span> <span class="Operator">=</span> <span class="Operator">-</span><span class="DecNumber">2</span>
|
|
<span class="Keyword">if</span> <span class="Identifier">posY</span> <span class="Operator"><=</span> <span class="DecNumber">0</span><span class="Punctuation">:</span> <span class="Identifier">dY</span> <span class="Operator">=</span> <span class="Operator">+</span><span class="DecNumber">2</span>
|
|
|
|
<span class="Keyword">discard</span> <span class="Identifier">renderer</span><span class="Operator">.</span><span class="Identifier">setDrawColor</span><span class="Punctuation">(</span><span class="DecNumber">0</span><span class="Punctuation">,</span> <span class="DecNumber">0</span><span class="Punctuation">,</span> <span class="DecNumber">255</span><span class="Punctuation">,</span> <span class="DecNumber">255</span><span class="Punctuation">)</span>
|
|
<span class="Keyword">discard</span> <span class="Identifier">renderer</span><span class="Operator">.</span><span class="Identifier">clear</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
|
|
<span class="Keyword">discard</span> <span class="Identifier">renderer</span><span class="Operator">.</span><span class="Identifier">setDrawColor</span><span class="Punctuation">(</span><span class="DecNumber">255</span><span class="Punctuation">,</span> <span class="DecNumber">128</span><span class="Punctuation">,</span> <span class="DecNumber">128</span><span class="Punctuation">,</span> <span class="DecNumber">0</span><span class="Punctuation">)</span>
|
|
|
|
<span class="Keyword">var</span> <span class="Identifier">rect</span><span class="Punctuation">:</span> <span class="Identifier">Rect</span> <span class="Operator">=</span> <span class="Punctuation">(</span><span class="Identifier">x</span><span class="Punctuation">:</span> <span class="Identifier">posX</span> <span class="Operator">-</span> <span class="DecNumber">25</span><span class="Punctuation">,</span> <span class="Identifier">y</span><span class="Punctuation">:</span> <span class="Identifier">posY</span> <span class="Operator">-</span> <span class="DecNumber">25</span><span class="Punctuation">,</span> <span class="Identifier">w</span><span class="Punctuation">:</span> <span class="FloatNumber">50.</span><span class="Identifier">cint</span><span class="Punctuation">,</span> <span class="Identifier">h</span><span class="Punctuation">:</span> <span class="FloatNumber">50.</span><span class="Identifier">cint</span><span class="Punctuation">)</span>
|
|
<span class="Keyword">discard</span> <span class="Identifier">renderer</span><span class="Operator">.</span><span class="Identifier">fillRect</span><span class="Punctuation">(</span><span class="Identifier">rect</span><span class="Punctuation">)</span>
|
|
<span class="Identifier">delay</span><span class="Punctuation">(</span><span class="DecNumber">16</span><span class="Punctuation">)</span>
|
|
<span class="Identifier">renderer</span><span class="Operator">.</span><span class="Identifier">present</span><span class="Punctuation">(</span><span class="Punctuation">)</span></pre></p>
|
|
<p><pre class="listing"><span class="Comment"># mymain.nim</span>
|
|
<span class="Keyword">import</span> <span class="Identifier">logic</span>
|
|
|
|
<span class="Keyword">proc</span> <span class="Identifier">main</span><span class="Punctuation">(</span><span class="Punctuation">)</span> <span class="Operator">=</span>
|
|
<span class="Identifier">init</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
|
|
<span class="Keyword">while</span> <span class="Identifier">runGame</span><span class="Punctuation">:</span>
|
|
<span class="Identifier">update</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
|
|
<span class="Identifier">destroy</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
|
|
|
|
<span class="Identifier">main</span><span class="Punctuation">(</span><span class="Punctuation">)</span></pre></p>
|
|
<p>Compile this example via:</p>
|
|
<p><pre class="listing"><span class="program">nim</span> <span class="option">c</span> <span class="option">--hotcodereloading:on</span> <span class="Identifier">mymain.nim</span></pre></p>
|
|
<p>Now start the program and KEEP it running!</p>
|
|
<p><pre class="listing"><span class="Comment"># Unix:</span>
|
|
<span class="program">mymain</span> <span class="Operator">&</span>
|
|
<span class="Comment"># or Windows (click on the .exe)</span>
|
|
<span class="program">mymain.exe</span>
|
|
<span class="Comment"># edit</span></pre></p>
|
|
<p>For example, change the line:</p>
|
|
<p><pre class="listing"><span class="Keyword">discard</span> <span class="Identifier">renderer</span><span class="Operator">.</span><span class="Identifier">setDrawColor</span><span class="Punctuation">(</span><span class="DecNumber">255</span><span class="Punctuation">,</span> <span class="DecNumber">128</span><span class="Punctuation">,</span> <span class="DecNumber">128</span><span class="Punctuation">,</span> <span class="DecNumber">0</span><span class="Punctuation">)</span></pre></p>
|
|
<p>into:</p>
|
|
<p><pre class="listing"><span class="Keyword">discard</span> <span class="Identifier">renderer</span><span class="Operator">.</span><span class="Identifier">setDrawColor</span><span class="Punctuation">(</span><span class="DecNumber">255</span><span class="Punctuation">,</span> <span class="DecNumber">255</span><span class="Punctuation">,</span> <span class="DecNumber">128</span><span class="Punctuation">,</span> <span class="DecNumber">0</span><span class="Punctuation">)</span></pre></p>
|
|
<p>(This will change the color of the rectangle.)</p>
|
|
<p>Then recompile the project, but do not restart or quit the mymain.exe program!</p>
|
|
<p><pre class="listing"><span class="program">nim</span> <span class="option">c</span> <span class="option">--hotcodereloading:on</span> <span class="Identifier">mymain.nim</span></pre></p>
|
|
<p>Now give the <tt class="docutils literal"><span class="pre"><span class="Identifier">mymain</span></span></tt> SDL window the focus, press F9, and watch the updated version of the program.</p>
|
|
|
|
<h1 id="reloading-api">Reloading API</h1><p>One can use the special event handlers <tt class="docutils literal"><span class="pre"><span class="Identifier">beforeCodeReload</span></span></tt> and <tt class="docutils literal"><span class="pre"><span class="Identifier">afterCodeReload</span></span></tt> to reset the state of a particular variable or to force the execution of certain statements:</p>
|
|
<p><pre class="listing"><span class="Keyword">var</span>
|
|
<span class="Identifier">settings</span> <span class="Operator">=</span> <span class="Identifier">initTable</span><span class="Punctuation">[</span><span class="Identifier">string</span><span class="Punctuation">,</span> <span class="Identifier">string</span><span class="Punctuation">]</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
|
|
<span class="Identifier">lastReload</span><span class="Punctuation">:</span> <span class="Identifier">Time</span>
|
|
|
|
<span class="Keyword">for</span> <span class="Identifier">k</span><span class="Punctuation">,</span> <span class="Identifier">v</span> <span class="Keyword">in</span> <span class="Identifier">loadSettings</span><span class="Punctuation">(</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
|
|
<span class="Identifier">settings</span><span class="Punctuation">[</span><span class="Identifier">k</span><span class="Punctuation">]</span> <span class="Operator">=</span> <span class="Identifier">v</span>
|
|
|
|
<span class="Identifier">initProgram</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
|
|
|
|
<span class="Identifier">afterCodeReload</span><span class="Punctuation">:</span>
|
|
<span class="Identifier">lastReload</span> <span class="Operator">=</span> <span class="Identifier">now</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
|
|
<span class="Identifier">resetProgramState</span><span class="Punctuation">(</span><span class="Punctuation">)</span></pre></p>
|
|
<p>On each code reload, Nim will first execute all <span id="beforecodereload_1">beforeCodeReload</span> handlers registered in the previous version of the program and then all <span id="aftercodereload_1">afterCodeReload</span> handlers appearing in the newly loaded code. Please note that any handlers appearing in modules that weren't reloaded will also be executed. To prevent this behavior, one can guard the code with the <span id="hasmodulechanged_1">hasModuleChanged()</span> API:</p>
|
|
<p><pre class="listing"><span class="Keyword">import</span> <span class="Identifier">mydb</span>
|
|
|
|
<span class="Keyword">var</span> <span class="Identifier">myCache</span> <span class="Operator">=</span> <span class="Identifier">initTable</span><span class="Punctuation">[</span><span class="Identifier">Key</span><span class="Punctuation">,</span> <span class="Identifier">Value</span><span class="Punctuation">]</span><span class="Punctuation">(</span><span class="Punctuation">)</span>
|
|
|
|
<span class="Identifier">afterCodeReload</span><span class="Punctuation">:</span>
|
|
<span class="Keyword">if</span> <span class="Identifier">hasModuleChanged</span><span class="Punctuation">(</span><span class="Identifier">mydb</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
|
|
<span class="Identifier">resetCache</span><span class="Punctuation">(</span><span class="Identifier">myCache</span><span class="Punctuation">)</span></pre></p>
|
|
<p>The hot code reloading is based on dynamic library hot swapping in the native targets and direct manipulation of the global namespace in the JavaScript target. The Nim compiler does not specify the mechanism for detecting the conditions when the code must be reloaded. Instead, the program code is expected to call <span id="performcodereload_1">performCodeReload()</span> every time it wishes to reload its code.</p>
|
|
<p>It's expected that most projects will implement the reloading with a suitable build-system triggered IPC notification mechanism, but a polling solution is also possible through the provided <span id="hasanymodulechanged_1">hasAnyModuleChanged()</span> API.</p>
|
|
<p>In order to access <tt class="docutils literal"><span class="pre"><span class="Identifier">beforeCodeReload</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Identifier">afterCodeReload</span></span></tt>, <tt class="docutils literal"><span class="pre"><span class="Identifier">hasModuleChanged</span></span></tt> or <tt class="docutils literal"><span class="pre"><span class="Identifier">hasAnyModuleChanged</span></span></tt> one must import the <span id="hotcodereloading_2">hotcodereloading</span> module.</p>
|
|
|
|
<h1 id="native-code-targets">Native code targets</h1><p>Native projects using the hot code reloading option will be implicitly compiled with the <tt class="docutils literal"><span class="pre option">-d:useNimRtl</span></tt> option and they will depend on both the <tt class="docutils literal"><span class="pre"><span class="Identifier">nimrtl</span></span></tt> library and the <tt class="docutils literal"><span class="pre"><span class="Identifier">nimhcr</span></span></tt> library which implements the hot code reloading run-time. Both libraries can be found in the <tt class="docutils literal"><span class="pre"><span class="Identifier">lib</span></span></tt> folder of Nim and can be compiled into dynamic libraries to satisfy runtime demands of the example code above. An example of compiling <tt class="docutils literal"><span class="pre">nimhcr.nim</span></tt> and <tt class="docutils literal"><span class="pre">nimrtl.nim</span></tt> when the source dir of Nim is installed with choosenim follows.</p>
|
|
<p><pre class="listing"><span class="Comment"># Unix/MacOS</span>
|
|
<span class="Comment"># Make sure you are in the directory containing your .nim files</span>
|
|
<span class="Prompt">$ </span><span class="program">cd</span> <span class="option">your-source-directory</span>
|
|
|
|
<span class="Comment"># Compile two required files and set their output directory to current dir</span>
|
|
<span class="Prompt">$ </span><span class="program">nim</span> <span class="option">c</span> <span class="option">--outdir:$PWD</span> <span class="Identifier">~/.choosenim/toolchains/nim-#devel/lib/nimhcr.nim</span>
|
|
<span class="Prompt">$ </span><span class="program">nim</span> <span class="option">c</span> <span class="option">--outdir:$PWD</span> <span class="Identifier">~/.choosenim/toolchains/nim-#devel/lib/nimrtl.nim</span>
|
|
|
|
<span class="Comment"># verify that you have two files named libnimhcr and libnimrtl in your</span>
|
|
<span class="Comment"># source directory (.dll for Windows, .so for Unix, .dylib for MacOS)</span></pre></p>
|
|
<p>All modules of the project will be compiled to separate dynamic link libraries placed in the <tt class="docutils literal"><span class="pre"><span class="Identifier">nimcache</span></span></tt> directory. Please note that during the execution of the program, the hot code reloading run-time will load only copies of these libraries in order to not interfere with any newly issued build commands.</p>
|
|
<p>The main module of the program is considered non-reloadable. Please note that procs from reloadable modules should not appear in the call stack of program while <tt class="docutils literal"><span class="pre"><span class="Identifier">performCodeReload</span></span></tt> is being called. Thus, the main module is a suitable place for implementing a program loop capable of calling <tt class="docutils literal"><span class="pre"><span class="Identifier">performCodeReload</span></span></tt>.</p>
|
|
<p>Please note that reloading won't be possible when any of the type definitions in the program has been changed. When closure iterators are used (directly or through async code), the reloaded definitions will affect only newly created instances. Existing iterator instances will execute their original code to completion.</p>
|
|
|
|
<h1 id="javascript-target">JavaScript target</h1><p>Once your code is compiled for hot reloading, a convenient solution for implementing the actual reloading in the browser using a framework such as <a class="reference external" href="https://livereload.com/">LiveReload</a> or <a class="reference external" href="https://browsersync.io/">BrowserSync</a>. </p>
|
|
|
|
|
|
|
|
<div class="twelve-columns footer">
|
|
<span class="nim-sprite"></span>
|
|
<br>
|
|
<small style="color: var(--hint);">Made with Nim. Generated: 2025-12-28 15:54:16 UTC</small>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<script defer data-domain="nim-lang.org" src="https://plausible.io/js/plausible.js"></script>
|
|
|
|
</body>
|
|
</html>
|