Merge branch 'master' of github.com:JetBrains/kotlin

This commit is contained in:
James Strachan
2012-05-18 15:18:24 +01:00
250 changed files with 3683 additions and 1549 deletions
+2
View File
@@ -88,6 +88,7 @@ public inline fun FloatArray.copyOf(newLength: Int = this.size) : FloatArray
public inline fun DoubleArray.copyOf(newLength: Int = this.size) : DoubleArray = Arrays.copyOf(this, newLength).sure()
public inline fun CharArray.copyOf(newLength: Int = this.size) : CharArray = Arrays.copyOf(this, newLength).sure()
// TODO: resuling array may contain nulls even if T is non-nullable
public inline fun <T> Array<T>.copyOf(newLength: Int = this.size) : Array<T> = Arrays.copyOf(this as Array<T?>, newLength) as Array<T>
public inline fun BooleanArray.copyOfRange(from: Int, to: Int) : BooleanArray = Arrays.copyOfRange(this, from, to).sure()
@@ -99,6 +100,7 @@ public inline fun FloatArray.copyOfRange(from: Int, to: Int) : FloatArray =
public inline fun DoubleArray.copyOfRange(from: Int, to: Int) : DoubleArray = Arrays.copyOfRange(this, from, to).sure()
public inline fun CharArray.copyOfRange(from: Int, to: Int) : CharArray = Arrays.copyOfRange(this, from, to).sure()
// TODO: resuling array may contain nulls even if T is non-nullable
public inline fun <T> Array<T>.copyOfRange(from: Int, to: Int) : Array<T> = Arrays.copyOfRange(this as Array<T?>, from, to) as Array<T>
public inline val ByteArray.inputStream : ByteArrayInputStream
@@ -0,0 +1,99 @@
package kotlin
import java.util.List
import java.util.AbstractList
private class ImmutableArrayList<T>(
private val array: Array<T>,
private val offset: Int,
private val length: Int
) : AbstractList<T>() {
{
// impossible
if (offset < 0) {
throw IllegalArgumentException("negative offset ($offset)")
}
// impossible
if (length < 0) {
throw IllegalArgumentException("negative length ($length)")
}
// possible when builder is used from different threads
if (offset + length > array.size) {
throw IllegalArgumentException("offset ($offset) + length ($length) > array.length (${array.size})")
}
}
protected fun indexInArray(index: Int): Int {
if (index < 0) {
throw IndexOutOfBoundsException("negative index ($index)")
}
if (index >= length) {
throw IndexOutOfBoundsException("index ($index) >= length ($length)")
}
return index + offset
}
public override fun get(index: Int): T = array[indexInArray(index)] as T
public override fun size() : Int = length
public override fun subList(fromIndex: Int, toIndex: Int) : List<T> {
if (fromIndex < 0) {
throw IndexOutOfBoundsException("negative from index ($fromIndex)")
}
if (toIndex < fromIndex) {
throw IndexOutOfBoundsException("toIndex ($toIndex) < fromIndex ($fromIndex)")
}
if (toIndex > length) {
throw IndexOutOfBoundsException("fromIndex ($fromIndex) + toIndex ($toIndex) > length ($length)")
}
if (fromIndex == toIndex) {
return emptyImmutableArrayList as List<T>
}
if (fromIndex == 0 && toIndex == length) {
return this
}
return ImmutableArrayList(array, offset + fromIndex, toIndex - fromIndex)
}
// TODO: efficiently implement iterator and other stuff
}
// TODO: make private val, see http://youtrack.jetbrains.com/issue/KT-2028
internal val emptyArray = arrayOfNulls<Any?>(0)
internal val emptyImmutableArrayList = ImmutableArrayList<Any?>(emptyArray, 0, 0)
public class ImmutableArrayListBuilder<T>() {
private var array = emptyArray
private var length = 0
public fun build(): List<T> {
if (length == 0) {
return emptyImmutableArrayList as List<T>
}
else {
val r = ImmutableArrayList<T>(array as Array<T>, 0, length)
array = emptyArray
length = 0
return r
}
}
public fun ensureCapacity(capacity: Int) {
if (array.size < capacity) {
val newSize = Math.max(capacity, Math.max(array.size * 2, 11))
array = array.copyOf(newSize)
}
}
public fun add(item: T) {
ensureCapacity(length + 1)
array[length] = item
++length
}
}
// default list builder
public fun <T> listBuilder(): ImmutableArrayListBuilder<T> = ImmutableArrayListBuilder<T>()
@@ -0,0 +1,63 @@
package test.collection
import kotlin.test.*
import junit.framework.TestCase
import java.util.List
import java.util.Random
class ImmutableArrayListTest() : TestCase() {
fun testSimple() {
val builder = ImmutableArrayListBuilder<Int>()
builder.add(17)
val list = builder.build()
assertEquals(1, list.size())
assertEquals(17, list[0])
}
fun testGet() {
for (length in 0 .. 55) {
val list = buildIntArray(length, 19)
assertEquals(length, list.size)
checkList(list, length, 19)
}
}
private fun buildIntArray(length: Int, firstValue: Int): List<Int> {
val builder = ImmutableArrayListBuilder<Int>()
for (j in 0 .. length - 1) {
builder.add(firstValue + j)
}
return builder.build()
}
private fun checkList(list: List<Int>, expectedLength: Int, expectedFirstValue: Int) {
assertEquals(expectedLength, list.size)
for (i in 0 .. expectedLength - 1) {
assertEquals(expectedFirstValue + i, list[i])
}
try {
list[expectedLength]
fail()
} catch (e: IndexOutOfBoundsException) {
// expected
}
}
fun testSublist() {
val r = Random(1)
for (i in 0 .. 200) {
val length = r.nextInt(55)
val list = buildIntArray(length, 23)
val fromIndex = r.nextInt(length + 1)
val toIndex = fromIndex + r.nextInt(length - fromIndex + 1)
val sublist = list.subList(fromIndex, toIndex)
checkList(sublist, toIndex - fromIndex, 23 + fromIndex)
}
}
}
+5 -6
View File
@@ -38,7 +38,7 @@
<resources>
<resource>
<!-- jdkHeaders -->
<!-- altHeaders -->
<directory>${kotlin-sdk}/lib/alt</directory>
<filtering>false</filtering>
</resource>
@@ -56,7 +56,6 @@
<version>2.9</version>
</plugin>
<!--
<plugin>
<artifactId>maven-invoker-plugin</artifactId>
<version>1.5</version>
@@ -66,15 +65,16 @@
<pomIncludes>
<pomInclude>*/pom.xml</pomInclude>
</pomIncludes>
&lt;!&ndash;This could speed up test by providing local repo as remote repo for test but might be tricky on different OS&ndash;&gt;
&lt;!&ndash;<settingsFile>src/it/settings.xml</settingsFile>&ndash;&gt;
<!--This could speed up test by providing local repo as remote repo for test but might be tricky on different OS-->
<!--<settingsFile>src/it/settings.xml</settingsFile>-->
<localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath>
<postBuildHookScript>verify.bsh</postBuildHookScript>
<streamLogs>true</streamLogs>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<phase>package</phase>
<!--<phase>package</phase>-->
<goals>
<goal>install</goal>
<goal>run</goal>
@@ -82,7 +82,6 @@
</execution>
</executions>
</plugin>
-->
</plugins>
</build>
</project>
@@ -45,13 +45,7 @@ public class K2JSCompilerPlugin implements CompilerPlugin {
List<JetFile> sources = context.getFiles();
if (bindingContext != null && sources != null && project != null) {
Config config = new Config(project) {
@NotNull
@Override
protected List<JetFile> generateLibFiles() {
return new ArrayList<JetFile>();
}
};
Config config = Config.getEmptyConfig(project);
try {
@@ -23,10 +23,9 @@ import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.logging.Log;
import org.jetbrains.jet.cli.jvm.K2JVMCompilerArguments;
import org.jetbrains.jet.cli.jvm.K2JVMCompiler;
import org.jetbrains.jet.cli.common.ExitCode.*;
import org.jetbrains.jet.cli.common.ExitCode;
import org.jetbrains.jet.cli.jvm.K2JVMCompiler;
import org.jetbrains.jet.cli.jvm.K2JVMCompilerArguments;
import java.io.File;
import java.io.IOException;
@@ -204,8 +203,8 @@ public abstract class KotlinCompileMojoBase extends AbstractMojo {
log.info("Classes directory is " + output);
arguments.setOutputDir(output);
arguments.jdkHeaders = getJdkHeaders().getPath();
log.debug("Using jdk headers from " + arguments.jdkHeaders);
arguments.altHeaders = getAltHeaders().getPath();
log.debug("Using alt headers from " + arguments.altHeaders);
}
// TODO: Make a better runtime detection or get rid of it entirely
@@ -223,25 +222,25 @@ public abstract class KotlinCompileMojoBase extends AbstractMojo {
return null;
}
private File jdkHeadersPath;
private File altHeadersPath;
protected File getJdkHeaders() {
if (jdkHeadersPath != null)
return jdkHeadersPath;
protected File getAltHeaders() {
if (altHeadersPath != null)
return altHeadersPath;
try {
jdkHeadersPath = extractJdkHeaders();
altHeadersPath = extractAltHeaders();
if (jdkHeadersPath == null)
throw new RuntimeException("Can't find kotlin jdk headers in maven plugin resources");
if (altHeadersPath == null)
throw new RuntimeException("Can't find kotlin alt headers in maven plugin resources");
} catch (IOException e) {
throw new RuntimeException(e);
}
return jdkHeadersPath;
return altHeadersPath;
}
private File extractJdkHeaders() throws IOException {
private File extractAltHeaders() throws IOException {
final String kotlin_jdk_headers = "kotlin-jdk-headers.jar";
final URL jdkHeadersResource = Resources.getResource(kotlin_jdk_headers);