JS backend: basic support for class literals.

Added:
* the ability to get KClass using class literals (`::class`);
* the ability to get KClass from JsClass and vice versa;
* the ability to get simpleName.

 #KT-13345 Fixed
This commit is contained in:
Zalim Bashorov
2016-10-04 17:39:37 +03:00
parent ace10f46b2
commit 3c520a3ce3
14 changed files with 310 additions and 21 deletions
@@ -16,13 +16,24 @@
package kotlin.js
import kotlin.reflect.KClass
import kotlin.reflect.js.internal.KClassImpl
@native
interface JsClass<T : Any> {
val name: String
}
@native
@Deprecated("Use class literal and extension property `js` instead.", replaceWith = ReplaceWith("T::class.js"))
fun <T : Any> jsClass(): JsClass<T> = noImpl
@Deprecated("Use class literal and extension property `js` instead.", replaceWith = ReplaceWith("this::class.js"))
val <T : Any> T.jsClass: JsClass<T>
get() = js("Object").getPrototypeOf(this).constructor
val <T : Any> KClass<T>.js: JsClass<T>
get() = (this as KClassImpl<T>).jClass
val <T : Any> JsClass<T>.kotlin: KClass<T>
get() = KClassImpl<T>(this)
+81
View File
@@ -0,0 +1,81 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kotlin.reflect.js.internal
import kotlin.reflect.*
internal class KClassImpl<T : Any>(
internal val jClass: JsClass<T>
) : KClass<T> {
private val metadata = jClass.asDynamic().`$metadata$`
// TODO: use FQN
private val hashCode = simpleName?.hashCode() ?: 0
override val simpleName: String?
get() = metadata?.simpleName
override val annotations: List<Annotation>
get() = TODO()
override val constructors: Collection<KFunction<T>>
get() = TODO()
override val isAbstract: Boolean
get() = TODO()
override val isCompanion: Boolean
get() = TODO()
override val isData: Boolean
get() = TODO()
override val isFinal: Boolean
get() = TODO()
override val isInner: Boolean
get() = TODO()
override val isOpen: Boolean
get() = TODO()
override val isSealed: Boolean
get() = TODO()
override val members: Collection<KCallable<*>>
get() = TODO()
override val nestedClasses: Collection<KClass<*>>
get() = TODO()
override val objectInstance: T?
get() = TODO()
override val qualifiedName: String?
get() = TODO()
override val supertypes: List<KType>
get() = TODO()
override val typeParameters: List<KTypeParameter>
get() = TODO()
override val visibility: KVisibility?
get() = TODO()
override fun equals(other: Any?): Boolean {
return other is KClassImpl<*> && jClass == other.jClass
}
override fun hashCode(): Int {
return hashCode
}
override fun isInstance(value: Any?): Boolean {
TODO()
}
override fun toString(): String {
// TODO: use FQN
return "class $simpleName"
}
}
+44
View File
@@ -0,0 +1,44 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// a package is omitted to get declarations directly under the module
import kotlin.reflect.KClass
import kotlin.reflect.js.internal.KClassImpl
@JsName("getKClass")
private fun <T : Any> getKClass(jClass: JsClass<T>): KClass<T> = getOrCreateKClass(jClass)
@JsName("getKClassFromExpression")
private fun <T : Any> getKClassFromExpression(e: T): KClass<T> = getOrCreateKClass(e.jsClass)
private fun <T : Any> getOrCreateKClass(jClass: JsClass<T>): KClass<T> {
val metadata = jClass.asDynamic().`$metadata$`
return if (metadata != null) {
if (metadata.`$kClass$` == null) {
val kClass = KClassImpl(jClass)
metadata.`$kClass$` = kClass
kClass
}
else {
metadata.`$kClass$`
}
}
else {
KClassImpl(jClass)
}
}