Add code generation for asList methods in js.libraries.

This commit is contained in:
Ilya Gorbunov
2015-03-26 21:57:43 +03:00
committed by Ilya Gorbunov
parent abdac27b61
commit d724ce3f7e
5 changed files with 121 additions and 0 deletions
@@ -0,0 +1,76 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import java.util.*
import java.util.Collections // TODO: it's temporary while we have java.util.Collections in js
/**
* Returns a list that wraps the original array
*/
public fun <T> Array<out T>.asList(): List<T> {
val al = ArrayList<T>()
(al: dynamic).array = this // black dynamic magic
return al
}
/**
* Returns a list that wraps the original array
*/
public inline fun BooleanArray.asList(): List<Boolean> {
return (this as Array<Boolean>).asList()
}
/**
* Returns a list that wraps the original array
*/
public inline fun ByteArray.asList(): List<Byte> {
return (this as Array<Byte>).asList()
}
/**
* Returns a list that wraps the original array
*/
public inline fun CharArray.asList(): List<Char> {
return (this as Array<Char>).asList()
}
/**
* Returns a list that wraps the original array
*/
public inline fun DoubleArray.asList(): List<Double> {
return (this as Array<Double>).asList()
}
/**
* Returns a list that wraps the original array
*/
public inline fun FloatArray.asList(): List<Float> {
return (this as Array<Float>).asList()
}
/**
* Returns a list that wraps the original array
*/
public inline fun IntArray.asList(): List<Int> {
return (this as Array<Int>).asList()
}
/**
* Returns a list that wraps the original array
*/
public inline fun LongArray.asList(): List<Long> {
return (this as Array<Long>).asList()
}
/**
* Returns a list that wraps the original array
*/
public inline fun ShortArray.asList(): List<Short> {
return (this as Array<Short>).asList()
}
@@ -269,6 +269,21 @@ class ArraysTest {
assertEquals(iter4.toList(), emptyList<String>())
}
test fun asList() {
assertEquals(listOf(1, 2, 3), intArray(1, 2, 3).asList())
assertEquals(listOf<Byte>(1, 2, 3), byteArray(1, 2, 3).asList())
assertEquals(listOf(true, false), booleanArray(true, false).asList())
assertEquals(listOf(1, 2, 3), array(1, 2, 3).asList())
assertEquals(listOf("abc", "def"), array("abc", "def").asList())
val ints = intArray(1, 5, 7)
val intsAsList = ints.asList()
assertEquals(5, intsAsList[1])
ints[1] = 10
assertEquals(10, intsAsList[1], "Should reflect changes in original array")
}
/*
TODO FIXME ASAP: These currently fail on JS due to missing upto() method on numbers
@@ -37,3 +37,7 @@ fun generateCollectionsAPI(outDir: File) {
}
}
fun generateCollectionsJsAPI(outDir: File) {
specialJS().writeTo(File(outDir, "kotlin_special.kt")) { build() }
}
@@ -29,6 +29,7 @@ fun main(args: Array<String>) {
generateDomEventsAPI(File(jsCoreDir, "domEvents.kt"))
generateCollectionsAPI(outDir)
generateCollectionsJsAPI(jsCoreDir)
generateDownTos(File(outDir, "_DownTo.kt"), "package kotlin")
}
@@ -0,0 +1,25 @@
package templates
import templates.Family.*
fun specialJS(): List<GenericFunction> {
val templates = arrayListOf<GenericFunction>()
templates add f("asList()") {
only(ArraysOfObjects, ArraysOfPrimitives)
doc { "Returns a list that wraps the original array" }
returns("List<T>")
body(ArraysOfObjects) {
"""
val al = ArrayList<T>()
(al: dynamic).array = this // black dynamic magic
return al
"""
}
inline(true, ArraysOfPrimitives)
body(ArraysOfPrimitives) {"""return (this as Array<T>).asList()"""}
}
return templates
}