Skip to content

Add EM_ASYNC_JS macro - #14728

Merged
RReverser merged 9 commits into
mainfrom
em-async-js
Jul 26, 2021
Merged

RReverser merged 9 commits into
mainfrom
em-async-js

Conversation

@RReverser

Copy link
Copy Markdown
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
Copy Markdown
Collaborator Author

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

@RReverser

Copy link
Copy Markdown
Collaborator Author

Oops, one more fix...

@RReverser

Copy link
Copy Markdown
Collaborator Author

Done.

@kripken kripken left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Very nice!

Comment thread tests/test_core.py Resolved
Comment thread tests/core/test_em_async_js.c Resolved
Comment thread emcc.py Resolved
Comment thread site/source/docs/porting/asyncify.rst Resolved
Comment thread site/source/docs/porting/asyncify.rst Resolved
Comment thread site/source/docs/porting/asyncify.rst 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
Copy Markdown
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
Copy Markdown
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 thread ChangeLog.md
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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