Skip to content

Commit 7de982b

Browse files
Pavel V. TalanovPavel V. Talanov
Pavel V. Talanov
authored and
Pavel V. Talanov
committed
Fix for converting negative floating point to integer values.
1 parent 3ce9022 commit 7de982b

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

js/js.translator/src/org/jetbrains/k2js/translate/intrinsic/functions/factories/NumberConversionFIF.java

+21-3
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717
package org.jetbrains.k2js.translate.intrinsic.functions.factories;
1818

1919
import closurecompiler.internal.com.google.common.collect.Sets;
20+
import com.google.dart.compiler.backend.js.ast.JsBinaryOperation;
21+
import com.google.dart.compiler.backend.js.ast.JsBinaryOperator;
2022
import com.google.dart.compiler.backend.js.ast.JsExpression;
23+
import com.google.dart.compiler.util.AstUtil;
2124
import org.jetbrains.annotations.NotNull;
2225
import org.jetbrains.annotations.Nullable;
2326
import org.jetbrains.jet.lang.resolve.name.Name;
2427
import org.jetbrains.jet.lang.types.expressions.OperatorConventions;
28+
import org.jetbrains.k2js.translate.context.TemporaryVariable;
2529
import org.jetbrains.k2js.translate.context.TranslationContext;
26-
import org.jetbrains.k2js.translate.intrinsic.functions.basic.CallStandardMethodIntrinsic;
2730
import org.jetbrains.k2js.translate.intrinsic.functions.basic.FunctionIntrinsic;
2831
import org.jetbrains.k2js.translate.intrinsic.functions.patterns.NamePredicate;
2932

@@ -32,6 +35,7 @@
3235

3336
import static org.jetbrains.jet.lang.types.expressions.OperatorConventions.*;
3437
import static org.jetbrains.k2js.translate.intrinsic.functions.patterns.PatternBuilder.pattern;
38+
import static org.jetbrains.k2js.translate.utils.JsAstUtils.subtract;
3539

3640
/**
3741
* @author Pavel Talanov
@@ -72,13 +76,27 @@ public JsExpression apply(@Nullable JsExpression receiver,
7276
public static final String INTEGER_NUMBER_TYPES = "Int|Byte|Short";
7377
//NOTE: treat Number as if it is floating point type
7478
@NotNull
75-
public static final String FLOATING_POINT_NUMBER_TYPES = "Float|Double|Number";
79+
private static final String FLOATING_POINT_NUMBER_TYPES = "Float|Double|Number";
80+
@NotNull
81+
private static final FunctionIntrinsic GET_INTEGER_PART = new FunctionIntrinsic() {
82+
@NotNull
83+
@Override
84+
public JsExpression apply(@Nullable JsExpression receiver,
85+
@NotNull List<JsExpression> arguments,
86+
@NotNull TranslationContext context) {
87+
assert receiver != null;
88+
assert arguments.isEmpty();
89+
TemporaryVariable toConvert = context.declareTemporary(receiver);
90+
JsBinaryOperation fractional = new JsBinaryOperation(JsBinaryOperator.MOD, toConvert.reference(), context.program().getNumberLiteral(1));
91+
return AstUtil.newSequence(toConvert.assignmentExpression(), subtract(toConvert.reference(), fractional));
92+
}
93+
};
7694
@NotNull
7795
public static final FunctionIntrinsicFactory INSTANCE = new NumberConversionFIF();
7896

7997
private NumberConversionFIF() {
8098
add(pattern(INTEGER_NUMBER_TYPES, SUPPORTED_CONVERSIONS), RETURN_RECEIVER);
81-
add(pattern(FLOATING_POINT_NUMBER_TYPES, INTEGER_CONVERSIONS), new CallStandardMethodIntrinsic("Math.floor", true, 0));
99+
add(pattern(FLOATING_POINT_NUMBER_TYPES, INTEGER_CONVERSIONS), GET_INTEGER_PART);
82100
add(pattern(FLOATING_POINT_NUMBER_TYPES, FLOATING_POINT_CONVERSIONS), RETURN_RECEIVER);
83101
}
84102
}

js/js.translator/testFiles/number/cases/doubleConversions.kt

+34
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@ fun box(): Boolean {
1818
return false
1919
}
2020

21+
val cn: Double = -3.6
22+
if (cn.toDouble() != -3.6) {
23+
return false
24+
}
25+
if (cn.toFloat() != -3.6.toFloat()) {
26+
return false
27+
}
28+
if (cn.toByte() != -3.toByte()) {
29+
return false
30+
}
31+
if (cn.toInt() != -3) {
32+
return false
33+
}
34+
if (cn.toShort() != -3.toShort()) {
35+
return false
36+
}
37+
2138
val f: Float = 3.6.toFloat()
2239
if (f.toDouble() != 3.6) {
2340
return false
@@ -34,5 +51,22 @@ fun box(): Boolean {
3451
if (f.toShort() != 3.toShort()) {
3552
return false
3653
}
54+
55+
val fn: Float = -3.6.toFloat()
56+
if (fn.toDouble() != -3.6) {
57+
return false
58+
}
59+
if (fn.toFloat() != -3.6.toFloat()) {
60+
return false
61+
}
62+
if (fn.toByte() != -3.toByte()) {
63+
return false
64+
}
65+
if (fn.toInt() != -3) {
66+
return false
67+
}
68+
if (fn.toShort() != -3.toShort()) {
69+
return false
70+
}
3771
return true
3872
}

js/js.translator/testFiles/number/cases/intConversions.kt

+7
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,12 @@ fun box(): Boolean {
1717
if (c.toShort() != 3.toShort()) {
1818
return false
1919
}
20+
val c2: Int = -5
21+
if (c2.toShort() != -5.toShort()) {
22+
return false
23+
}
24+
if (c2.toFloat() != -5.toFloat()) {
25+
return false
26+
}
2027
return true
2128
}

0 commit comments

Comments
 (0)