JS: concat vararg arguments using Kotlin.concat and Kotlin.concatPrimitive functions in order to be binary compatible with (Kotlin PrimitiveArray -> JS TypedArrays) mapping.

This commit is contained in:
Anton Bannykh
2017-02-13 15:27:48 +03:00
parent a903c4ab0e
commit 4c808e8691
7 changed files with 197 additions and 100 deletions
+21
View File
@@ -93,4 +93,25 @@ public class BoxedChar(val c: Char) : Comparable<Char> {
public fun valueOf(): Int {
return js("this.c")
}
}
/* For future binary compatibility with TypedArrays
* TODO: concat normal Array's and TypedArrays into an Array
*/
@PublishedApi
@JsName("arrayConcat")
internal fun <T> arrayConcat(a: T, b: T): T {
return a.asDynamic().concat.apply(js("[]"), js("arguments"));
}
/* For future binary compatibility with TypedArrays
* TODO: concat primitive arrays.
* For Byte-, Short-, Int-, Float-, and DoubleArray concat result into a TypedArray.
* For Boolean-, Char-, and LongArray return an Array with corresponding type property.
* Default to Array.prototype.concat for compatibility.
*/
@PublishedApi
@JsName("primitiveArrayConcat")
internal fun <T> primitiveArrayConcat(a: T, b: T): T {
return a.asDynamic().concat.apply(js("[]"), js("arguments"));
}
+26 -18
View File
@@ -9,6 +9,7 @@ package kotlin.collections
//
import kotlin.js.*
import primitiveArrayConcat
import kotlin.comparisons.*
/**
@@ -12944,8 +12945,15 @@ public inline fun BooleanArray.asList(): List<Boolean> {
/**
* Returns a [List] that wraps the original array.
*/
public inline fun CharArray.asList(): List<Char> {
return this.toTypedArray().asList()
public fun CharArray.asList(): List<Char> {
return object : AbstractList<Char>(), RandomAccess {
override val size: Int get() = this@asList.size
override fun isEmpty(): Boolean = this@asList.isEmpty()
override fun contains(element: Char): Boolean = this@asList.contains(element)
override fun get(index: Int): Char = this@asList[index]
override fun indexOf(element: Char): Int = this@asList.indexOf(element)
override fun lastIndexOf(element: Char): Int = this@asList.lastIndexOf(element)
}
}
/**
@@ -13168,7 +13176,7 @@ public inline operator fun <T> Array<out T>.plus(element: T): Array<T> {
*/
@Suppress("NOTHING_TO_INLINE")
public inline operator fun ByteArray.plus(element: Byte): ByteArray {
return this.asDynamic().concat(arrayOf(element))
return plus(byteArrayOf(element))
}
/**
@@ -13176,7 +13184,7 @@ public inline operator fun ByteArray.plus(element: Byte): ByteArray {
*/
@Suppress("NOTHING_TO_INLINE")
public inline operator fun ShortArray.plus(element: Short): ShortArray {
return this.asDynamic().concat(arrayOf(element))
return plus(shortArrayOf(element))
}
/**
@@ -13184,7 +13192,7 @@ public inline operator fun ShortArray.plus(element: Short): ShortArray {
*/
@Suppress("NOTHING_TO_INLINE")
public inline operator fun IntArray.plus(element: Int): IntArray {
return this.asDynamic().concat(arrayOf(element))
return plus(intArrayOf(element))
}
/**
@@ -13192,7 +13200,7 @@ public inline operator fun IntArray.plus(element: Int): IntArray {
*/
@Suppress("NOTHING_TO_INLINE")
public inline operator fun LongArray.plus(element: Long): LongArray {
return this.asDynamic().concat(arrayOf(element))
return plus(longArrayOf(element))
}
/**
@@ -13200,7 +13208,7 @@ public inline operator fun LongArray.plus(element: Long): LongArray {
*/
@Suppress("NOTHING_TO_INLINE")
public inline operator fun FloatArray.plus(element: Float): FloatArray {
return this.asDynamic().concat(arrayOf(element))
return plus(floatArrayOf(element))
}
/**
@@ -13208,7 +13216,7 @@ public inline operator fun FloatArray.plus(element: Float): FloatArray {
*/
@Suppress("NOTHING_TO_INLINE")
public inline operator fun DoubleArray.plus(element: Double): DoubleArray {
return this.asDynamic().concat(arrayOf(element))
return plus(doubleArrayOf(element))
}
/**
@@ -13216,7 +13224,7 @@ public inline operator fun DoubleArray.plus(element: Double): DoubleArray {
*/
@Suppress("NOTHING_TO_INLINE")
public inline operator fun BooleanArray.plus(element: Boolean): BooleanArray {
return this.asDynamic().concat(arrayOf(element))
return plus(booleanArrayOf(element))
}
/**
@@ -13224,7 +13232,7 @@ public inline operator fun BooleanArray.plus(element: Boolean): BooleanArray {
*/
@Suppress("NOTHING_TO_INLINE")
public inline operator fun CharArray.plus(element: Char): CharArray {
return this.asDynamic().concat(arrayOf(element))
return plus(charArrayOf(element))
}
/**
@@ -13303,7 +13311,7 @@ public inline operator fun <T> Array<out T>.plus(elements: Array<out T>): Array<
*/
@Suppress("NOTHING_TO_INLINE")
public inline operator fun ByteArray.plus(elements: ByteArray): ByteArray {
return this.asDynamic().concat(elements)
return primitiveArrayConcat(this, elements)
}
/**
@@ -13311,7 +13319,7 @@ public inline operator fun ByteArray.plus(elements: ByteArray): ByteArray {
*/
@Suppress("NOTHING_TO_INLINE")
public inline operator fun ShortArray.plus(elements: ShortArray): ShortArray {
return this.asDynamic().concat(elements)
return primitiveArrayConcat(this, elements)
}
/**
@@ -13319,7 +13327,7 @@ public inline operator fun ShortArray.plus(elements: ShortArray): ShortArray {
*/
@Suppress("NOTHING_TO_INLINE")
public inline operator fun IntArray.plus(elements: IntArray): IntArray {
return this.asDynamic().concat(elements)
return primitiveArrayConcat(this, elements)
}
/**
@@ -13327,7 +13335,7 @@ public inline operator fun IntArray.plus(elements: IntArray): IntArray {
*/
@Suppress("NOTHING_TO_INLINE")
public inline operator fun LongArray.plus(elements: LongArray): LongArray {
return this.asDynamic().concat(elements)
return primitiveArrayConcat(this, elements)
}
/**
@@ -13335,7 +13343,7 @@ public inline operator fun LongArray.plus(elements: LongArray): LongArray {
*/
@Suppress("NOTHING_TO_INLINE")
public inline operator fun FloatArray.plus(elements: FloatArray): FloatArray {
return this.asDynamic().concat(elements)
return primitiveArrayConcat(this, elements)
}
/**
@@ -13343,7 +13351,7 @@ public inline operator fun FloatArray.plus(elements: FloatArray): FloatArray {
*/
@Suppress("NOTHING_TO_INLINE")
public inline operator fun DoubleArray.plus(elements: DoubleArray): DoubleArray {
return this.asDynamic().concat(elements)
return primitiveArrayConcat(this, elements)
}
/**
@@ -13351,7 +13359,7 @@ public inline operator fun DoubleArray.plus(elements: DoubleArray): DoubleArray
*/
@Suppress("NOTHING_TO_INLINE")
public inline operator fun BooleanArray.plus(elements: BooleanArray): BooleanArray {
return this.asDynamic().concat(elements)
return primitiveArrayConcat(this, elements)
}
/**
@@ -13359,7 +13367,7 @@ public inline operator fun BooleanArray.plus(elements: BooleanArray): BooleanArr
*/
@Suppress("NOTHING_TO_INLINE")
public inline operator fun CharArray.plus(elements: CharArray): CharArray {
return this.asDynamic().concat(elements)
return primitiveArrayConcat(this, elements)
}
/**