diff --git a/js/js.libraries/src/core/javalang.kt b/js/js.libraries/src/core/javalang.kt index 506e980b36d..8d05e318787 100644 --- a/js/js.libraries/src/core/javalang.kt +++ b/js/js.libraries/src/core/javalang.kt @@ -30,6 +30,11 @@ public class UnsupportedOperationException(message: String = "") : Exception() { library public class NumberFormatException(message: String = "") : Exception() {} +library +public trait Runnable { + open fun run() : Unit; +} + public trait Comparable { fun compareTo(that: T): Int } @@ -39,4 +44,3 @@ public trait Appendable { open fun append(csq: CharSequence?, start: Int, end: Int): Appendable? open fun append(c: Char): Appendable? } - diff --git a/js/js.libraries/src/core/javautil.kt b/js/js.libraries/src/core/javautil.kt index 247eaed8ac1..414cd77f702 100644 --- a/js/js.libraries/src/core/javautil.kt +++ b/js/js.libraries/src/core/javautil.kt @@ -244,3 +244,8 @@ public class StringBuilder() : Appendable { library public class NoSuchElementException() : Exception() {} + +public trait Enumeration { + open fun hasMoreElements(): Boolean + open fun nextElement(): E? +} diff --git a/js/js.libraries/src/stdlib/JUMaps.kt b/js/js.libraries/src/stdlib/JUMaps.kt index 366629a8985..2a233b0e747 100644 --- a/js/js.libraries/src/stdlib/JUMaps.kt +++ b/js/js.libraries/src/stdlib/JUMaps.kt @@ -1,7 +1,26 @@ package kotlin -import java.util.Map as JMap +import java.util.Map + /** Provides [] access to maps */ -public fun JMap.set(key : K, value : V): Unit { +public fun Map.set(key : K, value : V): Unit { this.put(key, value) } + +/** + * Returns a new [[HashMap]] populated with the given tuple values where the first value in each tuple + * is the key and the second value is the value + * + * @includeFunctionBody ../../test/MapTest.kt createUsingTuples + */ +public inline fun hashMap(vararg values: #(K,V)): java.util.HashMap { + val answer = java.util.HashMap() + /** + TODO replace by this simpler call when we can pass vararg values into other methods + answer.putAll(values) + */ + for (v in values) { + answer.put(v._1, v._2) + } + return answer +} \ No newline at end of file diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibTestToJSTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibTestToJSTest.java index 25f84dd6c1e..8b86362b056 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibTestToJSTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibTestToJSTest.java @@ -24,8 +24,9 @@ public class StdLibTestToJSTest extends StdLibTestSupport { public void testGenerateTestCase() throws Exception { generateJavaScriptFiles(EcmaVersion.all(), "libraries/stdlib/test", + //"dom/DomTest.kt", + "js/MapTest.kt", "ListTest.kt", - "StringTest.kt", - "js/SimpleTest.kt"); + "StringTest.kt"); } } diff --git a/js/js.translator/src/org/jetbrains/k2js/config/Config.java b/js/js.translator/src/org/jetbrains/k2js/config/Config.java index 71512c40ae2..fd7c43ae621 100644 --- a/js/js.translator/src/org/jetbrains/k2js/config/Config.java +++ b/js/js.translator/src/org/jetbrains/k2js/config/Config.java @@ -93,6 +93,7 @@ public abstract class Config { @NotNull public static final List LIB_FILE_NAMES_DEPENDENT_ON_STDLIB = Arrays.asList( "/stdlib/jutil.kt", + "/stdlib/JUMaps.kt", "/stdlib/test.kt", "/core/stringDefs.kt", "/core/strings.kt" @@ -114,8 +115,9 @@ public abstract class Config { "/kotlin/JLangIterablesLazy.kt", "/kotlin/JLangIterablesSpecial.kt", "/kotlin/support/AbstractIterator.kt", - //"/kotlin/Ordering.kt", + "/kotlin/Standard.kt", "/kotlin/Strings.kt", + "/kotlin/dom/Dom.kt", "/kotlin/test/Test.kt" ); diff --git a/js/js.translator/testFiles/kotlin_lib.js b/js/js.translator/testFiles/kotlin_lib.js index 9d929114313..53e59a3cad9 100644 --- a/js/js.translator/testFiles/kotlin_lib.js +++ b/js/js.translator/testFiles/kotlin_lib.js @@ -220,6 +220,12 @@ } }); + Kotlin.Runnable = Kotlin.$createClass({ + run:function () { + throw Kotlin.$new(Kotlin.AbstractFunctionInvocationError)(); + } + }); + Kotlin.ArrayIterator = Kotlin.$createClass(Kotlin.Iterator, { initialize: function (array) { this.array = array; diff --git a/libraries/stdlib/src/kotlin/JUtil.kt b/libraries/stdlib/src/kotlin/JUtil.kt index a0190a6adba..cce812d1766 100644 --- a/libraries/stdlib/src/kotlin/JUtil.kt +++ b/libraries/stdlib/src/kotlin/JUtil.kt @@ -105,5 +105,5 @@ val List.head : T? */ val List.tail : List get() { - return drop(1) + return this.drop(1) } diff --git a/libraries/stdlib/src/kotlin/Standard.kt b/libraries/stdlib/src/kotlin/Standard.kt index d2f1613ecf1..3d100aa6b5d 100644 --- a/libraries/stdlib/src/kotlin/Standard.kt +++ b/libraries/stdlib/src/kotlin/Standard.kt @@ -1,15 +1,9 @@ package kotlin -import java.util.Collection import java.util.ArrayList -import java.util.LinkedList +import java.util.Collection import java.util.HashSet -import java.util.LinkedHashSet -import java.util.TreeSet -import java.util.SortedSet -import java.util.Comparator -import java.io.PrintWriter -import java.io.PrintStream +import java.util.LinkedList /** Helper to make jet.Iterator usable in for @@ -59,22 +53,6 @@ Add iterated elements to java.util.HashSet */ public inline fun Iterator.toHashSet() : HashSet = toCollection(HashSet()) -/** - * Add iterated elements to a [[LinkedHashSet]] to preserve insertion order - */ -public inline fun Iterator.toLinkedSet() : LinkedHashSet = toCollection(LinkedHashSet()) - -/** - * Add iterated elements to [[SortedSet]] to ensure iteration is in the order of the default comparator - * for the type - */ -public inline fun Iterator.toSortedSet() : SortedSet = toCollection(TreeSet()) - -/** - * Add iterated elements to [[SortedSet]] with the given *comparator* to ensure iteration is in the order of the given comparator - */ -public inline fun Iterator.toSortedSet(comparator: Comparator) : SortedSet = toCollection(TreeSet(comparator)) - /** * Creates a tuple of type [[#(A,B)]] from this and *that* which can be useful for creating [[Map]] literals @@ -99,19 +77,3 @@ public inline fun runnable(action: ()-> Unit): Runnable { } } } - -/** - * Allows a stack trace to be printed from Kotlin's [[Throwable]] - */ -public inline fun Throwable.printStackTrace(writer: PrintWriter): Unit { - val jlt = this as java.lang.Throwable - jlt.printStackTrace(writer) -} - -/** - * Allows a stack trace to be printed from Kotlin's [[Throwable]] - */ -public inline fun Throwable.printStackTrace(stream: PrintStream): Unit { - val jlt = this as java.lang.Throwable - jlt.printStackTrace(stream) -} diff --git a/libraries/stdlib/src/kotlin/StandardJVM.kt b/libraries/stdlib/src/kotlin/StandardJVM.kt new file mode 100644 index 00000000000..cf436cd2432 --- /dev/null +++ b/libraries/stdlib/src/kotlin/StandardJVM.kt @@ -0,0 +1,46 @@ +package kotlin + +import java.util.Collection +import java.util.ArrayList +import java.util.LinkedList +import java.util.HashSet +import java.util.LinkedHashSet +import java.util.TreeSet +import java.util.SortedSet +import java.util.Comparator +import java.io.PrintWriter +import java.io.PrintStream + +/** + * Add iterated elements to a [[LinkedHashSet]] to preserve insertion order + */ +public inline fun Iterator.toLinkedSet() : LinkedHashSet = toCollection(LinkedHashSet()) + +/** + * Add iterated elements to [[SortedSet]] to ensure iteration is in the order of the default comparator + * for the type + */ +public inline fun Iterator.toSortedSet() : SortedSet = toCollection(TreeSet()) + +/** + * Add iterated elements to [[SortedSet]] with the given *comparator* to ensure iteration is in the order of the given comparator + */ +public inline fun Iterator.toSortedSet(comparator: Comparator) : SortedSet = toCollection(TreeSet(comparator)) + + +/** + * Allows a stack trace to be printed from Kotlin's [[Throwable]] + */ +public inline fun Throwable.printStackTrace(writer: PrintWriter): Unit { + val jlt = this as java.lang.Throwable + jlt.printStackTrace(writer) +} + +/** + * Allows a stack trace to be printed from Kotlin's [[Throwable]] + */ +public inline fun Throwable.printStackTrace(stream: PrintStream): Unit { + val jlt = this as java.lang.Throwable + jlt.printStackTrace(stream) +} + diff --git a/libraries/stdlib/test/MapTest.kt b/libraries/stdlib/test/MapTest.kt index 3fd7cf23ca4..2f8917b99bc 100644 --- a/libraries/stdlib/test/MapTest.kt +++ b/libraries/stdlib/test/MapTest.kt @@ -3,7 +3,7 @@ package test.collections import kotlin.test.* import java.util.* -import org.junit.Test as test +import org.junit.Test as test class MapTest { diff --git a/libraries/stdlib/test/js/MapTest.kt b/libraries/stdlib/test/js/MapTest.kt new file mode 100644 index 00000000000..e370b353853 --- /dev/null +++ b/libraries/stdlib/test/js/MapTest.kt @@ -0,0 +1,182 @@ +package testPackage + +import kotlin.test.* + +import java.util.* +import org.junit.Test as test + +class MapTest { + + test fun getOrElse() { + val data = HashMap() + val a = data.getOrElse("foo"){2} + assertEquals(2, a) + + val b = data.getOrElse("foo"){3} + assertEquals(3, b) + assertEquals(0, data.size()) + } + + test fun getOrPut() { + val data = HashMap() + val a = data.getOrPut("foo"){2} + assertEquals(2, a) + + val b = data.getOrPut("foo"){3} + assertEquals(2, b) + + assertEquals(1, data.size()) + } + + test fun sizeAndEmpty() { + val data = HashMap() + assertTrue{ data.empty } + assertEquals(data.size, 0) + } + + /* + + TODO fix bug with .set() on Map... + + test fun setViaIndexOperators() { + val map = HashMap() + assertTrue{ map.empty } + assertEquals(map.size, 0) + + map["name"] = "James" + + assertTrue{ !map.empty } + assertEquals(map.size(), 1) + assertEquals("James", map["name"]) + } + */ + + test fun createUsingTuples() { + val map = hashMap(#("a", 1), #("b", 2)) + assertEquals(2, map.size) + assertEquals(1, map.get("a")) + assertEquals(2, map.get("b")) + } + + test fun createUsingTo() { + val map = hashMap("a" to 1, "b" to 2) + assertEquals(2, map.size) + assertEquals(1, map.get("a")) + assertEquals(2, map.get("b")) + } + + /* + test fun createLinkedMap() { + val map = linkedMap(#("c", 3), #("b", 2), #("a", 1)) + assertEquals(1, map.get("a")) + assertEquals(2, map.get("b")) + assertEquals(3, map.get("c")) + assertEquals(arrayList("c", "b", "a"), map.keySet().toList()) + } + + test fun iterate() { + val map = TreeMap() + map["beverage"] = "beer" + map["location"] = "Mells" + map["name"] = "James" + + val list = arrayList() + for (e in map) { + println("key = ${e.getKey()}, value = ${e.getValue()}") + list.add(e.getKey()) + list.add(e.getValue()) + } + + assertEquals(6, list.size()) + assertEquals("beverage,beer,location,Mells,name,James", list.makeString(",")) + } + + test fun iterateWithProperties() { + val map = TreeMap() + map["beverage"] = "beer" + map["location"] = "Mells" + map["name"] = "James" + + val list = arrayList() + for (e in map) { + println("key = ${e.key}, value = ${e.value}") + list.add(e.key) + list.add(e.value) + } + + assertEquals(6, list.size()) + assertEquals("beverage,beer,location,Mells,name,James", list.makeString(",")) + } + + test fun map() { + val m1 = TreeMap() + m1["beverage"] = "beer" + m1["location"] = "Mells" + + val list = m1.map{ it.value + " rocks" } + + println("Got new list $list") + assertEquals(arrayList("beer rocks", "Mells rocks"), list) + } + + test fun mapValues() { + val m1 = TreeMap() + m1["beverage"] = "beer" + m1["location"] = "Mells" + + val m2 = m1.mapValues{ it.value + "2" } + + println("Got new map $m2") + assertEquals(arrayList("beer2", "Mells2"), m2.values().toList()) + } + + test fun createSortedMap() { + val map = sortedMap(#("c", 3), #("b", 2), #("a", 1)) + assertEquals(1, map.get("a")) + assertEquals(2, map.get("b")) + assertEquals(3, map.get("c")) + assertEquals(arrayList("a", "b", "c"), map.keySet()!!.toList()) + } + + test fun toSortedMap() { + val map = hashMap(#("c", 3), #("b", 2), #("a", 1)) + val sorted = map.toSortedMap() + assertEquals(1, sorted.get("a")) + assertEquals(2, sorted.get("b")) + assertEquals(3, sorted.get("c")) + assertEquals(arrayList("a", "b", "c"), sorted.keySet()!!.toList()) + } + + test fun toSortedMapWithComparator() { + val map = hashMap(#("c", 3), #("bc", 2), #("bd", 4), #("abc", 1)) + val c = comparator{ a, b -> + val answer = a.length() - b.length() + if (answer == 0) a.compareTo(b) else answer + } + val sorted = map.toSortedMap(c) + assertEquals(arrayList("c", "bc", "bd", "abc"), sorted.keySet()!!.toList()) + assertEquals(1, sorted.get("abc")) + assertEquals(2, sorted.get("bc")) + assertEquals(3, sorted.get("c")) + } + + test fun compilerBug() { + val map = TreeMap() + map["beverage"] = "beer" + map["location"] = "Mells" + map["name"] = "James" + + var list = arrayList() + for (e in map) { + println("key = ${e.getKey()}, value = ${e.getValue()}") + list += e.getKey() + list += e.getValue() + } + + assertEquals(6, list.size()) + assertEquals("beverage,beer,location,Mells,name,James", list.makeString(",")) + println("==== worked! $list") + } + */ + +} diff --git a/libraries/tools/kotlin-js-library/pom.xml b/libraries/tools/kotlin-js-library/pom.xml index cea64f0ed12..d4bf68bd910 100644 --- a/libraries/tools/kotlin-js-library/pom.xml +++ b/libraries/tools/kotlin-js-library/pom.xml @@ -77,6 +77,7 @@ +