Skip to content

Commit d354cfd

Browse files
committed
[ASYNCIFY] Run continuations under callUserCallback
1 parent 48a147b commit d354cfd

9 files changed

+26
-32
lines changed

src/lib/libasync.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,9 @@ addToLibrary({
323323
// can just call the function with no args at all since the engine will produce zeros
324324
// for all arguments. However, for i64 arguments we get `undefined cannot be converted to
325325
// BigInt`.
326-
return func(...Asyncify.restoreRewindArguments(original));
327-
#else
328-
return func();
326+
func = func.bind(0, ...Asyncify.restoreRewindArguments(original));
329327
#endif
328+
return callUserCallback(func);
330329
},
331330

332331
// This receives a function to call to start the async operation, and
@@ -480,9 +479,8 @@ addToLibrary({
480479
#endif
481480
},
482481

483-
emscripten_sleep__deps: ['$safeSetTimeout'],
484482
emscripten_sleep__async: true,
485-
emscripten_sleep: (ms) => Asyncify.handleSleep((wakeUp) => safeSetTimeout(wakeUp, ms)),
483+
emscripten_sleep: (ms) => Asyncify.handleSleep((wakeUp) => setTimeout(wakeUp, ms)),
486484

487485
emscripten_wget_data__deps: ['$asyncLoad', 'malloc'],
488486
emscripten_wget_data__async: 'auto',
@@ -501,15 +499,14 @@ addToLibrary({
501499
}
502500
},
503501

504-
emscripten_scan_registers__deps: ['$safeSetTimeout'],
505502
emscripten_scan_registers__async: true,
506503
emscripten_scan_registers: (func) => {
507504
return Asyncify.handleSleep((wakeUp) => {
508505
// We must first unwind, so things are spilled to the stack. Then while
509506
// we are pausing we do the actual scan. After that we can resume. Note
510507
// how using a timeout here avoids unbounded call stack growth, which
511508
// could happen if we tried to scan the stack immediately after unwinding.
512-
safeSetTimeout(() => {
509+
setTimeout(() => {
513510
var stackBegin = Asyncify.currData + {{{ C_STRUCTS.asyncify_data_s.__size__ }}};
514511
var stackEnd = {{{ makeGetValue('Asyncify.currData', 0, '*') }}};
515512
{{{ makeDynCall('vpp', 'func') }}}(stackBegin, stackEnd);

src/lib/libcore.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1996,6 +1996,9 @@ addToLibrary({
19961996
err('Stack overflow detected. You can try increasing -sSTACK_SIZE (currently set to {{{ STACK_SIZE }}})');
19971997
}
19981998
}
1999+
#endif
2000+
#if RUNTIME_DEBUG
2001+
dbg("handleException: got unexpected exception, calling quit_")
19992002
#endif
20002003
quit_(1, e);
20012004
},
@@ -2066,10 +2069,11 @@ addToLibrary({
20662069
return;
20672070
}
20682071
try {
2069-
func();
2070-
maybeExit();
2072+
return func();
20712073
} catch (e) {
20722074
handleException(e);
2075+
} finally {
2076+
maybeExit();
20732077
}
20742078
},
20752079

test/codesize/test_codesize_cxx_lto.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"a.out.js": 18993,
3-
"a.out.js.gz": 7823,
2+
"a.out.js": 19001,
3+
"a.out.js.gz": 7824,
44
"a.out.nodebug.wasm": 106448,
55
"a.out.nodebug.wasm.gz": 42638,
6-
"total": 125441,
7-
"total_gz": 50461,
6+
"total": 125449,
7+
"total_gz": 50462,
88
"sent": [
99
"a (emscripten_resize_heap)",
1010
"b (_setitimer_js)",

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": 244848,
2+
"a.out.js": 244863,
33
"a.out.nodebug.wasm": 577879,
4-
"total": 822727,
4+
"total": 822742,
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": 7722,
3-
"a.out.js.gz": 3812,
2+
"a.out.js": 7737,
3+
"a.out.js.gz": 3816,
44
"a.out.nodebug.wasm": 19604,
55
"a.out.nodebug.wasm.gz": 9079,
6-
"total": 27326,
7-
"total_gz": 12891,
6+
"total": 27341,
7+
"total_gz": 12895,
88
"sent": [
99
"a (memory)",
1010
"b (emscripten_get_now)",

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": 8145,
3-
"a.out.js.gz": 4011,
2+
"a.out.js": 8160,
3+
"a.out.js.gz": 4015,
44
"a.out.nodebug.wasm": 19605,
55
"a.out.nodebug.wasm.gz": 9080,
6-
"total": 27750,
7-
"total_gz": 13091,
6+
"total": 27765,
7+
"total_gz": 13095,
88
"sent": [
99
"a (memory)",
1010
"b (emscripten_get_now)",

test/embind_with_asyncify.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,5 @@ int main() {
1515
val async_text = response.call<val>("text");
1616
std::string text = async_text.await().as<std::string>();
1717
assert(text == "foo");
18-
// This explicit exit() should not be necessary here.
19-
// TODO(https://github.com/emscripten-core/emscripten/issues/26093)
20-
exit(0);
2118
return 0;
2219
}

test/test_async_exit_after_wakeup.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@ addToLibrary({
22
async_func__async: true,
33
async_func: (value) => {
44
return Asyncify.handleSleep((wakeUp) => {
5-
// Currently with -sASYNCIFY the wakeUp call needs to be wrapped in
6-
// callUserCallback, otherwise things like `exit()` won't work from
7-
// the continuation.
8-
// TODO(https://github.com/emscripten-core/emscripten/issues/26093)
9-
setTimeout(() => callUserCallback(() => wakeUp(42)), 0);
5+
setTimeout(() => wakeUp(42), 0);
106
});
117
},
128
});

test/test_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9587,7 +9587,7 @@ def test_embind_lib_with_asyncify(self, args):
95879587
'-sINCOMING_MODULE_JS_API=onRuntimeInitialized',
95889588
]
95899589
self.cflags += args
9590-
self.do_core_test('embind_lib_with_asyncify.cpp')
9590+
self.do_core_test('embind_lib_with_asyncify.cpp', assert_returncode=NON_ZERO)
95919591

95929592
@no_asan('asyncify stack operations confuse asan')
95939593
@with_asyncify_and_jspi

0 commit comments

Comments
 (0)