@@ -899,9 +899,9 @@ var LibraryPThread = {
899
899
$proxyToMainThreadPtr: (...args) => BigInt(proxyToMainThread(...args)),
900
900
#endif
901
901
902
- $proxyToMainThread__deps: ['$stackSave', '$stackRestore', '$stackAlloc', '_emscripten_run_on_main_thread_js', ...i53ConversionDeps],
903
- $proxyToMainThread__docs: '/** @type{function(number, (number|boolean), ...number)} */',
904
- $proxyToMainThread: (funcIndex, emAsmAddr, sync, ...callArgs) => {
902
+ $proxyToMainThread__deps: ['$stackSave', '$stackRestore', '$stackAlloc', '_emscripten_run_on_main_thread_js', '_emscripten_await_on_main_thread_js', ...i53ConversionDeps],
903
+ $proxyToMainThread__docs: '/** @type{function(number, (number|boolean), number, (number|undefined), ...number)} */',
904
+ $proxyToMainThread: (funcIndex, emAsmAddr, sync, asyncAwait, ...callArgs) => {
905
905
// EM_ASM proxying is done by passing a pointer to the address of the EM_ASM
906
906
// content as ` emAsmAddr `. JS library proxying is done by passing an index
907
907
// into ` proxiedJSCallArgs ` as ` funcIndex `. If ` emAsmAddr ` is non-zero then
@@ -939,7 +939,12 @@ var LibraryPThread = {
939
939
HEAPF64[b + i] = arg;
940
940
#endif
941
941
}
942
- var rtn = __emscripten_run_on_main_thread_js(funcIndex, emAsmAddr, serializedNumCallArgs, args, sync);
942
+ var rtn;
943
+ if (asyncAwait) {
944
+ rtn = __emscripten_await_on_main_thread_js(funcIndex, emAsmAddr, serializedNumCallArgs, args);
945
+ } else {
946
+ rtn = __emscripten_run_on_main_thread_js(funcIndex, emAsmAddr, serializedNumCallArgs, args, sync);
947
+ }
943
948
stackRestore(sp);
944
949
return rtn;
945
950
},
@@ -950,7 +955,11 @@ var LibraryPThread = {
950
955
_emscripten_receive_on_main_thread_js__deps: [
951
956
'$proxyToMainThread',
952
957
'$proxiedJSCallArgs'],
953
- _emscripten_receive_on_main_thread_js: (funcIndex, emAsmAddr, callingThread, numCallArgs, args) => {
958
+ /**
959
+ * @param {number=} promiseCtx Optionally, when set, expect func to return a Promise
960
+ * and use promiseCtx to signal awaiting pthread.
961
+ */
962
+ _emscripten_receive_on_main_thread_js: (funcIndex, emAsmAddr, callingThread, numCallArgs, args, promiseCtx) => {
954
963
// Sometimes we need to backproxy events to the calling thread (e.g.
955
964
// HTML5 DOM events handlers such as
956
965
// emscripten_set_mousemove_callback()), so keep track in a globally
@@ -989,6 +998,24 @@ var LibraryPThread = {
989
998
PThread.currentProxiedOperationCallerThread = callingThread;
990
999
var rtn = func(...proxiedJSCallArgs);
991
1000
PThread.currentProxiedOperationCallerThread = 0;
1001
+ if (promiseCtx) {
1002
+ #if ASSERTIONS
1003
+ assert(!!rtn.then, 'Return value of proxied function expected to be a Promise but got' + rtn);
1004
+ #endif
1005
+ rtn.then(res => {
1006
+ #if MEMORY64
1007
+ // In memory64 mode some proxied functions return bigint/pointer but
1008
+ // our return type is i53/double.
1009
+ if (typeof res == "bigint") {
1010
+ res = bigintToI53Checked(res);
1011
+ }
1012
+ #endif
1013
+ __emscripten_proxy_promise_finish(promiseCtx, res);
1014
+ }).catch(err => {
1015
+ __emscripten_proxy_promise_finish(promiseCtx, 0);
1016
+ });
1017
+ return;
1018
+ }
992
1019
#if MEMORY64
993
1020
// In memory64 mode some proxied functions return bigint/pointer but
994
1021
// our return type is i53/double.
0 commit comments