Add basic iterators. (#58)
This commit is contained in:
@@ -373,3 +373,7 @@ task link(type: LinkKonanTest) {
|
||||
lib = "link/lib"
|
||||
} */
|
||||
|
||||
task for0(type: RunKonanTest) {
|
||||
goldValue = "2\n3\n4\n"
|
||||
source = "runtime/basic/for0.kt"
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
fun main(args : Array<String>) {
|
||||
val byteArray = ByteArray(3)
|
||||
byteArray[0] = 2
|
||||
byteArray[1] = 3
|
||||
byteArray[2] = 4
|
||||
for (item in byteArray) {
|
||||
println(item)
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
#include <cassert>
|
||||
#include "Assert.h"
|
||||
#include "TypeInfo.h"
|
||||
|
||||
extern "C" {
|
||||
@@ -10,7 +10,7 @@ int LookupFieldOffset(const TypeInfo* info, FieldNameHash nameSignature) {
|
||||
return info->fields_[i].fieldOffset_;
|
||||
}
|
||||
}
|
||||
assert(false);
|
||||
RuntimeAssert(false, "Unknown field");
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ void* LookupOpenMethod(const TypeInfo* info, MethodNameHash nameSignature) {
|
||||
return info->openMethods_[i].methodEntryPoint_;
|
||||
}
|
||||
}
|
||||
assert(false); // not implemented yet
|
||||
RuntimeAssert(false, "Unknown open method");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +1,37 @@
|
||||
package kotlin
|
||||
|
||||
/**
|
||||
* The root of the Kotlin class hierarchy. Every Kotlin class has [Any] as a superclass.
|
||||
*/
|
||||
@ExportTypeInfo("theAnyTypeInfo")
|
||||
public open class Any {
|
||||
/**
|
||||
* Indicates whether some other object is "equal to" this one. Implementations must fulfil the following
|
||||
* requirements:
|
||||
*
|
||||
* * Reflexive: for any non-null reference value x, x.equals(x) should return true.
|
||||
* * Symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
|
||||
* * Transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true
|
||||
* * Consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
|
||||
*
|
||||
* Note that the `==` operator in Kotlin code is translated into a call to [equals] when objects on both sides of the
|
||||
* operator are not null.
|
||||
*/
|
||||
@SymbolName("Kotlin_Any_equals")
|
||||
external public open operator fun equals(other: Any?): Boolean
|
||||
|
||||
// TODO: do we need that in Any?
|
||||
/**
|
||||
* Returns a hash code value for the object. The general contract of hashCode is:
|
||||
*
|
||||
* * Whenever it is invoked on the same object more than once, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified.
|
||||
* * If two objects are equal according to the equals() method, then calling the hashCode method on each of the two objects must produce the same integer result.
|
||||
*/
|
||||
@SymbolName("Kotlin_Any_hashCode")
|
||||
external public open fun hashCode(): Int
|
||||
|
||||
/**
|
||||
* Returns a string representation of the object.
|
||||
*/
|
||||
@SymbolName("Kotlin_Any_toString")
|
||||
external public open fun toString(): String
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package kotlin
|
||||
// TODO: remove that, as RTTI shall be per instantiation.
|
||||
@ExportTypeInfo("theArrayTypeInfo")
|
||||
public final class Array<T> : Cloneable {
|
||||
// TODO: actual constructor has initializer parameter, implement it once lambdas are implemented.
|
||||
// Constructors are handled with compiler magic.
|
||||
public constructor(@Suppress("UNUSED_PARAMETER") size: Int) {}
|
||||
|
||||
|
||||
@@ -1,7 +1,13 @@
|
||||
package kotlin
|
||||
|
||||
import kotlin.collections.*
|
||||
|
||||
/**
|
||||
* An array of bytes.
|
||||
* @constructor Creates a new array of the specified [size], with all elements initialized to zero.
|
||||
*/
|
||||
@ExportTypeInfo("theByteArrayTypeInfo")
|
||||
class ByteArray : Cloneable {
|
||||
public final class ByteArray : Cloneable {
|
||||
// Constructors are handled with compiler magic.
|
||||
public constructor(@Suppress("UNUSED_PARAMETER") size: Int) {}
|
||||
|
||||
@@ -19,10 +25,32 @@ class ByteArray : Cloneable {
|
||||
|
||||
@SymbolName("Kotlin_ByteArray_getArrayLength")
|
||||
external private fun getArrayLength(): Int
|
||||
|
||||
/** Creates an iterator over the elements of the array. */
|
||||
public operator fun iterator(): kotlin.collections.ByteIterator {
|
||||
return ByteIteratorImpl(this)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: replace with generics, once implemented.
|
||||
private class ByteIteratorImpl(val collection: ByteArray) : ByteIterator() {
|
||||
var index : Int = 0
|
||||
|
||||
public override fun nextByte(): Byte {
|
||||
return collection[index++]
|
||||
}
|
||||
|
||||
public override operator fun hasNext(): Boolean {
|
||||
return index < collection.size
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An array of chars.
|
||||
* @constructor Creates a new array of the specified [size], with all elements initialized to zero.
|
||||
*/
|
||||
@ExportTypeInfo("theCharArrayTypeInfo")
|
||||
class CharArray : Cloneable {
|
||||
public final class CharArray : Cloneable {
|
||||
// Constructors are handled with the compiler magic.
|
||||
public constructor(@Suppress("UNUSED_PARAMETER") size: Int) {}
|
||||
|
||||
@@ -40,10 +68,31 @@ class CharArray : Cloneable {
|
||||
|
||||
@SymbolName("Kotlin_CharArray_getArrayLength")
|
||||
external private fun getArrayLength(): Int
|
||||
|
||||
/** Creates an iterator over the elements of the array. */
|
||||
public operator fun iterator(): kotlin.collections.CharIterator {
|
||||
return CharIteratorImpl(this)
|
||||
}
|
||||
}
|
||||
|
||||
private class CharIteratorImpl(val collection: CharArray) : CharIterator() {
|
||||
var index : Int = 0
|
||||
|
||||
public override fun nextChar(): Char {
|
||||
return collection[index++]
|
||||
}
|
||||
|
||||
public override operator fun hasNext(): Boolean {
|
||||
return index < collection.size
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An array of shorts.
|
||||
* @constructor Creates a new array of the specified [size], with all elements initialized to zero.
|
||||
*/
|
||||
@ExportTypeInfo("theShortArrayTypeInfo")
|
||||
class ShortArray : Cloneable {
|
||||
public final class ShortArray : Cloneable {
|
||||
// Constructors are handled with the compiler magic.
|
||||
public constructor(@Suppress("UNUSED_PARAMETER") size: Int) {}
|
||||
|
||||
@@ -61,10 +110,31 @@ class ShortArray : Cloneable {
|
||||
|
||||
@SymbolName("Kotlin_ShortArray_getArrayLength")
|
||||
external private fun getArrayLength(): Int
|
||||
|
||||
/** Creates an iterator over the elements of the array. */
|
||||
public operator fun iterator(): kotlin.collections.ShortIterator {
|
||||
return ShortIteratorImpl(this)
|
||||
}
|
||||
}
|
||||
|
||||
private class ShortIteratorImpl(val collection: ShortArray) : ShortIterator() {
|
||||
var index : Int = 0
|
||||
|
||||
public override fun nextShort(): Short {
|
||||
return collection[index++]
|
||||
}
|
||||
|
||||
public override operator fun hasNext(): Boolean {
|
||||
return index < collection.size
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An array of ints. When targeting the JVM, instances of this class are represented as `int[]`.
|
||||
* @constructor Creates a new array of the specified [size], with all elements initialized to zero.
|
||||
*/
|
||||
@ExportTypeInfo("theIntArrayTypeInfo")
|
||||
class IntArray : Cloneable {
|
||||
public final class IntArray : Cloneable {
|
||||
// Constructors are handled with the compiler magic.
|
||||
public constructor(@Suppress("UNUSED_PARAMETER") size: Int) {}
|
||||
|
||||
@@ -82,10 +152,31 @@ class IntArray : Cloneable {
|
||||
|
||||
@SymbolName("Kotlin_IntArray_getArrayLength")
|
||||
external private fun getArrayLength(): Int
|
||||
|
||||
/** Creates an iterator over the elements of the array. */
|
||||
public operator fun iterator(): kotlin.collections.IntIterator {
|
||||
return IntIteratorImpl(this)
|
||||
}
|
||||
}
|
||||
|
||||
private class IntIteratorImpl(val collection: IntArray) : IntIterator() {
|
||||
var index : Int = 0
|
||||
|
||||
public override fun nextInt(): Int {
|
||||
return collection[index++]
|
||||
}
|
||||
|
||||
public override operator fun hasNext(): Boolean {
|
||||
return index < collection.size
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An array of longs.
|
||||
* @constructor Creates a new array of the specified [size], with all elements initialized to zero.
|
||||
*/
|
||||
@ExportTypeInfo("theLongArrayTypeInfo")
|
||||
class LongArray : Cloneable {
|
||||
public final class LongArray : Cloneable {
|
||||
// Constructors are handled with the compiler magic.
|
||||
public constructor(@Suppress("UNUSED_PARAMETER") size: Int) {}
|
||||
|
||||
@@ -103,10 +194,31 @@ class LongArray : Cloneable {
|
||||
|
||||
@SymbolName("Kotlin_LongArray_getArrayLength")
|
||||
external private fun getArrayLength(): Int
|
||||
|
||||
/** Creates an iterator over the elements of the array. */
|
||||
public operator fun iterator(): kotlin.collections.LongIterator {
|
||||
return LongIteratorImpl(this)
|
||||
}
|
||||
}
|
||||
|
||||
private class LongIteratorImpl(val collection: LongArray) : LongIterator() {
|
||||
var index : Int = 0
|
||||
|
||||
public override fun nextLong(): Long {
|
||||
return collection[index++]
|
||||
}
|
||||
|
||||
public override operator fun hasNext(): Boolean {
|
||||
return index < collection.size
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An array of floats.
|
||||
* @constructor Creates a new array of the specified [size], with all elements initialized to zero.
|
||||
*/
|
||||
@ExportTypeInfo("theFloatArrayTypeInfo")
|
||||
class FloatArray : Cloneable {
|
||||
public final class FloatArray : Cloneable {
|
||||
// Constructors are handled with the compiler magic.
|
||||
public constructor(@Suppress("UNUSED_PARAMETER") size: Int) {}
|
||||
|
||||
@@ -124,10 +236,27 @@ class FloatArray : Cloneable {
|
||||
|
||||
@SymbolName("Kotlin_FloatArray_getArrayLength")
|
||||
external private fun getArrayLength(): Int
|
||||
|
||||
/** Creates an iterator over the elements of the array. */
|
||||
public operator fun iterator(): kotlin.collections.FloatIterator {
|
||||
return FloatIteratorImpl(this)
|
||||
}
|
||||
}
|
||||
|
||||
private class FloatIteratorImpl(val collection: FloatArray) : FloatIterator() {
|
||||
var index : Int = 0
|
||||
|
||||
public override fun nextFloat(): Float {
|
||||
return collection[index++]
|
||||
}
|
||||
|
||||
public override operator fun hasNext(): Boolean {
|
||||
return index < collection.size
|
||||
}
|
||||
}
|
||||
|
||||
@ExportTypeInfo("theDoubleArrayTypeInfo")
|
||||
class DoubleArray : Cloneable {
|
||||
public final class DoubleArray : Cloneable {
|
||||
// Constructors are handled with the compiler magic.
|
||||
public constructor(@Suppress("UNUSED_PARAMETER") size: Int) {}
|
||||
|
||||
@@ -145,10 +274,27 @@ class DoubleArray : Cloneable {
|
||||
|
||||
@SymbolName("Kotlin_DoubleArray_getArrayLength")
|
||||
external private fun getArrayLength(): Int
|
||||
|
||||
/** Creates an iterator over the elements of the array. */
|
||||
public operator fun iterator(): kotlin.collections.DoubleIterator {
|
||||
return DoubleIteratorImpl(this)
|
||||
}
|
||||
}
|
||||
|
||||
private class DoubleIteratorImpl(val collection: DoubleArray) : DoubleIterator() {
|
||||
var index : Int = 0
|
||||
|
||||
public override fun nextDouble(): Double {
|
||||
return collection[index++]
|
||||
}
|
||||
|
||||
public override operator fun hasNext(): Boolean {
|
||||
return index < collection.size
|
||||
}
|
||||
}
|
||||
|
||||
@ExportTypeInfo("theBooleanArrayTypeInfo")
|
||||
class BooleanArray : Cloneable {
|
||||
public final class BooleanArray : Cloneable {
|
||||
// Constructors are handled with the compiler magic.
|
||||
public constructor(@Suppress("UNUSED_PARAMETER") size: Int) {}
|
||||
|
||||
@@ -166,4 +312,21 @@ class BooleanArray : Cloneable {
|
||||
|
||||
@SymbolName("Kotlin_BooleanArray_getArrayLength")
|
||||
external private fun getArrayLength(): Int
|
||||
}
|
||||
|
||||
/** Creates an iterator over the elements of the array. */
|
||||
public operator fun iterator(): kotlin.collections.BooleanIterator {
|
||||
return BooleanIteratorImpl(this)
|
||||
}
|
||||
}
|
||||
|
||||
private class BooleanIteratorImpl(val collection: BooleanArray) : BooleanIterator() {
|
||||
var index : Int = 0
|
||||
|
||||
public override fun nextBoolean(): Boolean {
|
||||
return collection[index++]
|
||||
}
|
||||
|
||||
public override operator fun hasNext(): Boolean {
|
||||
return index < collection.size
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package kotlin.collections
|
||||
|
||||
/**
|
||||
* Classes that inherit from this interface can be represented as a sequence of elements that can
|
||||
* be iterated over.
|
||||
* @param T the type of element being iterated over.
|
||||
*/
|
||||
public interface Iterable<out T> {
|
||||
/**
|
||||
* Returns an iterator over the elements of this object.
|
||||
*/
|
||||
public operator fun iterator(): Iterator<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* Classes that inherit from this interface can be represented as a sequence of elements that can
|
||||
* be iterated over and that supports removing elements during iteration.
|
||||
*/
|
||||
public interface MutableIterable<out T> : Iterable<T> {
|
||||
/**
|
||||
* Returns an iterator over the elementrs of this sequence that supports removing elements during iteration.
|
||||
*/
|
||||
override fun iterator(): MutableIterator<T>
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package kotlin.collections
|
||||
|
||||
/** An iterator over a sequence of values of type `Byte`. */
|
||||
public abstract class ByteIterator : Iterator<Byte> {
|
||||
override final fun next() = nextByte()
|
||||
|
||||
/** Returns the next value in the sequence without boxing. */
|
||||
public abstract fun nextByte(): Byte
|
||||
}
|
||||
|
||||
/** An iterator over a sequence of values of type `Char`. */
|
||||
public abstract class CharIterator : Iterator<Char> {
|
||||
override final fun next() = nextChar()
|
||||
|
||||
/** Returns the next value in the sequence without boxing. */
|
||||
public abstract fun nextChar(): Char
|
||||
}
|
||||
|
||||
/** An iterator over a sequence of values of type `Short`. */
|
||||
public abstract class ShortIterator : Iterator<Short> {
|
||||
override final fun next() = nextShort()
|
||||
|
||||
/** Returns the next value in the sequence without boxing. */
|
||||
public abstract fun nextShort(): Short
|
||||
}
|
||||
|
||||
/** An iterator over a sequence of values of type `Int`. */
|
||||
public abstract class IntIterator : Iterator<Int> {
|
||||
override final fun next() = nextInt()
|
||||
|
||||
/** Returns the next value in the sequence without boxing. */
|
||||
public abstract fun nextInt(): Int
|
||||
}
|
||||
|
||||
/** An iterator over a sequence of values of type `Long`. */
|
||||
public abstract class LongIterator : Iterator<Long> {
|
||||
override final fun next() = nextLong()
|
||||
|
||||
/** Returns the next value in the sequence without boxing. */
|
||||
public abstract fun nextLong(): Long
|
||||
}
|
||||
|
||||
/** An iterator over a sequence of values of type `Float`. */
|
||||
public abstract class FloatIterator : Iterator<Float> {
|
||||
override final fun next() = nextFloat()
|
||||
|
||||
/** Returns the next value in the sequence without boxing. */
|
||||
public abstract fun nextFloat(): Float
|
||||
}
|
||||
|
||||
/** An iterator over a sequence of values of type `Double`. */
|
||||
public abstract class DoubleIterator : Iterator<Double> {
|
||||
override final fun next() = nextDouble()
|
||||
|
||||
/** Returns the next value in the sequence without boxing. */
|
||||
public abstract fun nextDouble(): Double
|
||||
}
|
||||
|
||||
/** An iterator over a sequence of values of type `Boolean`. */
|
||||
public abstract class BooleanIterator : Iterator<Boolean> {
|
||||
override final fun next() = nextBoolean()
|
||||
|
||||
/** Returns the next value in the sequence without boxing. */
|
||||
public abstract fun nextBoolean(): Boolean
|
||||
}
|
||||
Reference in New Issue
Block a user