Support code hot reloading for JavaScript projects (#7362)

* Support code hot reloading for JavaScript projects

* Add some missing JavaScript symbols and APIs

* fix the Travis build

* (review changes) remove the js type from the standard library as it doesn't follow NEP-1

* more additions to the DOM module

* Follow NEP-1 in jsffi; spell 'hot code reloading' correctly

* introduce a jscore module

* Document jscore module.

* readded js type

* Remove the '$' operator that doesn't behave
This commit is contained in:
zah 2018-04-13 20:08:43 +03:00 • committed by Andreas Rumpf
commit e3037a2f33
14 changed files with 285 additions and 68 deletions

View file

@ -278,12 +278,12 @@ Define Effect
what's in the Nim file with what's in the C header
(requires a C compiler with _Static_assert support, like
any C11 compiler)
``tempDir`` This symbol takes a string as its value, like
``tempDir`` This symbol takes a string as its value, like
``--define:tempDir:/some/temp/path`` to override the
temporary directory returned by ``os.getTempDir()``.
The value **should** end with a directory separator
The value **should** end with a directory separator
character. (Relevant for the Android platform)
``useShPath`` This symbol takes a string as its value, like
``useShPath`` This symbol takes a string as its value, like
``--define:useShPath:/opt/sh/bin/sh`` to override the
path for the ``sh`` binary, in cases where it is not
located in the default location ``/bin/sh``
@ -325,6 +325,46 @@ Debugger option
The ``debugger`` option enables or disables the *Embedded Nim Debugger*.
See the documentation of endb_ for further information.
Hot code reloading
------------------
**Note:** At the moment hot code reloading is supported only in
JavaScript projects.
The `hotReloading` 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,
Nim will preserve the state of all global variables which are initialized with
a standard variable declaration in the code. All other top level code will be
executed repeatedly on each reload. If you want to prevent this behavior, you
can guard a block of code with the `once` construct:
.. code-block:: Nim
var settings = initTable[string, string]()
once:
myInit()
for k, v in loadSettings():
settings[k] = v
If you want to reset the state of a global variable on each reload, just
re-assign a value anywhere within the top-level code:
.. code-block:: Nim
var lastReload: Time
lastReload = now()
resetProgramState()
**Known limitations:** In the JavaScript target, global variables using the
`codegenDecl` pragma will be re-initialized on each reload. Please guard the
initialization with a `once` block to work-around this.
**Usage in JavaScript projects:**
Once your code is compiled for hot reloading, you can use a framework such
as `LiveReload <http://livereload.com/>` or `BrowserSync <https://browsersync.io/>`
to implement the actual reloading behavior in your project.
Breakpoint pragma
-----------------