Skip to content

Add 'callback' async variants for JS library functions - #27954

Open
guybedford wants to merge 1 commit into
gbedford/async-canwaitfrom
gbedford/callback-form
Open

guybedford wants to merge 1 commit into
gbedford/async-canwaitfrom
gbedford/callback-form

Conversation

@guybedford

@guybedford guybedford commented Oct 12, 2026 •

Copy link
Copy Markdown
Collaborator

Adds the _callback variant alongside the _promise variant of #27880, so the result of an asynchronous JS library function can be delivered to a C callback on the calling thread without a promise handle.

foo_callback(args..., callback, arg), listed as __async: ['callback'], runs the same body (with sync always false) and calls callback(err, result, arg) once the result is available (NULL for a void function), with err set (and result NULL) if the body's Promise rejects or the main thread exits before completing it. Delivery is always after the call has returned, including for a synchronous completion, and a pending callback holds the runtime alive like a timer. On the main thread the body's value or Promise is settled with .then under callUserCallback. From a pthread it is emscripten_proxy_callback_with_ctx of the main-thread body: the result rides in the proxied call's arg as for _promise, and the callback runs as a task on the pthread's proxying queue, so no Promise object or handle exists on that thread and a C event loop can collect it from its own queue processing. As with _promise, the variant is emitted only where used.

void lookup_callback(const char* name,
                     void (*callback)(bool err, void* result, void* arg),
                     void* arg);

Tests: jslib.test_jslib_async_auto_variants_callback in plain, ASYNCIFY, JSPI and PROXY_TO_PTHREAD builds, wasm32 and wasm64.

Made with AI assistance under my review

@guybedford
guybedford added this pull request to stack #27955 October 12, 2026 23:15
@guybedford
guybedford force-pushed the gbedford/callback-form branch 2 times, most recently from 8c29335 to 76ff676 Compare October 12, 2026 23:39
@guybedford
guybedford removed this pull request from stack #27955 October 12, 2026 23:39
@guybedford
guybedford added this pull request to stack #27957 October 12, 2026 23:40
@guybedford
guybedford removed this pull request from stack #27957 October 12, 2026 23:40
@guybedford
guybedford added this pull request to stack #27958 October 12, 2026 23:41
@guybedford
guybedford force-pushed the gbedford/callback-form branch 3 times, most recently from 96c852c to 7330e65 Compare October 13, 2026 00:35
@guybedford
guybedford force-pushed the gbedford/callback-form branch from 7330e65 to 05d7c96 Compare October 14, 2026 23:16
@guybedford guybedford changed the title Add _callback variants for __async: 'auto' library functions Add 'callback' async variants for JS library functions Oct 14, 2026
@guybedford
guybedford force-pushed the gbedford/callback-form branch from 05d7c96 to 1ca7889 Compare October 14, 2026 23:49
foo_callback(args..., callback, arg) runs the same body and calls
callback(err, result, arg) on the calling thread once the result is
available, err set if its Promise rejects, always after the call has
returned. A pending callback holds the runtime alive like a timer.

From a pthread it is emscripten_proxy_callback_with_ctx of the main-thread
body, with the result riding in the proxied call's arg as for _promise.
@guybedford
guybedford removed this pull request from stack #27958 October 20, 2026 04:34
@guybedford
guybedford added this pull request to stack #28023 October 20, 2026 04:34
@guybedford
guybedford force-pushed the gbedford/callback-form branch from 1ca7889 to a99adc5 Compare October 20, 2026 04:34
@guybedford
guybedford removed this pull request from stack #28023 October 20, 2026 04:42
@guybedford
guybedford added this pull request to stack #28024 October 20, 2026 04:42

This branch has not been deployed

No deployments
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.

1 participant