commonMain.com.sunnychung.lib.multiplatform.kotlite.model.ExtensionProperty.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlite-interpreter-jvm Show documentation
Show all versions of kotlite-interpreter-jvm Show documentation
A Kotlin Multiplatform library to interpret Kotlite code, which is a subset of Kotlin language, in runtime in a safe way.
The newest version!
package com.sunnychung.lib.multiplatform.kotlite.model
import com.sunnychung.lib.multiplatform.kotlite.Interpreter
import com.sunnychung.lib.multiplatform.kotlite.annotation.ModifyByAnalyzer
class ExtensionProperty(
val declaredName: String,
val typeParameters: List = emptyList(),
val receiver: String,
val type: String,
val getter: ((interpreter: Interpreter, receiver: RuntimeValue, typeArgs: Map) -> RuntimeValue)? = null,
val setter: ((interpreter: Interpreter, receiver: RuntimeValue, value: RuntimeValue, typeArgs: Map) -> Unit)? = null,
) {
init {
if (getter == null && setter == null) {
throw IllegalArgumentException("Missing getter or setter")
}
}
internal @ModifyByAnalyzer var transformedName: String? = null
internal @ModifyByAnalyzer var typeNode: TypeNode? = null
internal @ModifyByAnalyzer var receiverType: TypeNode? = null
fun typeArgumentsMap(actualReceiverType: DataType): Map {
return if (typeParameters.isNotEmpty()) {
var type: DataType? = actualReceiverType
// while (type != null && type.name != receiverType!!.name) {
// type = (type as? ObjectType)?.superType
// }
if (type != null && type.name != receiverType!!.name) {
type = (type as? ObjectType)?.findSuperType(receiverType!!.name)
}
if (type == null || type !is ObjectType) {
throw RuntimeException("Cannot find receiver type ${receiverType!!.name}")
}
type.clazz.typeParameters.mapIndexed { index, tp ->
tp.name to type.arguments[index]
}.toMap()
} else {
emptyMap()
}
}
}