Issue #843. If a for-body ends in a pure-statement, no need to try and return results.

This commit is contained in:
Jeremy Ashkenas
2010-11-20 19:12:39 -05:00
parent 1f12642af2
commit c8a2a78319
2 changed files with 13 additions and 13 deletions

View File

@@ -1754,10 +1754,12 @@
};
For.prototype.containsPureStatement = While.prototype.containsPureStatement;
For.prototype.compileNode = function(o) {
var body, defPart, forPart, guardPart, hasCode, idt1, index, ivar, lvar, name, namePart, ref, resultPart, returnResult, rvar, scope, source, stepPart, svar, varPart;
hasCode = this.body.contains(function(node) {
var body, defPart, forPart, guardPart, hasCode, hasPure, idt1, index, ivar, lvar, name, namePart, ref, resultPart, returnResult, rvar, scope, source, stepPart, svar, varPart, _ref;
body = Expressions.wrap([this.body]);
hasCode = body.contains(function(node) {
return node instanceof Code;
});
hasPure = (_ref = last(body.expressions)) != null ? _ref.containsPureStatement() : void 0;
source = this.range ? this.source.base : this.source;
scope = o.scope;
name = this.name && this.name.compile(o, LEVEL_LIST);
@@ -1774,14 +1776,13 @@
});
}
}
if (this.returns) {
if (this.returns && !hasPure) {
rvar = scope.freeVariable('results');
}
ivar = (this.range ? name : index) || scope.freeVariable('i');
varPart = '';
guardPart = '';
defPart = '';
body = Expressions.wrap([this.body]);
idt1 = this.tab + TAB;
if (this.range) {
forPart = source.compile(merge(o, {
@@ -1803,12 +1804,10 @@
forPart = "" + ivar + " = 0, " + lvar + " = " + svar + ".length; " + ivar + " < " + lvar + "; " + stepPart;
}
}
if (this.returns) {
if (this.returns && !hasPure) {
resultPart = "" + this.tab + rvar + " = [];\n";
returnResult = '\n' + (new Return(new Literal(rvar)).compile(o, LEVEL_PAREN));
if (this.returns) {
body = Push.wrap(rvar, body);
}
body = Push.wrap(rvar, body);
}
if (this.guard) {
body = Expressions.wrap([new If(this.guard, body)]);