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()
Got
RuntimeError: memory access out of bounds
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
I think I have a similar problem. I started messing with wasm using treeform's tutorial but after heavy use of tables and other heap-allocated stuff I'm starting to see "alignment fault" in the chrome console:
alignment fault
printErr @ (index):44
abort @ index.js:1243
alignfault @ index.js:667
SAFE_HEAP_LOAD_i32_4_4 @ index.wasm:0x15d043
eqdestroy___WpGOXMMV39bro5UuSUSaR9ag @ index.wasm:0x8f9d0
eqdestroy___z9abg9cB6iTWSPxOOJ8e3qlw @ index.wasm:0x6fc67
enlarge__69agktFYNa9cQtt05LJiQLrg @ index.wasm:0x7f5a1
X5BX5Deq___VXXeFqubmneOwa22dqL8VA @ index.wasm:0x7fdbe
I think the enlarge indicates it's happening in the table module. Is this a known issue with Nim + emscripten right now? I'm using gc:arc. When i do gc:none i no longer see the exception, but obviously it leaks memory in that case...