JS/RTTI. Fix run-time type checking against Comparable

This commit is contained in:
Alexey Tsvetkov
2015-06-25 00:11:17 +03:00
committed by Alexey Andreev
parent 390d71ac8d
commit 9bb60b48b2
6 changed files with 118 additions and 6 deletions
@@ -407,6 +407,11 @@ public final class Namer {
return invokeFunctionAndSetTypeCheckMetadata("isAny", null, TypeCheck.IS_ANY);
}
@NotNull
public JsExpression isComparable() {
return kotlin("isComparable");
}
@NotNull
private JsExpression invokeFunctionAndSetTypeCheckMetadata(
@NotNull String functionName,
@@ -27,6 +27,7 @@ import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor;
import org.jetbrains.kotlin.js.descriptorUtils.DescriptorUtilsKt;
import org.jetbrains.kotlin.js.patterns.NamePredicate;
import org.jetbrains.kotlin.js.patterns.typePredicates.TypePredicatesPackage;
import org.jetbrains.kotlin.js.translate.context.Namer;
import org.jetbrains.kotlin.js.translate.context.TemporaryVariable;
import org.jetbrains.kotlin.js.translate.context.TranslationContext;
@@ -130,15 +131,12 @@ public final class PatternTranslator extends AbstractTranslator {
@NotNull
private JsExpression doGetIsTypeCheckCallable(@NotNull KotlinType type) {
if (isAnyOrNullableAny(type)) return namer().isAny();
if (isFunctionTypeOrSubtype(type)) return namer().isTypeOf(program().getStringLiteral("function"));
if (isArray(type)) return Namer.IS_ARRAY_FUN_REF;
JsExpression builtinCheck = getIsTypeCheckCallableForBuiltin(type);
if (builtinCheck != null) return builtinCheck;
builtinCheck = getIsTypeCheckCallableForPrimitiveBuiltin(type);
if (builtinCheck != null) return builtinCheck;
TypeParameterDescriptor typeParameterDescriptor = getTypeParameterDescriptorOrNull(type);
if (typeParameterDescriptor != null) {
if (typeParameterDescriptor.isReified()) {
@@ -152,6 +150,19 @@ public final class PatternTranslator extends AbstractTranslator {
return namer().isInstanceOf(typeName);
}
@Nullable
private JsExpression getIsTypeCheckCallableForBuiltin(@NotNull KotlinType type) {
if (isAnyOrNullableAny(type)) return namer().isAny();
if (isFunctionOrExtensionFunctionType(type)) return namer().isTypeOf(program().getStringLiteral("function"));
if (isArray(type)) return Namer.IS_ARRAY_FUN_REF;
if (TypePredicatesPackage.getCOMPARABLE().apply(type)) return namer().isComparable();
return null;
}
@Nullable
private JsExpression getIsTypeCheckCallableForBuiltin(@NotNull KotlinType type) {
Name typeName = DescriptorUtilsKt.getNameIfStandardType(type);
+9
View File
@@ -126,6 +126,15 @@
return (typeof value) == "string" && value.length == 1;
};
Kotlin.isComparable = function (value) {
var type = typeof value;
return type === "string" ||
type === "boolean" ||
Kotlin.isNumber(value) ||
Kotlin.isType(value, Kotlin.Comparable);
};
Kotlin.charInc = function (value) {
return String.fromCharCode(value.charCodeAt(0)+1);
};
+25
View File
@@ -0,0 +1,25 @@
package foo
class A : Comparable<A> {
override fun compareTo(other: A): Int = 0
}
class B
fun test(x: Any?): Boolean = x is Comparable<*>
fun box(): String {
assertEquals(true, test(A()), "A()")
assertEquals(true, test("abc"), "\"abc\"")
assertEquals(true, test('a'), "\'a\'")
assertEquals(true, test(0), "0")
assertEquals(true, test(0.toChar()), "0.toChar()")
assertEquals(true, test(0.toByte()), "0.toByte()")
assertEquals(true, test(0.toShort()), "0.toShort()")
assertEquals(true, test(0.toLong()), "0.toLong()")
assertEquals(true, test(0.toDouble()), "0.toDouble()")
assertEquals(true, test(0.toFloat()), "0.toFloat()")
assertEquals(false, test(B()), "B()")
return "OK"
}