@@ -960,7 +960,7 @@ var LibraryPThread = {
960960 $proxyToMainThreadPtr: (...args) => BigInt(proxyToMainThread(...args)),
961961#endif
962962
963- $proxyToMainThread__deps: ['$withStackSave ', '$stackAlloc', '_emscripten_run_on_main_thread_js'].concat(i53ConversionDeps),
963+ $proxyToMainThread__deps: ['$stackSave', '$stackRestore ', '$stackAlloc', '_emscripten_run_on_main_thread_js'].concat(i53ConversionDeps),
964964 $proxyToMainThread__docs: '/** @type{function(number, (number|boolean), ...number)} */',
965965 $proxyToMainThread: (funcIndex, emAsmAddr, sync, ...callArgs) => {
966966 // EM_ASM proxying is done by passing a pointer to the address of the EM_ASM
@@ -973,34 +973,36 @@ var LibraryPThread = {
973973 // all the args here.
974974 // We also pass 'sync' to C separately, since C needs to look at it.
975975 // Allocate a buffer, which will be copied by the C code.
976- return withStackSave(() => {
977- // First passed parameter specifies the number of arguments to the function.
978- // When BigInt support is enabled, we must handle types in a more complex
979- // way, detecting at runtime if a value is a BigInt or not (as we have no
980- // type info here). To do that, add a "prefix" before each value that
981- // indicates if it is a BigInt, which effectively doubles the number of
982- // values we serialize for proxying. TODO: pack this?
983- var serializedNumCallArgs = callArgs.length {{{ WASM_BIGINT ? "* 2" : "" }}};
984- var args = stackAlloc(serializedNumCallArgs * 8);
985- var b = {{{ getHeapOffset('args', 'i64') }}};
986- for (var i = 0; i < callArgs.length; i++) {
987- var arg = callArgs[i];
976+ //
977+ // First passed parameter specifies the number of arguments to the function.
978+ // When BigInt support is enabled, we must handle types in a more complex
979+ // way, detecting at runtime if a value is a BigInt or not (as we have no
980+ // type info here). To do that, add a "prefix" before each value that
981+ // indicates if it is a BigInt, which effectively doubles the number of
982+ // values we serialize for proxying. TODO: pack this?
983+ var serializedNumCallArgs = callArgs.length {{{ WASM_BIGINT ? "* 2" : "" }}};
984+ var sp = stackSave();
985+ var args = stackAlloc(serializedNumCallArgs * 8);
986+ var b = {{{ getHeapOffset('args', 'i64') }}};
987+ for (var i = 0; i < callArgs.length; i++) {
988+ var arg = callArgs[i];
988989#if WASM_BIGINT
989- if (typeof arg == 'bigint') {
990- // The prefix is non-zero to indicate a bigint.
991- HEAP64[b + 2*i] = 1n;
992- HEAP64[b + 2*i + 1] = arg;
993- } else {
994- // The prefix is zero to indicate a JS Number.
995- HEAP64[b + 2*i] = 0n;
996- HEAPF64[b + 2*i + 1] = arg;
997- }
990+ if (typeof arg == 'bigint') {
991+ // The prefix is non-zero to indicate a bigint.
992+ HEAP64[b + 2*i] = 1n;
993+ HEAP64[b + 2*i + 1] = arg;
994+ } else {
995+ // The prefix is zero to indicate a JS Number.
996+ HEAP64[b + 2*i] = 0n;
997+ HEAPF64[b + 2*i + 1] = arg;
998+ }
998999#else
999- HEAPF64[b + i] = arg;
1000+ HEAPF64[b + i] = arg;
10001001#endif
1001- }
1002- return __emscripten_run_on_main_thread_js(funcIndex, emAsmAddr, serializedNumCallArgs, args, sync);
1003- });
1002+ }
1003+ var rtn = __emscripten_run_on_main_thread_js(funcIndex, emAsmAddr, serializedNumCallArgs, args, sync);
1004+ stackRestore(sp);
1005+ return rtn;
10041006 },
10051007
10061008 // Reuse global JS array to avoid creating JS garbage for each proxied call
0 commit comments