Skip to content

Commit c153f10

Browse files
committed
capture promise returned value
1 parent 88341ba commit c153f10

File tree

6 files changed

+17
-8
lines changed

6 files changed

+17
-8
lines changed

src/library.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,9 +1647,6 @@ addToLibrary({
16471647
return proxyToMainThread(0, emAsmAddr, sync, promise, ...args);
16481648
}
16491649
#endif
1650-
if (promise) {
1651-
throw new Error('emscripten_asm_const_int_await_promise_on_main_thread is not supported in single-threaded mode');
1652-
}
16531650
#if ASSERTIONS
16541651
assert(ASM_CONSTS.hasOwnProperty(emAsmAddr), `No EM_ASM constant found at address ${emAsmAddr}. The loaded WebAssembly file is likely out of sync with the generated JavaScript.`);
16551652
#endif
@@ -1659,7 +1656,14 @@ addToLibrary({
16591656
emscripten_asm_const_int_sync_on_main_thread: (emAsmAddr, sigPtr, argbuf) => runMainThreadEmAsm(emAsmAddr, sigPtr, argbuf, 1),
16601657

16611658
emscripten_asm_const_int_await_promise_on_main_thread__deps: ['$runMainThreadEmAsm'],
1662-
emscripten_asm_const_int_await_promise_on_main_thread: (emAsmAddr, sigPtr, argbuf) => runMainThreadEmAsm(emAsmAddr, sigPtr, argbuf, 1, 1),
1659+
emscripten_asm_const_int_await_promise_on_main_thread: (emAsmAddr, sigPtr, argbuf) => {
1660+
#if PTHREADS
1661+
if (ENVIRONMENT_IS_PTHREAD) {
1662+
return runMainThreadEmAsm(emAsmAddr, sigPtr, argbuf, 1, 1);
1663+
}
1664+
#endif
1665+
throw new Error('call to emscripten_asm_const_int_await_promise_on_main_thread is only supported from pthread (but was called from main thread)');
1666+
},
16631667

16641668
emscripten_asm_const_ptr_sync_on_main_thread__deps: ['$runMainThreadEmAsm'],
16651669
emscripten_asm_const_ptr_sync_on_main_thread: (emAsmAddr, sigPtr, argbuf) => runMainThreadEmAsm(emAsmAddr, sigPtr, argbuf, 1),

system/include/emscripten/em_asm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ const char __em_asm_sig_builder<__em_asm_type_tuple<Args...> >::buffer[] = { __e
254254
// functions.
255255
#define MAIN_THREAD_EM_ASM(code, ...) ((void)emscripten_asm_const_int_sync_on_main_thread(CODE_EXPR(#code) _EM_ASM_PREP_ARGS(__VA_ARGS__)))
256256

257-
#define MAIN_THREAD_EM_ASM_PROMISE_AWAIT(code, ...) ((void)emscripten_asm_const_int_await_promise_on_main_thread(CODE_EXPR(#code) _EM_ASM_PREP_ARGS(__VA_ARGS__)))
257+
#define MAIN_THREAD_EM_ASM_PROMISE_AWAIT(code, ...) emscripten_asm_const_int_await_promise_on_main_thread(CODE_EXPR(#code) _EM_ASM_PREP_ARGS(__VA_ARGS__))
258258

259259
// Runs the given JavaScript code synchronously on the main browser thread, and
260260
// returns an integer back.

system/lib/pthread/proxying.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,9 @@ static void run_js_promise_await(void* arg) {
649649
}
650650

651651
void _emscripten_proxy_promise_finish(void* res, em_proxying_ctx* ctx) {
652+
task* t = (task*)ctx->arg;
653+
proxied_js_func_t* func = (proxied_js_func_t*)t->arg;
654+
func->result = (double)(intptr_t)res;
652655
emscripten_proxy_finish(ctx);
653656
}
654657

test/core/test_main_thread_async_em_asm_promise_await.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@
99
int main()
1010
{
1111
printf("Before MAIN_THREAD_EM_ASM_PROMISE_AWAIT\n");
12-
MAIN_THREAD_EM_ASM_PROMISE_AWAIT({
12+
int res = MAIN_THREAD_EM_ASM_PROMISE_AWAIT({
1313
out('Inside MAIN_THREAD_EM_ASM_PROMISE_AWAIT: ' + $0 + ' ' + $1);
1414
const asyncOp = new Promise((resolve,reject) => {
1515
setTimeout(() => {
1616
out('Inside asyncOp');
17-
resolve();
17+
resolve(2);
1818
}, 1000);
1919
});
2020
return asyncOp;
2121
}, 42, 3.5);
2222
printf("After MAIN_THREAD_EM_ASM_PROMISE_AWAIT\n");
23+
printf("result: %d\n", res);
2324
return 0;
2425
}

test/core/test_main_thread_async_em_asm_promise_await.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ Before MAIN_THREAD_EM_ASM_PROMISE_AWAIT
22
Inside MAIN_THREAD_EM_ASM_PROMISE_AWAIT: 42 3.5
33
Inside asyncOp
44
After MAIN_THREAD_EM_ASM_PROMISE_AWAIT
5+
result: 2

test/test_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1905,7 +1905,7 @@ def test_main_thread_async_em_asm_promise_await(self, args, force_c=False):
19051905
if '-sPROXY_TO_PTHREAD' not in args:
19061906
# expect runtime to error
19071907
output = self.do_runf('core/test_main_thread_async_em_asm_promise_await.cpp', expected_output=None, assert_returncode=NON_ZERO, emcc_args=args)
1908-
self.assertContained('is not supported in single-threaded mode', output)
1908+
self.assertContained('call to emscripten_asm_const_int_await_promise_on_main_thread is only supported from pthread (but was called from main thread)', output)
19091909
else:
19101910
self.do_core_test('test_main_thread_async_em_asm_promise_await.cpp', emcc_args=args, force_c=force_c)
19111911

0 commit comments

Comments
 (0)