Hacker News new | ask | show | jobs
by oofsa 1097 days ago
Cool. This may be a bit nitpicky, but most of the interactive JavaScript environments I have seen with auto-run ("evaluates as-you-type"), including this one, freeze in the following case.

  for (let i = 0; i < 10;|) {}
                         ^ cursor
2 comments

No problem, there's an NPM package for that!

(no, I'm not kidding: https://www.npmjs.com/package/halting-problem)

An easy way to correct this is injecting a watchdog function call at the end of every for and while loop. It’s a trivial AST transformation that’s relatively fool proof as opposed to trying to figure out what condition would progress the loop. The semantics of the watchdog depend on the use case and ideally you can transform the evaled code in an async wrapper. Set a global variable to the current time on animation frames and then throw an exception inside the watchdog when it times out (when difference between the global variable and current time is > x ms).