From ae47130c8e2a595ebabe82dfd908a1e6c62eef02 Mon Sep 17 00:00:00 2001 From: Abduqodiri Qurbonzoda Date: Wed, 20 Feb 2019 17:53:45 +0300 Subject: [PATCH] Implement UArray.contains(element) workaround for JS --- generators/builtins/unsignedTypes.kt | 8 +++++++- libraries/stdlib/unsigned/src/kotlin/UByteArray.kt | 8 +++++++- libraries/stdlib/unsigned/src/kotlin/UIntArray.kt | 8 +++++++- libraries/stdlib/unsigned/src/kotlin/ULongArray.kt | 8 +++++++- libraries/stdlib/unsigned/src/kotlin/UShortArray.kt | 8 +++++++- 5 files changed, 35 insertions(+), 5 deletions(-) diff --git a/generators/builtins/unsignedTypes.kt b/generators/builtins/unsignedTypes.kt index 857bec755c9..27b23c016f5 100644 --- a/generators/builtins/unsignedTypes.kt +++ b/generators/builtins/unsignedTypes.kt @@ -351,7 +351,13 @@ class UnsignedArrayGenerator(val type: UnsignedType, out: PrintWriter) : BuiltIn override fun next$elementType() = if (index < array.size) array[index++].to$elementType() else throw NoSuchElementException(index.toString()) } - override fun contains(element: $elementType): Boolean = storage.contains(element.to$storageElementType()) + override fun contains(element: $elementType): Boolean { + // TODO: Eliminate this check after KT-30016 gets fixed. + // Currently JS BE does not generate special bridge method for this method. + if ((element as Any?) !is $elementType) return false + + return storage.contains(element.to$storageElementType()) + } override fun containsAll(elements: Collection<$elementType>): Boolean { if ((elements as Collection).any { it as? $elementType == null }) return false diff --git a/libraries/stdlib/unsigned/src/kotlin/UByteArray.kt b/libraries/stdlib/unsigned/src/kotlin/UByteArray.kt index 2973b72ebf7..dbc8cac0ff3 100644 --- a/libraries/stdlib/unsigned/src/kotlin/UByteArray.kt +++ b/libraries/stdlib/unsigned/src/kotlin/UByteArray.kt @@ -37,7 +37,13 @@ internal constructor(@PublishedApi internal val storage: ByteArray) : Collection override fun nextUByte() = if (index < array.size) array[index++].toUByte() else throw NoSuchElementException(index.toString()) } - override fun contains(element: UByte): Boolean = storage.contains(element.toByte()) + override fun contains(element: UByte): Boolean { + // TODO: Eliminate this check after KT-30016 gets fixed. + // Currently JS BE does not generate special bridge method for this method. + if ((element as Any?) !is UByte) return false + + return storage.contains(element.toByte()) + } override fun containsAll(elements: Collection): Boolean { if ((elements as Collection).any { it as? UByte == null }) return false diff --git a/libraries/stdlib/unsigned/src/kotlin/UIntArray.kt b/libraries/stdlib/unsigned/src/kotlin/UIntArray.kt index ad5c2bb56d6..646189c325f 100644 --- a/libraries/stdlib/unsigned/src/kotlin/UIntArray.kt +++ b/libraries/stdlib/unsigned/src/kotlin/UIntArray.kt @@ -37,7 +37,13 @@ internal constructor(@PublishedApi internal val storage: IntArray) : Collection< override fun nextUInt() = if (index < array.size) array[index++].toUInt() else throw NoSuchElementException(index.toString()) } - override fun contains(element: UInt): Boolean = storage.contains(element.toInt()) + override fun contains(element: UInt): Boolean { + // TODO: Eliminate this check after KT-30016 gets fixed. + // Currently JS BE does not generate special bridge method for this method. + if ((element as Any?) !is UInt) return false + + return storage.contains(element.toInt()) + } override fun containsAll(elements: Collection): Boolean { if ((elements as Collection).any { it as? UInt == null }) return false diff --git a/libraries/stdlib/unsigned/src/kotlin/ULongArray.kt b/libraries/stdlib/unsigned/src/kotlin/ULongArray.kt index 29b0cd646ac..d98e8968730 100644 --- a/libraries/stdlib/unsigned/src/kotlin/ULongArray.kt +++ b/libraries/stdlib/unsigned/src/kotlin/ULongArray.kt @@ -37,7 +37,13 @@ internal constructor(@PublishedApi internal val storage: LongArray) : Collection override fun nextULong() = if (index < array.size) array[index++].toULong() else throw NoSuchElementException(index.toString()) } - override fun contains(element: ULong): Boolean = storage.contains(element.toLong()) + override fun contains(element: ULong): Boolean { + // TODO: Eliminate this check after KT-30016 gets fixed. + // Currently JS BE does not generate special bridge method for this method. + if ((element as Any?) !is ULong) return false + + return storage.contains(element.toLong()) + } override fun containsAll(elements: Collection): Boolean { if ((elements as Collection).any { it as? ULong == null }) return false diff --git a/libraries/stdlib/unsigned/src/kotlin/UShortArray.kt b/libraries/stdlib/unsigned/src/kotlin/UShortArray.kt index cd6622f458e..8f2a0264dc5 100644 --- a/libraries/stdlib/unsigned/src/kotlin/UShortArray.kt +++ b/libraries/stdlib/unsigned/src/kotlin/UShortArray.kt @@ -37,7 +37,13 @@ internal constructor(@PublishedApi internal val storage: ShortArray) : Collectio override fun nextUShort() = if (index < array.size) array[index++].toUShort() else throw NoSuchElementException(index.toString()) } - override fun contains(element: UShort): Boolean = storage.contains(element.toShort()) + override fun contains(element: UShort): Boolean { + // TODO: Eliminate this check after KT-30016 gets fixed. + // Currently JS BE does not generate special bridge method for this method. + if ((element as Any?) !is UShort) return false + + return storage.contains(element.toShort()) + } override fun containsAll(elements: Collection): Boolean { if ((elements as Collection).any { it as? UShort == null }) return false