How to compile code in realtime in unity Webgl?

I am working on a project where I want to compile user-inputted code, and visualize that in Unity WebGL build that I have hosted on the website?

For that I figured out three ways, one to use roselyn compiler to build and run the code inputted by user, second is to compile the code to dll first and then load that dll files to unity WebGL, and third to compile the code to wasm and load that.

First one not worked, roslyn couldn’t compile code in runtime gave me error. Second dll loading also not worked, due to web restrictions, and third one I am trying right now but no luck still. I want to ask is there a way to achieve this.

could use LUA scripting plugins, if that would work for your use case?

At a minimum you would have to send the user’s code to a webserver, which then compiles the code and sends the output back. But if loading that output (ie DLL) at runtime isn’t possible, then there’s just no way to actually use the compiled code.

I’m sure you could use reflection to emit code at runtime but this would be an arduous process to support even a basic set of C#. Using an existing scripting language is the best choice. For Lua be sure to use Lua-CSharp as this will work on the web.

This is also recommended anyway because you HAVE to sandbox the code the user can write, compile and execute. Or else …