All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.sunnychung.lib.multiplatform.kotlite.model.ExtensionProperty.kt Maven / Gradle / Ivy

Go to download

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()
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy