mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
Issue #843. If a for-body ends in a pure-statement, no need to try and return results.
This commit is contained in:
parent
1f12642af2
commit
c8a2a78319
2 changed files with 13 additions and 13 deletions
15
lib/nodes.js
15
lib/nodes.js
|
@ -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)]);
|
||||
|
|
|
@ -1417,7 +1417,9 @@ exports.For = class For extends Base
|
|||
# comprehensions. Some of the generated code can be shared in common, and
|
||||
# some cannot.
|
||||
compileNode: (o) ->
|
||||
hasCode = @body.contains (node) -> node instanceof Code
|
||||
body = Expressions.wrap [@body]
|
||||
hasCode = body.contains (node) -> node instanceof Code
|
||||
hasPure = last(body.expressions)?.containsPureStatement()
|
||||
source = if @range then @source.base else @source
|
||||
scope = o.scope
|
||||
name = @name and @name.compile o, LEVEL_LIST
|
||||
|
@ -1425,12 +1427,11 @@ exports.For = class For extends Base
|
|||
unless hasCode
|
||||
scope.find(name, immediate: yes) if name and not @pattern
|
||||
scope.find(index, immediate: yes) if index
|
||||
rvar = scope.freeVariable 'results' if @returns
|
||||
rvar = scope.freeVariable 'results' if @returns and not hasPure
|
||||
ivar = (if @range then name else index) or scope.freeVariable 'i'
|
||||
varPart = ''
|
||||
guardPart = ''
|
||||
defPart = ''
|
||||
body = Expressions.wrap [@body]
|
||||
idt1 = @tab + TAB
|
||||
if @range
|
||||
forPart = source.compile merge(o, {index: ivar, @step})
|
||||
|
@ -1448,10 +1449,10 @@ exports.For = class For extends Base
|
|||
lvar = scope.freeVariable 'len'
|
||||
stepPart = if @step then "#{ivar} += #{ @step.compile(o, LEVEL_OP) }" else "#{ivar}++"
|
||||
forPart = "#{ivar} = 0, #{lvar} = #{svar}.length; #{ivar} < #{lvar}; #{stepPart}"
|
||||
if @returns
|
||||
if @returns and not hasPure
|
||||
resultPart = "#{@tab}#{rvar} = [];\n"
|
||||
returnResult = '\n' + (new Return(new Literal(rvar)).compile o, LEVEL_PAREN)
|
||||
body = Push.wrap rvar, body if @returns
|
||||
body = Push.wrap rvar, body
|
||||
if @guard
|
||||
body = Expressions.wrap [new If @guard, body]
|
||||
if hasCode
|
||||
|
|
Loading…
Reference in a new issue