About vanilla wasm and emcc SIDE_MODULE

455 views
Skip to first unread message

Jean Valjean

unread,
Aug 9, 2017, 9:28:19 PM8/9/17
to emscripten-discuss
Hello there,

Sorry for my approximate english.

It is now possible to make a standalone WebAssembly file. I'm gladly to see that is possible now with Emscripten, thanks. I can now make my own HTML source content, with my own way to load the WebAssembly bytecode. But I don't understand why the "__post_instantiate" function is exported too ? The wiki at https://github.com/WebAssembly/tool-conventions/blob/master/DynamicLinking.md says it's about a dynamic link feature... but I feel that I just need a vanilla wasm file without such feature.

It's how I compile my code currently :

C:\dev\emscripten\emscripten\em++
-s ONLY_MY_CODE=1
-s WASM=1 -s SIDE_MODULE=2
-s DISABLE_EXCEPTION_CATCHING=1
-s AGGRESSIVE_VARIABLE_ELIMINATION=1
-s EXPORTED_FUNCTIONS="['_myfunc','_myfunc2']"
-std=c++11 -g0 -O3
foo.cpp -o target.wasm

Thanks,
Jean

Alon Zakai

unread,
Aug 9, 2017, 11:58:58 PM8/9/17
to emscripten-discuss
__post_instantiate is a way to run code when the module is loaded. For example it could run global constructors, which languages like C++ have. This is necessary not just for dynamic linking but also a single file by itself.

(wasm modules also have an optional start(), but that isn't good enough for everything, due to reentrancy issues - start() is called when the wasm instance is still being created, while __post_instantiate is called after the instance was created.)


--
You received this message because you are subscribed to the Google Groups "emscripten-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsub...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jean Valjean

unread,
Aug 10, 2017, 12:26:43 AM8/10/17
to emscripten-discuss
Thanks for the detailed answer,

I appreciate your help :)
I have some questions about that:
1) Are the global destructors called ?
2) Also, "__post_instantiate" seems to allocate the stack but the Vanilla LLVM doesn't do that. Doesn't Wasm have its own default stack ?

And more :
3) How can I use the WebAssembly.Table feature in my wasm output (from my c++ code) ?

Jean Valjean

unread,
Aug 10, 2017, 12:27:58 AM8/10/17
to emscripten-discuss
Also, do you recommend the use of emscripten for "vanilla wasm" output or should I stick with the original LLVM distrib ?

Alon Zakai

unread,
Aug 10, 2017, 1:23:15 PM8/10/17
to emscripten-discuss
Global destructors aren't called for you, and so far the dynamic library spec (which is the basis for current side module support) doesn't have a feature for that. So you'd need to do that manually.

There are some differences between how asm2wasm and the wasm backend (vanilla llvm) allocate the stack. The wasm backend path is not stable yet so you might be seeing a bug there, but also I think that side modules haven't been tested with the wasm backend yet. I'd use the default compiler path (asm2wasm) until the wasm backend is stable.

Not sure what you're asking about Table. Your compiled c++ code should use it automatically.

On Wed, Aug 9, 2017 at 9:27 PM, Jean Valjean <me...@hotmail.com> wrote:
Also, do you recommend the use of emscripten for "vanilla wasm" output or should I stick with the original LLVM distrib ?

--

Jean Valjean

unread,
Aug 10, 2017, 7:41:02 PM8/10/17
to emscripten-discuss
Thanks for your detailed answer, it's really helpful.
For the "WebAssembly.Table" feature, I was talking about this
https://github.com/mdn/webassembly-examples/blob/master/js-api-examples/table2.wat

Alon Zakai

unread,
Aug 11, 2017, 1:36:21 PM8/11/17
to emscripten-discuss
Not sure what you are asking about Tables, though? Yes, as in that link, you can import a table from the outside (and the dynamic linking spec has a convention for the import name). Then you can use that table inside the wasm module (compiled code will use it automatically) and also you can use it on the outside in JS.

To unsubscribe from this group and stop receiving emails from it, send an email to emscripten-discuss+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages