You could certainly generate a new Wasm module on the fly and then execute it from Javascript. Linking and sharing of memory should be possible.
Pyodide can dynamically load libraries that are separate Wasm modules so it is worth checking out how that works.