C++ function returns incorrect value if it calls a JS function wrapped in Asyncify.handlerAsync

59 views
Skip to first unread message

Galuh Utama

unread,
Mar 16, 2021, 9:12:02 AM3/16/21
to emscripten-discuss
I'm trying to get a value returned from a C++ function, which calls a JS function declared with EM_JS and wrapped with Asyncify.handlerAsync.

The code below reproduces the issue:

// asyncify_return.cpp
#include <string>
#include <iostream>
#include <emscripten.h>
#include <emscripten/bind.h>

EM_JS(void, bar, (), {
  Asyncify.handleAsync(async () => {
    const cache = await caches.open('test123');
    console.log("BAR");
  });
});


int foo()
{
  std::cout << "FOO" << std::endl;
  bar();
  return 42;
}


EMSCRIPTEN_BINDINGS(File)
{
  emscripten::function("foo", &foo);
}

Then I built it with:
emcc asyncify_return.cpp -o asyncify_return.html --bind -s ASYNCIFY=1 -s ASYNCIFY_IMPORTS='["bar"]

Result (executed in Chrome 89.0.4389.82 developer console):

let foo = Module.foo()
FOO
BAR
console.log(foo) // yields 0 instead of 42


I'm wondering why I get 0 instead of 42 here. However, if I comment out Asyncify.handleAsync in bar(), everything works fine.

My emcc version:
$ emcc --version
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 2.0.14 (8dd277d191daee9adfad03e5f0663df2db4b8bb1)

Any help and pointers will be appreciated. Thanks.

Reply all
Reply to author
Forward
0 new messages