I am working on a developer tool that should evaluate user code the same way the console does. For example, if I type multiple expression into the console, like this:
var x = 0; x++; x;
I immediately get the result of the last expression (1).
The obvious solution to replicate this behavior in my web app would be to execute the user code with eval, like this:
let result = eval('var x = 0; x++; x;');
However, eval has obvious performance implications and security concerns, so I want to avoid it. My solution right now is to inject a script in a sandboxed environment (iframe).  I feel like this is better than eval, but it does not solve the main issues which are:
- Modifying the user code in such a way that it can be written to a variable
- Getting the result of the last expression, regardless of what the user inputs (could be any code, just like one would do in the console)
Also, I would like to avoid using libraries (for example to parse and execute the code) for such a simple task. If anyone has some ideas or suggestions it would be greatly appreciated.
 
    
