-
Notifications
You must be signed in to change notification settings - Fork 0
Patch Khalid's infinite-loop2 + translations #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // TODO: Clean up. Not really wriotten for modules and a bit of a hack as is. | ||
| // Imports should be better, also may as well make this a proper javascript | ||
| // class opposed to whatever this is. | ||
|
|
||
| var infiniteLoopDetector = (function() { | ||
| var map = {} | ||
|
|
||
| // define an InfiniteLoopError class | ||
| function InfiniteLoopError(msg, type) { | ||
| Error.call(this ,msg) | ||
| this.type = 'InfiniteLoopError' | ||
| } | ||
|
|
||
| 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) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hardcoded 1 second? Maybe we can make this adjustable? Or do you think 1 second is fine? |
||
| delete map[id] | ||
| throw new Error('Loop running too long!', 'InfiniteLoopError') | ||
| } | ||
| } else { // First run, record the time the loop started. All the judgments that are not first run are written in the previous if because the above will be executed more times | ||
| map[id] = Date.now() | ||
| } | ||
| } | ||
|
|
||
| infiniteLoopDetector.wrap = function(codeStr) { | ||
| if (typeof codeStr !== 'string') { | ||
| throw new Error('Can only wrap code represented by string, not any other thing at the time! If you want to wrap a function, convert it to string first.') | ||
| } | ||
| // Replaces potential infite loop areas with a bit of guard code that makes | ||
| // 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) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lol, this regex is such a hack- but works well enough I guess. |
||
| var id = parseInt(Math.random() * Number.MAX_SAFE_INTEGER) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kmsalah But this would let us create more meaningful errors as well opposed to "Loop running too long" |
||
| return `infDetector(${id});${loopHead}infDetector(${id});` | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. infDetector matches the var in frame.js. But obviously this is very brittle as is. |
||
| }); | ||
| // console.log(response); | ||
| return response; | ||
| } | ||
| infiniteLoopDetector.unwrap = function(codeStr) { | ||
| return codeStr.replace(/infiniteLoopDetector\([0-9]*?\);/g, '') | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Woops. Bug. This should be infDectector But also, maybe this function isn't needed? When are we ever going to call |
||
| } | ||
|
|
||
| return infiniteLoopDetector | ||
| }()) | ||
|
|
||
| module.exports.infiniteLoopDetector = infiniteLoopDetector; | ||
|
|
||
| // todo: copied from https://github.com/xieranmaya/infinite-loop-detector/blob/master/infinite-loop-detector.js | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a bit of a speil. I think to make this play better with modules, it should be rewritten