[JS] Support typeOf
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2010-2018 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.
|
||||
*/
|
||||
|
||||
// a package is omitted to get declarations directly under the module
|
||||
|
||||
// TODO: Remove once JsReflectionAPICallChecker supports more reflection types
|
||||
@file:Suppress("Unsupported")
|
||||
|
||||
import kotlin.reflect.*
|
||||
import kotlin.reflect.js.internal.*
|
||||
|
||||
@JsName("createKType")
|
||||
internal fun createKType(
|
||||
classifier: KClassifier,
|
||||
arguments: Array<KTypeProjection>,
|
||||
isMarkedNullable: Boolean
|
||||
) =
|
||||
KTypeImpl(classifier, arguments.asList(), isMarkedNullable)
|
||||
|
||||
@JsName("markKTypeNullable")
|
||||
internal fun markKTypeNullable(kType: KType) = KTypeImpl(kType.classifier!!, kType.arguments, true)
|
||||
|
||||
@JsName("createKTypeParameter")
|
||||
internal fun createKTypeParameter(
|
||||
name: String,
|
||||
upperBounds: Array<KType>,
|
||||
variance: String
|
||||
): KTypeParameter {
|
||||
val kVariance = when (variance) {
|
||||
"in" -> KVariance.IN
|
||||
"out" -> KVariance.OUT
|
||||
else -> KVariance.INVARIANT
|
||||
}
|
||||
|
||||
return KTypeParameterImpl(name, upperBounds.asList(), kVariance, false)
|
||||
}
|
||||
|
||||
@JsName("getStarKTypeProjection")
|
||||
internal fun getStarKTypeProjection(): KTypeProjection =
|
||||
KTypeProjection.STAR
|
||||
|
||||
@JsName("createCovariantKTypeProjection")
|
||||
internal fun createCovariantKTypeProjection(type: KType): KTypeProjection =
|
||||
KTypeProjection.covariant(type)
|
||||
|
||||
@JsName("createInvariantKTypeProjection")
|
||||
internal fun createInvariantKTypeProjection(type: KType): KTypeProjection =
|
||||
KTypeProjection.invariant(type)
|
||||
|
||||
@JsName("createContravariantKTypeProjection")
|
||||
internal fun createContravariantKTypeProjection(type: KType): KTypeProjection =
|
||||
KTypeProjection.contravariant(type)
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright 2010-2019 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.
|
||||
*/
|
||||
|
||||
package kotlin.reflect.js.internal
|
||||
|
||||
import kotlin.reflect.*
|
||||
|
||||
internal class KTypeImpl(
|
||||
override val classifier: KClassifier,
|
||||
override val arguments: List<KTypeProjection>,
|
||||
override val isMarkedNullable: Boolean
|
||||
) : KType {
|
||||
override val annotations: List<Annotation>
|
||||
get() = emptyList()
|
||||
|
||||
override fun equals(other: Any?): Boolean =
|
||||
other is KTypeImpl &&
|
||||
classifier == other.classifier && arguments == other.arguments && isMarkedNullable == other.isMarkedNullable
|
||||
|
||||
override fun hashCode(): Int =
|
||||
(classifier.hashCode() * 31 + arguments.hashCode()) * 31 + isMarkedNullable.hashCode()
|
||||
|
||||
override fun toString(): String {
|
||||
val kClass = (classifier as? KClass<*>)
|
||||
val classifierName = when {
|
||||
kClass == null -> classifier.toString()
|
||||
kClass.simpleName != null -> kClass.simpleName
|
||||
else -> "(non-denotable type)"
|
||||
}
|
||||
|
||||
val args =
|
||||
if (arguments.isEmpty()) ""
|
||||
else arguments.joinToString(", ", "<", ">") { it.asString() }
|
||||
val nullable = if (isMarkedNullable) "?" else ""
|
||||
|
||||
return classifierName + args + nullable
|
||||
}
|
||||
|
||||
// TODO: this should be the implementation of KTypeProjection.toString, see KT-30071
|
||||
private fun KTypeProjection.asString(): String {
|
||||
if (variance == null) return "*"
|
||||
return variance.prefixString() + type.toString()
|
||||
}
|
||||
}
|
||||
|
||||
internal fun KVariance.prefixString() =
|
||||
when (this) {
|
||||
KVariance.INVARIANT -> ""
|
||||
KVariance.IN -> "in "
|
||||
KVariance.OUT -> "out "
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* Copyright 2010-2019 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.
|
||||
*/
|
||||
|
||||
package kotlin.reflect.js.internal
|
||||
|
||||
import kotlin.reflect.*
|
||||
|
||||
internal data class KTypeParameterImpl(
|
||||
override val name: String,
|
||||
override val upperBounds: List<KType>,
|
||||
override val variance: KVariance,
|
||||
override val isReified: Boolean
|
||||
) : KTypeParameter {
|
||||
override fun toString(): String = name
|
||||
}
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
// a package is omitted to get declarations directly under the module
|
||||
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.reflect.*
|
||||
import kotlin.reflect.js.internal.*
|
||||
|
||||
@JsName("getKClass")
|
||||
@@ -62,4 +62,3 @@ private fun <T : Any> getOrCreateKClass(jClass: JsClass<T>): KClass<T> {
|
||||
SimpleKClassImpl(jClass)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user