diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java index d947f685776..91c126fd77d 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java @@ -200,7 +200,7 @@ public class ExpressionCodegen extends JetVisitor { final JetExpression loopRange = expression.getLoopRange(); final JetType expressionType = bindingContext.get(BindingContext.EXPRESSION_TYPE, loopRange); Type loopRangeType = typeMapper.mapType(expressionType); - if (state.getStandardLibrary().getArray().equals(expressionType.getConstructor().getDeclarationDescriptor())) { + if (loopRangeType.getSort() == Type.ARRAY) { new ForInArrayLoopGenerator(expression, loopRangeType).invoke(); return StackValue.none(); } @@ -1688,12 +1688,21 @@ public class ExpressionCodegen extends JetVisitor { private void generateNewArray(JetCallExpression expression, JetType arrayType) { List args = expression.getValueArguments(); - if (args.size() != 1) { - throw new CompilationException("array constructor requires one value argument"); + + boolean isArray = state.getStandardLibrary().getArray().equals(arrayType.getConstructor().getDeclarationDescriptor()); + if(isArray) { + if (args.size() != 2 && !arrayType.getArguments().get(0).getType().isNullable()) { + throw new CompilationException("array constructor of non-nullable type requires two arguments"); + } + } + else { + if (args.size() != 1) { + throw new CompilationException("primitive array constructor requires one argument"); + } } gen(args.get(0).getArgumentExpression(), Type.INT_TYPE); - if(state.getStandardLibrary().getArray().equals(arrayType.getConstructor().getDeclarationDescriptor())) { + if(isArray) { JetType elementType = typeMapper.getGenericsElementType(arrayType); if(elementType != null) { generateTypeInfo(elementType); @@ -1707,6 +1716,42 @@ public class ExpressionCodegen extends JetVisitor { Type type = typeMapper.mapType(arrayType, OwnerKind.IMPLEMENTATION); v.newarray(type.getElementType()); } + + if(args.size() == 2) { + int sizeIndex = myMap.enterTemp(2); + int indexIndex = sizeIndex+1; + + v.dup(); + v.arraylength(); + v.store(sizeIndex, Type.INT_TYPE); + + v.iconst(0); + v.store(indexIndex, Type.INT_TYPE); + + gen(args.get(1).getArgumentExpression(), JetTypeMapper.TYPE_FUNCTION1); + + Label begin = new Label(); + Label end = new Label(); + v.visitLabel(begin); + v.load(indexIndex, Type.INT_TYPE); + v.load(sizeIndex, Type.INT_TYPE); + v.ificmpge(end); + + v.dup2(); + v.load(indexIndex, Type.INT_TYPE); + v.invokestatic("java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;"); + v.invokevirtual("jet/Function1", "invoke", "(Ljava/lang/Object;)Ljava/lang/Object;"); + v.load(indexIndex, Type.INT_TYPE); + v.iinc(indexIndex, 1); + v.swap(); + v.astore(JetTypeMapper.TYPE_OBJECT); + + v.goTo(begin); + v.visitLabel(end); + v.pop(); + + myMap.leaveTemp(2); + } } @Override diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/JetTypeMapper.java b/compiler/backend/src/org/jetbrains/jet/codegen/JetTypeMapper.java index 59ed3af11e1..61a1584b381 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/JetTypeMapper.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/JetTypeMapper.java @@ -2,6 +2,7 @@ package org.jetbrains.jet.codegen; import com.intellij.psi.*; import com.intellij.psi.util.PsiTreeUtil; +import jet.Function1; import jet.JetObject; import jet.typeinfo.TypeInfo; import jet.typeinfo.TypeInfoProjection; @@ -56,6 +57,7 @@ public class JetTypeMapper { private final Map anonymousSubclassesCount = new HashMap(); private final HashMap knowTypes = new HashMap(); + public static final Type TYPE_FUNCTION1 = Type.getObjectType("jet/Function1"); public JetTypeMapper(JetStandardLibrary standardLibrary, BindingContext bindingContext) { this.standardLibrary = standardLibrary; diff --git a/compiler/frontend/src/jet/Library.jet b/compiler/frontend/src/jet/Library.jet index 672fc1ffe5a..f11575b99d3 100644 --- a/compiler/frontend/src/jet/Library.jet +++ b/compiler/frontend/src/jet/Library.jet @@ -49,7 +49,7 @@ trait Iterable { fun iterator() : Iterator } -class Array(val size : Int) { +class Array(val size : Int, init : fun(Int) : T = null ) { fun get(index : Int) : T fun set(index : Int, value : T) : Unit diff --git a/docs/CSClub_Kotlin.key b/docs/CSClub_Kotlin.key new file mode 100644 index 00000000000..23031fec620 Binary files /dev/null and b/docs/CSClub_Kotlin.key differ diff --git a/idea/testData/codegen/controlStructures/forIntArray.jet b/idea/testData/codegen/controlStructures/forIntArray.jet index 56b16c9bce3..abfce4113f7 100644 --- a/idea/testData/codegen/controlStructures/forIntArray.jet +++ b/idea/testData/codegen/controlStructures/forIntArray.jet @@ -1,5 +1,5 @@ fun box() : String { - val a = Array (5) + val a = Array (5, {0}) var i = 0 var sum = 0 for(el in 0..4) { diff --git a/idea/testData/codegen/controlStructures/forPrimitiveIntArray.jet b/idea/testData/codegen/controlStructures/forPrimitiveIntArray.jet new file mode 100644 index 00000000000..0822b4f6aac --- /dev/null +++ b/idea/testData/codegen/controlStructures/forPrimitiveIntArray.jet @@ -0,0 +1,14 @@ +fun box() : String { + val a = IntArray (5) + var i = 0 + var sum = 0 + for(el in 0..4) { + a[i] = i++ + } + for (el in a) { + sum = sum + el + } + if(sum != 10) return "a failed" + + return "OK" +} \ No newline at end of file diff --git a/idea/testData/codegen/controlStructures/forUserType.jet b/idea/testData/codegen/controlStructures/forUserType.jet index 1d90ceee938..0332dc28bf1 100644 --- a/idea/testData/codegen/controlStructures/forUserType.jet +++ b/idea/testData/codegen/controlStructures/forUserType.jet @@ -44,7 +44,7 @@ fun box() : String { } if(sum != 15) return "c4 failed" - val a : Array = Array (5) + val a : Array = Array (5, {0}) for(el in 0..4) { a[i] = i++ } diff --git a/idea/testData/codegen/regressions/kt237.jet b/idea/testData/codegen/regressions/kt237.jet index adc16c93b60..aaa60b5904f 100644 --- a/idea/testData/codegen/regressions/kt237.jet +++ b/idea/testData/codegen/regressions/kt237.jet @@ -5,7 +5,7 @@ fun main(args: Array?) { //do not compile System.out?.println(fff(())) //do not compile System.out?.println(id(y)) //do not compile - System.out?.println(fff(id(y)) == id(foreach(Array(0),{(e : Int) : Unit => }))) //do not compile + System.out?.println(fff(id(y)) == id(foreach(Array(0,{0}),{(e : Int) : Unit => }))) //do not compile } class A() @@ -30,7 +30,7 @@ fun almostFilter(array: Array, action: fun(Int): Int) { } fun box() : String { - val a = Array (3) + val a = Array (3,{-1}) a[0] = 0 a[1] = 1 a[2] = 2 diff --git a/idea/testData/codegen/regressions/kt238.jet b/idea/testData/codegen/regressions/kt238.jet index d890c1aea61..9332cbf870f 100644 --- a/idea/testData/codegen/regressions/kt238.jet +++ b/idea/testData/codegen/regressions/kt238.jet @@ -5,7 +5,7 @@ fun t1 () { } fun t2 () { - val a2 = Array(1) + val a2 = Array(1,{0}) a2[0] = 0 //ok var i = a2[0] //ok } diff --git a/idea/testData/codegen/regressions/kt326.jet b/idea/testData/codegen/regressions/kt326.jet index 1c5d3b19fdf..8afc31e7eab 100644 --- a/idea/testData/codegen/regressions/kt326.jet +++ b/idea/testData/codegen/regressions/kt326.jet @@ -28,7 +28,7 @@ fun box() : String { println(b.a[0]) val c = List>(1) - c.a[0] = Array(4) + c.a[0] = Array(4,{-1}) println(c.a[0].size) val e = List(5) diff --git a/idea/tests/org/jetbrains/jet/codegen/ArrayGenTest.java b/idea/tests/org/jetbrains/jet/codegen/ArrayGenTest.java index f788cb5a4e9..dcd5e4d9783 100644 --- a/idea/tests/org/jetbrains/jet/codegen/ArrayGenTest.java +++ b/idea/tests/org/jetbrains/jet/codegen/ArrayGenTest.java @@ -17,15 +17,24 @@ public class ArrayGenTest extends CodegenTestCase { } public void testCreateMultiInt () throws Exception { - loadText("fun foo() = Array> (5)"); + loadText("fun foo() = Array> (5, { Array(it, {239}) })"); Method foo = generateFunction(); - Object invoke = foo.invoke(null); - System.out.println(invoke.getClass()); - assertTrue(invoke instanceof Integer[][]); + Integer[][] invoke = (Integer[][]) foo.invoke(null); + assertEquals(invoke[2].length, 2); + assertEquals(invoke[4].length, 4); + assertEquals(invoke[4][2].intValue(), 239); + } + + public void testCreateMultiIntNullable () throws Exception { + loadText("fun foo() = Array> (5, { Array(it) })"); + Method foo = generateFunction(); + Integer[][] invoke = (Integer[][]) foo.invoke(null); + assertEquals(invoke[2].length, 2); + assertEquals(invoke[4].length, 4); } public void testCreateMultiString () throws Exception { - loadText("fun foo() = Array> (5)"); + loadText("fun foo() = Array> (5, { Array(0,{\"\"}) })"); Method foo = generateFunction(); Object invoke = foo.invoke(null); System.out.println(invoke.getClass()); diff --git a/idea/tests/org/jetbrains/jet/codegen/ControlStructuresTest.java b/idea/tests/org/jetbrains/jet/codegen/ControlStructuresTest.java index 4991959a8aa..581ef507d36 100644 --- a/idea/tests/org/jetbrains/jet/codegen/ControlStructuresTest.java +++ b/idea/tests/org/jetbrains/jet/codegen/ControlStructuresTest.java @@ -151,6 +151,10 @@ public class ControlStructuresTest extends CodegenTestCase { blackBoxFile("controlStructures/forIntArray.jet"); } + public void testForPrimitiveIntArray() throws Exception { + blackBoxFile("controlStructures/forPrimitiveIntArray.jet"); + } + public void testForNullableIntArray() throws Exception { blackBoxFile("controlStructures/forNullableIntArray.jet"); } diff --git a/idea/tests/org/jetbrains/jet/codegen/NamespaceGenTest.java b/idea/tests/org/jetbrains/jet/codegen/NamespaceGenTest.java index 8f43eec11e7..a1f2940a78e 100644 --- a/idea/tests/org/jetbrains/jet/codegen/NamespaceGenTest.java +++ b/idea/tests/org/jetbrains/jet/codegen/NamespaceGenTest.java @@ -374,13 +374,24 @@ public class NamespaceGenTest extends CodegenTestCase { } public void testArrayNew() throws Exception { - loadText("fun foo() = Array(4)"); + loadText("fun foo() = Array(4, { it })"); + System.out.println(generateToText()); + final Method main = generateFunction(); + Integer[] result = (Integer[]) main.invoke(null); + assertEquals(4, result.length); + assertEquals(0, result[0].intValue()); + assertEquals(1, result[1].intValue()); + assertEquals(2, result[2].intValue()); + assertEquals(3, result[3].intValue()); + } + + public void testArrayNewNullable() throws Exception { + loadText("fun foo() = Array(4)"); System.out.println(generateToText()); final Method main = generateFunction(); Integer[] result = (Integer[]) main.invoke(null); assertEquals(4, result.length); } - public void testFloatArrayNew() throws Exception { loadText("fun foo() = FloatArray(4)"); System.out.println(generateToText()); @@ -390,11 +401,12 @@ public class NamespaceGenTest extends CodegenTestCase { } public void testFloatArrayArrayNew() throws Exception { - loadText("fun foo() = Array(4)"); + loadText("fun foo() = Array(4, { FloatArray(5-it) })"); System.out.println(generateToText()); final Method main = generateFunction(); float[][] result = (float[][]) main.invoke(null); assertEquals(4, result.length); + assertEquals(2, result[3].length); } public void testArraySize() throws Exception {