Skip to content

Commit 35f230a

Browse files
committed
Add emscripten_queue_microtask() API to enable use case described in this thread: #25670 (comment).
1 parent 5d2b2f1 commit 35f230a

File tree

6 files changed

+43
-0
lines changed

6 files changed

+43
-0
lines changed

ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ See docs/process.md for more on how version tagging works.
2020

2121
4.0.20 (in development)
2222
-----------------------
23+
- Added new `emscripten_queue_microtask()` API to call the JS `queueMicrotask()`
24+
function.
2325

2426
4.0.19 - 11/04/25
2527
-----------------

src/lib/libhtml5.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2374,6 +2374,12 @@ var LibraryHTML5 = {
23742374
return requestAnimationFrame(tick);
23752375
},
23762376

2377+
emscripten_queue_microtask: (cb, userData) => {
2378+
queueMicrotask(() => {
2379+
{{{ makeDynCall('vp', 'cb') }}}(userData);
2380+
});
2381+
},
2382+
23772383
emscripten_get_device_pixel_ratio__proxy: 'sync',
23782384
emscripten_get_device_pixel_ratio: () => {
23792385
#if ENVIRONMENT_MAY_BE_NODE || ENVIRONMENT_MAY_BE_SHELL

src/lib/libsigs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,7 @@ sigs = {
732732
emscripten_promise_resolve__sig: 'vpip',
733733
emscripten_promise_then__sig: 'ppppp',
734734
emscripten_random__sig: 'f',
735+
emscripten_queue_microtask__sig: 'vp',
735736
emscripten_request_animation_frame__sig: 'ipp',
736737
emscripten_request_animation_frame_loop__sig: 'vpp',
737738
emscripten_request_fullscreen__sig: 'ipi',

system/include/emscripten/html5.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,8 @@ int emscripten_request_animation_frame(bool (*cb)(double time, void *userData),
466466
void emscripten_cancel_animation_frame(int requestAnimationFrameId);
467467
void emscripten_request_animation_frame_loop(bool (*cb)(double time, void *userData), void *userData);
468468

469+
int emscripten_queue_microtask(void (*cb)(void *userData), void *userData);
470+
469471
double emscripten_date_now(void);
470472
double emscripten_performance_now(void);
471473

test/emscripten_queue_microtask.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2025 The Emscripten Authors. All rights reserved.
3+
* Emscripten is available under two separate licenses, the MIT license and the
4+
* University of Illinois/NCSA Open Source License. Both these licenses can be
5+
* found in the LICENSE file.
6+
*/
7+
8+
// Test emscripten_queue_microtask() behavior
9+
10+
#include <stdio.h>
11+
#include <assert.h>
12+
#include <emscripten.h>
13+
#include <emscripten/html5.h>
14+
15+
void cb(void *userData) {
16+
printf("cb\n");
17+
assert(userData == (void*)42);
18+
emscripten_force_exit(0);
19+
}
20+
21+
int main() {
22+
emscripten_queue_microtask(cb, (void*)42);
23+
emscripten_exit_with_live_runtime();
24+
return 99; // We won't reach here, but return non-zero value to guard against refactors that might exit() with this value.
25+
}

test/test_browser.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5016,6 +5016,13 @@ def test_emscripten_request_animation_frame_loop(self):
50165016
def test_request_animation_frame(self):
50175017
self.btest_exit('test_request_animation_frame.c')
50185018

5019+
def test_emscripten_queue_microtask(self):
5020+
self.btest_exit('emscripten_queue_microtask.c')
5021+
5022+
@requires_shared_array_buffer
5023+
def test_emscripten_queue_microtask_pthread(self):
5024+
self.btest_exit('emscripten_queue_microtask.c', cflags=['-pthread', '-sPROXY_TO_PTHREAD'])
5025+
50195026
@requires_shared_array_buffer
50205027
def test_emscripten_set_timeout(self):
50215028
self.btest_exit('emscripten_set_timeout.c', cflags=['-pthread', '-sPROXY_TO_PTHREAD'])

0 commit comments

Comments
 (0)