@@ -894,9 +894,9 @@ var LibraryPThread = {
894894 $proxyToMainThreadPtr: (...args) => BigInt(proxyToMainThread(...args)),
895895#endif
896896
897- $proxyToMainThread__deps: ['$stackSave', '$stackRestore', '$stackAlloc', '_emscripten_run_on_main_thread_js', ...i53ConversionDeps],
898- $proxyToMainThread__docs: '/** @type{function(number, (number|boolean), ...number)} */',
899- $proxyToMainThread: (funcIndex, emAsmAddr, sync, ...callArgs) => {
897+ $proxyToMainThread__deps: ['$stackSave', '$stackRestore', '$stackAlloc', '_emscripten_run_on_main_thread_js', '_emscripten_await_on_main_thread_js', ...i53ConversionDeps],
898+ $proxyToMainThread__docs: '/** @type{function(number, (number|boolean), number, (number|undefined), ...number)} */',
899+ $proxyToMainThread: (funcIndex, emAsmAddr, sync, asyncAwait, ...callArgs) => {
900900 // EM_ASM proxying is done by passing a pointer to the address of the EM_ASM
901901 // content as ` emAsmAddr `. JS library proxying is done by passing an index
902902 // into ` proxiedJSCallArgs ` as ` funcIndex `. If ` emAsmAddr ` is non-zero then
@@ -934,7 +934,12 @@ var LibraryPThread = {
934934 HEAPF64[b + i] = arg;
935935#endif
936936 }
937- var rtn = __emscripten_run_on_main_thread_js(funcIndex, emAsmAddr, serializedNumCallArgs, args, sync);
937+ var rtn;
938+ if (asyncAwait) {
939+ rtn = __emscripten_await_on_main_thread_js(funcIndex, emAsmAddr, serializedNumCallArgs, args);
940+ } else {
941+ rtn = __emscripten_run_on_main_thread_js(funcIndex, emAsmAddr, serializedNumCallArgs, args, sync);
942+ }
938943 stackRestore(sp);
939944 return rtn;
940945 },
@@ -945,7 +950,11 @@ var LibraryPThread = {
945950 _emscripten_receive_on_main_thread_js__deps: [
946951 '$proxyToMainThread',
947952 '$proxiedJSCallArgs'],
948- _emscripten_receive_on_main_thread_js: (funcIndex, emAsmAddr, callingThread, numCallArgs, args) => {
953+ /**
954+ * @param {number=} promiseCtx Optionally, when set, expect func to return a Promise
955+ * and use promiseCtx to signal awaiting pthread.
956+ */
957+ _emscripten_receive_on_main_thread_js: (funcIndex, emAsmAddr, callingThread, numCallArgs, args, promiseCtx) => {
949958 // Sometimes we need to backproxy events to the calling thread (e.g.
950959 // HTML5 DOM events handlers such as
951960 // emscripten_set_mousemove_callback()), so keep track in a globally
@@ -984,6 +993,24 @@ var LibraryPThread = {
984993 PThread.currentProxiedOperationCallerThread = callingThread;
985994 var rtn = func(...proxiedJSCallArgs);
986995 PThread.currentProxiedOperationCallerThread = 0;
996+ if (promiseCtx) {
997+ #if ASSERTIONS
998+ assert(!!rtn.then, 'Return value of proxied function expected to be a Promise but got' + rtn);
999+ #endif
1000+ rtn.then(res => {
1001+ #if MEMORY64
1002+ // In memory64 mode some proxied functions return bigint/pointer but
1003+ // our return type is i53/double.
1004+ if (typeof res == "bigint") {
1005+ res = bigintToI53Checked(res);
1006+ }
1007+ #endif
1008+ __emscripten_proxy_promise_finish(promiseCtx, res);
1009+ }).catch(err => {
1010+ __emscripten_proxy_promise_finish(promiseCtx, 0);
1011+ });
1012+ return;
1013+ }
9871014#if MEMORY64
9881015 // In memory64 mode some proxied functions return bigint/pointer but
9891016 // our return type is i53/double.
0 commit comments