Fix #4889: for...range loop condition (#4891)

* fix #4889

* test

* move test from 'control_flow' to 'ranges'

* More range tests
This commit is contained in:
zdenko
2018-02-20 09:46:20 +01:00
committed by Geoffrey Booth
parent eb7009268d
commit 72ab6feb2f
3 changed files with 75 additions and 41 deletions

View File

@@ -2031,7 +2031,7 @@
// When compiled normally, the range returns the contents of the *for loop*
// needed to iterate over the values in the range. Used by comprehensions.
compileNode(o) {
var cond, condPart, from, gt, idx, idxName, known, lowerBound, lt, namedIndex, stepCond, stepPart, to, upperBound, varPart;
var cond, condPart, from, gt, idx, idxName, known, lowerBound, lt, namedIndex, ref1, ref2, stepCond, stepNotZero, stepPart, to, upperBound, varPart;
if (!this.fromVar) {
this.compileVariables(o);
}
@@ -2054,12 +2054,11 @@
// Generate the condition.
[from, to] = [this.fromNum, this.toNum];
// Always check if the `step` isn't zero to avoid the infinite loop.
stepCond = this.stepNum ? `${this.stepNum} !== 0` : `${this.stepVar} !== 0`;
condPart = known ? this.step == null ? from <= to ? `${lt} ${to}` : `${gt} ${ // from < to
to}` : (lowerBound = `${from} <= ${idx} && ${lt} ${// from > to
to}`, upperBound = `${from} >= ${idx} && ${gt} ${to}`, from <= to ? `${stepCond} && ${lowerBound}` : `${stepCond} && ${// from < to
upperBound}`) : (lowerBound = `${this.fromVar} <= ${idx} && ${lt} ${// from > to
this.toVar}`, upperBound = `${this.fromVar} >= ${idx} && ${gt} ${this.toVar}`, `${stepCond} && (${this.fromVar} <= ${this.toVar} ? ${lowerBound} : ${upperBound})`);
stepNotZero = `${(ref1 = this.stepNum) != null ? ref1 : this.stepVar} !== 0`;
stepCond = `${(ref2 = this.stepNum) != null ? ref2 : this.stepVar} > 0`;
lowerBound = `${lt} ${(known ? to : this.toVar)}`;
upperBound = `${gt} ${(known ? to : this.toVar)}`;
condPart = this.step != null ? `${stepNotZero} && (${stepCond} ? ${lowerBound} : ${upperBound})` : known ? `${(from <= to ? lt : gt)} ${to}` : `(${this.fromVar} <= ${this.toVar} ? ${lowerBound} : ${upperBound})`;
cond = this.stepVar ? `${this.stepVar} > 0` : `${this.fromVar} <= ${this.toVar}`;
// Generate the step.
stepPart = this.stepVar ? `${idx} += ${this.stepVar}` : known ? namedIndex ? from <= to ? `++${idx}` : `--${idx}` : from <= to ? `${idx}++` : `${idx}--` : namedIndex ? `${cond} ? ++${idx} : --${idx}` : `${cond} ? ${idx}++ : ${idx}--`;