From eb31d86212ae79b60c8a3f055cdae1333c2f822f Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Fri, 10 Feb 2023 12:17:50 -0800 Subject: [PATCH] jsifier.js: Move `__proxy` handling so it happens before wasm64 wrapping Logically the code make more sense here in processLibraryFunction, but its also important that the wasm64 wrapper is the outermost one. Without this the wasm64 adapter is applied to the inner, non-proxied function only. Split out from #18705 --- src/jsifier.js | 58 ++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/src/jsifier.js b/src/jsifier.js index 0dcb46771a94b..a05dc93c846d5 100644 --- a/src/jsifier.js +++ b/src/jsifier.js @@ -151,6 +151,33 @@ function ${name}(${args}) { }`); } + if (SHARED_MEMORY) { + const proxyingMode = LibraryManager.library[symbol + '__proxy']; + if (proxyingMode) { + if (proxyingMode !== 'sync' && proxyingMode !== 'async') { + throw new Error(`Invalid proxyingMode ${symbol}__proxy: '${proxyingMode}' specified!`); + } + const sync = proxyingMode === 'sync'; + if (USE_PTHREADS) { + snippet = modifyFunction(snippet, (name, args, body) => ` +function ${name}(${args}) { +if (ENVIRONMENT_IS_PTHREAD) + return _emscripten_proxy_to_main_thread_js(${proxiedFunctionTable.length}, ${+sync}${args ? ', ' : ''}${args}); +${body} +}\n`); + } else if (WASM_WORKERS && ASSERTIONS) { + // In ASSERTIONS builds add runtime checks that proxied functions are not attempted to be called in Wasm Workers + // (since there is no automatic proxying architecture available) + snippet = modifyFunction(snippet, (name, args, body) => ` +function ${name}(${args}) { + assert(!ENVIRONMENT_IS_WASM_WORKER, "Attempted to call proxied function '${name}' in a Wasm Worker, but in Wasm Worker enabled builds, proxied function architecture is not available!"); + ${body} +}\n`); + } + proxiedFunctionTable.push(mangled); + } + } + if (MEMORY64) { const sig = LibraryManager.library[symbol + '__sig']; if (sig && sig.includes('p')) { @@ -362,36 +389,7 @@ function ${name}(${args}) { let contentText; if (isFunction) { // Emit the body of a JS library function. - const proxyingMode = LibraryManager.library[symbol + '__proxy']; - if (SHARED_MEMORY && proxyingMode) { - if (proxyingMode !== 'sync' && proxyingMode !== 'async') { - throw new Error(`Invalid proxyingMode ${symbol}__proxy: '${proxyingMode}' specified!`); - } - const sync = proxyingMode === 'sync'; - assert(typeof original == 'function'); - if (USE_PTHREADS) { - contentText = modifyFunction(snippet, (name, args, body) => ` -function ${name}(${args}) { - if (ENVIRONMENT_IS_PTHREAD) - return _emscripten_proxy_to_main_thread_js(${proxiedFunctionTable.length}, ${+sync}${args ? ', ' : ''}${args}); - ${body} -}\n`); - } else if (WASM_WORKERS) { - if (ASSERTIONS) { - // In ASSERTIONS builds add runtime checks that proxied functions are not attempted to be called in Wasm Workers - // (since there is no automatic proxying architecture available) - contentText = modifyFunction(snippet, (name, args, body) => ` - function ${name}(${args}) { - assert(!ENVIRONMENT_IS_WASM_WORKER, "Attempted to call proxied function '${name}' in a Wasm Worker, but in Wasm Worker enabled builds, proxied function architecture is not available!"); - ${body} - }\n`); - } else { - // In non-ASSERTIONS builds directly emit the original function. - contentText = snippet; - } - } - proxiedFunctionTable.push(mangled); - } else if ((USE_ASAN || USE_LSAN || UBSAN_RUNTIME) && LibraryManager.library[symbol + '__noleakcheck']) { + if ((USE_ASAN || USE_LSAN || UBSAN_RUNTIME) && LibraryManager.library[symbol + '__noleakcheck']) { contentText = modifyFunction(snippet, (name, args, body) => ` function ${name}(${args}) { return withBuiltinMalloc(function() {