Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EM_ASYNC_JS macro #14728

Merged
merged 9 commits into from
Jul 26, 2021
Merged

Add EM_ASYNC_JS macro #14728

merged 9 commits into from
Jul 26, 2021

Conversation

RReverser
Copy link
Collaborator

Adds an easy way to declare asynchronous JS functions. With this helper, users can use await directly inside of such snippets, don't need to wrap every such function into Asyncify.handleAsync manually, don't need to deal with propagating return values, and, finally, don't need to manually list all such functions in ASYNCIFY_IMPORTS since we know statically they're asynchronous.

I've also updated docs to merge "returning values" section into the first example, since it's now straightforward with EM_ASYNC_JS, a bit more involved but still easy with EM_JS + Asyncify.handleAsync, and only Asyncify.handleSleep API requires more explanation.

Fixes #9709.

@RReverser RReverser requested a review from kripken July 22, 2021 16:10
@RReverser
Copy link
Collaborator Author

(I'll add a CHANGELOG entry before merging if everything looks good otherwise)

@RReverser
Copy link
Collaborator Author

Oops, one more fix...

@RReverser
Copy link
Collaborator Author

Done.

Copy link
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice!

tests/test_core.py Outdated Show resolved Hide resolved
tests/core/test_em_async_js.c Show resolved Hide resolved
emcc.py Outdated Show resolved Hide resolved
site/source/docs/porting/asyncify.rst Outdated Show resolved Hide resolved
site/source/docs/porting/asyncify.rst Outdated Show resolved Hide resolved
site/source/docs/porting/asyncify.rst Outdated Show resolved Hide resolved
@RReverser RReverser force-pushed the em-async-js branch 2 times, most recently from 684f05d to f7eaee8 Compare July 23, 2021 17:45
@RReverser
Copy link
Collaborator Author

Fixed one more issue - apparently during the introduced EM_JS -> _EM_JS indirection C compiler was trying to expand anything that looked like a macro (e.g. assert(...)) before passing it on.

Now that's fixed and this PR should be ready to go pending CI & review.

RReverser and others added 8 commits July 26, 2021 19:12
Adds an easy way to declare asynchronous JS functions. With this helper, users can use `await` directly inside of such snippets, don't need to wrap every such function into `Asyncify.handleAsync` manually, don't need to deal with propagating return values, and, finally, don't need to manually list all such functions in `ASYNCIFY_IMPORTS` since we know statically they're asynchronous.

I've also updated docs to merge "returning values" section into the first example, since it's now straightforward with `EM_ASYNC_JS`, a bit more involved but still easy with `EM_JS` + `Asyncify.handleAsync`, and only `Asyncify.handleSleep` API requires more explanation.

Fixes #9709.
Co-authored-by: Alon Zakai <[email protected]>
If __VA_ARGS__ contains a macro call, compiler would try to expand it before passing to the nested _EM_JS call, causing errors like when `assert(...)` is expanded on the C side instead of being copied literally to JS.

To prevent them, stringify __VA_ARGS__ at the top level of each macro.
@RReverser
Copy link
Collaborator Author

I've noticed there is a WebGPU test by @austinEng that relies on Asyncify.handleAsync, so I took the liberty of updating it to use EM_ASYNC_JS too: 72b952e (#14728)

@RReverser RReverser enabled auto-merge (squash) July 26, 2021 20:41
@RReverser RReverser merged commit e77e71c into main Jul 26, 2021
@RReverser RReverser deleted the em-async-js branch July 26, 2021 20:48
Comment on lines +55 to +57
- Added `EM_ASYNC_JS` macro - similar to `EM_JS`, but allows using `await`
inside the JS block and automatically integrates with Asyncify without
the need for listing the declared function in `ASYNCIFY_IMPORTS` (#9709).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changelog note should probably be moved to 2.0.27 (since 2.0.26 was tagged before this PR landed).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, was it? At least the release PR came after but I'm not sure about tag itself: https://github.com/emscripten-core/emscripten/commits/main

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see, it does look like tag came in earlier.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@curiousdannii
Copy link
Contributor

It's so clean, I love it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add EM_ASYNC_JS for native async function support
5 participants