├── .gitignore ├── .editorconfig ├── src ├── test.js ├── nim.cfg └── nim_wasm_example.nim ├── README.md ├── nim_wasm_example.nimble └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | nimcache/ 2 | src/nim_wasm_example.js 3 | src/nim_wasm_example.wasm -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 2 6 | charset = utf-8 7 | insert_final_newline = true -------------------------------------------------------------------------------- /src/test.js: -------------------------------------------------------------------------------- 1 | const Module = require('./nim_wasm_example.js') 2 | 3 | Module['onRuntimeInitialized'] = onRuntimeInitialized 4 | 5 | function onRuntimeInitialized() { 6 | console.log(Module._fromNimFunction()) 7 | } 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Nim WebAssembly Example 2 | 3 | ```sh 4 | git clone https://github.com/2vg/nim-wasm-example 5 | cd nim-wasm-example 6 | nimble installEmcc 7 | nimble buildwasm 8 | nimble execwasm 9 | ``` 10 | 11 | 12 | # Requirements 13 | 14 | - Java (for the Closure Compiler, can be OpenJDK Headless at least). 15 | -------------------------------------------------------------------------------- /src/nim.cfg: -------------------------------------------------------------------------------- 1 | @if emscripten: 2 | cc = clang 3 | clang.exe = "emcc" 4 | clang.linkerexe = "emcc" 5 | clang.options.linker = "" 6 | cpu = "i386" 7 | warning[GcMem]= off 8 | passC = "-s WASM=1" 9 | passL = "-s WASM=1 -Oz -s EXTRA_EXPORTED_RUNTIME_METHODS='[\"ccall\", \"cwrap\"]' --closure 1" 10 | @end 11 | -------------------------------------------------------------------------------- /src/nim_wasm_example.nim: -------------------------------------------------------------------------------- 1 | import macros 2 | 3 | macro EMSCRIPTEN_KEEPALIVE(someProc: untyped) = 4 | result = someProc 5 | result.addPragma(newIdentNode("exportc")) 6 | when defined(cpp): 7 | result.addPragma(newNimNode(nnkExprColonExpr).add( 8 | newIdentNode("codegenDecl"), 9 | newLit("__attribute__((used)) extern \"C\" $# $#$#"))) 10 | else: 11 | result.addPragma(newNimNode(nnkExprColonExpr).add( 12 | newIdentNode("codegenDecl"), 13 | newLit("__attribute__((used)) $# $#$#"))) 14 | 15 | func fromNimFunction: int {.EMSCRIPTEN_KEEPALIVE.} = 16 | result = 114514 17 | -------------------------------------------------------------------------------- /nim_wasm_example.nimble: -------------------------------------------------------------------------------- 1 | # Package 2 | 3 | version = "0.1.0" 4 | author = "momf" 5 | description = "example 'Nim to wasm'" 6 | license = "MIT" 7 | srcDir = "src" 8 | bin = @["nim_wasm_example"] 9 | 10 | 11 | # Dependencies 12 | requires "nim >= 1.0.4" 13 | 14 | 15 | import distros 16 | 17 | task installEmcc, "setup emscripten(install to /emsdk)": 18 | exec "git clone https://github.com/emscripten-core/emsdk.git ~/emsdk" 19 | cd getEnv("HOME") & "/emsdk" 20 | exec "./emsdk install latest" 21 | exec "./emsdk activate latest" 22 | exec "source ~/emsdk/emsdk_env.sh" 23 | foreignDep "java" # ERROR: fatal: closure compiler is not configured correctly. java does not seem to exist, required for closure compiler. 24 | 25 | task buildwasm, "build nim_wasm_example.nim": 26 | exec "nim c -d:release -d:emscripten -o:src/nim_wasm_example.js src/nim_wasm_example.nim" 27 | 28 | task execwasm, "execute src/test.js": 29 | exec "node src/test.js" 30 | 31 | task buildrun, "build and execute": 32 | exec "nimble buildwasm" 33 | exec "nimble execwasm" 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 momf 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | --------------------------------------------------------------------------------