Hello all,
I am currently in the process of attempting to compile a small test Nim code to WebAssembly and running it in a web page; the Webassembly program would contain only a lone export function ⟪reverse⟫, taking a string as input and outputting a reversed string.
https://github.com/Ntsekees/test-wasm-1
I have succeeded achieving the same by compiling an equivalent code in C (⟪reverse.c⟫) using the Emscripten compiler ⟪emcc⟫, and it works seamlessly, but when I attempt to do the same with the Nim version, I'm finding myself unable to export the ⟪reverse⟫ function. When I pass the switch ⟪``-s EXPORTED_FUNCTIONS='["_reverse"]'⟫ to emcc, I get an error whereby ⟪_reverse⟫ is undefined (same if I remove the leading underscore); if I don't pass the switch, the file compiles correctly, the WASM loads correctly in the web page but as expected the function ⟪reverse⟫ cannot be imported by the Javascript code. If I use ⟪-Wl,--export-all``⟫ instead of ⟪EXPORTED_FUNCTIONS⟫, the compiler complains that ⟪main⟫ is undefined, even though I had set the switch ⟪noMain⟫ to ⟪off⟫…
I have been suggested to use WASI-SDK instead of Emscripten, but I don't know how to use WASI as a backend for the Nim compiler.
Has somebody an idea of how I could get the ⟪reverse⟫ function exported? If it can't be done with Emscripten (which would be surprising, since it works seamlessly with the C source file), does any of you know how I could use WASI as a backend instead of Emscripten?
Thanking you in advance for any pointer, —Ntsékees.
Nope, I hadn't tried this, and this is precisely what I needed for making it compile and export successfully, thank you!
However, now I'm facing a puzzling ⟪Uncaught (in promise) RuntimeError: index out of bounds⟫ occurring whenever the ⟪reverse⟫ function attempts to modify the buffer provided as its first argument (even if editing only the first element, which should be in range). The WASM generated from the C version doesn't have this problem and works seamlessly…