Add MAIN_THREAD_EM_ASM_PROMISE_AWAIT
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
@sbc100 Thanks for taking the time to review this. I addressed all of your comments and did the following:
- Added an error if the macro is called from main thread (and added test)
- Added support for receiving a return integer from the macro
- Added documentation LMK what you think :)
@sbc100 ping 🤞
@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.
Great, this looks a lot simpler overall, even though I agree that it's a little awkward to get the
ctxinto the right place. Thanks!
I am sorry I completely messed up git while trying to rebase.
@tlively any tips for passing all the CI tests?
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.
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.
Can you try rebase onto to merging with the latest changes on main?
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.
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
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 Thanks! I'll give it a go
@tlively @sbc100 All tests are now green :)
@sbc100 @tlively ping 😎
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 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.