Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ See docs/process.md for more on how version tagging works.

4.0.20 (in development)
-----------------------
- Added new `emscripten_queue_microtask()` API to call the JS `queueMicrotask()`
function. (#25741)

4.0.19 - 11/04/25
-----------------
Expand Down
9 changes: 9 additions & 0 deletions src/lib/libhtml5.js
Original file line number Diff line number Diff line change
Expand Up @@ -2374,6 +2374,15 @@ var LibraryHTML5 = {
return requestAnimationFrame(tick);
},

emscripten_queue_microtask__deps: ['$callUserCallback'],
emscripten_queue_microtask: (cb, userData) => {
{{{ runtimeKeepalivePush(); }}}
return queueMicrotask(() => {
{{{ runtimeKeepalivePop(); }}}
callUserCallback(() => {{{ makeDynCall('vp', 'cb') }}}(userData));
});
},

emscripten_get_device_pixel_ratio__proxy: 'sync',
emscripten_get_device_pixel_ratio: () => {
#if ENVIRONMENT_MAY_BE_NODE || ENVIRONMENT_MAY_BE_SHELL
Expand Down
1 change: 1 addition & 0 deletions src/lib/libsigs.js
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,7 @@ sigs = {
emscripten_promise_race__sig: 'ppp',
emscripten_promise_resolve__sig: 'vpip',
emscripten_promise_then__sig: 'ppppp',
emscripten_queue_microtask__sig: 'vpp',
emscripten_random__sig: 'f',
emscripten_request_animation_frame__sig: 'ipp',
emscripten_request_animation_frame_loop__sig: 'vpp',
Expand Down
2 changes: 2 additions & 0 deletions system/include/emscripten/html5.h
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,8 @@ int emscripten_request_animation_frame(bool (*cb)(double time, void *userData),
void emscripten_cancel_animation_frame(int requestAnimationFrameId);
void emscripten_request_animation_frame_loop(bool (*cb)(double time, void *userData), void *userData);

void emscripten_queue_microtask(void (*cb)(void *userData), void *userData);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should probably go in emscripten/eventloop.h.


double emscripten_date_now(void);
double emscripten_performance_now(void);

Expand Down
25 changes: 25 additions & 0 deletions test/emscripten_queue_microtask.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2025 The Emscripten Authors. All rights reserved.
* Emscripten is available under two separate licenses, the MIT license and the
* University of Illinois/NCSA Open Source License. Both these licenses can be
* found in the LICENSE file.
*/

// Test emscripten_queue_microtask() behavior

#include <stdio.h>
#include <assert.h>
#include <emscripten.h>
#include <emscripten/html5.h>

void cb(void *userData) {
printf("cb\n");
assert(userData == (void*)42);
emscripten_force_exit(0);
}

int main() {
emscripten_queue_microtask(cb, (void*)42);
emscripten_exit_with_live_runtime();
return 99; // We won't reach here, but return non-zero value to guard against refactors that might exit() with this value.
}
7 changes: 7 additions & 0 deletions test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5016,6 +5016,13 @@ def test_emscripten_request_animation_frame_loop(self):
def test_request_animation_frame(self):
self.btest_exit('test_request_animation_frame.c')

@parameterized({
'': ([],),
'proxy_to_pthread': (['-pthread', '-sPROXY_TO_PTHREAD'],),
})
def test_emscripten_queue_microtask(self, flags):
self.btest_exit('emscripten_queue_microtask.c', cflags=cflags)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This test can run on node too so maybe can live in test_other.py? But maybe we want to specifically test in the browser?

See https://nodejs.org/api/globals.html#queuemicrotaskcallback


@requires_shared_array_buffer
def test_emscripten_set_timeout(self):
self.btest_exit('emscripten_set_timeout.c', cflags=['-pthread', '-sPROXY_TO_PTHREAD'])
Expand Down
Loading