Details
-
Task
-
Resolution: Done
-
P2: Important
-
None
Description
Enable bidirectional communication between the embedding web page and Qt application code.
Topics
- Javascript <-> C++ control flow
- Type converserions (QString <-> JavaScript string)
Control flow
- C++ -> Javascript: use EM_ASM to inject js.
- Javascript -> C++: use Module.ccall().
- The linker may not include functions not called from C++ (javascript use is invisible to it.)
- Tag functions with EMSCRIPTEN_KEEPALIVE to mark them as used.
- Add functions to -s EXPORTED_FUNCTIONS='["_main"]' linker line to mark them as used
For Qt we would like to have a solution that can be contained in the Qt libraries, and that also works with static builds. EMSCRIPTEN_KEEPALIVE is not sufficient when the function is contained in a static library. EXPORTED_FUNCTIONS "leaks" from the library: there is a single entry on the final linker line where all functions has to be listed.
Proposed solution for the HTML5 platform plugin:
- Tag the function with EMSCRIPTEN_KEEPALIVE
- Call it once from platform plugin init, with a "no-op" flag set.
Strings
It is not clear what assumptions we can make about the internal encoding of Javascript strings. Convert via utf8.
Javascript -> QString:
- Module.stringToUTF8()
- QString::fromUtf8();
QString -> Javascript
- QString::toUtf8()
- Module.Pointer_stringify()
This incurs at least two string scan/copies in each direction, and allocation of a temporary UTF8 buffer. Should be OK for small strings.
Prototype Code
https://github.com/msorvig/qt-webassembly-examples/tree/master/emscripten_interop