HaxeFoundation/haxe — pull request #3179 by nadako (2014-07-15 07:31:01+00:00)
Title: Inline List iterator

This changes List.iterator() to return a concrete inlineable ListIterator class that is inlined on all platforms except PHP (because it has its own List implementation and some kind of enhanced iterator that I don't know what to do with).

So this code:
```haxe
class Main {
static function main() {
var a = new List();
for (v in a) {
}
}
}
```

Was producing:
```js
(function () { "use strict";
var List = function() {
this.length = 0;
};
List.prototype = {
iterator: function() {
return { h : this.h, hasNext : function() {
return this.h != null;
}, next : function() {
if(this.h == null) return null;
var x = this.h[0];
this.h = this.h[1];
return x;
}};
}
};
var Main = function() { };
Main.main = function() {
var a = new List();
var $it0 = a.iterator();
while( $it0.hasNext() ) {
var v = $it0.next();
}
};
Main.main();
})();
```

Now it produces:
```js
(function () { "use strict";
var List = function() {
this.length = 0;
};
var Main = function() { };
Main.main = function() {
var a = new List();
var _g_head = a.h;
var _g_val = null;
while(_g_head != null) {
var v;
_g_val = _g_head[0];
_g_head = _g_head[1];
v = _g_val;
}
};
Main.main();
})();
```

Note that I had to introduce a field in the iterator class for storing head element, instead of just using a temp var. This is currently required for an iterator to be properly inlined.