You don't need to modify an already running program, you can plug new functions into an existing Wasm program via a table, and even attach variables via globals or function arguments.
I'd recommend checking the work on making SpiderMonkey emit Wasm as a backend