Skip to content

Commit c37202e

Browse files
committed
Removing variable indirection for simple steps
1 parent 1f5b19b commit c37202e

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

lib/coffee-script/nodes.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2566,7 +2566,7 @@
25662566
For.prototype.children = ['body', 'source', 'guard', 'step'];
25672567

25682568
For.prototype.compileNode = function(o) {
2569-
var body, defPart, forPart, forVarPart, guardPart, idt1, index, ivar, kvar, kvarAssign, lastJumps, lvar, name, namePart, ref, resultPart, returnResult, rvar, scope, source, stepPart, stepvar, svar, varPart, _ref2;
2569+
var body, defPart, forPart, forVarPart, guardPart, idt1, index, ivar, kvar, kvarAssign, lastJumps, lvar, name, namePart, ref, resultPart, returnResult, rvar, scope, source, stepPart, stepvar, svar, varPart, _base, _ref2;
25702570
body = Block.wrap([this.body]);
25712571
lastJumps = (_ref2 = last(body.expressions)) != null ? _ref2.jumps() : void 0;
25722572
if (lastJumps && lastJumps instanceof Return) {
@@ -2589,7 +2589,8 @@
25892589
kvar = (this.range && name) || index || ivar;
25902590
kvarAssign = kvar !== ivar ? "" + kvar + " = " : "";
25912591
if (this.step && !this.range) {
2592-
stepvar = scope.freeVariable("step");
2592+
this.complexStep = !(typeof (_base = this.step).isSimpleNumber === "function" ? _base.isSimpleNumber() : void 0);
2593+
stepvar = this.complexStep ? scope.freeVariable("step") : this.step.compile(o, LEVEL_OP);
25932594
}
25942595
if (this.pattern) {
25952596
name = ivar;
@@ -2616,7 +2617,7 @@
26162617
if (!this.object) {
26172618
lvar = scope.freeVariable('len');
26182619
forVarPart = "" + kvarAssign + ivar + " = 0, " + lvar + " = " + svar + ".length";
2619-
if (this.step) {
2620+
if (this.complexStep) {
26202621
forVarPart += ", " + stepvar + " = " + (this.step.compile(o, LEVEL_OP));
26212622
}
26222623
stepPart = "" + kvarAssign + (this.step ? "" + ivar + " += " + stepvar : (kvar !== ivar ? "++" + ivar : "" + ivar + "++"));

src/nodes.coffee

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1742,7 +1742,9 @@ exports.For = class For extends While
17421742
kvar = (@range and name) or index or ivar
17431743
kvarAssign = if kvar isnt ivar then "#{kvar} = " else ""
17441744
# the `_by` variable is created twice in `Range`s if we don't prevent it from being declared here
1745-
stepvar = scope.freeVariable "step" if @step and not @range
1745+
if @step and not @range
1746+
@complexStep = not @step.isSimpleNumber?()
1747+
stepvar = if @complexStep then scope.freeVariable "step" else @step.compile(o, LEVEL_OP)
17461748
name = ivar if @pattern
17471749
varPart = ''
17481750
guardPart = ''
@@ -1760,7 +1762,7 @@ exports.For = class For extends While
17601762
unless @object
17611763
lvar = scope.freeVariable 'len'
17621764
forVarPart = "#{kvarAssign}#{ivar} = 0, #{lvar} = #{svar}.length"
1763-
forVarPart += ", #{stepvar} = #{@step.compile o, LEVEL_OP}" if @step
1765+
forVarPart += ", #{stepvar} = #{@step.compile o, LEVEL_OP}" if @complexStep
17641766
stepPart = "#{kvarAssign}#{if @step then "#{ivar} += #{stepvar}" else (if kvar isnt ivar then "++#{ivar}" else "#{ivar}++")}"
17651767
forPart = "#{forVarPart}; #{ivar} < #{lvar}; #{stepPart}"
17661768
if @returns

0 commit comments

Comments
 (0)