[JS] Support typeOf
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
// !USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi
|
||||
// TARGET_BACKEND: JS
|
||||
// IGNORE_BACKEND: JS_IR
|
||||
// WITH_REFLECT
|
||||
|
||||
package test
|
||||
|
||||
import kotlin.reflect.KType
|
||||
import kotlin.reflect.typeOf
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class C
|
||||
|
||||
fun check(expected: String, actual: KType) {
|
||||
assertEquals(expected, actual.toString())
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
check("Any", typeOf<Any>())
|
||||
check("String", typeOf<String>())
|
||||
check("String?", typeOf<String?>())
|
||||
check("Unit", typeOf<Unit>())
|
||||
|
||||
check("C", typeOf<C>())
|
||||
check("C?", typeOf<C?>())
|
||||
|
||||
check("List<String>", typeOf<List<String>>())
|
||||
check("Map<in Number, *>?", typeOf<Map<in Number, *>?>())
|
||||
check("Enum<*>", typeOf<Enum<*>>())
|
||||
check("Enum<AnnotationRetention>", typeOf<Enum<AnnotationRetention>>())
|
||||
|
||||
check("Array<Any>", typeOf<Array<Any>>())
|
||||
check("Array<*>", typeOf<Array<*>>())
|
||||
check("Array<IntArray>", typeOf<Array<IntArray>>())
|
||||
check("Array<in Array<C>?>", typeOf<Array<in Array<C>?>>())
|
||||
|
||||
check("Int", typeOf<Int>())
|
||||
check("Int?", typeOf<Int?>())
|
||||
check("Boolean", typeOf<Boolean>())
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
// !USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi
|
||||
// TARGET_BACKEND: JS
|
||||
// IGNORE_BACKEND: JS_IR
|
||||
// WITH_REFLECT
|
||||
|
||||
package test
|
||||
|
||||
import kotlin.reflect.KType
|
||||
import kotlin.reflect.typeOf
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
inline class Z(val value: String)
|
||||
|
||||
fun check(expected: String, actual: KType) {
|
||||
assertEquals(expected, actual.toString())
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
check("Z", typeOf<Z>())
|
||||
check("Z?", typeOf<Z?>())
|
||||
check("Array<Z>", typeOf<Array<Z>>())
|
||||
check("Array<Z?>", typeOf<Array<Z?>>())
|
||||
|
||||
check("UInt", typeOf<UInt>())
|
||||
check("UInt?", typeOf<UInt?>())
|
||||
check("ULong?", typeOf<ULong?>())
|
||||
check("UShortArray", typeOf<UShortArray>())
|
||||
check("UShortArray?", typeOf<UShortArray?>())
|
||||
check("Array<UByteArray>", typeOf<Array<UByteArray>>())
|
||||
check("Array<UByteArray?>?", typeOf<Array<UByteArray?>?>())
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
// !USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi
|
||||
// TARGET_BACKEND: JS
|
||||
// IGNORE_BACKEND: JS_IR
|
||||
// WITH_REFLECT
|
||||
|
||||
import kotlin.test.*
|
||||
import kotlin.reflect.*
|
||||
|
||||
inline fun <reified R> kType() = typeOf<R>()
|
||||
|
||||
inline fun <reified R> kType(obj: R) = kType<R>()
|
||||
|
||||
class C<T>
|
||||
class D
|
||||
|
||||
fun <T> kTypeForCWithTypeParameter() = kType<C<T>>()
|
||||
|
||||
class Outer<T> {
|
||||
companion object Friend
|
||||
inner class Inner<S>
|
||||
}
|
||||
|
||||
object Object
|
||||
|
||||
fun testBasics1() {
|
||||
assertEquals("C<Int?>", kType<C<Int?>>().toString())
|
||||
assertEquals("C<C<Any>>", kType<C<C<Any>>>().toString())
|
||||
|
||||
assertEquals("C<T>", kTypeForCWithTypeParameter<D>().toString())
|
||||
assertEquals("Object", kType<Object>().toString())
|
||||
assertEquals("Friend", kType<Outer.Friend>().toString())
|
||||
}
|
||||
|
||||
fun testInner() {
|
||||
val innerKType = kType<Outer<D>.Inner<String>>()
|
||||
assertEquals(Outer.Inner::class, innerKType.classifier)
|
||||
assertEquals(String::class, innerKType.arguments.first().type!!.classifier)
|
||||
assertEquals(D::class, innerKType.arguments.last().type!!.classifier)
|
||||
}
|
||||
|
||||
fun testAnonymousObject() {
|
||||
val obj = object {}
|
||||
val objType = kType(obj)
|
||||
|
||||
assertEquals("(non-denotable type)", objType.toString())
|
||||
assertEquals(obj::class, objType.classifier)
|
||||
|
||||
assertTrue(objType.arguments.isEmpty())
|
||||
assertFalse(objType.isMarkedNullable)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
testBasics1()
|
||||
testInner()
|
||||
testAnonymousObject()
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// !USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi
|
||||
// TARGET_BACKEND: JS
|
||||
// IGNORE_BACKEND: JS_IR
|
||||
// WITH_REFLECT
|
||||
|
||||
package test
|
||||
|
||||
import kotlin.reflect.typeOf
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
interface I1
|
||||
interface I2
|
||||
interface I3
|
||||
interface I4
|
||||
interface I5
|
||||
interface I6
|
||||
interface I7
|
||||
|
||||
class C<T1, T2, T3, T4, T5, T6, T7>
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(
|
||||
"C<I1, I2, I3?, in I4, out I5, I6, I7?>",
|
||||
typeOf<C<I1, I2, I3?, in I4, out I5, I6, I7?>>().toString()
|
||||
)
|
||||
assertEquals(
|
||||
"C<out I1, I2?, I3, I4, I5, I6?, in I7>?",
|
||||
typeOf<C<out I1, I2?, I3, I4, I5, I6?, in I7>?>().toString()
|
||||
)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// !USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi
|
||||
// TARGET_BACKEND: JS
|
||||
// IGNORE_BACKEND: JS_IR
|
||||
// WITH_REFLECT
|
||||
|
||||
package test
|
||||
|
||||
import kotlin.reflect.typeOf
|
||||
import kotlin.reflect.KType
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
interface C
|
||||
|
||||
inline fun <reified T> get() = typeOf<T>()
|
||||
|
||||
inline fun <reified U> get1() = get<U?>()
|
||||
|
||||
inline fun <reified V> get2(): KType {
|
||||
return get1<Map<in V?, Array<V>>>()
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals("C?", get1<C>().toString())
|
||||
assertEquals("Map<in C?, Array<C>>?", get2<C>().toString())
|
||||
assertEquals("Map<in List<C>?, Array<List<C>>>?", get2<List<C>>().toString())
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
// !USE_EXPERIMENTAL: kotlin.ExperimentalStdlibApi
|
||||
// TARGET_BACKEND: JS
|
||||
// IGNORE_BACKEND: JS_IR
|
||||
// WITH_REFLECT
|
||||
|
||||
// MODULE: lib1
|
||||
// FILE: lib1.kt
|
||||
import kotlin.reflect.typeOf
|
||||
|
||||
inline fun <reified T> get() = typeOf<T>()
|
||||
|
||||
// MODULE: lib2(lib1)
|
||||
// FILE: lib2.kt
|
||||
inline fun <reified U> get1() = get<U?>()
|
||||
|
||||
// MODULE: lib3(lib1, lib2)
|
||||
// FILE: lib3.kt
|
||||
import kotlin.reflect.KType
|
||||
|
||||
inline fun <reified V> get2(): KType {
|
||||
return get1<Map<in V?, Array<V>>>()
|
||||
}
|
||||
|
||||
// MODULE: main(lib1, lib2, lib3)
|
||||
// FILE: lib4.kt
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
interface C
|
||||
|
||||
fun box(): String {
|
||||
assertEquals("C?", get1<C>().toString())
|
||||
assertEquals("Map<in C?, Array<C>>?", get2<C>().toString())
|
||||
assertEquals("Map<in List<C>?, Array<List<C>>>?", get2<List<C>>().toString())
|
||||
|
||||
assertEquals("Short?", get1<Short>().toString())
|
||||
assertEquals("Map<in Short?, Array<Short>>?", get2<Short>().toString())
|
||||
assertEquals("Map<in List<Short>?, Array<List<Short>>>?", get2<List<Short>>().toString())
|
||||
|
||||
return "OK"
|
||||
}
|
||||
Reference in New Issue
Block a user