EM_ASYNC_JS Uncaught Error: undefined when out of the main
-
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: undefinedIt 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?