Patch Khalid's infinite-loop2 + translations#74
Conversation
|
For your reference. I think this still needs work though. Once you understand what it's doing, I think it makes sense to rewrite it completely. I converted the comments over from Chinese, some hopefully that gives a bit more context, and added some comments of my own I used this game to verify it works: #UucsDSEXrhzETmYu-4 |
dmadisetti
left a comment
There was a problem hiding this comment.
@kmsalah Thoughts? Hope this helped
| // | ||
| // this is not a strong regex, but enough to use at the time | ||
| let response = codeStr.replace(/for *\(.*\{|while *\(.*\{|do *\{/g, function(loopHead) { | ||
| var id = parseInt(Math.random() * Number.MAX_SAFE_INTEGER) |
There was a problem hiding this comment.
@kmsalah
Opposed to random number here, maybe we use the line number and character offset. Need to be careful since we could feasible have multiple loops on one line.
But this would let us create more meaningful errors as well opposed to "Loop running too long"
| // and makes sure that the loop doesn't exceed some value of execution time. | ||
| // | ||
| // this is not a strong regex, but enough to use at the time | ||
| let response = codeStr.replace(/for *\(.*\{|while *\(.*\{|do *\{/g, function(loopHead) { |
There was a problem hiding this comment.
Lol, this regex is such a hack- but works well enough I guess.
| @@ -0,0 +1,49 @@ | |||
| // TODO: Clean up. Not really wriotten for modules and a bit of a hack as is. | |||
There was a problem hiding this comment.
Just a bit of a speil. I think to make this play better with modules, it should be rewritten
| return response; | ||
| } | ||
| infiniteLoopDetector.unwrap = function(codeStr) { | ||
| return codeStr.replace(/infiniteLoopDetector\([0-9]*?\);/g, '') |
There was a problem hiding this comment.
Woops. Bug. This should be infDectector
But also, maybe this function isn't needed? When are we ever going to call unwrap
| // this is not a strong regex, but enough to use at the time | ||
| let response = codeStr.replace(/for *\(.*\{|while *\(.*\{|do *\{/g, function(loopHead) { | ||
| var id = parseInt(Math.random() * Number.MAX_SAFE_INTEGER) | ||
| return `infDetector(${id});${loopHead}infDetector(${id});` |
There was a problem hiding this comment.
infDetector matches the var in frame.js. But obviously this is very brittle as is.
|
|
||
| function infiniteLoopDetector(id) { | ||
| if (id in map) { // Not the first execution, it can be optimized here, the performance is too low | ||
| if (Date.now() - map[id] > 1000) { |
There was a problem hiding this comment.
Hardcoded 1 second? Maybe we can make this adjustable? Or do you think 1 second is fine?
Cleaned version of #73