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)
- Embind now requires C++17 or newer. See #24850.

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 @@ -2335,6 +2335,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));
});
},
Copy link
Collaborator

Choose a reason for hiding this comment

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

Move to libeventloops.js?


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/eventloop.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ void emscripten_set_immediate_loop(bool (*cb)(void *user_data), void *user_data)
int emscripten_set_interval(void (*cb)(void *user_data) __attribute__((nonnull)), double interval_ms, void *user_data);
void emscripten_clear_interval(int id);

void emscripten_queue_microtask(void (*cb)(void *userData), void *userData);

void emscripten_runtime_keepalive_push(void);
void emscripten_runtime_keepalive_pop(void);
bool emscripten_runtime_keepalive_check(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/eventloop.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 @@ -5024,6 +5024,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

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, did you have a mechanism for running browser suite with 'node' as the browser executable? I recall glancing over something along those lines.

Otherwise I presume we could duplicate the test in other + browser?

Copy link
Collaborator

Choose a reason for hiding this comment

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

You can do --browser=node as a quick hack, but the there is no president for doing that anywhere except on your local machine.

The current method is to just duplicate the tests that it makes sense to duplicate.


@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