[Wasm] Support lazy associated object initialisation

Fix #KT-63939
This commit is contained in:
Igor Yakovlev
2024-01-24 16:01:14 +01:00
committed by Space Team
parent e7c1de1356
commit 552ee1ee38
12 changed files with 140 additions and 58 deletions
@@ -8,35 +8,16 @@ package kotlin.wasm.internal
import kotlin.reflect.wasm.internal.KClassImpl
import kotlin.reflect.KClass
internal var associatedObjects: Map<ULong, Any>? = null
@PublishedApi
internal fun findAssociatedObject(klass: KClass<*>, key: Int): Any? {
val klassId = (klass as? KClassImpl<*>)?.typeData?.typeId ?: return null
val map = associatedObjects ?: run {
val newMap: MutableMap<ULong, Any> = mutableMapOf()
initAssociatedObjects(newMap)
associatedObjects = newMap
newMap
}
return map[packIntoULong(klassId, key)]
return tryGetAssociatedObject(klassId, key)
}
internal fun packIntoULong(a: Int, b: Int): ULong =
(a.toUInt().toULong() shl Int.SIZE_BITS) or b.toUInt().toULong()
internal fun addAssociatedObject(
mapToInit: MutableMap<ULong, Any>,
klassId: Int,
keyId: Int,
instance: Any
) {
mapToInit[packIntoULong(klassId, keyId)] = instance
}
internal fun initAssociatedObjects(@Suppress("UNUSED_PARAMETER") mapToInit: MutableMap<ULong, Any>) {
internal fun tryGetAssociatedObject(
@Suppress("UNUSED_PARAMETER") klassId: Int,
@Suppress("UNUSED_PARAMETER") keyId: Int,
): Any? {
// Init implicitly with AssociatedObjectsLowering
// addAssociatedObject(mapToInit, ...)
return null
}