Skip to content

Commit f970811

Browse files
committed
CR
1 parent 07f9d26 commit f970811

12 files changed

+64
-63
lines changed

src/library.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,7 +1631,7 @@ addToLibrary({
16311631
'$proxyToMainThread'
16321632
#endif
16331633
],
1634-
$runMainThreadEmAsm: (emAsmAddr, sigPtr, argbuf, sync, promise) => {
1634+
$runMainThreadEmAsm: (emAsmAddr, sigPtr, argbuf, sync, asnycAwait) => {
16351635
var args = readEmAsmArgs(sigPtr, argbuf);
16361636
#if PTHREADS
16371637
if (ENVIRONMENT_IS_PTHREAD) {
@@ -1644,7 +1644,7 @@ addToLibrary({
16441644
// of using __proxy. (And dor simplicity, do the same in the sync
16451645
// case as well, even though it's not strictly necessary, to keep the two
16461646
// code paths as similar as possible on both sides.)
1647-
return proxyToMainThread(0, emAsmAddr, sync, promise, ...args);
1647+
return proxyToMainThread(0, emAsmAddr, sync, asnycAwait, ...args);
16481648
}
16491649
#endif
16501650
#if ASSERTIONS
@@ -1655,14 +1655,17 @@ addToLibrary({
16551655
emscripten_asm_const_int_sync_on_main_thread__deps: ['$runMainThreadEmAsm'],
16561656
emscripten_asm_const_int_sync_on_main_thread: (emAsmAddr, sigPtr, argbuf) => runMainThreadEmAsm(emAsmAddr, sigPtr, argbuf, 1),
16571657

1658-
emscripten_asm_const_int_await_promise_on_main_thread__deps: ['$runMainThreadEmAsm'],
1659-
emscripten_asm_const_int_await_promise_on_main_thread: (emAsmAddr, sigPtr, argbuf) => {
1658+
emscripten_asm_const_int_await_on_main_thread__deps: ['$runMainThreadEmAsm'],
1659+
emscripten_asm_const_int_await_on_main_thread: (emAsmAddr, sigPtr, argbuf) => {
16601660
#if PTHREADS
16611661
if (ENVIRONMENT_IS_PTHREAD) {
1662-
return runMainThreadEmAsm(emAsmAddr, sigPtr, argbuf, 1, 1);
1662+
return runMainThreadEmAsm(emAsmAddr, sigPtr, argbuf, /*sync=*/1, /*asyncAwait=*/1);
16631663
}
16641664
#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)');
1665+
#if ASSERTIONS
1666+
assert((typeof ENVIRONMENT_IS_PTHREAD !== 'undefined' && ENVIRONMENT_IS_PTHREAD), "emscripten_asm_const_int_await_on_main_thread is not available on the main thread");
1667+
#endif
1668+
return runMainThreadEmAsm(emAsmAddr, sigPtr, argbuf, /*sync*/1, /*asyncAwait=*/1);
16661669
},
16671670

16681671
emscripten_asm_const_ptr_sync_on_main_thread__deps: ['$runMainThreadEmAsm'],

src/library_pthread.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ var LibraryPThread = {
895895
896896
$proxyToMainThread__deps: ['$stackSave', '$stackRestore', '$stackAlloc', '_emscripten_run_on_main_thread_js', ...i53ConversionDeps],
897897
$proxyToMainThread__docs: '/** @type{function(number, (number|boolean), ...number)} */',
898-
$proxyToMainThread: (funcIndex, emAsmAddr, sync, promise, ...callArgs) => {
898+
$proxyToMainThread: (funcIndex, emAsmAddr, sync, asyncAwait, ...callArgs) => {
899899
// EM_ASM proxying is done by passing a pointer to the address of the EM_ASM
900900
// content as `emAsmAddr`. JS library proxying is done by passing an index
901901
// into `proxiedJSCallArgs` as `funcIndex`. If `emAsmAddr` is non-zero then
@@ -933,7 +933,7 @@ var LibraryPThread = {
933933
HEAPF64[b + i] = arg;
934934
#endif
935935
}
936-
var rtn = __emscripten_run_on_main_thread_js(funcIndex, emAsmAddr, serializedNumCallArgs, args, sync, promise);
936+
var rtn = __emscripten_run_on_main_thread_js(funcIndex, emAsmAddr, serializedNumCallArgs, args, sync, asyncAwait);
937937
stackRestore(sp);
938938
return rtn;
939939
},
@@ -987,9 +987,6 @@ var LibraryPThread = {
987987
#if ASSERTIONS
988988
assert(!!rtn.then, 'Return value of proxied function expected to be a Promise but got' + rtn);
989989
#endif
990-
if (!rtn.then) {
991-
throw new Error('Return value of proxied function expected to be a Promise but got' + rtn);
992-
}
993990
rtn.then(res => {
994991
#if MEMORY64
995992
// In memory64 mode some proxied functions return bigint/pointer but
@@ -998,9 +995,9 @@ var LibraryPThread = {
998995
res = bigintToI53Checked(res);
999996
}
1000997
#endif
1001-
__emscripten_proxy_promise_finish(res, promiseCtx);
998+
__emscripten_proxy_promise_finish(promiseCtx, res);
1002999
}).catch(err => {
1003-
__emscripten_proxy_promise_finish(err, promiseCtx);
1000+
__emscripten_proxy_promise_finish(promiseCtx, err);
10041001
});
10051002
return;
10061003
}

src/library_sigs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ sigs = {
569569
emscripten_asm_const_double__sig: 'dppp',
570570
emscripten_asm_const_double_sync_on_main_thread__sig: 'dppp',
571571
emscripten_asm_const_int__sig: 'ippp',
572-
emscripten_asm_const_int_await_promise_on_main_thread__sig: 'ippp',
572+
emscripten_asm_const_int_await_on_main_thread__sig: 'ippp',
573573
emscripten_asm_const_int_sync_on_main_thread__sig: 'ippp',
574574
emscripten_asm_const_ptr__sig: 'pppp',
575575
emscripten_asm_const_ptr_sync_on_main_thread__sig: 'pppp',

system/include/emscripten/em_asm.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ __attribute__((nothrow))
2828
int emscripten_asm_const_int_sync_on_main_thread(
2929
const char* code, const char* arg_sigs, ...);
3030
__attribute__((nothrow))
31-
int emscripten_asm_const_int_await_promise_on_main_thread(
31+
int emscripten_asm_const_int_await_on_main_thread(
3232
const char* code, const char* arg_sigs, ...);
3333
__attribute__((nothrow))
3434
void* emscripten_asm_const_ptr_sync_on_main_thread(
@@ -54,7 +54,7 @@ void emscripten_asm_const_async_on_main_thread(
5454
#define EM_ASM_PTR(...) EM_ASM_ERROR
5555
#define EM_ASM_DOUBLE(...) EM_ASM_ERROR
5656
#define MAIN_THREAD_EM_ASM(...) EM_ASM_ERROR
57-
#define MAIN_THREAD_EM_ASM_PROMISE_AWAIT(...) EM_ASM_ERROR
57+
#define MAIN_THREAD_EM_ASM_AWAIT(...) EM_ASM_ERROR
5858
#define MAIN_THREAD_EM_ASM_INT(...) EM_ASM_ERROR
5959
#define MAIN_THREAD_EM_ASM_PTR(...) EM_ASM_ERROR
6060
#define MAIN_THREAD_EM_ASM_DOUBLE(...) EM_ASM_ERROR
@@ -259,7 +259,7 @@ const char __em_asm_sig_builder<__em_asm_type_tuple<Args...> >::buffer[] = { __e
259259
// The code must return a promise, and this function will wait for the promise
260260
// to resolve or reject, essentially blocking the calling thread until then.
261261
// In either case the function will return an integer, which is the result of the promise.
262-
#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__))
262+
#define MAIN_THREAD_EM_ASM_AWAIT(code, ...) emscripten_asm_const_int_await_on_main_thread(CODE_EXPR(#code) _EM_ASM_PREP_ARGS(__VA_ARGS__))
263263

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

system/lib/pthread/proxying.c

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ int emscripten_proxy_sync_with_ctx(em_proxying_queue* q,
402402
return ret;
403403
}
404404

405-
int emscripten_proxy_promise_await_with_ctx(em_proxying_queue* q,
405+
int emscripten_proxy_async_await_with_ctx(em_proxying_queue* q,
406406
pthread_t target_thread,
407407
void (*func)(em_proxying_ctx*, void*),
408408
void* arg, proxied_js_func_t* f) {
@@ -446,12 +446,12 @@ int emscripten_proxy_sync(em_proxying_queue* q,
446446
q, target_thread, call_then_finish_task, &t);
447447
}
448448

449-
int emscripten_proxy_promise_await(em_proxying_queue* q,
449+
int emscripten_proxy_async_await(em_proxying_queue* q,
450450
pthread_t target_thread,
451451
void (*func)(void*),
452452
proxied_js_func_t* f) {
453453
task t = {.func = func, .arg = (void*)f};
454-
return emscripten_proxy_promise_await_with_ctx(
454+
return emscripten_proxy_async_await_with_ctx(
455455
q, target_thread, call_task, &t, f);
456456
}
457457

@@ -642,25 +642,18 @@ static void run_js_func(void* arg) {
642642
}
643643
}
644644

645-
static void run_js_promise_await(void* arg) {
645+
static void run_js_async_await(void* arg) {
646646
proxied_js_func_t* f = (proxied_js_func_t*)arg;
647647
f->result = _emscripten_receive_on_main_thread_js(
648648
f->funcIndex, f->emAsmAddr, f->callingThread, f->numArgs, f->argBuffer, (void*)f->ctx);
649649
}
650650

651-
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;
655-
emscripten_proxy_finish(ctx);
656-
}
657-
658651
double _emscripten_run_on_main_thread_js(int func_index,
659652
void* em_asm_addr,
660653
int num_args,
661654
double* buffer,
662655
int sync,
663-
int promise) {
656+
int asyncAwait) {
664657
proxied_js_func_t f = {
665658
.funcIndex = func_index,
666659
.emAsmAddr = em_asm_addr,
@@ -673,18 +666,19 @@ double _emscripten_run_on_main_thread_js(int func_index,
673666
em_proxying_queue* q = emscripten_proxy_get_system_queue();
674667
pthread_t target = emscripten_main_runtime_thread_id();
675668

676-
if (sync && !promise) {
677-
if (!emscripten_proxy_sync(q, target, run_js_func, &f)) {
669+
// make sure it is not the case that sync==false and asyncAwait==true
670+
assert(!(sync == 0 && asyncAwait == 1) && "asyncAwait cannot be true if sync is false");
671+
if (sync) {
672+
if (asyncAwait) {
673+
if (!emscripten_proxy_async_await(q, target, run_js_async_await, &f)) {
674+
assert(false && "emscripten_proxy_promise failed");
675+
return 0;
676+
}
677+
} else if (!emscripten_proxy_sync(q, target, run_js_func, &f)) {
678678
assert(false && "emscripten_proxy_sync failed");
679679
return 0;
680680
}
681681
return f.result;
682-
} else if (sync && promise) {
683-
if (!emscripten_proxy_promise_await(q, target, run_js_promise_await, &f)) {
684-
assert(false && "emscripten_proxy_promise failed");
685-
return 0;
686-
}
687-
return f.result;
688682
}
689683

690684
// Make a heap-heap allocated copy of the proxied_js_func_t
@@ -701,3 +695,10 @@ double _emscripten_run_on_main_thread_js(int func_index,
701695
}
702696
return 0;
703697
}
698+
699+
void _emscripten_proxy_promise_finish(em_proxying_ctx* ctx, void* res) {
700+
task* t = (task*)ctx->arg;
701+
proxied_js_func_t* func = (proxied_js_func_t*)t->arg;
702+
func->result = (double)(intptr_t)res;
703+
emscripten_proxy_finish(ctx);
704+
}

test/core/test_main_thread_async_em_asm_promise_await.cpp renamed to test/core/test_main_thread_async_em_asm_await.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
int main()
1010
{
11-
printf("Before MAIN_THREAD_EM_ASM_PROMISE_AWAIT\n");
12-
int res = MAIN_THREAD_EM_ASM_PROMISE_AWAIT({
13-
out('Inside MAIN_THREAD_EM_ASM_PROMISE_AWAIT: ' + $0 + ' ' + $1);
11+
printf("Before MAIN_THREAD_EM_ASM_AWAIT\n");
12+
int res = MAIN_THREAD_EM_ASM_AWAIT({
13+
out('Inside MAIN_THREAD_EM_ASM_AWAIT: ' + $0 + ' ' + $1);
1414
const asyncOp = new Promise((resolve,reject) => {
1515
setTimeout(() => {
1616
out('Inside asyncOp');
@@ -19,7 +19,7 @@ int main()
1919
});
2020
return asyncOp;
2121
}, 42, 3.5);
22-
printf("After MAIN_THREAD_EM_ASM_PROMISE_AWAIT\n");
22+
printf("After MAIN_THREAD_EM_ASM_AWAIT\n");
2323
printf("result: %d\n", res);
2424
return 0;
2525
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Before MAIN_THREAD_EM_ASM_AWAIT
2+
Inside MAIN_THREAD_EM_ASM_AWAIT: 42 3.5
3+
Inside asyncOp
4+
After MAIN_THREAD_EM_ASM_AWAIT
5+
result: 2

test/core/test_main_thread_async_em_asm_promise_await_reject.cpp renamed to test/core/test_main_thread_async_em_asm_await_reject.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ int main()
1212
// start new thread
1313
pthread_t thread;
1414
pthread_create(&thread, NULL, [](void*) -> void* {
15-
printf("Before MAIN_THREAD_EM_ASM_PROMISE_AWAIT\n");
16-
int res = MAIN_THREAD_EM_ASM_PROMISE_AWAIT({
17-
out('Inside MAIN_THREAD_EM_ASM_PROMISE_AWAIT: ' + $0 + ' ' + $1);
15+
printf("Before MAIN_THREAD_EM_ASM_AWAIT\n");
16+
int res = MAIN_THREAD_EM_ASM_AWAIT({
17+
out('Inside MAIN_THREAD_EM_ASM_AWAIT: ' + $0 + ' ' + $1);
1818
const asyncOp = new Promise((resolve,reject) => {
1919
setTimeout(() => {
2020
out('Inside asyncOp');
@@ -23,7 +23,7 @@ int main()
2323
});
2424
return asyncOp;
2525
}, 42, 3.5);
26-
printf("After MAIN_THREAD_EM_ASM_PROMISE_AWAIT rejected\n");
26+
printf("After MAIN_THREAD_EM_ASM_AWAIT rejected\n");
2727
printf("result: %d\n", res);
2828
return NULL;
2929
}, NULL);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Before MAIN_THREAD_EM_ASM_AWAIT
2+
Inside MAIN_THREAD_EM_ASM_AWAIT: 42 3.5
3+
Inside asyncOp
4+
After MAIN_THREAD_EM_ASM_AWAIT rejected
5+
result: 2

test/core/test_main_thread_async_em_asm_promise_await.out

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)