From 0dbaf2a6055c8707d7d9d7054142181e7e0f08df Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Thu, 17 Nov 2016 19:26:07 +0300 Subject: [PATCH] Make toTypedArray reified again and thus inline, rewrite copyToArray and copyToArrayImpl in kotlin. --- js/js.libraries/src/core/collections.kt | 21 +++++++++++++++++---- js/js.translator/testData/kotlin_lib.js | 17 +---------------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/js/js.libraries/src/core/collections.kt b/js/js.libraries/src/core/collections.kt index f083de320ee..27dbbb259aa 100644 --- a/js/js.libraries/src/core/collections.kt +++ b/js/js.libraries/src/core/collections.kt @@ -18,12 +18,25 @@ package kotlin.collections import kotlin.comparisons.naturalOrder -@library("copyToArray") -public fun Collection.toTypedArray(): Array = noImpl +@Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE") +public inline fun Collection.toTypedArray(): Array = copyToArray(this) +@JsName("copyToArray") +internal fun copyToArray(collection: Collection): Array { + return if (collection.asDynamic().toArray !== undefined) + collection.asDynamic().toArray() + else + copyToArrayImpl(collection).asDynamic() +} -@library("copyToArrayImpl") -internal fun copyToArrayImpl(collection: Collection<*>): Array = noImpl +@JsName("copyToArrayImpl") +internal fun copyToArrayImpl(collection: Collection<*>): Array { + val array = emptyArray() + val iterator = collection.iterator() + while (iterator.hasNext()) + array.asDynamic().push(iterator.next()) + return array +} @library("arrayToString") internal fun arrayToString(array: Array<*>): String = noImpl diff --git a/js/js.translator/testData/kotlin_lib.js b/js/js.translator/testData/kotlin_lib.js index 61e294f51eb..4f0b2b74b70 100644 --- a/js/js.translator/testData/kotlin_lib.js +++ b/js/js.translator/testData/kotlin_lib.js @@ -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); };