Skip to content

Commit 4f9f408

Browse files
committed
Simplify setImmediate polyfill handling in EM_TIMING_SETIMMEDIATE
1 parent 8672d43 commit 4f9f408

7 files changed

Lines changed: 26 additions & 18 deletions

src/lib/libeventloop.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -344,14 +344,15 @@ LibraryJSEventLoop = {
344344
if (globalThis.setImmediate) {
345345
MainLoop.setImmediate = setImmediate;
346346
} else {
347+
#if RUNTIME_DEBUG
348+
dbg('using polyfil for setImmediate');
349+
#endif
347350
// Emulate setImmediate. (note: not a complete polyfill, we don't emulate clearImmediate() to keep code size to minimum, since not needed)
348351
var setImmediates = [];
349352
var emscriptenMainLoopMessageId = 'setimmediate';
350353
/** @param {Event} event */
351354
var MainLoop_setImmediate_messageHandler = (event) => {
352-
// When called in current thread or Worker, the main loop ID is structured slightly different to accommodate for --proxy-to-worker runtime listening to Worker events,
353-
// so check for both cases.
354-
if (event.data === emscriptenMainLoopMessageId || event.data.target === emscriptenMainLoopMessageId) {
355+
if (event.data === emscriptenMainLoopMessageId) {
355356
event.stopPropagation();
356357
setImmediates.shift()();
357358
}
@@ -360,10 +361,12 @@ LibraryJSEventLoop = {
360361
MainLoop.setImmediate = /** @type{function(function(): ?, ...?): number} */((func) => {
361362
setImmediates.push(func);
362363
if (ENVIRONMENT_IS_WORKER) {
363-
Module['setImmediates'] ??= [];
364-
Module['setImmediates'].push(func);
365-
postMessage({target: emscriptenMainLoopMessageId}); // In --proxy-to-worker, route the message via proxyClient.js
366-
} else postMessage(emscriptenMainLoopMessageId, "*"); // On the main thread, can just send the message to itself.
364+
// The postMessge API in a Worker, sends message to the main
365+
// thread and does not support the `targetOrigin` (*) argument.
366+
postMessage(emscriptenMainLoopMessageId);
367+
} else {
368+
postMessage(emscriptenMainLoopMessageId, '*');
369+
}
367370
});
368371
}
369372
}

src/lib/libpthread.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ var LibraryPThread = {
305305
}
306306
#endif
307307
onFinishedLoading(worker);
308-
} else if (d.target === 'setimmediate') {
308+
} else if (d === 'setimmediate') {
309309
// Worker wants to postMessage() to itself to implement setImmediate()
310310
// emulation.
311311
worker.postMessage(d);

test/codesize/test_codesize_hello_dylink_all.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"a.out.js": 268231,
2+
"a.out.js": 268108,
33
"a.out.nodebug.wasm": 587563,
4-
"total": 855794,
4+
"total": 855671,
55
"sent": [
66
"IMG_Init",
77
"IMG_Load",

test/codesize/test_codesize_minimal_pthreads.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"a.out.js": 7365,
3-
"a.out.js.gz": 3587,
2+
"a.out.js": 7358,
3+
"a.out.js.gz": 3583,
44
"a.out.nodebug.wasm": 19037,
55
"a.out.nodebug.wasm.gz": 8787,
6-
"total": 26402,
7-
"total_gz": 12374,
6+
"total": 26395,
7+
"total_gz": 12370,
88
"sent": [
99
"a (memory)",
1010
"b (exit)",

test/codesize/test_codesize_minimal_pthreads_memgrowth.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"a.out.js": 7774,
3-
"a.out.js.gz": 3793,
2+
"a.out.js": 7767,
3+
"a.out.js.gz": 3790,
44
"a.out.nodebug.wasm": 19038,
55
"a.out.nodebug.wasm.gz": 8788,
6-
"total": 26812,
7-
"total_gz": 12581,
6+
"total": 26805,
7+
"total_gz": 12578,
88
"sent": [
99
"a (memory)",
1010
"b (exit)",

test/test_browser.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,6 +1864,10 @@ def test_emscripten_main_loop_and_blocker_exit(self):
18641864
def test_emscripten_main_loop_setimmediate(self, args):
18651865
self.btest_exit('test_emscripten_main_loop_setimmediate.c', cflags=args)
18661866

1867+
def test_emscripten_main_loop_setimmediate_polyfill(self):
1868+
create_file('remove_setimmediate.js', 'globalThis.setImmediate = undefined;')
1869+
self.btest_exit('test_emscripten_main_loop_setimmediate.c', cflags=['-sRUNTIME_DEBUG', '--pre-js=remove_setimmediate.js'])
1870+
18671871
@parameterized({
18681872
'': ([],),
18691873
'O1': (['-O1'],),

test/test_emscripten_main_loop_setimmediate.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ void looper() {
2727
// Expecting to run extremely fast unthrottled, and certainly not bounded by
2828
// vsync, so less than common 16.667 msecs per frame (this is assuming 60hz
2929
// display)
30+
assert(msecsPerFrame < 5);
3031
exit((msecsPerFrame < 5) ? 0 : 1);
3132
}
3233
}

0 commit comments

Comments
 (0)