diff --git a/js/js.libraries/src/core/collections.kt b/js/js.libraries/src/core/collections.kt index 3e8e5b36c97..6ee2d2c25e4 100644 --- a/js/js.libraries/src/core/collections.kt +++ b/js/js.libraries/src/core/collections.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2015 JetBrains s.r.o. + * Copyright 2010-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,12 @@ package kotlin.collections public fun Collection.toTypedArray(): Array = noImpl +@library("copyToArrayImpl") +internal fun copyToArrayImpl(collection: Collection<*>): Array = noImpl + +@library("arrayToString") +internal fun arrayToString(array: Array<*>): String = noImpl + /** * Returns an immutable list containing only the specified object [element]. */ diff --git a/js/js.libraries/src/core/collections/AbstractCollection.kt b/js/js.libraries/src/core/collections/AbstractCollection.kt new file mode 100644 index 00000000000..db620255d77 --- /dev/null +++ b/js/js.libraries/src/core/collections/AbstractCollection.kt @@ -0,0 +1,81 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package kotlin.collections + + +public abstract class AbstractCollection protected constructor() : MutableCollection { + + abstract override val size: Int + abstract override fun iterator(): MutableIterator + + override fun add(element: E): Boolean = throw UnsupportedOperationException() + + override fun remove(element: E): Boolean { + val iterator = iterator() + while (iterator.hasNext()) { + if (iterator.next() == element) { + iterator.remove() + return true + } + } + return false + } + + override fun isEmpty(): Boolean = size == 0 + + override fun contains(element: E): Boolean { + for (e in this) { + if (e == element) return true + } + return false + } + + override fun containsAll(elements: Collection): Boolean = elements.all { contains(it) } + + + override fun addAll(elements: Collection): Boolean { + var modified = false + for (element in elements) { + if (add(element)) modified = true + } + return modified + } + + override fun removeAll(elements: Collection): Boolean = (this as MutableIterable).removeAll { it in elements } + override fun retainAll(elements: Collection): Boolean = (this as MutableIterable).removeAll { it !in elements } + + override fun clear(): Unit { + val iterator = this.iterator() + while (iterator.hasNext()) { + iterator.next() + iterator.remove() + } + } + + abstract override fun hashCode(): Int + + abstract override fun equals(other: Any?): Boolean + + override fun toString(): String = joinToString(", ", "[", "]") { + if (it === this) "(this Collection)" else it.toString() + } + + protected open fun toArray(): Array = copyToArrayImpl(this) + + open fun toJSON(): Any = this.toArray() +} + diff --git a/js/js.libraries/src/core/collections/AbstractList.kt b/js/js.libraries/src/core/collections/AbstractList.kt new file mode 100644 index 00000000000..1332e4df1aa --- /dev/null +++ b/js/js.libraries/src/core/collections/AbstractList.kt @@ -0,0 +1,248 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * Based on GWT AbstractList + * Copyright 2007 Google Inc. +*/ + + +package kotlin.collections + +public abstract class AbstractList protected constructor() : AbstractCollection(), MutableList { + abstract override val size: Int + abstract override fun get(index: Int): E + + protected var modCount: Int = 0 + + override fun add(index: Int, element: E): Unit = throw UnsupportedOperationException("Add not supported on this list") + override fun removeAt(index: Int): E = throw UnsupportedOperationException("Remove not supported on this list") + override fun set(index: Int, element: E): E = throw UnsupportedOperationException("Set not supported on this list") + + override fun add(element: E): Boolean { + add(size, element) + return true + } + + override fun addAll(index: Int, elements: Collection): Boolean { + var index = index + var changed = false + for (e in elements) { + add(index++, e) + changed = true + } + return changed + } + + override fun clear() { + removeRange(0, size) + } + + + override fun iterator(): MutableIterator = IteratorImpl() + + override fun indexOf(element: E): Int { + for (i in 0..lastIndex) { + if (get(i) == element) { + return i + } + } + return -1 + } + + override fun lastIndexOf(element: E): Int { + for (i in lastIndex downTo 0) { + if (get(i) == element) { + return i + } + } + return -1 + } + + override fun listIterator(): MutableListIterator = listIterator(0) + override fun listIterator(from: Int): MutableListIterator = ListIteratorImpl(from) + + + override fun subList(fromIndex: Int, toIndex: Int): MutableList = SubList(this, fromIndex, toIndex) + + protected open fun removeRange(fromIndex: Int, endIndex: Int) { + val iterator = listIterator(fromIndex) + for (i in fromIndex..endIndex - 1) { + iterator.next() + iterator.remove() + } + } + + override fun equals(other: Any?): Boolean { + if (other === this) return true + if (other !is List<*>) return false + if (size != other.size) return false + + val otherIterator = other.iterator() + for (elem in this) { + val elemOther = otherIterator.next() + if (elem != elemOther) { + return false + } + } + + return true + } + + override fun hashCode(): Int { + return Collections_hashCode(this) + } + + + private open inner class IteratorImpl : MutableIterator { + /* + * i is the index of the item that will be returned on the next call to + * next() last is the index of the item that was returned on the previous + * call to next() or previous (for ListIterator), -1 if no such item exists. + */ + + internal var i = 0 + internal var last = -1 + + override fun hasNext(): Boolean = i < size + + override fun next(): E { + if (!hasNext()) throw NoSuchElementException() + last = i++ + return get(last) + } + + override fun remove() { + check(last != -1) + + removeAt(last) + i = last + last = -1 + } + } + + /** + * Implementation of `ListIterator` for abstract lists. + */ + private inner class ListIteratorImpl(start: Int) : IteratorImpl(), MutableListIterator { + /* + * i is the index of the item that will be returned on the next call to + * next() last is the index of the item that was returned on the previous + * call to next() or previous (for ListIterator), -1 if no such item exists. + */ + + init { + checkPositionIndex(start, this@AbstractList.size) + + i = start + } + + override fun add(o: E) { + add(i, o) + i++ + last = -1 + } + + override fun hasPrevious(): Boolean = i > 0 + + override fun nextIndex(): Int = i + + override fun previous(): E { + if (!hasPrevious()) throw NoSuchElementException() + + last = --i + return get(last) + } + + override fun previousIndex(): Int = i - 1 + + override fun set(o: E) { + require(last != -1) + + this@AbstractList[last] = o + } + } + + private class SubList(private val wrapped: AbstractList, private val fromIndex: Int, toIndex: Int) : AbstractList() { + private var _size: Int = 0 + + init { + checkCriticalPositionIndexes(fromIndex, toIndex, wrapped.size) + this._size = toIndex - fromIndex + } + + override fun add(index: Int, element: E) { + checkPositionIndex(index, _size) + + wrapped.add(fromIndex + index, element) + _size++ + } + + override fun get(index: Int): E { + checkElementIndex(index, _size) + + return wrapped[fromIndex + index] + } + + override fun removeAt(index: Int): E { + checkElementIndex(index, _size) + + val result = wrapped.removeAt(fromIndex + index) + _size-- + return result + } + + override fun set(index: Int, element: E): E { + checkElementIndex(index, _size) + + return wrapped.set(fromIndex + index, element) + } + + override val size: Int get() = _size + } + + companion object { + internal fun checkElementIndex(index: Int, size: Int) { + if (index < 0 || index >= size) { + throw IndexOutOfBoundsException("Index: $index, Size: $size") + } + } + + internal fun checkPositionIndex(index: Int, size: Int) { + if (index < 0 || index > size) { + throw IndexOutOfBoundsException("Index: $index, Size: $size") + } + } + + internal fun checkCriticalPositionIndexes(start: Int, end: Int, size: Int) { + if (start < 0 || end > size) { + throw IndexOutOfBoundsException("fromIndex: $start, toIndex: $end, size: $size") + } + if (start > end) { + throw IllegalArgumentException("fromIndex: $start > toIndex: $end") + } + } + + internal fun Collections_hashCode(list: List): Int { + var hashCode = 1 + for (e in list) { + hashCode = 31 * hashCode + (e?.hashCode() ?: 0) + hashCode = hashCode or 0 // make sure we don't overflow + } + return hashCode + } + } + +} diff --git a/js/js.libraries/src/core/collections/ArrayList.kt b/js/js.libraries/src/core/collections/ArrayList.kt new file mode 100644 index 00000000000..42b53ad46b7 --- /dev/null +++ b/js/js.libraries/src/core/collections/ArrayList.kt @@ -0,0 +1,112 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package kotlin.collections + +//TODO: should be JS Array-like (https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Predefined_Core_Objects#Working_with_Array-like_objects) + +public open class ArrayList internal constructor(private var array: Array) : AbstractList(), RandomAccess { + + public constructor(capacity: Int = 0) : this(emptyArray()) {} + public constructor(elements: Collection) : this(elements.toTypedArray()) {} + + + override val size: Int get() = array.size + override fun get(index: Int): E = array[rangeCheck(index)] as E + override fun set(index: Int, element: E): E { + rangeCheck(index) + return array[index].apply { array[index] = element } as E + } + + override fun add(element: E): Boolean { + array.asDynamic().push(element) + modCount++ + return true + } + + override fun add(index: Int, element: E): Unit { + array.asDynamic().splice(insertionRangeCheck(index), 0, element) + modCount++ + } + + override fun addAll(elements: Collection): Boolean { + if (elements.isEmpty()) return false + + array += elements.toTypedArray() + modCount++ + return true + } + + override fun addAll(index: Int, elements: Collection): Boolean { + insertionRangeCheck(index) + + if (index == size) return addAll(elements) + if (elements.isEmpty()) return false + + array = array.copyOfRange(0, index) + elements.toTypedArray() + array.copyOfRange(index, size) + modCount++ + return true + } + + override fun removeAt(index: Int): E { + rangeCheck(index) + modCount++ + return if (index == size) + array.asDynamic().pop() + else + array.asDynamic().splice(index, 1)[0] + } + + override fun remove(element: E): Boolean { + for (index in array.indices) { + if (array[index] == element) { + array.asDynamic().splice(index, 1) + modCount++ + return true + } + } + return false + } + + override fun removeRange(fromIndex: Int, toIndex: Int) { + modCount++ + array.asDynamic().splice(fromIndex, toIndex - fromIndex) + } + + override fun clear() { + array = emptyArray() + modCount++ + } + + override fun contains(element: E): Boolean = indexOf(element) >= 0 + + override fun indexOf(element: E): Int = array.indexOf(element) + + override fun lastIndexOf(element: E): Int = array.lastIndexOf(element) + + override fun toString() = arrayToString(array) + override fun toArray(): Array = array.copyOf() + + private fun rangeCheck(index: Int) = index.apply { + if (index !in array.indices) + throw IndexOutOfBoundsException("Index: $index, Size: $size") + } + + private fun insertionRangeCheck(index: Int) = index.apply { + if (index !in 0..size) + throw IndexOutOfBoundsException("Index: $index, Size: $size") + } +} \ No newline at end of file diff --git a/js/js.libraries/src/core/collections/RandomAccess.kt b/js/js.libraries/src/core/collections/RandomAccess.kt new file mode 100644 index 00000000000..a94d131b63d --- /dev/null +++ b/js/js.libraries/src/core/collections/RandomAccess.kt @@ -0,0 +1,19 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package kotlin.collections + +public interface RandomAccess \ No newline at end of file diff --git a/js/js.libraries/src/core/exceptions.kt b/js/js.libraries/src/core/exceptions.kt index 4e4f15ce346..9a522357cc5 100644 --- a/js/js.libraries/src/core/exceptions.kt +++ b/js/js.libraries/src/core/exceptions.kt @@ -34,6 +34,9 @@ public open class IllegalStateException(message: String? = null) : RuntimeExcept @library public open class IndexOutOfBoundsException(message: String? = null) : RuntimeException(message) {} +@library +public open class ConcurrentModificationException(message: String? = null) : RuntimeException(message) {} + @library public open class UnsupportedOperationException(message: String? = null) : RuntimeException(message) {} diff --git a/js/js.libraries/src/core/javautil.kt b/js/js.libraries/src/core/javautil.kt index 2f1555f3d87..732d029f306 100644 --- a/js/js.libraries/src/core/javautil.kt +++ b/js/js.libraries/src/core/javautil.kt @@ -31,67 +31,24 @@ 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 - override fun contains(o: E): Boolean = noImpl - override fun iterator(): MutableIterator = noImpl +// in lack of type aliases +/* +public abstract class AbstractCollection : kotlin.collections.AbstractCollection() +public abstract class AbstractList : kotlin.collections.AbstractList() +public open class ArrayList(capacity: Int = 0) : kotlin.collections.ArrayList(capacity) +*/ - override fun add(e: E): Boolean = noImpl - override fun remove(o: E): Boolean = noImpl - - override fun addAll(c: Collection): Boolean = noImpl - override fun containsAll(c: Collection): Boolean = noImpl - override fun removeAll(c: Collection): Boolean = noImpl - override fun retainAll(c: Collection): Boolean = noImpl - - override fun clear(): Unit = noImpl - abstract override val size: Int - - override fun hashCode(): Int = noImpl - override fun equals(other: Any?): Boolean = noImpl -} - -@library -public abstract class AbstractList() : AbstractCollection(), MutableList { - abstract override fun get(index: Int): E - override fun set(index: Int, element: E): E = noImpl - - override fun add(e: E): Boolean = noImpl - override fun add(index: Int, element: E): Unit = noImpl - override fun addAll(index: Int, c: Collection): Boolean = noImpl - - override fun removeAt(index: Int): E = noImpl - - override fun indexOf(o: E): Int = noImpl - override fun lastIndexOf(o: E): Int = noImpl - - override fun listIterator(): MutableListIterator = noImpl - override fun listIterator(index: Int): MutableListIterator = noImpl - - override fun subList(fromIndex: Int, toIndex: Int): MutableList = noImpl - - abstract override val size: Int - - override fun equals(other: Any?): Boolean = noImpl - - override fun toString(): String = noImpl -} - -@library -public open class ArrayList(capacity: Int = 0) : AbstractList(), RandomAccess { - override fun get(index: Int): E = noImpl - override val size: Int get() = noImpl -} @library public open class HashSet( initialCapacity: Int = DEFAULT_INITIAL_CAPACITY, loadFactor: Float = DEFAULT_LOAD_FACTOR ) : AbstractCollection(), MutableSet { + override fun iterator(): MutableIterator = noImpl override val size: Int get() = noImpl + override fun equals(other: Any?): Boolean = noImpl + + override fun hashCode(): Int = noImpl } @library diff --git a/js/js.libraries/src/core/javautilCode.kt b/js/js.libraries/src/core/javautilCode.kt index 634afc4083f..f7ee0d7e84d 100644 --- a/js/js.libraries/src/core/javautilCode.kt +++ b/js/js.libraries/src/core/javautilCode.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2014 JetBrains s.r.o. + * Copyright 2010-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,6 +28,3 @@ public fun HashMap(m: Map): HashMap public fun LinkedHashMap(m: Map): LinkedHashMap = LinkedHashMap(m.size).apply { putAll(m) } -public fun ArrayList(c: Collection): ArrayList - = ArrayList().apply { asDynamic().array = c.toTypedArray() } // black dynamic magic - diff --git a/js/js.libraries/src/core/kotlin_special.kt b/js/js.libraries/src/core/kotlin_special.kt index 9ed756d76f9..b19dac237a8 100644 --- a/js/js.libraries/src/core/kotlin_special.kt +++ b/js/js.libraries/src/core/kotlin_special.kt @@ -13,9 +13,7 @@ import java.util.Collections // TODO: it's temporary while we have java.util.Col * Returns a [List] that wraps the original array. */ public fun Array.asList(): List { - val al = ArrayList() - al.asDynamic().array = this // black dynamic magic - return al + return ArrayList(this as Array) } /** diff --git a/js/js.libraries/src/core/regex.kt b/js/js.libraries/src/core/regex.kt index 031a6050fa0..b5a0681e2f8 100644 --- a/js/js.libraries/src/core/regex.kt +++ b/js/js.libraries/src/core/regex.kt @@ -17,7 +17,7 @@ package kotlin.text import kotlin.text.js.* -import java.util.ArrayList +import java.util.* /** * Provides enumeration values to use to set regular expression options. @@ -149,7 +149,7 @@ public class Regex(pattern: String, options: Set) { public fun split(input: CharSequence, limit: Int = 0): List { require(limit >= 0) { "Limit must be non-negative, but was $limit" } val matches = findAll(input).let { if (limit == 0) it else it.take(limit - 1) } - val result = ArrayList() + val result = mutableListOf() var lastStart = 0 for (match in matches) { @@ -216,7 +216,7 @@ private fun RegExp.findNext(input: String, from: Int): MatchResult? { override val groupValues: List get() { if (groupValues_ == null) { - groupValues_ = object : java.util.AbstractList() { + groupValues_ = object : AbstractList() { override val size: Int get() = match.length override fun get(index: Int): String = match[index] ?: "" } diff --git a/js/js.translator/testData/kotlin_lib.js b/js/js.translator/testData/kotlin_lib.js index 6cd1906cf3a..75a453236f3 100644 --- a/js/js.translator/testData/kotlin_lib.js +++ b/js/js.translator/testData/kotlin_lib.js @@ -210,6 +210,7 @@ Kotlin.IllegalStateException = createClassNowWithMessage(Kotlin.RuntimeException); Kotlin.UnsupportedOperationException = createClassNowWithMessage(Kotlin.RuntimeException); Kotlin.IndexOutOfBoundsException = createClassNowWithMessage(Kotlin.RuntimeException); + Kotlin.ConcurrentModificationException = createClassNowWithMessage(Kotlin.RuntimeException); Kotlin.ClassCastException = createClassNowWithMessage(Kotlin.RuntimeException); Kotlin.IOException = createClassNowWithMessage(Kotlin.Exception); Kotlin.AssertionError = createClassNowWithMessage(Kotlin.Error); @@ -292,50 +293,6 @@ } }); - /** - * @class - * @extends {ArrayIterator.} - * - * @constructor - * @param {Kotlin.AbstractList.} list - * @template T - */ - lazyInitClasses.ListIterator = Kotlin.createClass( - function () { - return [Kotlin.kotlin.collections.ListIterator]; // TODO: MutableListIterator - }, - /** @constructs */ - function (list, index) { - this.list = list; - this.size = list.size; - this.index = (index === undefined) ? 0 : index; - }, { - hasNext: function () { - return this.index < this.size; - }, - nextIndex: function () { - return this.index; - }, - next: function () { - var index = this.index; - var result = this.list.get_za3lpa$(index); - this.index = index + 1; - return result; - }, - hasPrevious: function() { - return this.index > 0; - }, - previousIndex: function () { - return this.index - 1; - }, - previous: function () { - var index = this.index - 1; - var result = this.list.get_za3lpa$(index); - this.index = index; - return result; - } - }); - lazyInitClasses.Enum = Kotlin.createClass( function() { return [Kotlin.Comparable]; @@ -369,300 +326,12 @@ } ); - Kotlin.RandomAccess = Kotlin.createTraitNow(null); - Kotlin.PropertyMetadata = Kotlin.createClassNow(null, function (name) { this.name = name; } ); - lazyInitClasses.AbstractCollection = Kotlin.createClass( - function () { - return [Kotlin.kotlin.collections.MutableCollection]; - }, null, { - addAll_wtfk93$: function (collection) { - var modified = false; - var it = collection.iterator(); - while (it.hasNext()) { - if (this.add_za3rmp$(it.next())) { - modified = true; - } - } - return modified - }, - removeAll_wtfk93$: function (c) { - var modified = false; - var it = this.iterator(); - while (it.hasNext()) { - if (c.contains_za3rmp$(it.next())) { - it.remove(); - modified = true; - } - } - return modified - }, - retainAll_wtfk93$: function (c) { - var modified = false; - var it = this.iterator(); - while (it.hasNext()) { - if (!c.contains_za3rmp$(it.next())) { - it.remove(); - modified = true; - } - } - return modified - }, - clear: function () { - // TODO: implement with mutable iterator - throw new Kotlin.NotImplementedError("Not implemented yet, see KT-7809"); - }, - containsAll_wtfk93$: function (c) { - var it = c.iterator(); - while (it.hasNext()) { - if (!this.contains_za3rmp$(it.next())) return false; - } - return true; - }, - isEmpty: function () { - return this.size === 0; - }, - iterator: function () { - // TODO: Do not implement mutable iterator() this way, make abstract - return new Kotlin.ArrayIterator(this.toArray()); - }, - equals_za3rmp$: function (o) { - if (this.size !== o.size) return false; - - var iterator1 = this.iterator(); - var iterator2 = o.iterator(); - var i = this.size; - while (i-- > 0) { - if (!Kotlin.equals(iterator1.next(), iterator2.next())) { - return false; - } - } - - return true; - }, - toString: function () { - var builder = "["; - var iterator = this.iterator(); - var first = true; - var i = this.size; - while (i-- > 0) { - if (first) { - first = false; - } - else { - builder += ", "; - } - builder += Kotlin.toString(iterator.next()); - } - builder += "]"; - return builder; - }, - toJSON: function () { - return this.toArray(); - } - }); - - /** - * @interface // actually it's abstract class - * @template T - */ - lazyInitClasses.AbstractList = Kotlin.createClass( - function () { - return [Kotlin.kotlin.collections.MutableList, Kotlin.AbstractCollection]; - }, null, { - iterator: function () { - return new Kotlin.ListIterator(this); - }, - listIterator: function() { - return new Kotlin.ListIterator(this); - }, - listIterator_za3lpa$: function(index) { - if (index < 0 || index > this.size) { - throw new Kotlin.IndexOutOfBoundsException("Index: " + index + ", size: " + this.size); - } - return new Kotlin.ListIterator(this, index); - }, - add_za3rmp$: function (element) { - this.add_vux3hl$(this.size, element); - return true; - }, - addAll_j97iir$: function (index, collection) { - // TODO: implement - throw new Kotlin.NotImplementedError("Not implemented yet, see KT-7809"); - }, - remove_za3rmp$: function (o) { - var index = this.indexOf_za3rmp$(o); - if (index !== -1) { - this.removeAt_za3lpa$(index); - return true; - } - return false; - }, - clear: function () { - // TODO: implement with remove range - throw new Kotlin.NotImplementedError("Not implemented yet, see KT-7809"); - }, - contains_za3rmp$: function (o) { - return this.indexOf_za3rmp$(o) !== -1; - }, - indexOf_za3rmp$: function (o) { - var i = this.listIterator(); - while (i.hasNext()) - if (Kotlin.equals(i.next(), o)) - return i.previousIndex(); - return -1; - }, - lastIndexOf_za3rmp$: function (o) { - var i = this.listIterator_za3lpa$(this.size); - while (i.hasPrevious()) - if (Kotlin.equals(i.previous(), o)) - return i.nextIndex(); - return -1; - }, - subList_vux9f0$: function(fromIndex, toIndex) { - if (fromIndex < 0 || toIndex > this.size) - throw new Kotlin.IndexOutOfBoundsException(); - if (fromIndex > toIndex) - throw new Kotlin.IllegalArgumentException(); - return new Kotlin.SubList(this, fromIndex, toIndex); - }, - hashCode: function() { - var result = 1; - var i = this.iterator(); - while (i.hasNext()) { - var obj = i.next(); - result = (31*result + Kotlin.hashCode(obj)) | 0; - } - return result; - } - }); - - lazyInitClasses.SubList = Kotlin.createClass( - function () { - return [Kotlin.AbstractList]; - }, - function (list, fromIndex, toIndex) { - this.list = list; - this.offset = fromIndex; - this._size = toIndex - fromIndex; - }, { - get_za3lpa$: function (index) { - this.checkRange(index); - return this.list.get_za3lpa$(index + this.offset); - }, - set_vux3hl$: function (index, value) { - this.checkRange(index); - this.list.set_vux3hl$(index + this.offset, value); - }, - size: { - get: function () { - return this._size; - } - }, - add_vux3hl$: function (index, element) { - if (index < 0 || index > this.size) { - throw new Kotlin.IndexOutOfBoundsException(); - } - this.list.add_vux3hl$(index + this.offset, element); - }, - removeAt_za3lpa$: function (index) { - this.checkRange(index); - var result = this.list.removeAt_za3lpa$(index + this.offset); - this._size--; - return result; - - }, - checkRange: function (index) { - if (index < 0 || index >= this._size) { - throw new Kotlin.IndexOutOfBoundsException(); - } - } - }); - - //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, Kotlin.RandomAccess]; - }, - function () { - this.array = []; - }, { - get_za3lpa$: function (index) { - this.checkRange(index); - return this.array[index]; - }, - set_vux3hl$: function (index, value) { - this.checkRange(index); - this.array[index] = value; - }, - size: { - get: function () { - return this.array.length; - } - }, - iterator: function () { - return Kotlin.arrayIterator(this.array); - }, - add_za3rmp$: function (element) { - this.array.push(element); - return true; - }, - add_vux3hl$: function (index, element) { - this.array.splice(index, 0, element); - }, - addAll_wtfk93$: function (collection) { - if (collection.size == 0) { - return false; - } - var it = collection.iterator(); - for (var i = this.array.length, n = collection.size; n-- > 0;) { - this.array[i++] = it.next(); - } - return true; - }, - removeAt_za3lpa$: function (index) { - this.checkRange(index); - return this.array.splice(index, 1)[0]; - }, - clear: function () { - this.array.length = 0; - }, - indexOf_za3rmp$: function (o) { - for (var i = 0; i < this.array.length; i++) { - if (Kotlin.equals(this.array[i], o)) { - return i; - } - } - return -1; - }, - lastIndexOf_za3rmp$: function (o) { - for (var i = this.array.length - 1; i >= 0; i--) { - if (Kotlin.equals(this.array[i], o)) { - return i; - } - } - return -1; - }, - toArray: function () { - return this.array.slice(0); - }, - toString: function () { - return Kotlin.arrayToString(this.array); - }, - toJSON: function () { - return this.array; - }, - checkRange: function (index) { - if (index < 0 || index >= this.array.length) { - throw new Kotlin.IndexOutOfBoundsException(); - } - } - }); Kotlin.Runnable = Kotlin.createTraitNow(null, null, { run: throwAbstractFunctionInvocationError("Runnable#run") @@ -1132,9 +801,13 @@ array.sort(Kotlin.primitiveCompareTo) }; + // TODO: Find out whether is it referenced 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()) { diff --git a/js/js.translator/testData/maps.js b/js/js.translator/testData/maps.js index b5d0c17ad2f..fe1dec91e29 100644 --- a/js/js.translator/testData/maps.js +++ b/js/js.translator/testData/maps.js @@ -382,12 +382,7 @@ Object.defineProperty(Hashtable.prototype, "values", { get: function () { var values = this._values(); - var i = values.length; - var result = new Kotlin.ArrayList(); - while (i--) { - result.add_za3rmp$(values[i]); - } - return result; + return Kotlin.kotlin.collections.asReversed_sqtfhv$(new Kotlin.kotlin.collections.ArrayList(values)); }, configurable: true }); @@ -570,7 +565,7 @@ */ lazyInitClasses.PrimitiveHashMapValues = Kotlin.createClass( function () { - return [Kotlin.AbstractCollection]; + return [Kotlin.kotlin.collections.AbstractCollection]; }, function (map) { this.map = map; @@ -1013,10 +1008,10 @@ function HashSet(hashingFunction, equalityFunction) { var hashTable = new Kotlin.HashTable(hashingFunction, equalityFunction); - this.addAll_wtfk93$ = Kotlin.AbstractCollection.prototype.addAll_wtfk93$; - this.removeAll_wtfk93$ = Kotlin.AbstractCollection.prototype.removeAll_wtfk93$; - this.retainAll_wtfk93$ = Kotlin.AbstractCollection.prototype.retainAll_wtfk93$; - this.containsAll_wtfk93$ = Kotlin.AbstractCollection.prototype.containsAll_wtfk93$; + this.addAll_wtfk93$ = Kotlin.kotlin.collections.AbstractCollection.prototype.addAll_wtfk93$; + this.removeAll_wtfk93$ = Kotlin.kotlin.collections.AbstractCollection.prototype.removeAll_wtfk93$; + this.retainAll_wtfk93$ = Kotlin.kotlin.collections.AbstractCollection.prototype.retainAll_wtfk93$; + this.containsAll_wtfk93$ = Kotlin.kotlin.collections.AbstractCollection.prototype.containsAll_wtfk93$; this.add_za3rmp$ = function (o) { return !hashTable.put_wn2jw4$(o, true); @@ -1115,7 +1110,7 @@ lazyInitClasses.HashSet = Kotlin.createClass( function () { - return [Kotlin.kotlin.collections.MutableSet, Kotlin.AbstractCollection]; + return [Kotlin.kotlin.collections.MutableSet, Kotlin.kotlin.collections.AbstractCollection]; }, function () { HashSet.call(this); diff --git a/libraries/stdlib/src/kotlin/collections/ReversedViews.kt b/libraries/stdlib/src/kotlin/collections/ReversedViews.kt index 95ac5dd0940..4bc8593dc22 100644 --- a/libraries/stdlib/src/kotlin/collections/ReversedViews.kt +++ b/libraries/stdlib/src/kotlin/collections/ReversedViews.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2015 JetBrains s.r.o. + * Copyright 2010-2016 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +18,7 @@ package kotlin.collections -import java.util.AbstractList +import java.util.* private open class ReversedListReadOnly(protected open val delegate: List) : AbstractList() { override val size: Int get() = delegate.size diff --git a/libraries/stdlib/test/collections/IterableTests.kt b/libraries/stdlib/test/collections/IterableTests.kt index bfc264796c3..3fb8f34de62 100644 --- a/libraries/stdlib/test/collections/IterableTests.kt +++ b/libraries/stdlib/test/collections/IterableTests.kt @@ -17,8 +17,7 @@ package test.collections import org.junit.Test -import java.util.ArrayList -import java.util.LinkedHashSet +import java.util.* import kotlin.test.* fun iterableOf(vararg items: T): Iterable = Iterable { items.iterator() } diff --git a/libraries/stdlib/test/js/JsArrayTest.kt b/libraries/stdlib/test/js/JsArrayTest.kt index c6dd4d9cbc4..c1eecb49a59 100644 --- a/libraries/stdlib/test/js/JsArrayTest.kt +++ b/libraries/stdlib/test/js/JsArrayTest.kt @@ -1,8 +1,24 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package test.collections.js import org.junit.Test as test import kotlin.test.* -import java.util.ArrayList; +import java.util.* class JsArrayTest { diff --git a/libraries/stdlib/test/js/SetJsTest.kt b/libraries/stdlib/test/js/SetJsTest.kt index 0858c4a87f6..21fd4340cb2 100644 --- a/libraries/stdlib/test/js/SetJsTest.kt +++ b/libraries/stdlib/test/js/SetJsTest.kt @@ -4,8 +4,7 @@ import kotlin.test.* import org.junit.Test import test.collections.* import test.collections.behaviors.* -import java.util.HashSet -import java.util.LinkedHashSet +import java.util.* class ComplexSetJsTest : SetJsTest() { // Helper function with generic parameter to force to use ComlpexHashMap diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt index 4eb0bb2aa2f..505ba61d3aa 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJS.kt @@ -1,3 +1,19 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package templates import templates.Family.* @@ -11,9 +27,7 @@ fun specialJS(): List { returns("List") body(ArraysOfObjects) { """ - val al = ArrayList() - al.asDynamic().array = this // black dynamic magic - return al + return ArrayList(this as Array) """ }