Spread should always copy arrays.

Introduce a special (package private) utility class ArraysUtilJVM
to fix Array<T>.asList() issues.
This commit is contained in:
Dmitry Petrov
2015-10-05 19:04:46 +03:00
parent 97946feb9a
commit 57869d85e8
7 changed files with 77 additions and 5 deletions
@@ -2685,14 +2685,24 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
}
if (hasSpread) {
boolean arrayOfReferences = KotlinBuiltIns.isArray(outType);
if (size == 1) {
gen(arguments.get(0).getArgumentExpression(), type);
// Arrays.copyOf(array, newLength)
ValueArgument argument = arguments.get(0);
Type arrayType = arrayOfReferences ? Type.getType("[Ljava/lang/Object;")
: Type.getType("[" + elementType.getDescriptor());
gen(argument.getArgumentExpression(), type);
v.dup();
v.arraylength();
v.invokestatic("java/util/Arrays", "copyOf", Type.getMethodDescriptor(arrayType, arrayType, Type.INT_TYPE), false);
if (arrayOfReferences) {
v.checkcast(type);
}
}
else {
String owner;
String addDescriptor;
String toArrayDescriptor;
boolean arrayOfReferences = KotlinBuiltIns.isArray(outType);
if (arrayOfReferences) {
owner = "kotlin/jvm/internal/SpreadBuilder";
addDescriptor = "(Ljava/lang/Object;)V";