From d168b659e96693caa92fb2bdd940b53009344043 Mon Sep 17 00:00:00 2001 From: Arnold Date: Mon, 10 Aug 2026 04:21:42 +1000 Subject: [PATCH] Fix WASM binding resolution across module scopes --- CSharpWasmExpo/main.js | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/CSharpWasmExpo/main.js b/CSharpWasmExpo/main.js index b8b414d6..2f08bd69 100644 --- a/CSharpWasmExpo/main.js +++ b/CSharpWasmExpo/main.js @@ -9,9 +9,31 @@ const parseMethods = (methods) => { const bindingsFunctions = {}; + const frame = document.querySelector("iframe"); + const wasmScope = frame?.contentWindow; + for (const name of methodList) { try { - bindingsFunctions[name] = eval(name); + let fn = + globalThis[name] ?? + wasmScope?.[name] ?? + wasmScope?.[`_CPP_${name}`]; + + if (typeof fn !== "function") { + const sklibMatch = Object.keys(wasmScope || {}).find( + (key) => key === `__sklib__${name}` || key.startsWith(`__sklib__${name}__`) + ); + + if (sklibMatch && typeof wasmScope[sklibMatch] === "function") { + fn = wasmScope[sklibMatch]; + } + } + + if (typeof fn === "function") { + bindingsFunctions[name] = fn; + } else { + console.warn(`Missing binding: ${name}`); + } } catch (e) { console.warn(e); } @@ -68,4 +90,4 @@ const CompileAndRun = async (code, reportError) => { // This event will be trigger by the csharp compiler document.addEventListener("compileAndRun", (ev) => { CompileAndRun(ev.detail.program[0].source, ev.detail.reportError); -}); +}); \ No newline at end of file