Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-64062

webassembly: Javascript <-> C++ Interop

    XMLWordPrintable

Details

    Description

      Enable bidirectional communication between the embedding web page and Qt application code.

      Topics

      • Javascript <-> C++ control flow
      • Type converserions (QString <-> JavaScript string)

      emscripten documentation

      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:

      1. Module.stringToUTF8()
      2. QString::fromUtf8();

       QString -> Javascript

      1. QString::toUtf8()
      2. 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

       

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            Unassigned Unassigned
            sorvig Morten Sørvig
            Votes:
            3 Vote for this issue
            Watchers:
            9 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes