From 7668f446555e97cd6e7f157ab0e8e74ccb0a7068 Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Mon, 29 Dec 2014 14:28:41 +0300 Subject: [PATCH] JS lib: added hasNext to Kotlin.ListIterator and fixed next. #KT-6506 Fixed Additionally made abstract AbstractList::get and AbstractCollection::size. --- js/js.libraries/src/core/javautil.kt | 15 ++++++--- .../k2js/test/semantics/AbstractListTest.java | 28 ++++++++++++++++ .../java/abstractList/cases/iterator.kt | 32 +++++++++++++++++++ js/js.translator/testData/kotlin_lib.js | 5 ++- 4 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 js/js.tests/test/org/jetbrains/k2js/test/semantics/AbstractListTest.java create mode 100644 js/js.translator/testData/java/abstractList/cases/iterator.kt diff --git a/js/js.libraries/src/core/javautil.kt b/js/js.libraries/src/core/javautil.kt index bc01dda3cca..6e32b6ae569 100644 --- a/js/js.libraries/src/core/javautil.kt +++ b/js/js.libraries/src/core/javautil.kt @@ -26,7 +26,7 @@ public abstract class AbstractCollection() : MutableCollection { override fun retainAll(c: Collection): Boolean = noImpl override fun clear(): Unit = noImpl - override fun size(): Int = noImpl + abstract override fun size(): Int override fun hashCode(): Int = noImpl override fun equals(other: Any?): Boolean = noImpl @@ -34,7 +34,7 @@ public abstract class AbstractCollection() : MutableCollection { library public abstract class AbstractList() : AbstractCollection(), MutableList { - override fun get(index: Int): E = noImpl + abstract override fun get(index: Int): E override fun set(index: Int, element: E): E = noImpl override fun add(e: E): Boolean = noImpl @@ -60,6 +60,8 @@ public abstract class AbstractList() : AbstractCollection(), MutableList(capacity: Int = 0) : AbstractList() { + override fun get(index: Int): E = noImpl + override fun size(): Int = noImpl } library @@ -78,7 +80,9 @@ public open class LinkedList() : AbstractList() { library public open class HashSet( initialCapacity: Int = DEFAULT_INITIAL_CAPACITY, loadFactor: Float = DEFAULT_LOAD_FACTOR -) : AbstractCollection(), MutableSet +) : AbstractCollection(), MutableSet { + override fun size(): Int = noImpl +} library public trait SortedSet : Set { @@ -86,12 +90,15 @@ public trait SortedSet : Set { library public open class TreeSet() : AbstractCollection(), MutableSet, SortedSet { + override fun size(): Int = noImpl } library public open class LinkedHashSet( initialCapacity: Int = DEFAULT_INITIAL_CAPACITY, loadFactor: Float = DEFAULT_LOAD_FACTOR -) : HashSet(initialCapacity, loadFactor), MutableSet +) : HashSet(initialCapacity, loadFactor), MutableSet { + override fun size(): Int = noImpl +} library public open class HashMap(initialCapacity: Int = DEFAULT_INITIAL_CAPACITY, loadFactor: Float = DEFAULT_LOAD_FACTOR) : MutableMap { diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/AbstractListTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/AbstractListTest.java new file mode 100644 index 00000000000..200039a5323 --- /dev/null +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/AbstractListTest.java @@ -0,0 +1,28 @@ +/* + * Copyright 2010-2014 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 org.jetbrains.k2js.test.semantics; + +public final class AbstractListTest extends JavaClassesTest { + + public AbstractListTest() { + super("abstractList/"); + } + + public void testIterator() throws Exception { + checkFooBoxIsOk(); + } +} diff --git a/js/js.translator/testData/java/abstractList/cases/iterator.kt b/js/js.translator/testData/java/abstractList/cases/iterator.kt new file mode 100644 index 00000000000..e7de9083a08 --- /dev/null +++ b/js/js.translator/testData/java/abstractList/cases/iterator.kt @@ -0,0 +1,32 @@ +package foo + +import java.util.AbstractList + +class MyList(vararg val data: T) : AbstractList() { + override fun get(index: Int) = data[index] + override fun size() = data.size() +} + +fun test(expected: String, list: List) { + var s = "" + for (e in list) { + s += "$e," + } + assertEquals(expected, s) + + val it = list.iterator() + s = "" + while (it.hasNext()) { + val e = it.next() + s += "$e," + } + assertEquals(expected, s) +} + +fun box(): String { + + test("32,12,23,444,", MyList(32, 12, 23, 444)) + test("", MyList()) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/kotlin_lib.js b/js/js.translator/testData/kotlin_lib.js index c244dee9deb..c3e4f3b1d0d 100644 --- a/js/js.translator/testData/kotlin_lib.js +++ b/js/js.translator/testData/kotlin_lib.js @@ -276,8 +276,11 @@ this.size = list.size(); this.index = 0; }, { + hasNext: function () { + return this.index < this.size; + }, next: function () { - return this.list.get(this.index++); + return this.list.get_za3lpa$(this.index++); } });