Test for Safari 6 bug
The code does not work in recent (Safari 6.0) versions of Safari on mobile iOS devices like iPad 2. It seems the JIT has a bug when optimizing post-increment operations in tight loops. Pressing click me multiple times in mobile Safari 6 yields different results after a while.
by yGuy
HTML
<button onclick="utility.testAndOutput()">Click Me!</button>
<button onclick="utility.dump()">Dump!</button>
<div id="log"></div>
JavaScript
// a list cell implementation with a field that needs to hold the index.
function Cell(next){
this.next = next;
this.$f = -1;
this.getNext = function(){
return this.next;
}
}
// the last cell has no successor
var c = new Cell(null);
var last = c;
// we add 500 cells in front of the last one - resulting in a list of 500 items
for (var i = 0; i < 500; i++){
c = new Cell(c);
}
// the first cell
var first = c;
// dummy utility "class"
utility = {};
// test function
utility.test = function(){
var a = 0; // counter for index
for (var b = this.getStart(); b !== null; b = b.getNext()) // iterate over all cells
b.$f = a++; // assign index to cell and then increment
this.$f5 = !1; // random code
};
utility.getStart = function(){
return first;
};
utility.testAndOutput = function(){
first.$f = -1;
last.$f = -1;
this.test();
document.getElementById("log").innerHTML += "from " + first.$f + " to " + last.$f + "<br>";
};
utility.dump = function(){
document.getElementById("log").innerHTML = "Check:";
var count = 0;
for (var b = first; b !== null; b = b.next, count++){
if (b.$f !== count){
document.getElementById("log").innerHTML += (" b.$f = "+ b.$f + " ("+count+")<br>");
}
}
};