Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for WebAssembly
  4. Calling C++ function from JavaScript through Webassembly
QtWS25 Last Chance

Calling C++ function from JavaScript through Webassembly

Scheduled Pinned Locked Moved Unsolved Qt for WebAssembly
webassemblywasmjavascriptexportfunction
5 Posts 3 Posters 2.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AliGB
    wrote on last edited by AliGB
    #1

    I need to call a function in my Qt program (C++) from JavaScript but I don't know how Qt calls C++ functions from JS.

    Using this does not work since Qt does not export ccall.

    var result = Module.ccall(
                'myFunction',	// name of C function
                null,	// return type
                null,	// argument types
                null	// arguments );
    

    How can I do this?

    1 Reply Last reply
    0
    • lorn.potterL Offline
      lorn.potterL Offline
      lorn.potter
      wrote on last edited by
      #2

      You need to export those by adding linker arguments:

      https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html#interacting-with-code-ccall-cwrap

      Freelance Software Engineer, Platform Maintainer QtWebAssembly, Maintainer QtSensors
      Author, Hands-On Mobile and Embedded Development with Qt 5 http://bit.ly/HandsOnMobileEmbedded

      M 2 Replies Last reply
      1
      • lorn.potterL lorn.potter

        You need to export those by adding linker arguments:

        https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html#interacting-with-code-ccall-cwrap

        M Offline
        M Offline
        Marcin22
        wrote on last edited by Marcin22
        #3

        @lorn-potter said in Calling C++ function from JavaScript through Webassembly:

        Could you provide an example of how exactly should this be used in Qt? I am trying to export a simple function returning an integer and even that fails. I've added -s EXPORTED_FUNCTIONS=_getInt and -sEXPORTED_RUNTIME_METHODS=ccall,cwrap as an argument and even tried adding QT_WASM_EXPORTED_FUNCTIONS =_getInt to my .pro file. Both failed.

        Here's my function:
        extern "C" {
        int getInt() {
        int number = 5;
        return number;
        }
        }

        I tried writing a minimal example using C++-only code and everything worked properly. Yet, in Qt, I keep receiving undefined symbol errors for the getInt() function, ccall, cwrap. What I am a bit curious about, is that the "Module" variable is not accessible in the browser. Thus there's no possibility to interact with the WASM module directly. After digging into the qtloader.js I found out it might be accessible through qtLoader.module(). However, calling qtLoader.module().ccall returns Uncaught RuntimeError: Aborted('ccall' was not exported. add it to EXPORTED_RUNTIME_METHODS (see the FAQ))

        I am using Qt 6.4.0 with emscripten 3.1.4 on macOS 12.6 with XCode 14.1

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Marcin22
          wrote on last edited by Marcin22
          #4

          If anyone will have this problem in the future, the EMSCRIPTEN_KEEPALVIE macro seems to seal the deal:

          extern "C" {
          int EMSCRIPTEN_KEEPALIVE getInt() {
          int number = 5;
          return number;
          }
          }

          The previously mentioned compilation arguments are not required when using this macro.
          The function has to be called in a following manner: qtLoader.module()._getInt()

          1 Reply Last reply
          0
          • lorn.potterL lorn.potter

            You need to export those by adding linker arguments:

            https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html#interacting-with-code-ccall-cwrap

            M Offline
            M Offline
            Marcin22
            wrote on last edited by
            #5

            @lorn-potter

            I figured out how to export custom functions, but there's still a problem with exporting the runtime ones. I am adding the -s EXPORTED_RUNTIME_METHODS=UTF8ToString argument and keep receiving "Uncaught RuntimeError: Aborted('UTF8ToString' was not exported. add it to EXPORTED_RUNTIME_METHODS (see the FAQ))" error.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved