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)
}
/**
@@ -17,11 +17,11 @@
package org.jetbrains.kotlin.js.translate.intrinsic.functions.factories;
import com.google.common.collect.Lists;
import org.jetbrains.kotlin.js.backend.ast.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
import org.jetbrains.kotlin.builtins.PrimitiveType;
import org.jetbrains.kotlin.js.backend.ast.*;
import org.jetbrains.kotlin.js.patterns.DescriptorPredicate;
import org.jetbrains.kotlin.js.patterns.NamePredicate;
import org.jetbrains.kotlin.js.translate.context.Namer;
@@ -16,12 +16,12 @@
package org.jetbrains.kotlin.js.translate.reference
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.js.backend.ast.*
import org.jetbrains.kotlin.js.backend.ast.metadata.SideEffectKind
import org.jetbrains.kotlin.js.backend.ast.metadata.sideEffects
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.js.translate.context.Namer
import org.jetbrains.kotlin.js.translate.context.TemporaryConstVariable
import org.jetbrains.kotlin.js.translate.context.TranslationContext
@@ -83,6 +83,7 @@ class CallArgumentTranslator private constructor(
var argsBeforeVararg: List<JsExpression>? = null
var concatArguments: MutableList<JsExpression>? = null
val argsToJsExpr = translateUnresolvedArguments(context(), resolvedCall)
var isVarargTypePrimitive: Boolean? = null
for (parameterDescriptor in valueParameters) {
val actualArgument = valueArgumentsByIndex[parameterDescriptor.index]
@@ -95,6 +96,8 @@ class CallArgumentTranslator private constructor(
hasSpreadOperator = arguments.any { it.getSpreadElement() != null }
}
isVarargTypePrimitive = KotlinBuiltIns.isPrimitiveType(parameterDescriptor.original.varargElementType!!)
if (hasSpreadOperator) {
if (isNativeFunctionCall) {
argsBeforeVararg = result
@@ -102,7 +105,10 @@ class CallArgumentTranslator private constructor(
concatArguments = prepareConcatArguments(arguments, translateResolvedArgument(actualArgument, argsToJsExpr))
}
else {
result.addAll(translateVarargArgument(actualArgument, argsToJsExpr, actualArgument.arguments.size > 1))
result.addAll(translateVarargArgument(actualArgument,
argsToJsExpr,
actualArgument.arguments.size > 1,
isVarargTypePrimitive))
}
}
else {
@@ -110,7 +116,7 @@ class CallArgumentTranslator private constructor(
result.addAll(translateResolvedArgument(actualArgument, argsToJsExpr))
}
else {
result.addAll(translateVarargArgument(actualArgument, argsToJsExpr, true))
result.addAll(translateVarargArgument(actualArgument, argsToJsExpr, true, isVarargTypePrimitive))
}
}
}
@@ -123,13 +129,15 @@ class CallArgumentTranslator private constructor(
assert(argsBeforeVararg != null) { "argsBeforeVararg should not be null" }
assert(concatArguments != null) { "concatArguments should not be null" }
concatArguments!!.addAll(result)
if (!argsBeforeVararg!!.isEmpty()) {
concatArguments.add(0, JsArrayLiteral(argsBeforeVararg).apply { sideEffects = SideEffectKind.DEPENDS_ON_STATE })
if (!result.isEmpty()) {
concatArguments!!.add(JsArrayLiteral(result).apply { sideEffects = SideEffectKind.DEPENDS_ON_STATE })
}
result = mutableListOf(concatArgumentsIfNeeded(concatArguments))
if (!argsBeforeVararg!!.isEmpty()) {
concatArguments!!.add(0, JsArrayLiteral(argsBeforeVararg).apply { sideEffects = SideEffectKind.DEPENDS_ON_STATE })
}
result = mutableListOf(concatArgumentsIfNeeded(concatArguments!!, isVarargTypePrimitive!!, true))
if (receiver != null) {
cachedReceiver = context().getOrDeclareTemporaryConstVariable(receiver)
@@ -235,7 +243,8 @@ class CallArgumentTranslator private constructor(
private fun translateVarargArgument(
resolvedArgument: ResolvedValueArgument,
translatedArgs: Map<ValueArgument, JsExpression>,
shouldWrapVarargInArray: Boolean
shouldWrapVarargInArray: Boolean,
isVarargTypePrimitive: Boolean
): List<JsExpression> {
val arguments = resolvedArgument.arguments
if (arguments.isEmpty()) {
@@ -251,7 +260,7 @@ class CallArgumentTranslator private constructor(
return if (shouldWrapVarargInArray) {
val concatArguments = prepareConcatArguments(arguments, list)
val concatExpression = concatArgumentsIfNeeded(concatArguments)
val concatExpression = concatArgumentsIfNeeded(concatArguments, isVarargTypePrimitive, false)
listOf(concatExpression)
}
else {
@@ -259,12 +268,22 @@ class CallArgumentTranslator private constructor(
}
}
private fun concatArgumentsIfNeeded(concatArguments: List<JsExpression>): JsExpression {
private fun concatArgumentsIfNeeded(
concatArguments: List<JsExpression>,
isVarargTypePrimitive: Boolean,
isMixed: Boolean
): JsExpression {
assert(concatArguments.isNotEmpty()) { "concatArguments.size should not be 0" }
if (concatArguments.size > 1) {
return JsInvocation(JsNameRef("concat", concatArguments[0]), concatArguments.subList(1, concatArguments.size))
if (isVarargTypePrimitive) {
val method = if (isMixed) "arrayConcat" else "primitiveArrayConcat"
return JsAstUtils.invokeKotlinFunction(method, concatArguments[0],
*concatArguments.subList(1, concatArguments.size).toTypedArray())
}
else {
return JsInvocation(JsNameRef("concat", concatArguments[0]), concatArguments.subList(1, concatArguments.size))
}
}
else {
return concatArguments[0]
+8
View File
@@ -56,6 +56,12 @@ external fun sumFunValuesOnParameters(x: Int, y: Int, vararg a: Int, f: (Int) ->
external fun <T> idArrayVarArg(vararg a: Array<T>): Array<T> = definedExternally
@JsName("paramCount")
external fun oneMoreParamCount(before: IntArray, vararg middle: Int, after: IntArray): Int
@JsName("paramCount")
external fun <T> oneMoreGenericParamCount(before: Array<T>, vararg middle: T, after: Array<T>): Int
fun box(): String {
if (paramCount() != 0)
return "failed when call native function without args"
@@ -138,5 +144,7 @@ fun box(): String {
assertEquals(3, idArrayVarArg(arrayOf(1, 2), *arrayOf(arrayOf(3, 4), arrayOf(5, 6))).size)
assertEquals(6, idArrayVarArg(arrayOf(1, 2), *arrayOf(arrayOf(3, 4), arrayOf(5, 6)), arrayOf(7), *arrayOf(arrayOf(8, 9), arrayOf(10, 11))).size)
assertEquals(6, oneMoreParamCount(intArrayOf(1, 2), 3, *intArrayOf(4, 5), 6, after = intArrayOf(7, 8)))
assertEquals(6, oneMoreGenericParamCount(arrayOf("1", "2"), "3", *arrayOf("4", "5"), "6", after = arrayOf("7", "8")))
return "OK"
}