Improve object arrays support. (#182)
Also fix the way how symbol names are being generated. Before arrays2.kt was not linking.
This commit is contained in:
committed by
Konstantin Anisimov
parent
709113fde7
commit
56bdfc663a
+1
-1
@@ -1731,7 +1731,7 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
|
|||||||
val typeInfo = codegen.typeInfoValue(containingClass)
|
val typeInfo = codegen.typeInfoValue(containingClass)
|
||||||
val allocHint = Int32(1).llvm
|
val allocHint = Int32(1).llvm
|
||||||
val thisValue = if (containingClass.isArray) {
|
val thisValue = if (containingClass.isArray) {
|
||||||
assert(args.size == 1 && args[0].type == int32Type)
|
assert(args.size >= 1 && args[0].type == int32Type)
|
||||||
val allocArrayInstanceArgs = listOf(typeInfo, allocHint, args[0])
|
val allocArrayInstanceArgs = listOf(typeInfo, allocHint, args[0])
|
||||||
call(context.llvm.allocArrayFunction, allocArrayInstanceArgs)
|
call(context.llvm.allocArrayFunction, allocArrayInstanceArgs)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+1
-1
@@ -21,7 +21,7 @@ private val exportForCppRuntimeAnnotation = FqName("konan.internal.ExportForCppR
|
|||||||
fun typeToHashString(type: KotlinType): String {
|
fun typeToHashString(type: KotlinType): String {
|
||||||
if (TypeUtils.isTypeParameter(type)) return "GENERIC"
|
if (TypeUtils.isTypeParameter(type)) return "GENERIC"
|
||||||
|
|
||||||
var hashString = type.constructor.toString()
|
var hashString = TypeUtils.getClassDescriptor(type)!!.fqNameSafe.asString()
|
||||||
if (!type.arguments.isEmpty()) {
|
if (!type.arguments.isEmpty()) {
|
||||||
hashString += "<${type.arguments.map {
|
hashString += "<${type.arguments.map {
|
||||||
typeToHashString(it.type)
|
typeToHashString(it.type)
|
||||||
|
|||||||
@@ -301,6 +301,11 @@ task array1(type: RunKonanTest) {
|
|||||||
source = "runtime/collections/array1.kt"
|
source = "runtime/collections/array1.kt"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
task array2(type: RunKonanTest) {
|
||||||
|
goldValue = "0\n2\n4\n6\n8\n40\n"
|
||||||
|
source = "runtime/collections/array2.kt"
|
||||||
|
}
|
||||||
|
|
||||||
task if_else(type: RunKonanTest) {
|
task if_else(type: RunKonanTest) {
|
||||||
source = "codegen/branching/if_else.kt"
|
source = "codegen/branching/if_else.kt"
|
||||||
}
|
}
|
||||||
@@ -536,6 +541,10 @@ task moderately_large_array(type: RunKonanTest) {
|
|||||||
source = "runtime/collections/moderately_large_array.kt"
|
source = "runtime/collections/moderately_large_array.kt"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
task moderately_large_array1(type: RunKonanTest) {
|
||||||
|
goldValue = "-45392\n"
|
||||||
|
source = "runtime/collections/moderately_large_array1.kt"
|
||||||
|
}
|
||||||
|
|
||||||
task string_builder0(type: RunKonanTest) {
|
task string_builder0(type: RunKonanTest) {
|
||||||
goldValue = "OK\n"
|
goldValue = "OK\n"
|
||||||
|
|||||||
@@ -3,5 +3,5 @@ fun main(args: Array<String>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun foo(): Any {
|
fun foo(): Any {
|
||||||
return Array<Any?>(0)
|
return Array<Any?>(0, { i -> null })
|
||||||
}
|
}
|
||||||
@@ -24,6 +24,6 @@ fun main(args : Array<String>) {
|
|||||||
val booleanArray = BooleanArray(12)
|
val booleanArray = BooleanArray(12)
|
||||||
println(booleanArray.size.toString())
|
println(booleanArray.size.toString())
|
||||||
|
|
||||||
val stringArray = Array<String>(13)
|
val stringArray = Array<String>(13, { i -> ""})
|
||||||
println(stringArray.size.toString())
|
println(stringArray.size.toString())
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
fun main(args : Array<String>) {
|
||||||
|
val byteArray = Array<Byte>(5, { i -> (i * 2).toByte() })
|
||||||
|
byteArray.map { println(it) }
|
||||||
|
|
||||||
|
val intArray = Array<Int>(5, { i -> i * 4 })
|
||||||
|
println(intArray.sum())
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
fun main(args: Array<String>) {
|
||||||
|
val a = Array<Byte>(100000, { i -> i.toByte()})
|
||||||
|
|
||||||
|
var sum = 0
|
||||||
|
for (b in a) {
|
||||||
|
sum += b
|
||||||
|
}
|
||||||
|
|
||||||
|
println(sum)
|
||||||
|
}
|
||||||
|
|
||||||
@@ -3,9 +3,15 @@ package kotlin
|
|||||||
// TODO: remove that, as RTTI shall be per instantiation.
|
// TODO: remove that, as RTTI shall be per instantiation.
|
||||||
@ExportTypeInfo("theArrayTypeInfo")
|
@ExportTypeInfo("theArrayTypeInfo")
|
||||||
public final class Array<T> : Cloneable {
|
public final class Array<T> : Cloneable {
|
||||||
// TODO: actual constructor has initializer parameter, implement it once lambdas are implemented.
|
|
||||||
// Constructors are handled with compiler magic.
|
// Constructors are handled with compiler magic.
|
||||||
public constructor(@Suppress("UNUSED_PARAMETER") size: Int) {}
|
public constructor(size: Int, init: (Int) -> T) {
|
||||||
|
var index = 0
|
||||||
|
while (index < size) {
|
||||||
|
this[index] = init(index)
|
||||||
|
index++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
internal constructor(@Suppress("UNUSED_PARAMETER") size: Int) {}
|
||||||
|
|
||||||
public val size: Int
|
public val size: Int
|
||||||
get() = getArrayLength()
|
get() = getArrayLength()
|
||||||
|
|||||||
@@ -507,3 +507,89 @@ public val <T> Array<out T>.indices: IntRange
|
|||||||
*/
|
*/
|
||||||
public val <T> Array<out T>.lastIndex: Int
|
public val <T> Array<out T>.lastIndex: Int
|
||||||
get() = size - 1
|
get() = size - 1
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Applies the given [transform] function to each element of the original array
|
||||||
|
* and appends the results to the given [destination].
|
||||||
|
*/
|
||||||
|
public inline fun <T, R, C : MutableCollection<in R>> Array<out T>.mapTo(destination: C, transform: (T) -> R): C {
|
||||||
|
for (item in this)
|
||||||
|
destination.add(transform(item))
|
||||||
|
return destination
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a list containing the results of applying the given [transform] function
|
||||||
|
* to each element in the original array.
|
||||||
|
*/
|
||||||
|
public inline fun <T, R> Array<out T>.map(transform: (T) -> R): List<R> {
|
||||||
|
return mapTo(ArrayList<R>(size), transform)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the sum of all elements in the array.
|
||||||
|
*/
|
||||||
|
public fun Array<out Byte>.sum(): Int {
|
||||||
|
var sum: Int = 0
|
||||||
|
for (element in this) {
|
||||||
|
sum += element
|
||||||
|
}
|
||||||
|
return sum
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the sum of all elements in the array.
|
||||||
|
*/
|
||||||
|
public fun Array<out Short>.sum(): Int {
|
||||||
|
var sum: Int = 0
|
||||||
|
for (element in this) {
|
||||||
|
sum += element
|
||||||
|
}
|
||||||
|
return sum
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the sum of all elements in the array.
|
||||||
|
*/
|
||||||
|
public fun Array<out Int>.sum(): Int {
|
||||||
|
var sum: Int = 0
|
||||||
|
for (element in this) {
|
||||||
|
sum += element
|
||||||
|
}
|
||||||
|
return sum
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the sum of all elements in the array.
|
||||||
|
*/
|
||||||
|
public fun Array<out Long>.sum(): Long {
|
||||||
|
var sum: Long = 0L
|
||||||
|
for (element in this) {
|
||||||
|
sum += element
|
||||||
|
}
|
||||||
|
return sum
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the sum of all elements in the array.
|
||||||
|
*/
|
||||||
|
public fun Array<out Float>.sum(): Float {
|
||||||
|
var sum: Float = 0.0f
|
||||||
|
for (element in this) {
|
||||||
|
sum += element
|
||||||
|
}
|
||||||
|
return sum
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the sum of all elements in the array.
|
||||||
|
*/
|
||||||
|
public fun Array<out Double>.sum(): Double {
|
||||||
|
var sum: Double = 0.0
|
||||||
|
for (element in this) {
|
||||||
|
sum += element
|
||||||
|
}
|
||||||
|
return sum
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
package kotlin.collections
|
package kotlin.collections
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an array of objects of the given type with the given [size], initialized with **lateinit** _uninitialized_ values.
|
* Returns an array of objects of the given type with the given [size], initialized with _uninitialized_ values.
|
||||||
* Attempts to read _uninitialized_ values from this array work in implementation-dependent manner,
|
* Attempts to read _uninitialized_ values from this array work in implementation-dependent manner,
|
||||||
* either throwing exception or returning some kind of implementation-specific default value.
|
* either throwing exception or returning some kind of implementation-specific default value.
|
||||||
*/
|
*/
|
||||||
fun <E> arrayOfUninitializedElements(size: Int): Array<E> {
|
internal fun <E> arrayOfUninitializedElements(size: Int): Array<E> {
|
||||||
// return (if (size == 0) emptyArray else Array<Any>(size)) as Array<E>
|
// TODO: special case for size == 0?
|
||||||
return Array<E>(size)
|
return Array<E>(size)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -16,7 +16,7 @@ fun <E> arrayOfUninitializedElements(size: Int): Array<E> {
|
|||||||
* either throwing exception or returning some kind of implementation-specific default value.
|
* either throwing exception or returning some kind of implementation-specific default value.
|
||||||
*/
|
*/
|
||||||
fun <E> Array<E>.copyOfUninitializedElements(newSize: Int): Array<E> {
|
fun <E> Array<E>.copyOfUninitializedElements(newSize: Int): Array<E> {
|
||||||
val result = Array<E>(newSize)
|
val result = arrayOfUninitializedElements<E>(newSize)
|
||||||
this.copyRangeTo(result, 0, if (newSize > this.size) this.size else newSize, 0)
|
this.copyRangeTo(result, 0, if (newSize > this.size) this.size else newSize, 0)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
@@ -33,7 +33,7 @@ fun IntArray.copyOfUninitializedElements(newSize: Int): IntArray {
|
|||||||
* Attempts to read _uninitialized_ value work in implementation-dependent manner,
|
* Attempts to read _uninitialized_ value work in implementation-dependent manner,
|
||||||
* either throwing exception or returning some kind of implementation-specific default value.
|
* either throwing exception or returning some kind of implementation-specific default value.
|
||||||
*/
|
*/
|
||||||
fun <E> Array<E>.resetAt(index: Int) {
|
internal fun <E> Array<E>.resetAt(index: Int) {
|
||||||
(@Suppress("UNCHECKED_CAST")(this as Array<Any?>))[index] = null
|
(@Suppress("UNCHECKED_CAST")(this as Array<Any?>))[index] = null
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,11 +50,11 @@ external private fun fillImpl(array: IntArray, fromIndex: Int, toIndex: Int, val
|
|||||||
* Attempts to read _uninitialized_ values work in implementation-dependent manner,
|
* Attempts to read _uninitialized_ values work in implementation-dependent manner,
|
||||||
* either throwing exception or returning some kind of implementation-specific default value.
|
* either throwing exception or returning some kind of implementation-specific default value.
|
||||||
*/
|
*/
|
||||||
fun <E> Array<E>.resetRange(fromIndex: Int, toIndex: Int) {
|
internal fun <E> Array<E>.resetRange(fromIndex: Int, toIndex: Int) {
|
||||||
fillImpl(@Suppress("UNCHECKED_CAST") (this as Array<Any>), fromIndex, toIndex, null)
|
fillImpl(@Suppress("UNCHECKED_CAST") (this as Array<Any>), fromIndex, toIndex, null)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun IntArray.fill(fromIndex: Int, toIndex: Int, value: Int) {
|
internal fun IntArray.fill(fromIndex: Int, toIndex: Int, value: Int) {
|
||||||
fillImpl(this, fromIndex, toIndex, value)
|
fillImpl(this, fromIndex, toIndex, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user