mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-05-08 08:31:26 +00:00
Make enum entries initialize before companion object. This helps in situation when companion object initializer refers to enum fields. JVM be generates <clinit> method which first initializes all enum fields and then runs companion object initializer. This commit introduces the similar behaviour in JS BE. The old behaviour was: initialize companion object in constructor. In enum, constructor is called to initialize enum fields, so previously companion object was initialized first, which is incorrect. See KT-16745
46 lines
785 B
Kotlin
Vendored
46 lines
785 B
Kotlin
Vendored
var result = ""
|
|
|
|
enum class E(a: String) {
|
|
X("x"),
|
|
Y("y");
|
|
|
|
init {
|
|
result += "E.init($a);"
|
|
}
|
|
|
|
companion object {
|
|
init {
|
|
result += "E.companion.init;"
|
|
val value = E.values()[0].name
|
|
result += "$value;"
|
|
}
|
|
}
|
|
}
|
|
|
|
enum class F(a: String) {
|
|
X("x"),
|
|
Y("y");
|
|
|
|
init {
|
|
result += "F.init($a);"
|
|
}
|
|
|
|
companion object {
|
|
init {
|
|
result += "F.companion.init;"
|
|
}
|
|
|
|
fun foo() {
|
|
result += "F.foo();$X;"
|
|
}
|
|
}
|
|
}
|
|
|
|
fun box(): String {
|
|
val y = E.Y
|
|
result += "${y.name};"
|
|
F.foo()
|
|
if (result != "E.init(x);E.init(y);E.companion.init;X;Y;F.init(x);F.init(y);F.companion.init;F.foo();X;") return "fail: $result"
|
|
|
|
return "OK"
|
|
} |