boxing for constants

This commit is contained in:
Dmitry Jemerov
2011-04-07 15:56:20 +02:00
parent 16d475249f
commit a053bda91b
4 changed files with 36 additions and 30 deletions
@@ -187,35 +187,6 @@ public class ExpressionCodegen extends JetVisitor {
@Override
public void visitConstantExpression(JetConstantExpression expression) {
myStack.push(StackValue.constant(expression.getValue()));
/*
Object value = element.getValue();
v.aconst(value);
if (value instanceof Integer) {
v.invokestatic("java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;");
}
else if (value instanceof Boolean) {
v.invokestatic("java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;");
}
else if (value instanceof Character) {
v.invokestatic("java/lang/Character", "valueOf", "(C)Ljava/lang/Character;");
}
else if (value instanceof Short) {
v.invokestatic("java/lang/Short", "valueOf", "(S)Ljava/lang/Short;");
}
else if (value instanceof Long) {
v.invokestatic("java/lang/Long", "valueOf", "(J)Ljava/lang/Long;");
}
else if (value instanceof Byte) {
v.invokestatic("java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;");
}
else if (value instanceof Float) {
v.invokestatic("java/lang/Float", "valueOf", "(F)Ljava/lang/Float;");
}
else if (value instanceof Double) {
v.invokestatic("java/lang/Double", "valueOf", "(D)Ljava/lang/Double;");
}
*/
}
@Override
@@ -20,6 +20,9 @@ public class JetTypeMapper {
if (jetType.equals(standardLibrary.getIntType())) {
return Type.INT_TYPE;
}
if (jetType.equals(TypeUtils.makeNullable(standardLibrary.getIntType()))) {
return Type.getObjectType("java/lang/Integer");
}
if (jetType.equals(standardLibrary.getLongType())) {
return Type.LONG_TYPE;
}
@@ -97,6 +97,32 @@ public abstract class StackValue {
@Override
public void put(Type type, InstructionAdapter v) {
v.aconst(value);
if (type.getSort() == Type.OBJECT) {
if (value instanceof Integer) {
v.invokestatic("java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;");
}
else if (value instanceof Boolean) {
v.invokestatic("java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;");
}
else if (value instanceof Character) {
v.invokestatic("java/lang/Character", "valueOf", "(C)Ljava/lang/Character;");
}
else if (value instanceof Short) {
v.invokestatic("java/lang/Short", "valueOf", "(S)Ljava/lang/Short;");
}
else if (value instanceof Long) {
v.invokestatic("java/lang/Long", "valueOf", "(J)Ljava/lang/Long;");
}
else if (value instanceof Byte) {
v.invokestatic("java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;");
}
else if (value instanceof Float) {
v.invokestatic("java/lang/Float", "valueOf", "(F)Ljava/lang/Float;");
}
else if (value instanceof Double) {
v.invokestatic("java/lang/Double", "valueOf", "(D)Ljava/lang/Double;");
}
}
}
@Override
@@ -221,12 +221,18 @@ public class NamespaceGenTest extends LightCodeInsightFixtureTestCase {
assertEquals(239, main.invoke(null, new Object[]{null}));
}
public void testBoxingInCall() throws Exception {
public void testIntBoxed() throws Exception {
loadText("fun foo(s: String): Int? = Integer.getInteger(s, 239)");
final Method main = generateFunction();
assertEquals(239, main.invoke(null, "no.such.system.property"));
}
public void testBoxConstant() throws Exception {
loadText("fun foo(): Int? = 239");
final Method main = generateFunction();
assertEquals(239, main.invoke(null));
}
public void testLong() throws Exception {
loadText("fun foo(a: Long, b: Long): Long = a + b");
System.out.println(generateToText());