emscripten icon indicating copy to clipboard operation
emscripten copied to clipboard

Add MAIN_THREAD_EM_ASM_PROMISE_AWAIT

Open hedwigz opened this issue 1 year ago • 14 comments

Introduce a new macro MAIN_THREAD_EM_ASM_PROMISE_AWAIT which is used to write JavaScript code that returns a promise and block the C code from progressing until the promise is resolved (or errors).

int main() {
  printf("1st print\n");
  // This call will block until the internal promise resolves (at least 1 second in this case) 
  MAIN_THREAD_EM_ASM_PROMISE_AWAIT({
    out('2nd print');
    // do some async operation that involves the main thread
    return new Promise((resolve,reject) => {
      setTimeout(() => {
        out('3rd print');
        resolve();
      }, 1000);
    });
  });
  printf("4th print\n");
  return 0;
}

The motivation for this was discussed before
We have multiple web workers, each of them calls javascript code which is async and has to be ran on the main thread and need to wait for the promise to be resolved.
I am open for suggestions on how to add this more elegantly

hedwigz avatar Dec 01 '24 21:12 hedwigz

@sbc100 Thanks for taking the time to review this. I addressed all of your comments and did the following:

  1. Added an error if the macro is called from main thread (and added test)
  2. Added support for receiving a return integer from the macro
  3. Added documentation LMK what you think :)

hedwigz avatar Dec 06 '24 15:12 hedwigz

@sbc100 ping 🤞

hedwigz avatar Dec 13 '24 05:12 hedwigz

@sbc100 @tlively 🙏 I really appreciate the thorough review and comments!
I addressed all of your comments and I think the code looks way better now.

hedwigz avatar Dec 14 '24 18:12 hedwigz

Great, this looks a lot simpler overall, even though I agree that it's a little awkward to get the ctx into the right place. Thanks!

I am sorry I completely messed up git while trying to rebase.

hedwigz avatar Dec 18 '24 20:12 hedwigz

@tlively any tips for passing all the CI tests?

hedwigz avatar Dec 18 '24 20:12 hedwigz

proxying.c looks good to me.

It looks like the flake8 bot is failing because of a formatting issue. The other CI bots might be aborting early because of that issue. Merging in main to pick up any unrelated fixes could also help.

tlively avatar Dec 18 '24 21:12 tlively

The fact that flake8 is being included at all in CI makes me think that perhaps you are not up-to-date on your branch? The flake8 step was replaced with a step call ruff few days back.

sbc100 avatar Dec 18 '24 22:12 sbc100

Can you try rebase onto to merging with the latest changes on main?

sbc100 avatar Dec 18 '24 22:12 sbc100

Any updates or blockers here? I'd like to see this land as well, we're also using workers that need to sync await some main thread code.

hybridherbst avatar Apr 02 '25 09:04 hybridherbst

Any updates or blockers here? I'd like to see this land as well, we're also using workers that need to sync await some main thread code.

Hey @hybridherbst , glad to see more interest in this. The current blocker is that some wasm64 test fail. I was struggling making the test run on my machine due to the need in d8/node24

hedwigz avatar Apr 03 '25 13:04 hedwigz

Any updates or blockers here? I'd like to see this land as well, we're also using workers that need to sync await some main thread code.

Hey @hybridherbst , glad to see more interest in this. The current blocker is that some wasm64 test fail. I was struggling making the test run on my machine due to the need in d8/node24

d8 you can get usinfg the jsvu tool.

node-canary you can you get from https://nodejs.org/download/v8-canary/

sbc100 avatar Apr 03 '25 15:04 sbc100

@sbc100 Thanks! I'll give it a go

hedwigz avatar Apr 04 '25 10:04 hedwigz

@tlively @sbc100 All tests are now green :)

hedwigz avatar Apr 07 '25 03:04 hedwigz

@sbc100 @tlively ping 😎

hedwigz avatar Apr 13 '25 06:04 hedwigz

Thanks for continuing to work on this.

Apologies for the review process taking so long. I had no imagined this being such an invasive change (i.e.changing the code proxying functions).

sbc100 avatar May 27 '25 19:05 sbc100

@sbc100 Thanks, I appreciate the continued work on this. I didn't have much time recently to cater to this PR. I'll hopefully get back to it soon.

hedwigz avatar Jun 08 '25 07:06 hedwigz