提交 3250b8cf 编写于 作者: D Dmitry Jemerov

boxing for variables

上级 a053bda9
......@@ -275,7 +275,8 @@ public class ExpressionCodegen extends JetVisitor {
else {
int index = myMap.getIndex(descriptor);
if (index >= 0) {
myStack.push(StackValue.local(index, ((PropertyDescriptor) descriptor).getOutType()));
final JetType outType = ((PropertyDescriptor) descriptor).getOutType();
myStack.push(StackValue.local(index, typeMapper.mapType(outType)));
}
else {
throw new UnsupportedOperationException("don't know how to generate reference " + descriptor);
......
......@@ -26,24 +26,45 @@ public class JetTypeMapper {
if (jetType.equals(standardLibrary.getLongType())) {
return Type.LONG_TYPE;
}
if (jetType.equals(TypeUtils.makeNullable(standardLibrary.getLongType()))) {
return Type.getObjectType("java/lang/Long");
}
if (jetType.equals(standardLibrary.getShortType())) {
return Type.SHORT_TYPE;
}
if (jetType.equals(TypeUtils.makeNullable(standardLibrary.getShortType()))) {
return Type.getObjectType("java/lang/Short");
}
if (jetType.equals(standardLibrary.getByteType())) {
return Type.BYTE_TYPE;
}
if (jetType.equals(TypeUtils.makeNullable(standardLibrary.getByteType()))) {
return Type.getObjectType("java/lang/Byte");
}
if (jetType.equals(standardLibrary.getCharType())) {
return Type.CHAR_TYPE;
}
if (jetType.equals(TypeUtils.makeNullable(standardLibrary.getCharType()))) {
return Type.getObjectType("java/lang/Char");
}
if (jetType.equals(standardLibrary.getFloatType())) {
return Type.FLOAT_TYPE;
}
if (jetType.equals(TypeUtils.makeNullable(standardLibrary.getFloatType()))) {
return Type.getObjectType("java/lang/Float");
}
if (jetType.equals(standardLibrary.getDoubleType())) {
return Type.DOUBLE_TYPE;
}
if (jetType.equals(TypeUtils.makeNullable(standardLibrary.getDoubleType()))) {
return Type.getObjectType("java/lang/Double");
}
if (jetType.equals(standardLibrary.getBooleanType())) {
return Type.BOOLEAN_TYPE;
}
if (jetType.equals(TypeUtils.makeNullable(standardLibrary.getBooleanType()))) {
return Type.getObjectType("java/lang/Boolean");
}
if (jetType.equals(standardLibrary.getStringType())) {
return Type.getType(String.class);
}
......
package org.jetbrains.jet.codegen;
import com.intellij.psi.tree.IElementType;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lexer.JetTokens;
import org.objectweb.asm.Label;
import org.objectweb.asm.Opcodes;
......@@ -14,7 +13,7 @@ import org.objectweb.asm.commons.InstructionAdapter;
public abstract class StackValue {
public abstract void put(Type type, InstructionAdapter v);
public static StackValue local(int index, JetType type) {
public static StackValue local(int index, Type type) {
return new Local(index, type);
}
......@@ -35,17 +34,43 @@ public abstract class StackValue {
public static class Local extends StackValue {
private final int index;
private final JetType type;
private final Type type;
public Local(int index, JetType type) {
public Local(int index, Type type) {
this.index = index;
this.type = type;
}
@Override
public void put(Type type, InstructionAdapter v) {
v.load(index, type);
// TODO box/unbox
v.load(index, this.type);
if (type.getSort() == Type.OBJECT) {
if (this.type == Type.INT_TYPE) {
v.invokestatic("java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;");
}
else if (this.type == Type.BOOLEAN_TYPE) {
v.invokestatic("java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;");
}
else if (this.type == Type.CHAR_TYPE) {
v.invokestatic("java/lang/Character", "valueOf", "(C)Ljava/lang/Character;");
}
else if (this.type == Type.SHORT_TYPE) {
v.invokestatic("java/lang/Short", "valueOf", "(S)Ljava/lang/Short;");
}
else if (this.type == Type.LONG_TYPE) {
v.invokestatic("java/lang/Long", "valueOf", "(J)Ljava/lang/Long;");
}
else if (this.type == Type.BYTE_TYPE) {
v.invokestatic("java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;");
}
else if (this.type == Type.FLOAT_TYPE) {
v.invokestatic("java/lang/Float", "valueOf", "(F)Ljava/lang/Float;");
}
else if (this.type == Type.DOUBLE_TYPE) {
v.invokestatic("java/lang/Double", "valueOf", "(D)Ljava/lang/Double;");
}
}
// TODO unbox
}
@Override
......
......@@ -233,6 +233,13 @@ public class NamespaceGenTest extends LightCodeInsightFixtureTestCase {
assertEquals(239, main.invoke(null));
}
public void testBoxVariable() throws Exception {
loadText("fun foo(): Int? { var x = 239; return x; }");
System.out.println(generateToText());
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());
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册