Here is the code
import macros
import random
macro EMSCRIPTEN_KEEPALIVE*(someProc: untyped): typed =
result = someProc
result.addPragma(newIdentNode("exportc"))
when defined(cpp):
result.addPragma(newNimNode(nnkExprColonExpr).add(
newIdentNode("codegenDecl"),
newLit("__attribute__((used)) extern \"C\" $# $#$#")))
else:
result.addPragma(newNimNode(nnkExprColonExpr).add(
newIdentNode("codegenDecl"),
newLit("__attribute__((used)) $# $#$#")))
proc test: int32 =
var a:seq[int] = @[1, 2, 3]
a.add(1)
return int32(a.len)
proc fromNimFunction: int32{.EMSCRIPTEN_KEEPALIVE.} =
return test()
Run
Got
RuntimeError: memory access out of bounds
Run
My nim.cfg
@if wasm:
d:emscripten
@end
@if emscripten or wasm:
cc = clang
clang.exe = "emcc"
clang.linkerexe = "emcc"
clang.options.linker = ""
cpu = "i386"
@if wasm:
passC = "-s WASM=1 -s 'BINARYEN_METHOD=\"native-wasm\"' -Iemscripten -s
SINGLE_FILE=1 "
passL = "-s WASM=1 -s 'BINARYEN_METHOD=\"native-wasm\"' -Lemscripten -s
SINGLE_FILE=1 -s ALLOW_MEMORY_GROWTH=1 "
@end
@if release:
passC %= "-O3 -flto -ffast-math"
passL %= "-O3 -flto -ffast-math"
@end
#Fix _setjmp/longjmp problem.
https://irclogs.nim-lang.org/24-09-2017.html#12:19:50
d:nimStdSetjmp #
https://irclogs.nim-lang.org/24-09-2017.html#20:13:18
@end
Run