To anyone trying to do this currently for Linux **or** Windows (I am using 
Windows), here are the steps as of 22-September-2019:

  1. Install 
[emscripten](https://emscripten.org/docs/getting_started/downloads.html) for 
your platform including Windows.
  2. Thanks to [@yglukhov](https://forum.nim-lang.org/t/4232#26349), we know we 
have to cross-compile to linux to make it work, especially for Windows, which 
was what I was missing. This can be used by putting the following "nim.cfg" 
file into your project directory:


    
    
    @if emscripten:
        cc = clang
        @if windows:
            clang.exe = "emcc.bat"
            clang.linkerexe = "emcc.bat"
        @else:
             clang.exe = "emcc"
            clang.linkerexe = "emcc"
        @end
        os = linux
        cpu = i386
        @if asmjs:
            passC = "-s WASM=0"
            passL = "-s WASM=0"
        @end
    @end
    
    
    Run

3\. Call the nim compiler with the following for compilation to wasm: 
    
    
    nim c -d:emscripten -d:release -d:danger -o:./index.html 
-o:<path_to_output_file> <path_to_Nim_source_file>
    
    
    Run

or the following for compilation to asm.js: 
    
    
    nim c -d:emscripten -d:asmjs -d:release -d:danger -o:./index.html 
-o:<path_to_output_file> <path_to_Nim_source_file>
    
    
    Run

  1. The output file extension can be ".html" for a pre-canned emscripten 
standard web page with the console output embedded or with a ".js" file 
extension for a file that can be run with node.
  2. From these, one can work out how to build a custom HTML document file to 
embed the project complete with custom JavaScript to handling the data to/from 
between wasm and the HTML DOM.



Use of wasm is generally at least a little faster than the use of even asm.js, 
but for many applications it may not be of "killer speed" considering that 
JavaScript engines such as Chrome's V8 already Just In Time (JIT) optimize "hot 
spots" in the code quite efficiently.

I haven't tried it yet but plan to but one of the most important reasons to use 
wasm instead of JavaScript or asm.js output is that one can do single web page 
multi-threading within the same page with shared data by using the new 
experimental feature that [can be enabled in Chrome since version 
70](https://developers.google.com/web/updates/2018/10/wasm-threads) (now up to 
version 77), if not other web browsers, (especially Firefox?). Finally, we 
should be able to make proper web page use of multi-threading and all the cores 
we have available even in smartphones!

Reply via email to