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. EM_ASYNC_JS Uncaught Error: undefined when out of the main
Forum Updated to NodeBB v4.3 + New Features

EM_ASYNC_JS Uncaught Error: undefined when out of the main

Scheduled Pinned Locked Moved Unsolved Qt for WebAssembly
1 Posts 1 Posters 90 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.
  • F Offline
    F Offline
    Francois M
    wrote on last edited by
    #1

    I'm trying to use JavaScript with promises using the JSPI option.
    I created a test project like this:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    #include <QObject>
    #include <cstdio>
    #include <iostream>
    
    #ifdef __EMSCRIPTEN__
    #include <emscripten.h>
    #endif
    
    using namespace std;
    
    EM_ASYNC_JS(int, do_fetch, (), {
        out("waiting for a fetch");
        const response = await fetch("vtkqml.html");
        out("got the fetch response");
        return 42;
    });
    
    class ButtonHandler : public QObject
    {
        Q_OBJECT
    
    public:
        ButtonHandler(QObject* parent = nullptr)
            : QObject(parent)
        { }
    
    public slots:
        void onButtonClicked()
        {
            cout << "Calling" << endl;
            do_fetch();
            cout << "Returned from fetch" << endl;
        }
    };
    
    
    QGuiApplication*       g_app     = nullptr;
    QQmlApplicationEngine* g_engine  = nullptr;
    ButtonHandler*         g_handler = nullptr;
    
    int main(int argc, char* argv[])
    {
        g_app = new QGuiApplication(argc, argv);
    
        g_engine = new QQmlApplicationEngine();
    
        g_handler = new ButtonHandler();
        g_engine->rootContext()->setContextProperty("buttonHandler", g_handler);
    
        g_engine->loadFromModule("vtkqml", "Main");
    
        cout << "Calling" << endl;
        do_fetch();
        cout << "Returned from do_fetch" << endl;
    
        if (g_engine->rootObjects().isEmpty())
        {
            return -1;
        }
    
        return 0;
    }
    

    You can see that I call the EM_ASYNC_JS function first in main and then in a slot.
    As a result, I get the following output:

    Calling
    waiting for a fetch
    got the fetch response
    Returned from do_fetch
    Calling
    Uncaught Error: undefined 
    

    It seems that the call only works when it's made from the main function, and I don't understand why.
    Does anyone have an idea what might be causing this?

    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