IR JS: support findAssociatedObject feature (KT-37418 fixed)

This commit is contained in:
Anton Bannykh
2020-04-16 17:33:44 +03:00
parent f4b6e1bff6
commit 442331acc9
14 changed files with 354 additions and 5 deletions
@@ -0,0 +1,19 @@
/*
* Copyright 2010-2020 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.
*/
import kotlin.reflect.*
import kotlin.reflect.js.internal.*
@PublishedApi
internal fun <T : Annotation> KClass<*>.findAssociatedObject(annotationClass: KClass<T>): Any? {
return if (this is KClassImpl<*> && annotationClass is KClassImpl<T>) {
val key = annotationClass.jClass.asDynamic().`$metadata$`.associatedObjectKey?.unsafeCast<Int>() ?: return null
val map = this.jClass.asDynamic().`$metadata$`.associatedObjects ?: return null
val factory = map[key] ?: return null
return factory()
} else {
null
}
}