I'm working on JavaScript code editor where users can write their own JavaScript code in the browser and run it. I need to find a way to break out of infinite loops. When I am given the code:
while (1) {
doSomething();
}
I want to transform the code to something like this:
var start = Date.now();
while (1) {
if (Date.now() - start > 1000) { break; }
doSomething();
}
I stumbled upon Web-Maker, which has a function that does exactly this. I couldn't get the function to transform the code passed in. I've tried addInfiniteLoopProtection('while (1) doSomething()', { timeout: 1000 })
but it returns 'while (1) doSomething()'
instead of changing the code to break out of the infinite loop.