[JS BE] Support passing an array as argument of vararg in named form
#KT-38059 fixed
This commit is contained in:
+8
-4
@@ -1,7 +1,11 @@
|
|||||||
// !LANGUAGE: +AllowAssigningArrayElementsToVarargsInNamedFormForFunctions
|
// !LANGUAGE: +AllowAssigningArrayElementsToVarargsInNamedFormForFunctions
|
||||||
// IGNORE_BACKEND: JS
|
|
||||||
|
|
||||||
fun test(vararg s: String) = s[1]
|
fun test(vararg s: String) = s[1] + s.size
|
||||||
|
|
||||||
fun box(): String =
|
fun box(): String {
|
||||||
test(s = arrayOf("", "OK"))
|
val r = test(s = arrayOf("aaa", "Bb"))
|
||||||
|
|
||||||
|
if (r != "Bb2") return "fail: $r"
|
||||||
|
|
||||||
|
return "OK"
|
||||||
|
}
|
||||||
|
|||||||
+10
-3
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.js.translate.reference
|
|||||||
import org.jetbrains.kotlin.builtins.PrimitiveType
|
import org.jetbrains.kotlin.builtins.PrimitiveType
|
||||||
import org.jetbrains.kotlin.builtins.functions.FunctionInvokeDescriptor
|
import org.jetbrains.kotlin.builtins.functions.FunctionInvokeDescriptor
|
||||||
import org.jetbrains.kotlin.builtins.getFunctionalClassKind
|
import org.jetbrains.kotlin.builtins.getFunctionalClassKind
|
||||||
|
import org.jetbrains.kotlin.config.LanguageFeature
|
||||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||||
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
|
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
|
||||||
@@ -70,6 +71,12 @@ class CallArgumentTranslator private constructor(
|
|||||||
result.subList(i + 1, result.size).clear()
|
result.subList(i + 1, result.size).clear()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun ValueArgument.hasSpreadElementOrNamedArgument() =
|
||||||
|
getSpreadElement() != null ||
|
||||||
|
context.config.languageVersionSettings
|
||||||
|
.supportsFeature(LanguageFeature.AllowAssigningArrayElementsToVarargsInNamedFormForFunctions) &&
|
||||||
|
isNamed()
|
||||||
|
|
||||||
private fun translate(): ArgumentsInfo {
|
private fun translate(): ArgumentsInfo {
|
||||||
val valueParameters = resolvedCall.resultingDescriptor.valueParameters
|
val valueParameters = resolvedCall.resultingDescriptor.valueParameters
|
||||||
var hasSpreadOperator = false
|
var hasSpreadOperator = false
|
||||||
@@ -91,7 +98,7 @@ class CallArgumentTranslator private constructor(
|
|||||||
val arguments = actualArgument.getArguments()
|
val arguments = actualArgument.getArguments()
|
||||||
|
|
||||||
if (!hasSpreadOperator) {
|
if (!hasSpreadOperator) {
|
||||||
hasSpreadOperator = arguments.any { it.getSpreadElement() != null }
|
hasSpreadOperator = arguments.any { it.hasSpreadElementOrNamedArgument()}
|
||||||
}
|
}
|
||||||
|
|
||||||
varargElementType = parameterDescriptor.original.varargElementType!!
|
varargElementType = parameterDescriptor.original.varargElementType!!
|
||||||
@@ -280,7 +287,7 @@ class CallArgumentTranslator private constructor(
|
|||||||
val valueArgument = arguments[index]
|
val valueArgument = arguments[index]
|
||||||
val expressionArgument = list[index]
|
val expressionArgument = list[index]
|
||||||
|
|
||||||
if (valueArgument.getSpreadElement() != null) {
|
if (valueArgument.hasSpreadElementOrNamedArgument()) {
|
||||||
if (lastArrayContent.size > 0) {
|
if (lastArrayContent.size > 0) {
|
||||||
concatArguments.add(toArray(varargElementType, lastArrayContent))
|
concatArguments.add(toArray(varargElementType, lastArrayContent))
|
||||||
lastArrayContent = mutableListOf()
|
lastArrayContent = mutableListOf()
|
||||||
|
|||||||
+5
-1
@@ -135,9 +135,13 @@ fun box(): String {
|
|||||||
|
|
||||||
assertEquals(45, sumOfParameters(1, 2, 3, 4, 5, 6, 7, 8, 9))
|
assertEquals(45, sumOfParameters(1, 2, 3, 4, 5, 6, 7, 8, 9))
|
||||||
assertEquals(45, sumOfParameters(1, 2, *intArrayOf(3, 4, 5, 6, 7, 8, 9)))
|
assertEquals(45, sumOfParameters(1, 2, *intArrayOf(3, 4, 5, 6, 7, 8, 9)))
|
||||||
|
assertEquals(45, sumOfParameters(1, 2, a = *intArrayOf(3, 4, 5, 6, 7, 8, 9)))
|
||||||
|
assertEquals(45, sumOfParameters(1, 2, a = intArrayOf(3, 4, 5, 6, 7, 8, 9)))
|
||||||
assertEquals(45, sumOfParameters(1, 2, 3, 4, *intArrayOf(5, 6, 7, 8, 9)))
|
assertEquals(45, sumOfParameters(1, 2, 3, 4, *intArrayOf(5, 6, 7, 8, 9)))
|
||||||
assertEquals(90, sumFunValuesOnParameters(1, 2, 3, 4, 5, 6, 7, 8, 9) { 2*it })
|
assertEquals(90, sumFunValuesOnParameters(1, 2, 3, 4, 5, 6, 7, 8, 9) { 2*it })
|
||||||
assertEquals(90, sumFunValuesOnParameters(1, 2, *intArrayOf(3, 4, 5, 6, 7, 8, 9)) { 2*it })
|
assertEquals(90, sumFunValuesOnParameters(1, 2, *intArrayOf(3, 4, 5, 6, 7, 8, 9)) { 2*it })
|
||||||
|
assertEquals(90, sumFunValuesOnParameters(1, 2, a = *intArrayOf(3, 4, 5, 6, 7, 8, 9)) { 2*it })
|
||||||
|
assertEquals(90, sumFunValuesOnParameters(1, 2, a = intArrayOf(3, 4, 5, 6, 7, 8, 9)) { 2*it })
|
||||||
assertEquals(90, sumFunValuesOnParameters(1, 2, 3, 4, *intArrayOf(5, 6, 7, 8, 9)) { 2*it })
|
assertEquals(90, sumFunValuesOnParameters(1, 2, 3, 4, *intArrayOf(5, 6, 7, 8, 9)) { 2*it })
|
||||||
assertEquals(90, sumFunValuesOnParameters(1, 2, *intArrayOf(3, 4, 5, 6, 7), 8, 9) { 2*it })
|
assertEquals(90, sumFunValuesOnParameters(1, 2, *intArrayOf(3, 4, 5, 6, 7), 8, 9) { 2*it })
|
||||||
assertEquals(90, sumFunValuesOnParameters(1, 2, *intArrayOf(3, 4, 5), *intArrayOf(6, 7, 8, 9)) { 2*it })
|
assertEquals(90, sumFunValuesOnParameters(1, 2, *intArrayOf(3, 4, 5), *intArrayOf(6, 7, 8, 9)) { 2*it })
|
||||||
@@ -156,4 +160,4 @@ fun box(): String {
|
|||||||
assertEquals(11111, runCallable(::sumOfParameters, 1, 10, 100, 1000, 10000))
|
assertEquals(11111, runCallable(::sumOfParameters, 1, 10, 100, 1000, 10000))
|
||||||
|
|
||||||
return "OK"
|
return "OK"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user