Double conversions.
This commit is contained in:
@@ -29,4 +29,8 @@ public final class NumberTest extends SingleFileTranslationTest {
|
|||||||
public void testIntConversions() throws Exception {
|
public void testIntConversions() throws Exception {
|
||||||
fooBoxTest();
|
fooBoxTest();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testDoubleConversions() throws Exception {
|
||||||
|
fooBoxTest();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-3
@@ -21,7 +21,9 @@ import com.google.dart.compiler.backend.js.ast.JsExpression;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.lang.resolve.name.Name;
|
import org.jetbrains.jet.lang.resolve.name.Name;
|
||||||
|
import org.jetbrains.jet.lang.types.expressions.OperatorConventions;
|
||||||
import org.jetbrains.k2js.translate.context.TranslationContext;
|
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.basic.FunctionIntrinsic;
|
||||||
import org.jetbrains.k2js.translate.intrinsic.functions.patterns.NameChecker;
|
import org.jetbrains.k2js.translate.intrinsic.functions.patterns.NameChecker;
|
||||||
|
|
||||||
@@ -36,16 +38,23 @@ import static org.jetbrains.k2js.translate.intrinsic.functions.patterns.PatternB
|
|||||||
*/
|
*/
|
||||||
public final class NumberConversionFIF {
|
public final class NumberConversionFIF {
|
||||||
@NotNull
|
@NotNull
|
||||||
private static final NameChecker CONVERSIONS;
|
private static final NameChecker SUPPORTED_CONVERSIONS;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
HashSet<Name> supportedConversions = Sets.newHashSet(NUMBER_CONVERSIONS);
|
HashSet<Name> supportedConversions = Sets.newHashSet(NUMBER_CONVERSIONS);
|
||||||
//TODO: support longs and chars
|
//TODO: support longs and chars
|
||||||
supportedConversions.remove(CHAR);
|
supportedConversions.remove(CHAR);
|
||||||
supportedConversions.remove(LONG);
|
supportedConversions.remove(LONG);
|
||||||
CONVERSIONS = new NameChecker(supportedConversions);
|
SUPPORTED_CONVERSIONS = new NameChecker(supportedConversions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private static final NameChecker FLOATING_POINT_CONVERSIONS = new NameChecker(OperatorConventions.FLOAT, OperatorConventions.DOUBLE);
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private static final NameChecker INTEGER_CONVERSIONS = new NameChecker(OperatorConventions.INT, OperatorConventions.SHORT,
|
||||||
|
OperatorConventions.BYTE);
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private static final FunctionIntrinsic RETURN_RECEIVER = new FunctionIntrinsic() {
|
private static final FunctionIntrinsic RETURN_RECEIVER = new FunctionIntrinsic() {
|
||||||
@NotNull
|
@NotNull
|
||||||
@@ -61,7 +70,9 @@ public final class NumberConversionFIF {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public static final FunctionIntrinsicFactory INSTANCE = FIFBuilder.start()
|
public static final FunctionIntrinsicFactory INSTANCE = FIFBuilder.start()
|
||||||
.add(pattern("Int", CONVERSIONS), RETURN_RECEIVER)
|
.add(pattern("Int", SUPPORTED_CONVERSIONS), RETURN_RECEIVER)
|
||||||
|
.add(pattern("Double", INTEGER_CONVERSIONS), new CallStandardMethodIntrinsic("Math.floor", true, 0))
|
||||||
|
.add(pattern("Double", FLOATING_POINT_CONVERSIONS), RETURN_RECEIVER)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
private NumberConversionFIF() {
|
private NumberConversionFIF() {
|
||||||
|
|||||||
+4
@@ -49,6 +49,10 @@ public final class NameChecker {
|
|||||||
this.validNames.addAll(validNames);
|
this.validNames.addAll(validNames);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public NameChecker(@NotNull Name... validNames) {
|
||||||
|
this.validNames.addAll(Lists.newArrayList(validNames));
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isValid(@NotNull Name name) {
|
public boolean isValid(@NotNull Name name) {
|
||||||
for (Name validName : validNames) {
|
for (Name validName : validNames) {
|
||||||
if (name.equals(validName)) {
|
if (name.equals(validName)) {
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package foo
|
||||||
|
|
||||||
|
fun box(): Boolean {
|
||||||
|
val c: Double = 3.6
|
||||||
|
if (c.toDouble() != 3.6) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if (c.toFloat() != 3.6.toFloat()) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if (c.toByte() != 3.toByte()) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if (c.toInt() != 3) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if (c.toShort() != 3.toShort()) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user