Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

emscripten function that uses ASYNCIFY returns to Javascript before completion

I have a computationally intensive WASM C function that uses ASYNCIFY to send status updates to the DOM:

EM_JS(void, updateStatus, (int number), {
  Asyncify.handleAsync(async() => {
    await Promise.resolve()
    .then(function() {
      console.log("status: ", number);
      document.getElementById("status").innerHTML = "Status: " + number;
    });

    await new Promise(r => setTimeout(r, 50));
  });
});

int myWASM(int param) {
  printf("Running myWASM()\n");
  ...
  for(int number = 0; number < MAX; number++) {
    ...
    updateStatus(number);
    ...
  }
  ...
  printf("done with myWASM()\n");

  return 0;
}

The Javascript caller is equivalent to:

  var startTime = performance.now();
  var callMyWASM = cwrap("myWASM", "number", ["number"]);
  var ret = callMyWASM(arg);
  var totalTime = performance.now() - startTime;

  console.log("myWASM ran in ", totalTime, "ms and returned", ret);

Strangely, the console reads:

Running myWASM()
myWASM ran in 10166.440000000875 ms and returned 0
done with myWASM()

And indeed, the DOM updates continue even after the Javascript reports that myWASM returned. I assume myWASM() is now an asyncronous function, and I wonder if there's a way to await or similar until it finishes before proceeding?

Edit: Following up on @Bergi's suggestion, changing myWASM() to return 43 results in the same output: Javascript gets a return value of 0 regardless.

like image 376
Amy Avatar asked Aug 09 '26 06:08

Amy


1 Answers

It is seen that it was a known bug by the Emscripten developers.

They fixed it in this pull request https://github.com/emscripten-core/emscripten/pull/14664.

My advice is to try the experimental versions or, once these changes are included in production, try the stable version

like image 144
Sergio Martinez Avatar answered Aug 10 '26 21:08

Sergio Martinez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!