Make toTypedArray reified again and thus inline, rewrite copyToArray and copyToArrayImpl in kotlin.

This commit is contained in:
Ilya Gorbunov
2016-11-17 19:26:07 +03:00
parent 698bc7bd31
commit 0dbaf2a605
2 changed files with 18 additions and 20 deletions
+17 -4
View File
@@ -18,12 +18,25 @@ package kotlin.collections
import kotlin.comparisons.naturalOrder
@library("copyToArray")
public fun <T> Collection<T>.toTypedArray(): Array<T> = noImpl
@Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE")
public inline fun <reified T> Collection<T>.toTypedArray(): Array<T> = copyToArray(this)
@JsName("copyToArray")
internal fun <T> copyToArray(collection: Collection<T>): Array<T> {
return if (collection.asDynamic().toArray !== undefined)
collection.asDynamic().toArray()
else
copyToArrayImpl(collection).asDynamic()
}
@library("copyToArrayImpl")
internal fun copyToArrayImpl(collection: Collection<*>): Array<Any?> = noImpl
@JsName("copyToArrayImpl")
internal fun copyToArrayImpl(collection: Collection<*>): Array<Any?> {
val array = emptyArray<Any?>()
val iterator = collection.iterator()
while (iterator.hasNext())
array.asDynamic().push(iterator.next())
return array
}
@library("arrayToString")
internal fun arrayToString(array: Array<*>): String = noImpl
+1 -16
View File
@@ -406,7 +406,7 @@ Kotlin.collectionsSort = function (mutableList, comparator) {
}
if (mutableList.size > 1) {
var array = Kotlin.copyToArray(mutableList);
var array = _.kotlin.collections.copyToArray(mutableList);
array.sort(boundComparator);
@@ -420,21 +420,6 @@ Kotlin.primitiveArraySort = function(array) {
array.sort(Kotlin.primitiveCompareTo)
};
Kotlin.copyToArray = function (collection) {
if (typeof collection.toArray !== "undefined") return collection.toArray();
return Kotlin.copyToArrayImpl(collection);
};
Kotlin.copyToArrayImpl = function (collection) {
var array = [];
var it = collection.iterator();
while (it.hasNext()) {
array.push(it.next());
}
return array;
};
Kotlin.splitString = function (str, regex, limit) {
return str.split(new RegExp(regex), limit);
};