[Wasm] Replace ClassId and InterfaceId with single TypeId

This commit is contained in:
Igor Yakovlev
2023-03-30 20:27:47 +02:00
committed by Space Team
parent 5a46cb1c40
commit 49beec33b4
13 changed files with 103 additions and 86 deletions
@@ -19,7 +19,10 @@ internal const val TYPE_INFO_SUPER_TYPE_OFFSET = TYPE_INFO_TYPE_SIMPLE_NAME_PRT_
internal const val TYPE_INFO_ITABLE_SIZE_OFFSET = TYPE_INFO_SUPER_TYPE_OFFSET + TYPE_INFO_ELEMENT_SIZE
internal const val TYPE_INFO_ITABLE_OFFSET = TYPE_INFO_ITABLE_SIZE_OFFSET + TYPE_INFO_ELEMENT_SIZE
internal class TypeInfoData(val typeId: Int, val isInterface: Boolean, val packageName: String, val typeName: String)
internal class TypeInfoData(val typeId: Int, val packageName: String, val typeName: String)
internal val TypeInfoData.isInterfaceType
get() = typeId < 0
internal fun getTypeInfoTypeDataByPtr(typeInfoPtr: Int): TypeInfoData {
val fqNameLength = wasm_i32_load(typeInfoPtr + TYPE_INFO_TYPE_PACKAGE_NAME_LENGTH_OFFSET)
@@ -30,7 +33,7 @@ internal fun getTypeInfoTypeDataByPtr(typeInfoPtr: Int): TypeInfoData {
val simpleNamePtr = wasm_i32_load(typeInfoPtr + TYPE_INFO_TYPE_SIMPLE_NAME_PRT_OFFSET)
val packageName = stringLiteral(fqNameId, fqNamePtr, fqNameLength)
val simpleName = stringLiteral(simpleNameId, simpleNamePtr, simpleNameLength)
return TypeInfoData(typeInfoPtr, isInterface = false, packageName, simpleName)
return TypeInfoData(typeInfoPtr, packageName, simpleName)
}
internal fun getSuperTypeId(typeInfoPtr: Int): Int =
@@ -57,11 +60,7 @@ internal fun <T> wasmIsInterface(obj: Any): Boolean =
implementedAsIntrinsic
@ExcludedFromCodegen
internal fun <T> wasmClassId(): Int =
implementedAsIntrinsic
@ExcludedFromCodegen
internal fun <T> wasmInterfaceId(): Int =
internal fun <T> wasmTypeId(): Int =
implementedAsIntrinsic
@ExcludedFromCodegen
@@ -5,7 +5,7 @@
package kotlin.reflect
import kotlin.wasm.internal.wasmClassId
import kotlin.wasm.internal.wasmTypeId
import kotlin.wasm.internal.findAssociatedObject
/**
@@ -40,4 +40,4 @@ public annotation class AssociatedObjectKey
@ExperimentalAssociatedObjects
@Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE")
public inline fun <reified T : Annotation> KClass<*>.findAssociatedObject(): Any? =
findAssociatedObject(this, wasmClassId<T>())
findAssociatedObject(this, wasmTypeId<T>())
@@ -8,6 +8,7 @@ import kotlin.reflect.*
import kotlin.wasm.internal.TypeInfoData
import kotlin.wasm.internal.getSuperTypeId
import kotlin.wasm.internal.isInterfaceById
import kotlin.wasm.internal.isInterfaceType
internal object NothingKClassImpl : KClass<Nothing> {
override val simpleName: String = "Nothing"
@@ -37,12 +38,16 @@ internal class KClassImpl<T : Any>(internal val typeData: TypeInfoData) : KClass
return false
}
override fun isInstance(value: Any?): Boolean = value?.let {
if (typeData.isInterface) isInterfaceById(it, typeData.typeId) else checkSuperTypeInstance(it)
} ?: false
override fun isInstance(value: Any?): Boolean {
if (value !is Any) return false
return when (typeData.isInterfaceType) {
true -> isInterfaceById(value, typeData.typeId)
false -> checkSuperTypeInstance(value)
}
}
override fun equals(other: Any?): Boolean =
(this === other) || (other is KClassImpl<*> && other.typeData.isInterface == typeData.isInterface && other.typeData.typeId == typeData.typeId)
(this === other) || (other is KClassImpl<*> && other.typeData.typeId == typeData.typeId)
override fun hashCode(): Int = typeData.typeId