[JS IR BE] Support enumValues<T>() and enumValueOf<T>(name)

This commit is contained in:
Svyatoslav Kuzmich
2018-10-01 15:51:52 +03:00
parent bdc3daf972
commit 7074909230
28 changed files with 122 additions and 27 deletions
+14 -2
View File
@@ -5,7 +5,9 @@
package kotlin
public class Enum<E : Enum<E>>(val name: String, val ordinal: Int) : Comparable<E> {
import kotlin.js.*
abstract class Enum<E : Enum<E>>(val name: String, val ordinal: Int) : Comparable<E> {
override fun compareTo(other: E) = ordinal.compareTo(other.ordinal)
@@ -16,4 +18,14 @@ public class Enum<E : Enum<E>>(val name: String, val ordinal: Int) : Comparable<
override fun toString() = name
companion object
}
}
// Use non-inline calls to enumValuesIntrinsic and enumValueOfIntrinsic calls in order
// for compiler to replace them with method calls of concrete enum classes after inlining.
// TODO: Figure out better solution (Inline hacks? Dynamic calls to stable mangled names?)
@SinceKotlin("1.1")
public inline fun <reified T : Enum<T>> enumValues(): Array<T> = enumValuesIntrinsic<T>()
@SinceKotlin("1.1")
public inline fun <reified T : Enum<T>> enumValueOf(name: String): T = enumValueOfIntrinsic<T>(name)
@@ -0,0 +1,14 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package kotlin.js
@PublishedApi
internal fun <T : Enum<T>> enumValuesIntrinsic(): Array<T> =
throw IllegalStateException("Should be replaced by compiler")
@PublishedApi
internal fun <T : Enum<T>> enumValueOfIntrinsic(name: String): T =
throw IllegalStateException("Should be replaced by compiler")