Fix for converting negative floating point to integer values.
This commit is contained in:
+21
-3
@@ -17,13 +17,16 @@
|
||||
package org.jetbrains.k2js.translate.intrinsic.functions.factories;
|
||||
|
||||
import closurecompiler.internal.com.google.common.collect.Sets;
|
||||
import com.google.dart.compiler.backend.js.ast.JsBinaryOperation;
|
||||
import com.google.dart.compiler.backend.js.ast.JsBinaryOperator;
|
||||
import com.google.dart.compiler.backend.js.ast.JsExpression;
|
||||
import com.google.dart.compiler.util.AstUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.resolve.name.Name;
|
||||
import org.jetbrains.jet.lang.types.expressions.OperatorConventions;
|
||||
import org.jetbrains.k2js.translate.context.TemporaryVariable;
|
||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
||||
import org.jetbrains.k2js.translate.intrinsic.functions.basic.CallStandardMethodIntrinsic;
|
||||
import org.jetbrains.k2js.translate.intrinsic.functions.basic.FunctionIntrinsic;
|
||||
import org.jetbrains.k2js.translate.intrinsic.functions.patterns.NamePredicate;
|
||||
|
||||
@@ -32,6 +35,7 @@ import java.util.Set;
|
||||
|
||||
import static org.jetbrains.jet.lang.types.expressions.OperatorConventions.*;
|
||||
import static org.jetbrains.k2js.translate.intrinsic.functions.patterns.PatternBuilder.pattern;
|
||||
import static org.jetbrains.k2js.translate.utils.JsAstUtils.subtract;
|
||||
|
||||
/**
|
||||
* @author Pavel Talanov
|
||||
@@ -72,13 +76,27 @@ public final class NumberConversionFIF extends CompositeFIF {
|
||||
public static final String INTEGER_NUMBER_TYPES = "Int|Byte|Short";
|
||||
//NOTE: treat Number as if it is floating point type
|
||||
@NotNull
|
||||
public static final String FLOATING_POINT_NUMBER_TYPES = "Float|Double|Number";
|
||||
private static final String FLOATING_POINT_NUMBER_TYPES = "Float|Double|Number";
|
||||
@NotNull
|
||||
private static final FunctionIntrinsic GET_INTEGER_PART = new FunctionIntrinsic() {
|
||||
@NotNull
|
||||
@Override
|
||||
public JsExpression apply(@Nullable JsExpression receiver,
|
||||
@NotNull List<JsExpression> arguments,
|
||||
@NotNull TranslationContext context) {
|
||||
assert receiver != null;
|
||||
assert arguments.isEmpty();
|
||||
TemporaryVariable toConvert = context.declareTemporary(receiver);
|
||||
JsBinaryOperation fractional = new JsBinaryOperation(JsBinaryOperator.MOD, toConvert.reference(), context.program().getNumberLiteral(1));
|
||||
return AstUtil.newSequence(toConvert.assignmentExpression(), subtract(toConvert.reference(), fractional));
|
||||
}
|
||||
};
|
||||
@NotNull
|
||||
public static final FunctionIntrinsicFactory INSTANCE = new NumberConversionFIF();
|
||||
|
||||
private NumberConversionFIF() {
|
||||
add(pattern(INTEGER_NUMBER_TYPES, SUPPORTED_CONVERSIONS), RETURN_RECEIVER);
|
||||
add(pattern(FLOATING_POINT_NUMBER_TYPES, INTEGER_CONVERSIONS), new CallStandardMethodIntrinsic("Math.floor", true, 0));
|
||||
add(pattern(FLOATING_POINT_NUMBER_TYPES, INTEGER_CONVERSIONS), GET_INTEGER_PART);
|
||||
add(pattern(FLOATING_POINT_NUMBER_TYPES, FLOATING_POINT_CONVERSIONS), RETURN_RECEIVER);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,23 @@ fun box(): Boolean {
|
||||
return false
|
||||
}
|
||||
|
||||
val cn: Double = -3.6
|
||||
if (cn.toDouble() != -3.6) {
|
||||
return false
|
||||
}
|
||||
if (cn.toFloat() != -3.6.toFloat()) {
|
||||
return false
|
||||
}
|
||||
if (cn.toByte() != -3.toByte()) {
|
||||
return false
|
||||
}
|
||||
if (cn.toInt() != -3) {
|
||||
return false
|
||||
}
|
||||
if (cn.toShort() != -3.toShort()) {
|
||||
return false
|
||||
}
|
||||
|
||||
val f: Float = 3.6.toFloat()
|
||||
if (f.toDouble() != 3.6) {
|
||||
return false
|
||||
@@ -34,5 +51,22 @@ fun box(): Boolean {
|
||||
if (f.toShort() != 3.toShort()) {
|
||||
return false
|
||||
}
|
||||
|
||||
val fn: Float = -3.6.toFloat()
|
||||
if (fn.toDouble() != -3.6) {
|
||||
return false
|
||||
}
|
||||
if (fn.toFloat() != -3.6.toFloat()) {
|
||||
return false
|
||||
}
|
||||
if (fn.toByte() != -3.toByte()) {
|
||||
return false
|
||||
}
|
||||
if (fn.toInt() != -3) {
|
||||
return false
|
||||
}
|
||||
if (fn.toShort() != -3.toShort()) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -17,5 +17,12 @@ fun box(): Boolean {
|
||||
if (c.toShort() != 3.toShort()) {
|
||||
return false
|
||||
}
|
||||
val c2: Int = -5
|
||||
if (c2.toShort() != -5.toShort()) {
|
||||
return false
|
||||
}
|
||||
if (c2.toFloat() != -5.toFloat()) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user