You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
In ASYNCIFY=2 build mode function arguments come in as nulls #20490
doesn't, and instead raises the exception opts cannot be null.
Tested in Chrome Canary 120.0.6076.0 (Official Build) canary (64-bit) on Windows.
I find there's not much documentation on JSPI, but based on the test suite code, it looks like it should work by just replacing -sASYNCIFY=1 with -sASYNCIFY=2 (and adding the __sig directives)?
em++ a.cpp --js-library lib_a.js -sASYNCIFY=1 -sASYNCIFY_IMPORTS=['navigator_gpu_request_adapter_sync'] -o a.html
works as expected
Actually, looks like ASYNCIFY=1 does not work like I expect it to. What I am seeing is that the function navigator_gpu_request_adapter_sync() ends up being called twice:
I wonder if I've got the syntax wrong? Although I see I first implemented ASYNCIFY=1 support into wasm_webgpu two years ago, and back then I did not experience this double call mechanism - and the code has not changed since. That is a bit puzzling..
@juj The problem is that Asyncify.handleAsync must wrap the entire import. The wrapper is what takes care of imports being called twice - once for actual call before unwind and once for returning result during rewind - and ensures that the JS function inside is actually called only once like a regular JS function.
This requirement is only for JSPI, and not for ASYNCIFY=1 right?
No, that's for both. In JSPI it matters less actually because it does less work in the wrapper (only runtime keepalive counter tracking), but it's still useful to keep it for both.
How do I handle parameters that are passed in to the function on the stack?
From the PoV of JS function inside, parameters should always just be there and don't need special handling.
They're captured by closure + Wasm itself is suspended so pointers are always valid during the entire lifetime of the async function, both before and after suspending.
Alright, I was able to get -sASYNCIFY=1 to work by wrapping the code over the whole function (juj/wasm_webgpu@750df43). Although there is one issue, I'll open a separate ticket about that.
Still struggling to get -sASYNCIFY=2 to work, I am unable to get function parameters into the asyncified functions.
Side-note: if you're not limited by very old browsers (which, in case of WebGPU, I can only assume you're not), I'd really recommend to use async-await over .then. Aside from readability, thanks to sequential nature and avoiding closures, it can be better optimised by modern engines and results in shallower and more readable stacktraces in DevTools and errors. https://v8.dev/blog/fast-async
Emscripten already has problems with very deep stacktraces due to usage of .then to simulate async loops (e.g. in proxying and Asyncify itself) but Emscripten is limited by having to support old engines, so can't use native async-await everywhere. But for a project targeting specifically modern browsers anyway it shouldn't be an issue.
Thanks, I didn't know that there were any functional differences with these keywords. I thought they were just syntactic sugar. I'll look into migrating the code over.
This issue looks to have been fixed at some point.
Claude's test log:
Issue #20490 is no longer relevant: on current main the pointer argument arrives intact under JSPI, with both the original library code from the issue and the wrapped form RReverser suggested.
I used your a.cpp and library, replacing only the navigator.gpu.requestAdapter() call with a timer-based promise so it can run in Node, and making the library also print opts.x:
Engines: all six ran in Node 24.19 with --experimental-wasm-jspi. The original-form -sJSPI build also ran correctly in headless Chrome 150, since the issue was reported in Chrome.
Double call under -sASYNCIFY=1: the original form still runs its JS body twice, as you noted in your first comment. That is the expected behaviour RReverser explained (the import is re-entered on rewind unless Asyncify.handleAsync wraps the whole function), not a bug.
Flag name: -sASYNCIFY=2 still works but is documented as deprecated in favour of -sJSPI.
I did not find which change fixed the null argument, and I did not test the real WebGPU call. Tested on 504e57f, three commits behind today's origin/main; none of those touch the compiler.
Activity
juj commentedon Oct 19, 2023
Actually, looks like ASYNCIFY=1 does not work like I expect it to. What I am seeing is that the function
navigator_gpu_request_adapter_sync()ends up being called twice:I wonder if I've got the syntax wrong? Although I see I first implemented ASYNCIFY=1 support into wasm_webgpu two years ago, and back then I did not experience this double call mechanism - and the code has not changed since. That is a bit puzzling..
Add support for JSPI (-sASYNCIFY=2) and add a new sample buffer_map_s…
RReverser commentedon Oct 19, 2023
@juj The problem is that
Asyncify.handleAsyncmust wrap the entire import. The wrapper is what takes care of imports being called twice - once for actual call before unwind and once for returning result during rewind - and ensures that the JS function inside is actually called only once like a regular JS function.RReverser commentedon Oct 19, 2023
Something like this should work as expected (made couple more minor changes along the way):
juj commentedon Oct 19, 2023
Thanks. This requirement is only for JSPI, and not for ASYNCIFY=1 right?
Would that form of code work on both ASYNCIFY=1 and ASYNCIFY=2?
RReverser commentedon Oct 19, 2023
No, that's for both. In JSPI it matters less actually because it does less work in the wrapper (only runtime keepalive counter tracking), but it's still useful to keep it for both.
RReverser commentedon Oct 19, 2023
For the sake of another example, this is how we implement internal functions too, e.g.:
emscripten/src/embind/emval.js
Lines 455 to 461 in a8e9dd5
emscripten/src/library_idbstore.js
Lines 95 to 113 in a8e9dd5
juj commentedon Oct 19, 2023
How do I handle parameters that are passed in to the function on the stack?
If I do
is that first part executed synchronously before yielding back from the function? (so that I know that the caller's stack is still intact?)
juj commentedon Oct 19, 2023
I find that with that example, I do still get the error
Error: opts cannot be nullwhen building with-sASYNCIFY=2.juj commentedon Oct 19, 2023
Hmm looks like that is the case.. so all good there.
RReverser commentedon Oct 19, 2023
From the PoV of JS function inside, parameters should always just be there and don't need special handling.
They're captured by closure + Wasm itself is suspended so pointers are always valid during the entire lifetime of the async function, both before and after suspending.
juj commentedon Oct 19, 2023
Alright, I was able to get
-sASYNCIFY=1to work by wrapping the code over the whole function (juj/wasm_webgpu@750df43). Although there is one issue, I'll open a separate ticket about that.Still struggling to get
-sASYNCIFY=2to work, I am unable to get function parameters into the asyncified functions.RReverser commentedon Oct 19, 2023
Yeah sorry can't help with JSPI, I only played with it briefly and also ran into couple of bugs before.
RReverser commentedon Oct 19, 2023
Side-note: if you're not limited by very old browsers (which, in case of WebGPU, I can only assume you're not), I'd really recommend to use async-await over
.then. Aside from readability, thanks to sequential nature and avoiding closures, it can be better optimised by modern engines and results in shallower and more readable stacktraces in DevTools and errors. https://v8.dev/blog/fast-asyncEmscripten already has problems with very deep stacktraces due to usage of
.thento simulate async loops (e.g. in proxying and Asyncify itself) but Emscripten is limited by having to support old engines, so can't use native async-await everywhere. But for a project targeting specifically modern browsers anyway it shouldn't be an issue.juj commentedon Oct 19, 2023
Thanks, I didn't know that there were any functional differences with these keywords. I thought they were just syntactic sugar. I'll look into migrating the code over.
juj commentedon Dec 1, 2026
This issue looks to have been fixed at some point.
Claude's test log:
Issue #20490 is no longer relevant: on current main the pointer argument arrives intact under JSPI, with both the original library code from the issue and the wrapped form RReverser suggested.
I used your a.cpp and library, replacing only the navigator.gpu.requestAdapter() call with a timer-based promise so it can run in Node, and making the library also print opts.x:
I did not find which change fixed the null argument, and I did not test the real WebGPU call. Tested on 504e57f, three commits behind today's origin/main; none of those touch the compiler.