Files
kotlin-fork/compiler/testData/codegen/box/bridges/substitutionInSuperClass/simple.kt
T
Sergej Jaskiewicz 4a724611d8 [JS IR] Extract varargs in bridges generated for external methods
The issue this commit fixes occurs when we have an external interface
implemented by a Kotlin class, if that interface has methods with
varargs.

Kotlin functions expect varargs passed as arrays, but JavaScript code
may be unaware of this convention.

So, when generating a bridge for external interface method
implementaion, we insert some additional logic for extracting varargs
using the JavaScript `arguments` object.

A simplified example:

```kotlin
external interface Adder {
    fun sum(vararg numbers: Int): Int
}

class AdderImpl: Adder {
    override fun sum(vararg numbers: Int) = numbers.sum()
}
```

For `AdderImpl` we generate the following JS code:
```js
AdderImpl.prototype.sum_69wd7h_k$ = function (numbers) {
  return sum(numbers);
};
AdderImpl.prototype.sum = function () {
  var numbers = new Int32Array([].slice.call(arguments));
  return this.sum_69wd7h_k$(numbers);
};
```

#KT-15223 Fixed
2022-01-27 11:06:17 +00:00

23 lines
407 B
Kotlin
Vendored

open class A<T> {
open fun foo(t: T, vararg xs: Int) = "A"
}
open class B : A<String>()
class Z : B() {
override fun foo(t: String, vararg xs: Int) = "Z"
}
fun box(): String {
val z = Z()
val b: B = z
val a: A<String> = z
return when {
z.foo("") != "Z" -> "Fail #1"
b.foo("") != "Z" -> "Fail #2"
a.foo("") != "Z" -> "Fail #3"
else -> "OK"
}
}