From 1a77c360400120e571c543c5ac01a037c5d82036 Mon Sep 17 00:00:00 2001 From: James Strachan Date: Tue, 29 May 2012 15:02:17 +0100 Subject: [PATCH] got the kotlin/Dom.kt, Preconditions.kt and AbstractIterator.kt compiling as JS; needs more testing of using these APIs from JS though... --- js/js.libraries/src/core/javalang.kt | 12 ++++ js/js.libraries/src/core/javautil.kt | 16 +++++ .../k2js/test/semantics/StdLibToJSTest.java | 6 +- js/js.translator/testFiles/kotlin_lib.js | 5 +- libraries/stdlib/src/kotlin/AssertionsJVM.kt | 31 +++++++++ libraries/stdlib/src/kotlin/Preconditions.kt | 32 +--------- libraries/stdlib/src/kotlin/dom/Dom.kt | 63 +++++++++++++++++++ libraries/stdlib/src/kotlin/dom/DomJVM.kt | 54 ---------------- .../src/kotlin/support/AbstractIterator.kt | 3 + libraries/tools/kotlin-js-library/pom.xml | 3 + 10 files changed, 137 insertions(+), 88 deletions(-) create mode 100644 libraries/stdlib/src/kotlin/AssertionsJVM.kt diff --git a/js/js.libraries/src/core/javalang.kt b/js/js.libraries/src/core/javalang.kt index cb15d768496..4e2c2ce288b 100644 --- a/js/js.libraries/src/core/javalang.kt +++ b/js/js.libraries/src/core/javalang.kt @@ -10,3 +10,15 @@ trait Iterable { library("splitString") public fun String.split(regex : String) : Array = js.noImpl + +library +class IllegalArgumentException(message: String = "") : Exception() {} + +library +class IllegalStateException(message: String = "") : Exception() {} + +library +class IndexOutOfBoundsException(message: String = "") : Exception() {} + +library +class UnsupportedOperationException(message: String = "") : Exception() {} diff --git a/js/js.libraries/src/core/javautil.kt b/js/js.libraries/src/core/javautil.kt index 166f390544e..68407ba14a1 100644 --- a/js/js.libraries/src/core/javautil.kt +++ b/js/js.libraries/src/core/javautil.kt @@ -73,6 +73,22 @@ public abstract open class AbstractCollection : Collection { library public abstract open class AbstractList : AbstractCollection, List { + public override fun isEmpty() : Boolean = js.noImpl + public override fun contains(o : Any?) : Boolean = js.noImpl + public override fun iterator() : Iterator = js.noImpl + // public override fun indexOf(o : Any?) : Int = js.noImpl + // public override fun lastIndexOf(o : Any?) : Int = js.noImpl + // public override fun toArray() : Array = js.noImpl + // public override fun toArray(a : Array) : Array = js.noImpl + public override fun set(index : Int, element : E) : E = js.noImpl + public override fun add(e : E) : Boolean = js.noImpl + public override fun add(index : Int, element : E) : Unit = js.noImpl + library("removeByIndex") + public override fun remove(index : Int) : E = js.noImpl + public override fun remove(o : Any?) : Boolean = js.noImpl + public override fun clear() : Unit = js.noImpl + public override fun addAll(c : java.util.Collection) : Boolean = js.noImpl + // public override fun addAll(index : Int, c : java.util.Collection) : Boolean = js.noImpl } library diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibToJSTest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibToJSTest.java index 3144673b67a..b79de1a6b82 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibToJSTest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/StdLibToJSTest.java @@ -44,12 +44,10 @@ public final class StdLibToJSTest extends SingleFileTranslationTest { super("stdlib/"); } - public void testDummy() { - } - - public void TODO_testCompileStandardLibraryFiles() throws Exception { + public void testCompileStandardLibraryFiles() throws Exception { generateJavaScriptFiles(MainCallParameters.noCall(), EcmaVersion.all(), + "kotlin/Preconditions.kt", "kotlin/dom/Dom.kt", "kotlin/support/AbstractIterator.kt" ); diff --git a/js/js.translator/testFiles/kotlin_lib.js b/js/js.translator/testFiles/kotlin_lib.js index e50f1a9b059..17541e01fbc 100644 --- a/js/js.translator/testFiles/kotlin_lib.js +++ b/js/js.translator/testFiles/kotlin_lib.js @@ -289,8 +289,11 @@ var Kotlin; Kotlin.RuntimeException = Kotlin.Class.create(Kotlin.Exception); Kotlin.Exceptions.IndexOutOfBounds = Kotlin.Class.create(Kotlin.Exception); Kotlin.Exceptions.NullPointerException = Kotlin.Class.create(Kotlin.Exception); - Kotlin.Exceptions.UnsupportedOperationException = Kotlin.Class.create(Kotlin.Exception); Kotlin.Exceptions.NoSuchElementException = Kotlin.Class.create(Kotlin.Exception); + Kotlin.Exceptions.IllegalArgumentException = Kotlin.Class.create(Kotlin.Exception); + Kotlin.Exceptions.IllegalStateException = Kotlin.Class.create(Kotlin.Exception); + Kotlin.Exceptions.IndexOutOfBoundsException = Kotlin.Class.create(Kotlin.Exception); + Kotlin.Exceptions.UnsupportedOperationException = Kotlin.Class.create(Kotlin.Exception); Kotlin.throwNPE = function() { throw new Kotlin.Exceptions.NullPointerException(); diff --git a/libraries/stdlib/src/kotlin/AssertionsJVM.kt b/libraries/stdlib/src/kotlin/AssertionsJVM.kt new file mode 100644 index 00000000000..d2aa631bc35 --- /dev/null +++ b/libraries/stdlib/src/kotlin/AssertionsJVM.kt @@ -0,0 +1,31 @@ +package kotlin + +object Assertions { + // TODO make private once KT-1528 is fixed. + val _ENABLED = (javaClass()).desiredAssertionStatus() +} + +/** +* Throws an [[AssertionError]] with an optional *message* if the *value* is false +* and runtime assertions have been enabled on the JVM using the *-ea* JVM option. +*/ +public inline fun assert(value: Boolean, message: Any = "Assertion failed") { + if (Assertions._ENABLED) { + if (!value) { + throw AssertionError(message) + } + } +} + +/** + * Throws an [[AssertionError]] with the specified *lazyMessage* if the *value* is false + * and runtime assertions have been enabled on the JVM using the *-ea* JVM option. + */ +public inline fun assert(value: Boolean, lazyMessage: () -> String) { + if (Assertions._ENABLED) { + if (!value) { + val message = lazyMessage() + throw AssertionError(message) + } + } +} diff --git a/libraries/stdlib/src/kotlin/Preconditions.kt b/libraries/stdlib/src/kotlin/Preconditions.kt index 87281b77b35..a4676e88059 100644 --- a/libraries/stdlib/src/kotlin/Preconditions.kt +++ b/libraries/stdlib/src/kotlin/Preconditions.kt @@ -1,34 +1,8 @@ package kotlin -object Assertions { - // TODO make private once KT-1528 is fixed. - val _ENABLED = (javaClass()).desiredAssertionStatus() -} - -/** -* Throws an [[AssertionError]] with an optional *message* if the *value* is false -* and runtime assertions have been enabled on the JVM using the *-ea* JVM option. -*/ -public inline fun assert(value: Boolean, message: Any = "Assertion failed") { - if (Assertions._ENABLED) { - if (!value) { - throw AssertionError(message) - } - } -} - -/** - * Throws an [[AssertionError]] with the specified *lazyMessage* if the *value* is false - * and runtime assertions have been enabled on the JVM using the *-ea* JVM option. - */ -public inline fun assert(value: Boolean, lazyMessage: () -> String) { - if (Assertions._ENABLED) { - if (!value) { - val message = lazyMessage() - throw AssertionError(message) - } - } -} +// TODO should not need this - its here for the JS stuff +import java.lang.IllegalArgumentException +import java.lang.IllegalStateException /** * Throws an [[IllegalArgumentException]] with an optional *message* if the *value* is false. diff --git a/libraries/stdlib/src/kotlin/dom/Dom.kt b/libraries/stdlib/src/kotlin/dom/Dom.kt index 62a4cb842aa..1a9093166c7 100644 --- a/libraries/stdlib/src/kotlin/dom/Dom.kt +++ b/libraries/stdlib/src/kotlin/dom/Dom.kt @@ -5,6 +5,9 @@ import kotlin.support.* import java.util.* import org.w3c.dom.* +// TODO should not need this - its here for the JS stuff +import java.lang.IllegalArgumentException +import java.lang.IndexOutOfBoundsException // Properties @@ -76,6 +79,66 @@ set(value) { this.setAttribute("class", value) } +/** Returns the children of the element as a list */ +inline fun Element?.children(): List { + return this?.getChildNodes().toList() +} + +/** The child elements of this document */ +val Document?.elements : List +get() = this?.getElementsByTagName("*").toElementList() + +/** The child elements of this elements */ +val Element?.elements : List +get() = this?.getElementsByTagName("*").toElementList() + + +/** Returns all the child elements given the local element name */ +inline fun Element?.elements(localName: String): List { + return this?.getElementsByTagName(localName).toElementList() +} + +/** Returns all the elements given the local element name */ +inline fun Document?.elements(localName: String): List { + return this?.getElementsByTagName(localName).toElementList() +} + +/** Returns all the child elements given the namespace URI and local element name */ +inline fun Element?.elements(namespaceUri: String, localName: String): List { + return this?.getElementsByTagNameNS(namespaceUri, localName).toElementList() +} + +/** Returns all the elements given the namespace URI and local element name */ +inline fun Document?.elements(namespaceUri: String, localName: String): List { + return this?.getElementsByTagNameNS(namespaceUri, localName).toElementList() +} + +inline fun NodeList?.toList(): List { + return if (this == null) { + // TODO the following is easier to convert to JS + //Collections.EMPTY_LIST as List + ArrayList() + } + else { + NodeListAsList(this) + } +} + +inline fun NodeList?.toElementList(): List { + return if (this == null) { + // TODO the following is easier to convert to JS + //Collections.EMPTY_LIST as List + ArrayList() + } + else { + ElementListAsList(this) + } +} + + + + + // Helper methods /** TODO this approach generates compiler errors... diff --git a/libraries/stdlib/src/kotlin/dom/DomJVM.kt b/libraries/stdlib/src/kotlin/dom/DomJVM.kt index a904cefca1a..087b9b80701 100644 --- a/libraries/stdlib/src/kotlin/dom/DomJVM.kt +++ b/libraries/stdlib/src/kotlin/dom/DomJVM.kt @@ -19,12 +19,6 @@ import javax.xml.transform.stream.StreamResult import org.w3c.dom.* import org.xml.sax.InputSource -/** Returns the children of the element as a list */ -inline fun Element?.children(): List { - return this?.getChildNodes().toList() -} - - /** Returns an [[Iterator]] of all the next [[Element]] siblings */ fun Node.nextElements(): Iterator = nextSiblings().filterIsInstance(javaClass()) @@ -74,36 +68,6 @@ fun Element.get(selector: String): List { } } -/** The child elements of this document */ -val Document?.elements : List -get() = this?.getElementsByTagName("*").toElementList() - -/** The child elements of this elements */ -val Element?.elements : List -get() = this?.getElementsByTagName("*").toElementList() - - -/** Returns all the child elements given the local element name */ -inline fun Element?.elements(localName: String?): List { - return this?.getElementsByTagName(localName).toElementList() -} - -/** Returns all the elements given the local element name */ -inline fun Document?.elements(localName: String?): List { - return this?.getElementsByTagName(localName).toElementList() -} - -/** Returns all the child elements given the namespace URI and local element name */ -inline fun Element?.elements(namespaceUri: String?, localName: String?): List { - return this?.getElementsByTagNameNS(namespaceUri, localName).toElementList() -} - -/** Returns all the elements given the namespace URI and local element name */ -inline fun Document?.elements(namespaceUri: String?, localName: String?): List { - return this?.getElementsByTagNameNS(namespaceUri, localName).toElementList() -} - - var Element.classSet : Set get() { val answer = LinkedHashSet() @@ -156,24 +120,6 @@ fun NodeList?.toXmlString(xmlDeclaration: Boolean = false): String { } } -inline fun NodeList?.toList(): List { - return if (this == null) { - Collections.EMPTY_LIST as List - } - else { - NodeListAsList(this) - } -} - -inline fun NodeList?.toElementList(): List { - return if (this == null) { - Collections.EMPTY_LIST as List - } - else { - ElementListAsList(this) - } -} - /** Creates a new document with the given document builder*/ public fun createDocument(builder: DocumentBuilder): Document { return builder.newDocument().sure() diff --git a/libraries/stdlib/src/kotlin/support/AbstractIterator.kt b/libraries/stdlib/src/kotlin/support/AbstractIterator.kt index 1c67ec0ad74..722a2faba02 100644 --- a/libraries/stdlib/src/kotlin/support/AbstractIterator.kt +++ b/libraries/stdlib/src/kotlin/support/AbstractIterator.kt @@ -2,6 +2,9 @@ package kotlin.support import java.util.NoSuchElementException +// TODO should not need this - its here for the JS stuff +import java.lang.UnsupportedOperationException + enum class State { Ready NotReady diff --git a/libraries/tools/kotlin-js-library/pom.xml b/libraries/tools/kotlin-js-library/pom.xml index 89d0d59ca94..7992b6ca9a6 100644 --- a/libraries/tools/kotlin-js-library/pom.xml +++ b/libraries/tools/kotlin-js-library/pom.xml @@ -41,6 +41,9 @@ + + +