I have a project where I have to execute potentially unsafe code and I thought about using eval and it does work because most of the use cases imply having code written by oneself or other team members (so the code is trusted most of the use cases). But I want to expand it to other use cases and want to make the code safe.

I want to execute very simple code, things like if-else, math operations, loops, and such. I don't really need classes or fancy stuff. I want all I/O interfaces to be blocked, and no cookies, indexedDB, etc.

I know there are a few interpreters for some languages only, such as Opal for Ruby, but another requirement is that I need to set a timeout time so that code that stalls is forcefully exited.

Things like:


Will ruin it.

I think any language is fine, as long as there's an available interpreter that can be run in the browser (therefore it has to be Javascript). A second option is to create a web server and run the code there, using an interpreter not necessarily written in Javascript, but I want to stay away from this option as it could be costly performance-wise. Also hopefully a popular language is best (Ruby, Javascript, etc).

Thanks in advance.

  • "I need to set a timeout time so that code that stalls is forcefully exited" - run it in a thread and start a timer. If the timer expires before the thread finished, kill the thread – Mawg says reinstate Monica Oct 25 '19 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.