Skip to content

Commit bb4645c

Browse files
committed
fix rounding constant folding
1 parent 603e0c1 commit bb4645c

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

src/compiler/jsgen.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -622,12 +622,16 @@ class JSGenerator {
622622
this.usedMathFunctions.add('PI');
623623
return new TypedInput(`((atan(${this.descendInput(node.value).asNumber()}) * 180) / PI)`, TYPES.NUMBER);
624624
case BLOCKS.OP.CEILING: {
625-
const value = this.descendInput(node.value);
626-
if (value.isAlwaysInt()) {
627-
return new TypedInput(`${value.asInt()}`, TYPES.NUMBER_INT);
625+
const inp = this.descendInput(node.value);
626+
if (inp.isAlwaysConstant()) {
627+
const val = +inp.constantValue;
628+
return new ConstantInput(toNotNaN(Math.ceil(val)), false);
629+
}
630+
if (inp.isAlwaysInt()) {
631+
return new TypedInput(`${inp.asInt()}`, TYPES.NUMBER_INT);
628632
}
629633
this.usedMathFunctions.add('ceil');
630-
return new TypedInput(`ceil(${value.asNumber()})`, TYPES.NUMBER_INT);
634+
return new TypedInput(`ceil(${inp.asNumber()})`, TYPES.NUMBER_INT);
631635
}
632636
case BLOCKS.OP.CONTAINS: {
633637
const string = this.descendInput(node.string);
@@ -683,12 +687,16 @@ class JSGenerator {
683687
this.usedMathFunctions.add('exp');
684688
return new TypedInput(`exp(${this.descendInput(node.value).asNumber()})`, TYPES.NUMBER);
685689
case BLOCKS.OP.FLOOR: {
686-
const value = this.descendInput(node.value);
687-
if (value.isAlwaysInt()) {
688-
return new TypedInput(`${value.asNumber()}`, TYPES.NUMBER_INT);
690+
const inp = this.descendInput(node.value);
691+
if (inp.isAlwaysConstant()) {
692+
const value = +inp.constantValue;
693+
return new ConstantInput(toNotNaN(Math.floor(value)), false);
694+
}
695+
if (inp.isAlwaysInt()) {
696+
return new TypedInput(`${inp.asNumber()}`, TYPES.NUMBER_INT);
689697
}
690698
this.usedMathFunctions.add('floor');
691-
return new TypedInput(`floor(${this.descendInput(node.value).asNumber()})`, TYPES.NUMBER_INT);
699+
return new TypedInput(`floor(${inp.asNumber()})`, TYPES.NUMBER_INT);
692700
}
693701
case BLOCKS.OP.GREATER: {
694702
const left = this.descendInput(node.left);
@@ -832,8 +840,8 @@ class JSGenerator {
832840
case BLOCKS.OP.ROUND: {
833841
const inp = this.descendInput(node.value);
834842
if (inp.isAlwaysConstant()) {
835-
const value = Math.round(+inp.constantValue);
836-
return new ConstantInput(value, false);
843+
const value = +inp.constantValue;
844+
return new ConstantInput(toNotNaN(Math.round(value)), false);
837845
}
838846
if (inp.isAlwaysInt()) {
839847
return new TypedInput(`${inp.asNumber()}`, TYPES.NUMBER_INT);

0 commit comments

Comments
 (0)