codepen / infiniteloopbuster Goto Github PK
View Code? Open in Web Editor NEWUsing Esprima to stop infinite loops before they happen.
Using Esprima to stop infinite loops before they happen.
Consider this snippet
https://codepen.io/gaearon/pen/aWWQOG?editors=0010
Just try writing
<button className="square" onClick={this.setState({value: 'X'})>
instead of
<button className="square" onClick={() => this.setState({value: 'X'})}>
Codepen just auto refreshes when typing the first statement, hangs eventually leading to browser and system crash (Latest chrome, Ubuntu 16).
I'm not sure if this is the right place to report this, but this is really a pain. The code is not even saved, and codepen just starts refreshing while typing and just crashes.
Say you take the entire https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.min.js and instrument it with what is here.
You'll get some invalid JS. Here's a formatted-ish chunk of the broken JS:
if (window.CP.shouldStopExecution(22)){break;}
d=g.value,y=h+r(d,x++),v+=o(d,y,n,i);}
// Invalid right here.
window.CP.exitedLoop(22);
else for(;!(g=E.next()).done;){if (window.CP.shouldStopExecution(23)){break;}var _=g.value;_&&(d=_[1],y=h+c.escape(_[0])+f+r(d,0),v+=o(d,y,n,i))}
window.CP.exitedLoop(23);
}
I added the stuff required for testing as 8-in.js
and 8-out-broken.js
.
You can run node output-checker.js
to instrument and see results quickly.
We should probably NOT instrument code at all if it's being used as a
https://twitter.com/scriptype/status/863190656738631681
We can check for the existence of window
, but, that happens at a higher level than instrumentation, which just deals with strings.
var isSquare = function(arr){
var num = 0;
if (arr.length < 1) {
return undefined;
};
for (var i = 0; i < arr.length; i++) {
for (var x = 0; x < arr[i]; x++) {
if (arr[i] === 1) {
num += 1;
} else if (x * x === arr[i]) {
num += 1;
}
}
};
if (num === arr.length) {
return true;
} else {
return false;
}
};
console.log(isSquare([1, 4, 9, 16, 25, 36, 225, 7225, 9604, 5563518921]));
function timer() {
var stop = false;
window.setTimeout(function() {
stop = true;
}, 5000);
while(!stop) {
window.setInterval(function() {
console.log("1");
}, 1000);
}
}
function endClock() {
}
timer();
When I pass the while(1) console.log(Date.now())
loop, the output is
while(1) {
if (window.CP.shouldStopExecution(1)) {
break;
}
console.log(Date.now());
window.CP.exitedLoop(1);
}
Which isn't correct, since window.CP.exitedLoop(1); should come after the closing bracket. If I however pass it the same function but with a body (aka. while(1) { console.log(Date.now()); }
) I get
while(1) {
if (window.CP.shouldStopExecution(1)) {
break;
}
console.log(Date.now());
}
window.CP.exitedLoop(1);
I'll get this out of the way first: I have absolutely no idea why this works, I stumbled upon it by accident.
Switching the last two lines in
patches.push({ pos: start, str: prolog });
patches.push({ pos: end, str: epilog });
patches.push({ pos: node.range[1], str: LOOP_EXIT.replace('%d', loopId) });
To get
patches.push({ pos: start, str: prolog });
patches.push({ pos: node.range[1], str: LOOP_EXIT.replace('%d', loopId) });
patches.push({ pos: end, str: epilog });
Solves this problem for me.
I am learning react so i wrote wrong code for one my event handlers on react, and this made the whole browser page to freeze. I also cannot acces my codepen dashboard because of that.I cant send you the link to the proplematic pen because i cant even open any of the pens on the dashboard. my user name is Clayton Sibanda. i dont know if you guys can help with that
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.