got the kotlin/Dom.kt, Preconditions.kt and AbstractIterator.kt compiling as JS; needs more testing of using these APIs from JS though...

This commit is contained in:
James Strachan
2012-05-29 15:02:17 +01:00
parent 64703106ea
commit 1a77c36040
10 changed files with 137 additions and 88 deletions
@@ -0,0 +1,31 @@
package kotlin
object Assertions {
// TODO make private once KT-1528 is fixed.
val _ENABLED = (javaClass<java.lang.System>()).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)
}
}
}
+3 -29
View File
@@ -1,34 +1,8 @@
package kotlin
object Assertions {
// TODO make private once KT-1528 is fixed.
val _ENABLED = (javaClass<java.lang.System>()).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.
+63
View File
@@ -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<Node> {
return this?.getChildNodes().toList()
}
/** The child elements of this document */
val Document?.elements : List<Element>
get() = this?.getElementsByTagName("*").toElementList()
/** The child elements of this elements */
val Element?.elements : List<Element>
get() = this?.getElementsByTagName("*").toElementList()
/** Returns all the child elements given the local element name */
inline fun Element?.elements(localName: String): List<Element> {
return this?.getElementsByTagName(localName).toElementList()
}
/** Returns all the elements given the local element name */
inline fun Document?.elements(localName: String): List<Element> {
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<Element> {
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<Element> {
return this?.getElementsByTagNameNS(namespaceUri, localName).toElementList()
}
inline fun NodeList?.toList(): List<Node> {
return if (this == null) {
// TODO the following is easier to convert to JS
//Collections.EMPTY_LIST as List<Node>
ArrayList<Node>()
}
else {
NodeListAsList(this)
}
}
inline fun NodeList?.toElementList(): List<Element> {
return if (this == null) {
// TODO the following is easier to convert to JS
//Collections.EMPTY_LIST as List<Element>
ArrayList<Element>()
}
else {
ElementListAsList(this)
}
}
// Helper methods
/** TODO this approach generates compiler errors...
-54
View File
@@ -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<Node> {
return this?.getChildNodes().toList()
}
/** Returns an [[Iterator]] of all the next [[Element]] siblings */
fun Node.nextElements(): Iterator<Element> = nextSiblings().filterIsInstance<Node, Element>(javaClass<Element>())
@@ -74,36 +68,6 @@ fun Element.get(selector: String): List<Element> {
}
}
/** The child elements of this document */
val Document?.elements : List<Element>
get() = this?.getElementsByTagName("*").toElementList()
/** The child elements of this elements */
val Element?.elements : List<Element>
get() = this?.getElementsByTagName("*").toElementList()
/** Returns all the child elements given the local element name */
inline fun Element?.elements(localName: String?): List<Element> {
return this?.getElementsByTagName(localName).toElementList()
}
/** Returns all the elements given the local element name */
inline fun Document?.elements(localName: String?): List<Element> {
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<Element> {
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<Element> {
return this?.getElementsByTagNameNS(namespaceUri, localName).toElementList()
}
var Element.classSet : Set<String>
get() {
val answer = LinkedHashSet<String>()
@@ -156,24 +120,6 @@ fun NodeList?.toXmlString(xmlDeclaration: Boolean = false): String {
}
}
inline fun NodeList?.toList(): List<Node> {
return if (this == null) {
Collections.EMPTY_LIST as List<Node>
}
else {
NodeListAsList(this)
}
}
inline fun NodeList?.toElementList(): List<Element> {
return if (this == null) {
Collections.EMPTY_LIST as List<Element>
}
else {
ElementListAsList(this)
}
}
/** Creates a new document with the given document builder*/
public fun createDocument(builder: DocumentBuilder): Document {
return builder.newDocument().sure()
@@ -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