diff --git a/js/js.libraries/src/core/javautil.kt b/js/js.libraries/src/core/javautil.kt index dc93338377d..0ebca43e04f 100644 --- a/js/js.libraries/src/core/javautil.kt +++ b/js/js.libraries/src/core/javautil.kt @@ -31,6 +31,9 @@ public inline fun Comparator(crossinline comparison: (T, T) -> Int): Compara override fun compare(obj1: T, obj2: T): Int = comparison(obj1, obj2) } +@library +public interface RandomAccess + @library public abstract class AbstractCollection() : MutableCollection { override fun isEmpty(): Boolean = noImpl @@ -79,7 +82,7 @@ public abstract class AbstractList() : AbstractCollection(), MutableList(capacity: Int = 0) : AbstractList() { +public open class ArrayList(capacity: Int = 0) : AbstractList(), RandomAccess { override fun get(index: Int): E = noImpl override val size: Int get() = noImpl } diff --git a/js/js.translator/testData/kotlin_lib.js b/js/js.translator/testData/kotlin_lib.js index b33f544fbf3..b9989c39e65 100644 --- a/js/js.translator/testData/kotlin_lib.js +++ b/js/js.translator/testData/kotlin_lib.js @@ -350,6 +350,8 @@ } ); + Kotlin.RandomAccess = Kotlin.createTraitNow(null); + Kotlin.PropertyMetadata = Kotlin.createClassNow(null, function (name) { this.name = name; @@ -566,7 +568,7 @@ //TODO: should be JS Array-like (https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Predefined_Core_Objects#Working_with_Array-like_objects) lazyInitClasses.ArrayList = Kotlin.createClass( function () { - return [Kotlin.AbstractList]; + return [Kotlin.AbstractList, Kotlin.RandomAccess]; }, function () { this.array = []; diff --git a/libraries/stdlib/src/kotlin/collections/Collections.kt b/libraries/stdlib/src/kotlin/collections/Collections.kt index 5898eb8400f..6b8d3c43b2d 100644 --- a/libraries/stdlib/src/kotlin/collections/Collections.kt +++ b/libraries/stdlib/src/kotlin/collections/Collections.kt @@ -16,7 +16,7 @@ internal object EmptyIterator : ListIterator { override fun previous(): Nothing = throw NoSuchElementException() } -internal object EmptyList : List, Serializable { +internal object EmptyList : List, Serializable, RandomAccess { override fun equals(other: Any?): Boolean = other is List<*> && other.isEmpty() override fun hashCode(): Int = 1 override fun toString(): String = "[]" diff --git a/libraries/stdlib/test/collections/CollectionTest.kt b/libraries/stdlib/test/collections/CollectionTest.kt index b17b6dc077a..06724638c8e 100644 --- a/libraries/stdlib/test/collections/CollectionTest.kt +++ b/libraries/stdlib/test/collections/CollectionTest.kt @@ -854,4 +854,11 @@ class CollectionTest { // we need toString() inside pattern because of KT-8666 assertEquals("[1, a, null, ${Long.MAX_VALUE.toString()}]", listOf(1, "a", null, Long.MAX_VALUE).toString()) } + + @test fun randomAccess() { + assertTrue(arrayListOf(1) is RandomAccess, "ArrayList is RandomAccess") + assertTrue(listOf(1, 2) is RandomAccess, "Default read-only list implementation is RandomAccess") + assertTrue(listOf(1) is RandomAccess, "Default singleton list is RandomAccess") + assertTrue(emptyList() is RandomAccess, "Empty list is RandomAccess") + } }