Added copyToArray intrinsic method.

#KT-3352 fixed
This commit is contained in:
Evgeny Gerashchenko
2013-09-30 20:09:09 +04:00
parent 41d1f277dc
commit 2ae1a2c81b
13 changed files with 110 additions and 7 deletions
@@ -2018,7 +2018,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
for (ValueArgument argument : call.getValueArguments()) {
args.add(argument.getArgumentExpression());
}
JetType type = resolvedCall.getCandidateDescriptor().getReturnType();
JetType type = resolvedCall.getResultingDescriptor().getReturnType();
assert type != null;
Type callType = typeMapper.mapType(type);
@@ -0,0 +1,36 @@
package org.jetbrains.jet.codegen.intrinsics;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.asm4.Type;
import org.jetbrains.asm4.commons.InstructionAdapter;
import org.jetbrains.jet.codegen.ExpressionCodegen;
import org.jetbrains.jet.codegen.StackValue;
import org.jetbrains.jet.codegen.state.GenerationState;
import org.jetbrains.jet.lang.psi.JetExpression;
import java.util.List;
public class CopyToArray implements IntrinsicMethod {
@Override
public StackValue generate(
ExpressionCodegen codegen,
InstructionAdapter v,
@NotNull Type expectedType,
@Nullable PsiElement element,
@Nullable List<JetExpression> arguments,
@Nullable StackValue receiver,
@NotNull GenerationState state
) {
assert receiver != null;
receiver.put(receiver.type, v);
v.dup();
v.invokeinterface("java/util/Collection", "size", "()I");
v.newarray(expectedType.getElementType());
v.invokeinterface("java/util/Collection", "toArray", "([Ljava/lang/Object;)[Ljava/lang/Object;");
return StackValue.onStack(expectedType);
}
}
@@ -58,8 +58,9 @@ public class IntrinsicMethods {
private static final ArrayGet ARRAY_GET = new ArrayGet();
private static final StringPlus STRING_PLUS = new StringPlus();
public static final String KOTLIN_JAVA_CLASS_FUNCTION = "kotlin.javaClass.function";
public static final String KOTLIN_ARRAYS_ARRAY = "kotlin.arrays.array";
private static final String KOTLIN_JAVA_CLASS_PROPERTY = "kotlin.javaClass.property";
public static final String KOTLIN_ARRAYS_ARRAY = "kotlin.arrays.array";
public static final String KOTLIN_COPY_TO_ARRAY = "kotlin.collections.copyToArray";
private static final String KOTLIN_TO_STRING = "kotlin.toString";
private static final String KOTLIN_HASH_CODE = "kotlin.hashCode";
private static final EnumValues ENUM_VALUES = new EnumValues();
@@ -76,6 +77,7 @@ public class IntrinsicMethods {
namedMethods.put(KOTLIN_JAVA_CLASS_FUNCTION, new JavaClassFunction());
namedMethods.put(KOTLIN_JAVA_CLASS_PROPERTY, new JavaClassProperty());
namedMethods.put(KOTLIN_ARRAYS_ARRAY, new JavaClassArray());
namedMethods.put(KOTLIN_COPY_TO_ARRAY, new CopyToArray());
namedMethods.put(KOTLIN_HASH_CODE, HASH_CODE);
namedMethods.put(KOTLIN_TO_STRING, TO_STRING);
@@ -0,0 +1,11 @@
import java.util.Arrays
fun box(): String {
val array = Arrays.asList(2, 3, 9).copyToArray()
if (array !is Array<Int>) return array.javaClass.toString()
val str = Arrays.toString(array)
if (str != "[2, 3, 9]") return str
return "OK"
}
@@ -0,0 +1,15 @@
import java.util.ArrayList
import java.util.Arrays
fun box(): String {
val list = ArrayList<Pair<String,String>>()
list.add(Pair("Sample", "http://cyber.law.harvard.edu/rss/examples/rss2sample.xml"))
list.add(Pair("Scripting", "http://static.scripting.com/rss.xml"))
val keys = list.map { it.first }.copyToArray<String>()
val keysToString = Arrays.toString(keys)
if (keysToString != "[Sample, Scripting]") return keysToString
return "OK"
}
@@ -31,7 +31,7 @@ import org.jetbrains.jet.codegen.generated.AbstractBlackBoxCodegenTest;
/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("compiler/testData/codegen/boxWithStdlib")
@InnerTestClasses({BlackBoxWithStdlibCodegenTestGenerated.Annotations.class, BlackBoxWithStdlibCodegenTestGenerated.Arrays.class, BlackBoxWithStdlibCodegenTestGenerated.Casts.class, BlackBoxWithStdlibCodegenTestGenerated.DataClasses.class, BlackBoxWithStdlibCodegenTestGenerated.FullJdk.class, BlackBoxWithStdlibCodegenTestGenerated.JdkAnnotations.class, BlackBoxWithStdlibCodegenTestGenerated.Ranges.class, BlackBoxWithStdlibCodegenTestGenerated.Regressions.class, BlackBoxWithStdlibCodegenTestGenerated.Strings.class})
@InnerTestClasses({BlackBoxWithStdlibCodegenTestGenerated.Annotations.class, BlackBoxWithStdlibCodegenTestGenerated.Arrays.class, BlackBoxWithStdlibCodegenTestGenerated.Casts.class, BlackBoxWithStdlibCodegenTestGenerated.DataClasses.class, BlackBoxWithStdlibCodegenTestGenerated.FullJdk.class, BlackBoxWithStdlibCodegenTestGenerated.JdkAnnotations.class, BlackBoxWithStdlibCodegenTestGenerated.Ranges.class, BlackBoxWithStdlibCodegenTestGenerated.Regressions.class, BlackBoxWithStdlibCodegenTestGenerated.Strings.class, BlackBoxWithStdlibCodegenTestGenerated.ToArray.class})
public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
public void testAllFilesPresentInBoxWithStdlib() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/codegen/boxWithStdlib"), Pattern.compile("^(.+)\\.kt$"), true);
@@ -940,6 +940,24 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode
}
@TestMetadata("compiler/testData/codegen/boxWithStdlib/toArray")
public static class ToArray extends AbstractBlackBoxCodegenTest {
public void testAllFilesPresentInToArray() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/codegen/boxWithStdlib/toArray"), Pattern.compile("^(.+)\\.kt$"), true);
}
@TestMetadata("copyToArray.kt")
public void testCopyToArray() throws Exception {
doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/toArray/copyToArray.kt");
}
@TestMetadata("kt3177-copyToArray.kt")
public void testKt3177_copyToArray() throws Exception {
doTestWithStdlib("compiler/testData/codegen/boxWithStdlib/toArray/kt3177-copyToArray.kt");
}
}
public static Test suite() {
TestSuite suite = new TestSuite("BlackBoxWithStdlibCodegenTestGenerated");
suite.addTestSuite(BlackBoxWithStdlibCodegenTestGenerated.class);
@@ -952,6 +970,7 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode
suite.addTest(Ranges.innerSuite());
suite.addTestSuite(Regressions.class);
suite.addTestSuite(Strings.class);
suite.addTestSuite(ToArray.class);
return suite;
}
}
+2
View File
@@ -24,3 +24,5 @@ public fun byteArray(vararg content : Byte): ByteArray = js.noImpl
public fun booleanArray(vararg content : Boolean): BooleanArray = js.noImpl
library("copyToArray")
public fun <reified T> Collection<T>.copyToArray(): Array<T> = js.noImpl
@@ -47,7 +47,7 @@ fun box(): Boolean {
a.clear()
assertThat(a.isEmpty(), true)
val array = list.toArray(Array<Int>(0, {it}))
val array = list.copyToArray()
assertThat(array[0], 1)
assertThat(array[1], 500)
@@ -12,6 +12,6 @@ fun box(): Boolean {
// test addAt
list.add(1, 500)
val array = list.toArray()
val array = list.copyToArray()
return array[0] == 1 && array[1] == 500 && array[2] == 2 && array[3] == 3 && JSON.stringify(list) == "[1,500,2,3]";
}
+10
View File
@@ -492,6 +492,16 @@ String.prototype.contains = function (s) {
}
};
Kotlin.copyToArray = function (collection) {
var array = [];
var it = collection.iterator();
while (it.hasNext()) {
array.push(it.next());
}
return array;
};
Kotlin.StringBuilder = Kotlin.createClass(null,
function () {
+4 -1
View File
@@ -107,4 +107,7 @@ public inline fun ByteArray.inputStream(offset: Int, length: Int) : ByteArrayInp
public inline fun ByteArray.toString(encoding: String): String = String(this, encoding)
public inline fun ByteArray.toString(): String = String(this)
public inline fun ByteArray.toString(encoding: Charset) : String = String(this, encoding)
public inline fun ByteArray.toString(encoding: Charset) : String = String(this, encoding)
[Intrinsic("kotlin.collections.copyToArray")] public fun <reified T> Collection<T>.copyToArray(): Array<T> =
throw UnsupportedOperationException()
@@ -29,4 +29,9 @@ class JavautilCollectionsTest {
Collections.sort(list, COMPARATOR)
assertEquals(SORTED_TEST_LIST, list)
}
test fun collectionToArray() {
val array = TEST_LIST.copyToArray()
assertEquals(array.toList(), TEST_LIST)
}
}
@@ -279,7 +279,7 @@ open class GradleUtils() {
val configurationsContainer : ConfigurationContainer = project.getBuildscript().getConfigurations()
val deps = coordinates.map { dependencyHandler.create(it) }
val configuration = configurationsContainer.detachedConfiguration(*deps.toArray(array<Dependency>()))
val configuration = configurationsContainer.detachedConfiguration(*deps.copyToArray())
return configuration.getResolvedConfiguration().getFiles(KSpec({ dep -> true }))!!
}