varargs
This commit is contained in:
@@ -79,7 +79,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
|
||||
this.intrinsics = state.getIntrinsics();
|
||||
}
|
||||
|
||||
private CallableMethod asCallableMethod(FunctionDescriptor fd) {
|
||||
private static CallableMethod asCallableMethod(FunctionDescriptor fd) {
|
||||
Method descriptor = ClosureCodegen.erasedInvokeSignature(fd);
|
||||
String owner = ClosureCodegen.getInternalClassName(fd);
|
||||
final CallableMethod result = new CallableMethod(owner, descriptor, INVOKEVIRTUAL, Arrays.asList(descriptor.getArgumentTypes()));
|
||||
@@ -1187,7 +1187,24 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
|
||||
mask |= (1 << index);
|
||||
}
|
||||
else if(resolvedValueArgument instanceof VarargValueArgument) {
|
||||
throw new UnsupportedOperationException("Varargs are not supported yet");
|
||||
VarargValueArgument valueArgument = (VarargValueArgument) resolvedValueArgument;
|
||||
JetType outType = valueParameterDescriptor.getOutType();
|
||||
|
||||
Type type = typeMapper.mapType(outType);
|
||||
assert type.getSort() == Type.ARRAY;
|
||||
Type elementType = type.getElementType();
|
||||
int size = valueArgument.getArgumentExpressions().size();
|
||||
|
||||
v.iconst(valueArgument.getArgumentExpressions().size());
|
||||
v.newarray(elementType);
|
||||
for(int i = 0; i != size; ++i) {
|
||||
v.dup();
|
||||
v.iconst(i);
|
||||
gen(valueArgument.getArgumentExpressions().get(i), elementType);
|
||||
StackValue.arrayElement(elementType, false).store(v);
|
||||
}
|
||||
|
||||
// throw new UnsupportedOperationException("Varargs are not supported yet");
|
||||
}
|
||||
else {
|
||||
throw new UnsupportedOperationException();
|
||||
|
||||
@@ -361,10 +361,6 @@ public class JetTypeMapper {
|
||||
if (jetType.equals(standardLibrary.getNullableBooleanType())) {
|
||||
return JL_BOOLEAN_TYPE;
|
||||
}
|
||||
if (jetType.equals(standardLibrary.getStringType()) || jetType.equals(standardLibrary.getNullableStringType())) {
|
||||
return Type.getType(String.class);
|
||||
}
|
||||
|
||||
if(jetType.equals(standardLibrary.getByteArrayType())){
|
||||
return ARRAY_BYTE_TYPE;
|
||||
}
|
||||
@@ -390,6 +386,10 @@ public class JetTypeMapper {
|
||||
return ARRAY_BOOL_TYPE;
|
||||
}
|
||||
|
||||
if (jetType.equals(standardLibrary.getStringType()) || jetType.equals(standardLibrary.getNullableStringType())) {
|
||||
return Type.getType(String.class);
|
||||
}
|
||||
|
||||
DeclarationDescriptor descriptor = jetType.getConstructor().getDeclarationDescriptor();
|
||||
if (standardLibrary.getArray().equals(descriptor)) {
|
||||
if (jetType.getArguments().size() != 1) {
|
||||
@@ -491,9 +491,6 @@ public class JetTypeMapper {
|
||||
}
|
||||
for (ValueParameterDescriptor parameter : parameters) {
|
||||
Type type = mapType(parameter.getOutType());
|
||||
if(parameter.getVarargElementType() != null) {
|
||||
type = Type.getType("[" + type.getDescriptor());
|
||||
}
|
||||
valueParameterTypes.add(type);
|
||||
parameterTypes.add(type);
|
||||
}
|
||||
@@ -517,7 +514,7 @@ public class JetTypeMapper {
|
||||
return mapToCallableMethod(functionDescriptor, kind);
|
||||
}
|
||||
|
||||
CallableMethod mapToCallableMethod(PsiMethod method) {
|
||||
static CallableMethod mapToCallableMethod(PsiMethod method) {
|
||||
final PsiClass containingClass = method.getContainingClass();
|
||||
String owner = jvmName(containingClass);
|
||||
Method signature = getMethodDescriptor(method);
|
||||
@@ -591,13 +588,7 @@ public class JetTypeMapper {
|
||||
parameterTypes.add(mapType(receiver.getType()));
|
||||
}
|
||||
for (ValueParameterDescriptor parameter : parameters) {
|
||||
if(parameter.getVarargElementType() != null) {
|
||||
Type type = mapType(parameter.getOutType());
|
||||
type = Type.getType("[" + type.getDescriptor());
|
||||
parameterTypes.add(type);
|
||||
}
|
||||
else
|
||||
parameterTypes.add(mapType(parameter.getOutType()));
|
||||
parameterTypes.add(mapType(parameter.getOutType()));
|
||||
}
|
||||
Type returnType = mapReturnType(f.getReturnType());
|
||||
return new Method(name, returnType, parameterTypes.toArray(new Type[parameterTypes.size()]));
|
||||
|
||||
@@ -681,6 +681,9 @@ public abstract class StackValue {
|
||||
case Type.INT:
|
||||
return JetTypeMapper.TYPE_SHARED_INT;
|
||||
|
||||
case Type.LONG:
|
||||
return JetTypeMapper.TYPE_SHARED_LONG;
|
||||
|
||||
case Type.BOOLEAN:
|
||||
return JetTypeMapper.TYPE_SHARED_BOOLEAN;
|
||||
|
||||
|
||||
@@ -59,6 +59,11 @@ public class JetParameter extends JetNamedDeclaration {
|
||||
return findChildByType(JetTokens.VAR_KEYWORD) != null || isRef();
|
||||
}
|
||||
|
||||
public boolean isVarArg() {
|
||||
JetModifierList modifierList = getModifierList();
|
||||
return modifierList != null && modifierList.getModifierNode(JetTokens.VARARG_KEYWORD) != null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public ASTNode getValOrVarNode() {
|
||||
ASTNode val = getNode().findChildByType(JetTokens.VAL_KEYWORD);
|
||||
|
||||
@@ -8,22 +8,55 @@ import java.lang.reflect.Method;
|
||||
*/
|
||||
public class VarArgTest extends CodegenTestCase {
|
||||
public void testStringArray () throws InvocationTargetException, IllegalAccessException {
|
||||
/*
|
||||
loadText("fun test(vararg ts: String) = ts");
|
||||
System.out.println(generateToText());
|
||||
final Method main = generateFunction();
|
||||
Object[] args = {"mama", "papa"};
|
||||
assertTrue(args == main.invoke(null, args));
|
||||
*/
|
||||
String[] args = {"mama", "papa"};
|
||||
assertTrue(args == main.invoke(null, new Object[]{ args } ));
|
||||
}
|
||||
|
||||
public void testIntArray () throws InvocationTargetException, IllegalAccessException {
|
||||
/*
|
||||
loadText("fun test(vararg ts: Int) = ts");
|
||||
System.out.println(generateToText());
|
||||
final Method main = generateFunction();
|
||||
int[] args = {3, 4};
|
||||
assertTrue(args == main.invoke(null, args));
|
||||
*/
|
||||
assertTrue(args == main.invoke(null, new Object[]{ args }));
|
||||
}
|
||||
|
||||
public void testIntArrayKotlinNoArgs () throws InvocationTargetException, IllegalAccessException {
|
||||
loadText("fun test() = testf(); fun testf(vararg ts: Int) = ts");
|
||||
System.out.println(generateToText());
|
||||
final Method main = generateFunction();
|
||||
Object res = main.invoke(null, new Object[]{});
|
||||
assertTrue(((int[])res).length == 0);
|
||||
}
|
||||
|
||||
public void testIntArrayKotlin () throws InvocationTargetException, IllegalAccessException {
|
||||
loadText("fun test() = testf(239, 7); fun testf(vararg ts: Int) = ts");
|
||||
System.out.println(generateToText());
|
||||
final Method main = generateFunction();
|
||||
Object res = main.invoke(null, new Object[]{});
|
||||
assertTrue(((int[])res).length == 2);
|
||||
assertTrue(((int[])res)[0] == 239);
|
||||
assertTrue(((int[])res)[1] == 7);
|
||||
}
|
||||
|
||||
public void testNullableIntArrayKotlin () throws InvocationTargetException, IllegalAccessException {
|
||||
loadText("fun test() = testf(239.byt, 7.byt); fun testf(vararg ts: Byte?) = ts");
|
||||
System.out.println(generateToText());
|
||||
final Method main = generateFunction();
|
||||
Object res = main.invoke(null, new Object[]{});
|
||||
assertTrue(((Byte[])res).length == 2);
|
||||
assertTrue(((Byte[])res)[0] == (byte)239);
|
||||
assertTrue(((Byte[])res)[1] == 7);
|
||||
}
|
||||
|
||||
public void testIntArrayKotlinObj () throws InvocationTargetException, IllegalAccessException {
|
||||
loadText("fun test() = testf(\"239\"); fun testf(vararg ts: String) = ts");
|
||||
System.out.println(generateToText());
|
||||
final Method main = generateFunction();
|
||||
Object res = main.invoke(null, new Object[]{});
|
||||
assertTrue(((String[])res).length == 1);
|
||||
assertTrue(((String[])res)[0].equals("239"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user