@@ -899,9 +899,9 @@ var LibraryPThread = {
899899 $proxyToMainThreadPtr: (...args) => BigInt(proxyToMainThread(...args)),
900900#endif
901901
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) => {
905905 // EM_ASM proxying is done by passing a pointer to the address of the EM_ASM
906906 // content as ` emAsmAddr `. JS library proxying is done by passing an index
907907 // into ` proxiedJSCallArgs ` as ` funcIndex `. If ` emAsmAddr ` is non-zero then
@@ -939,7 +939,12 @@ var LibraryPThread = {
939939 HEAPF64[b + i] = arg;
940940#endif
941941 }
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+ }
943948 stackRestore(sp);
944949 return rtn;
945950 },
@@ -950,7 +955,11 @@ var LibraryPThread = {
950955 _emscripten_receive_on_main_thread_js__deps: [
951956 '$proxyToMainThread',
952957 '$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) => {
954963 // Sometimes we need to backproxy events to the calling thread (e.g.
955964 // HTML5 DOM events handlers such as
956965 // emscripten_set_mousemove_callback()), so keep track in a globally
@@ -989,6 +998,24 @@ var LibraryPThread = {
989998 PThread.currentProxiedOperationCallerThread = callingThread;
990999 var rtn = func(...proxiedJSCallArgs);
9911000 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+ }
9921019#if MEMORY64
9931020 // In memory64 mode some proxied functions return bigint/pointer but
9941021 // our return type is i53/double.
0 commit comments