From f3df648f4ac03fcbcfc2647a6b070bdc75c9cbd7 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Tue, 6 Dec 2016 07:23:36 +0300 Subject: [PATCH] Unify toTypedArray and asList templates between JVM and JS --- .../src/core/generated/_ArraysJs.kt | 112 +++++----- libraries/stdlib/src/generated/_Arrays.kt | 193 ++++++++---------- .../src/generators/GenerateStandardLib.kt | 3 +- .../src/templates/SpecialJS.kt | 30 --- .../src/templates/SpecialJVM.kt | 50 +++-- 5 files changed, 174 insertions(+), 214 deletions(-) diff --git a/js/js.libraries/src/core/generated/_ArraysJs.kt b/js/js.libraries/src/core/generated/_ArraysJs.kt index c3a1648b050..3dd8d4a5c08 100644 --- a/js/js.libraries/src/core/generated/_ArraysJs.kt +++ b/js/js.libraries/src/core/generated/_ArraysJs.kt @@ -12560,6 +12560,62 @@ public inline fun CharArray.asList(): List { return this.unsafeCast>().asList() } +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun ByteArray.toTypedArray(): Array { + return copyOf().unsafeCast>() +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun ShortArray.toTypedArray(): Array { + return copyOf().unsafeCast>() +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun IntArray.toTypedArray(): Array { + return copyOf().unsafeCast>() +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun LongArray.toTypedArray(): Array { + return copyOf().unsafeCast>() +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun FloatArray.toTypedArray(): Array { + return copyOf().unsafeCast>() +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun DoubleArray.toTypedArray(): Array { + return copyOf().unsafeCast>() +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun BooleanArray.toTypedArray(): Array { + return copyOf().unsafeCast>() +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun CharArray.toTypedArray(): Array { + return copyOf().unsafeCast>() +} + /** * Returns `true` if the two specified arrays are *deeply* equal to one another, * i.e. contain the same number of the same elements in the same order. @@ -13400,59 +13456,3 @@ public fun Array.sortWith(comparator: Comparator): Unit { sort { a, b -> comparator.compare(a, b) } } -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun ByteArray.toTypedArray(): Array { - return copyOf().unsafeCast>() -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun ShortArray.toTypedArray(): Array { - return copyOf().unsafeCast>() -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun IntArray.toTypedArray(): Array { - return copyOf().unsafeCast>() -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun LongArray.toTypedArray(): Array { - return copyOf().unsafeCast>() -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun FloatArray.toTypedArray(): Array { - return copyOf().unsafeCast>() -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun DoubleArray.toTypedArray(): Array { - return copyOf().unsafeCast>() -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun BooleanArray.toTypedArray(): Array { - return copyOf().unsafeCast>() -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -public fun CharArray.toTypedArray(): Array { - return copyOf().unsafeCast>() -} - diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt index 43720c45787..9299ada27b3 100644 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ b/libraries/stdlib/src/generated/_Arrays.kt @@ -12903,7 +12903,6 @@ public fun DoubleArray.sum(): Double { /** * Returns a [List] that wraps the original array. */ -@kotlin.jvm.JvmVersion public fun Array.asList(): List { return ArraysUtilJVM.asList(this) } @@ -12911,7 +12910,6 @@ public fun Array.asList(): List { /** * Returns a [List] that wraps the original array. */ -@kotlin.jvm.JvmVersion public fun ByteArray.asList(): List { return object : AbstractList(), RandomAccess { override val size: Int get() = this@asList.size @@ -12926,7 +12924,6 @@ public fun ByteArray.asList(): List { /** * Returns a [List] that wraps the original array. */ -@kotlin.jvm.JvmVersion public fun ShortArray.asList(): List { return object : AbstractList(), RandomAccess { override val size: Int get() = this@asList.size @@ -12941,7 +12938,6 @@ public fun ShortArray.asList(): List { /** * Returns a [List] that wraps the original array. */ -@kotlin.jvm.JvmVersion public fun IntArray.asList(): List { return object : AbstractList(), RandomAccess { override val size: Int get() = this@asList.size @@ -12956,7 +12952,6 @@ public fun IntArray.asList(): List { /** * Returns a [List] that wraps the original array. */ -@kotlin.jvm.JvmVersion public fun LongArray.asList(): List { return object : AbstractList(), RandomAccess { override val size: Int get() = this@asList.size @@ -12971,7 +12966,6 @@ public fun LongArray.asList(): List { /** * Returns a [List] that wraps the original array. */ -@kotlin.jvm.JvmVersion public fun FloatArray.asList(): List { return object : AbstractList(), RandomAccess { override val size: Int get() = this@asList.size @@ -12986,7 +12980,6 @@ public fun FloatArray.asList(): List { /** * Returns a [List] that wraps the original array. */ -@kotlin.jvm.JvmVersion public fun DoubleArray.asList(): List { return object : AbstractList(), RandomAccess { override val size: Int get() = this@asList.size @@ -13001,7 +12994,6 @@ public fun DoubleArray.asList(): List { /** * Returns a [List] that wraps the original array. */ -@kotlin.jvm.JvmVersion public fun BooleanArray.asList(): List { return object : AbstractList(), RandomAccess { override val size: Int get() = this@asList.size @@ -13016,7 +13008,6 @@ public fun BooleanArray.asList(): List { /** * Returns a [List] that wraps the original array. */ -@kotlin.jvm.JvmVersion public fun CharArray.asList(): List { return object : AbstractList(), RandomAccess { override val size: Int get() = this@asList.size @@ -13028,6 +13019,94 @@ public fun CharArray.asList(): List { } } +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun ByteArray.toTypedArray(): Array { + val result = arrayOfNulls(size) + for (index in indices) + result[index] = this[index] + @Suppress("UNCHECKED_CAST") + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun ShortArray.toTypedArray(): Array { + val result = arrayOfNulls(size) + for (index in indices) + result[index] = this[index] + @Suppress("UNCHECKED_CAST") + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun IntArray.toTypedArray(): Array { + val result = arrayOfNulls(size) + for (index in indices) + result[index] = this[index] + @Suppress("UNCHECKED_CAST") + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun LongArray.toTypedArray(): Array { + val result = arrayOfNulls(size) + for (index in indices) + result[index] = this[index] + @Suppress("UNCHECKED_CAST") + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun FloatArray.toTypedArray(): Array { + val result = arrayOfNulls(size) + for (index in indices) + result[index] = this[index] + @Suppress("UNCHECKED_CAST") + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun DoubleArray.toTypedArray(): Array { + val result = arrayOfNulls(size) + for (index in indices) + result[index] = this[index] + @Suppress("UNCHECKED_CAST") + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun BooleanArray.toTypedArray(): Array { + val result = arrayOfNulls(size) + for (index in indices) + result[index] = this[index] + @Suppress("UNCHECKED_CAST") + return result as Array +} + +/** + * Returns a *typed* object array containing all of the elements of this primitive array. + */ +public fun CharArray.toTypedArray(): Array { + val result = arrayOfNulls(size) + for (index in indices) + result[index] = this[index] + @Suppress("UNCHECKED_CAST") + return result as Array +} + /** * Searches the array or the range of the array for the provided [element] using the binary search algorithm. * The array is expected to be sorted according to the specified [comparator], otherwise the result is undefined. @@ -13964,99 +14043,3 @@ public fun Array.sortWith(comparator: Comparator, fromIndex: In java.util.Arrays.sort(this, fromIndex, toIndex, comparator) } -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -@kotlin.jvm.JvmVersion -public fun ByteArray.toTypedArray(): Array { - val result = arrayOfNulls(size) - for (index in indices) - result[index] = this[index] - @Suppress("UNCHECKED_CAST") - return result as Array -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -@kotlin.jvm.JvmVersion -public fun ShortArray.toTypedArray(): Array { - val result = arrayOfNulls(size) - for (index in indices) - result[index] = this[index] - @Suppress("UNCHECKED_CAST") - return result as Array -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -@kotlin.jvm.JvmVersion -public fun IntArray.toTypedArray(): Array { - val result = arrayOfNulls(size) - for (index in indices) - result[index] = this[index] - @Suppress("UNCHECKED_CAST") - return result as Array -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -@kotlin.jvm.JvmVersion -public fun LongArray.toTypedArray(): Array { - val result = arrayOfNulls(size) - for (index in indices) - result[index] = this[index] - @Suppress("UNCHECKED_CAST") - return result as Array -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -@kotlin.jvm.JvmVersion -public fun FloatArray.toTypedArray(): Array { - val result = arrayOfNulls(size) - for (index in indices) - result[index] = this[index] - @Suppress("UNCHECKED_CAST") - return result as Array -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -@kotlin.jvm.JvmVersion -public fun DoubleArray.toTypedArray(): Array { - val result = arrayOfNulls(size) - for (index in indices) - result[index] = this[index] - @Suppress("UNCHECKED_CAST") - return result as Array -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -@kotlin.jvm.JvmVersion -public fun BooleanArray.toTypedArray(): Array { - val result = arrayOfNulls(size) - for (index in indices) - result[index] = this[index] - @Suppress("UNCHECKED_CAST") - return result as Array -} - -/** - * Returns a *typed* object array containing all of the elements of this primitive array. - */ -@kotlin.jvm.JvmVersion -public fun CharArray.toTypedArray(): Array { - val result = arrayOfNulls(size) - for (index in indices) - result[index] = this[index] - @Suppress("UNCHECKED_CAST") - return result as Array -} - diff --git a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt index 3ec60fc4896..6010f69da7d 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt @@ -47,7 +47,8 @@ val commonGenerators = sequenceOf( ::sequences, ::ranges, ::numeric, - ::comparables + ::comparables, + CommonArrays::templates ) fun generateCollectionsAPI(outDir: File) { diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt index e7b4bb90375..b615dff4fe6 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt @@ -21,36 +21,6 @@ import templates.Family.* fun specialJS(): List { val templates = arrayListOf() - templates add f("asList()") { - only(ArraysOfObjects, ArraysOfPrimitives) - doc { "Returns a [List] that wraps the original array." } - returns("List") - body(ArraysOfObjects) { - """ - return ArrayList(this.unsafeCast>()) - """ - } - - inline(true, ArraysOfPrimitives) - body(ArraysOfPrimitives) {"""return this.unsafeCast>().asList()"""} - } - - - templates add f("toTypedArray()") { - only(ArraysOfPrimitives) - returns("Array") - doc { - """ - Returns a *typed* object array containing all of the elements of this primitive array. - """ - } - body { - """ - return copyOf().unsafeCast>() - """ - } - } - templates add f("copyOfRange(fromIndex: Int, toIndex: Int)") { // TODO: Arguments checking as in java? only(ArraysOfObjects, ArraysOfPrimitives) diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt index 243ee6f6b76..8bb3081ff48 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt @@ -247,17 +247,29 @@ fun specialJVM(): List { } } - templates add f("asList()") { + templates.forEach { it.apply { jvmOnly(true) } } + + return templates +} + +object CommonArrays { + fun f_asList() = f("asList()") { only(ArraysOfObjects, ArraysOfPrimitives) doc { "Returns a [List] that wraps the original array." } returns("List") - body(ArraysOfObjects) { + body(Platform.JVM, ArraysOfObjects) { """ return ArraysUtilJVM.asList(this) """ } - body(ArraysOfPrimitives) { + body(Platform.JS, ArraysOfObjects) { + """ + return ArrayList(this.unsafeCast>()) + """ + } + + body(Platform.JVM, ArraysOfPrimitives) { """ return object : AbstractList(), RandomAccess { override val size: Int get() = this@asList.size @@ -269,19 +281,14 @@ fun specialJVM(): List { } """ } -// -// body(ArraysOfObjects) { -// """ -// return ArrayList(this.unsafeCast>()) -// """ -// } -// -// inline(true, ArraysOfPrimitives) -// body(ArraysOfPrimitives) {"""return this.unsafeCast>().asList()"""} + + + inline(Platform.JS, Inline.Yes, ArraysOfPrimitives) + body(Platform.JS, ArraysOfPrimitives) {"""return this.unsafeCast>().asList()"""} } - templates add f("toTypedArray()") { + fun f_toTypedArray() = f("toTypedArray()") { only(ArraysOfPrimitives) returns("Array") doc { @@ -289,7 +296,7 @@ fun specialJVM(): List { Returns a *typed* object array containing all of the elements of this primitive array. """ } - body { + body(Platform.JVM) { """ val result = arrayOfNulls(size) for (index in indices) @@ -298,14 +305,13 @@ fun specialJVM(): List { return result as Array """ } -// body { -// """ -// return copyOf().unsafeCast>() -// """ -// } + body(Platform.JS) { + """ + return copyOf().unsafeCast>() + """ + } } - templates.forEach { it.apply { jvmOnly(true) } } - - return templates + // TODO: use reflection later to get all functions of matching type + fun templates() = listOf(this.f_asList(), this.f_toTypedArray()) } \ No newline at end of file