3 ms·
It's now two months that I'm trying to think how we can solve while(true){} bug in in-browser code editors. If you don't know what while(true){} bug is, go to
by msoad 14y ago
It's now two months that I'm trying to think how we can solve while(true){} bug in in-browser code editors.
If you don't know what while(true){} bug is, go to jsbin.com and type it.
There is no good solution so far...
- yuchi 14y agoThat's very interesting. Could you point me to some article explaining the reasons behind it?
- CaveTech 14y agoThis is known as "the halting problem". A very fundamental problem to computer science. There are some clever strategies used to minimize this issue, but it currently has no solution.
- beagle3 14y agoThere cannot be a perfect solution; that much was proved by Turing back in the '30s, when he founded a branch of math we now call "Computer Science" - http://en.wikipedia.org/wiki/Halting_problem http://en.wikipedia.org/wiki/Halting_problem (and http://simple.wikipedia.org/wiki/Halting_problem http://simple.wikipedia.org/wiki/Halting_problem) have the details. The question is, what is a desirable solution? It's easy enough to detect while(true){}, but it's impossible to detect longer equivalent pieces of code. I'd be fine with a "pause/halt running js" button; unfortunately, it's up to the browser to provide. User-provided solutions are essentially guaranteed to cause horrible slowdown.
- yuchi 14y agoOh gosh. I just understood the problem. It's simply the fact that it runs the code. I thought it was somehow a problem of the editor. Shame on me.
- edtechdev 14y agothat slowmo.js that I think was mentioned here recently inserts a timer that checks how long a loop is running https://github.com/toolness/slowmo-js https://github.com/toolness/slowmo-js An old tool I used would insert a check at the end of each loop for if the escape key was being pressed and exit
- marijn 14y agoThis is (to respond to sibling comments) not the halting problem per se. It is the problem of not having any control over a runaway loop in the browser. You could parse and transform code to add a check on every backwards jump (end of loop body, continue statements) that increments a counter or checks the time and throws an exception when it decides the code has ran for too long. (Of course, that'll slow the code down a lot.)
- mateo411 14y agoI've work for a company that lets people write their own Selenium scripts to test the performance their website. They use our service to monitor their website at 100 different locations. We solved the while(true){} by limiting the number of instructions the javascript program can run. To do this we use Rhino which is a Javascript implementation which runs in the JVM. It provides us a hook to get the instruction count. We had to figure this out, since we let our customers run their own scripts on our network. If they write a script that never terminates, then they've hosed a machine on our network.