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
@@ -0,0 +1,56 @@
/*
* 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 org.jetbrains.kotlin.js.patterns.typePredicates
import com.google.common.base.Predicate
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.types.KotlinType
interface TypePredicate : Predicate<KotlinType> {
override fun apply(type: KotlinType?): Boolean
}
private val KOTLIN = TypePredicateImpl("kotlin")
val COMPARABLE: TypePredicate = KOTLIN.inner("Comparable")
val CHAR_SEQUENCE: TypePredicate = KOTLIN.inner("CharSequence")
// TODO: replace all NamePredicate usages to TypePredicate
/**
* A more precise NamePredicate analog, that checks fully-qualified
* name rather than simple name.
*
* @see org.jetbrains.kotlin.js.patterns.NamePredicate
*/
private class TypePredicateImpl
private constructor (private val nameParts: List<String>)
: TypePredicate {
constructor(name: String) : this(listOf(name))
override fun apply(type: KotlinType?): Boolean {
var descriptor: DeclarationDescriptor? = type?.constructor?.declarationDescriptor ?: return false
for (i in nameParts.lastIndex downTo 0) {
if (nameParts[i] != descriptor?.name?.asString()) return false
descriptor = descriptor?.containingDeclaration
}
return true
}
fun inner(name: String) = TypePredicateImpl(nameParts + listOf(name))
}
@@ -41,6 +41,12 @@ public class RttiTestGenerated extends AbstractRttiTest {
doTest(fileName);
}
@TestMetadata("isComparable.kt")
public void testIsComparable() throws Exception {
String fileName = JetTestUtils.navigationMetadata("js/js.translator/testData/rtti/cases/isComparable.kt");
doTest(fileName);
}
@TestMetadata("isJsPrimitiveType.kt")
public void testIsJsPrimitiveType() throws Exception {
String fileName = JetTestUtils.navigationMetadata("js/js.translator/testData/rtti/cases/isJsPrimitiveType.kt");
@@ -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"
}