3

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.

2
  • I suppose you will have to explicitly return a promise (or call a callback, and then promisify in JS) from your wasm function. Commented Jan 23, 2021 at 2:36
  • I'm not totally sure the JS code is getting a valid return value. The timing suggests that it resumes when the first EM_JS updateStatus() timeout executes. Commented Jan 23, 2021 at 15:45

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.