JS: fixed subSequence in CharSequence descendants (KT-14035) + added support for function intrics in delegation

This commit is contained in:
Anton Bannykh
2017-01-20 14:06:06 +03:00
parent 4fe040eeb9
commit 847ec9e550
5 changed files with 60 additions and 3 deletions
@@ -227,7 +227,6 @@ class NameSuggestion {
if (overriddenDescriptor is FunctionDescriptor) {
when (overriddenDescriptor.fqNameUnsafe.asString()) {
"kotlin.CharSequence.subSequence" -> return NameAndStability("substring", true)
"kotlin.CharSequence.get" -> return NameAndStability("charAt", true)
"kotlin.Any.equals" -> return NameAndStability("equals", true)
}
+11 -1
View File
@@ -36,4 +36,14 @@ internal fun arrayIterator(array: dynamic): MutableIterator<dynamic> {
internal class PropertyMetadata(@JsName("callableName") val name: String)
@JsName("noWhenBranchMatched")
internal fun noWhenBranchMatched(): Nothing = throw NoWhenBranchMatchedException()
internal fun noWhenBranchMatched(): Nothing = throw NoWhenBranchMatchedException()
@JsName("subSequence")
fun subSequence(c: CharSequence, startIndex: Int, endIndex: Int): CharSequence {
if (c is String) {
return c.substring(startIndex, endIndex)
}
else {
return c.asDynamic().`subSequence_vux9f0$`(startIndex, endIndex)
}
}
@@ -63,6 +63,9 @@ public final class TopLevelFIF extends CompositeFIF {
@NotNull
public static final KotlinFunctionIntrinsic KOTLIN_EQUALS = new KotlinFunctionIntrinsic("equals");
@NotNull
public static final KotlinFunctionIntrinsic KOTLIN_SUBSEQUENCE = new KotlinFunctionIntrinsic("subSequence");
@NotNull
private static final DescriptorPredicate HASH_CODE_IN_ANY = pattern("kotlin", "Any", "hashCode");
@NotNull
@@ -145,6 +148,16 @@ public final class TopLevelFIF extends CompositeFIF {
}
};
private static final FunctionIntrinsic STRING_SUBSTRING = new FunctionIntrinsic() {
@NotNull
@Override
public JsExpression apply(
@Nullable JsExpression receiver, @NotNull List<JsExpression> arguments, @NotNull TranslationContext context
) {
return new JsInvocation(new JsNameRef("substring", receiver), arguments);
}
};
@NotNull
public static final KotlinFunctionIntrinsic TO_STRING = new KotlinFunctionIntrinsic("toString");
@@ -159,6 +172,8 @@ public final class TopLevelFIF extends CompositeFIF {
add(HASH_CODE_IN_ANY, KOTLIN_HASH_CODE);
add(pattern(NamePredicate.PRIMITIVE_NUMBERS, "equals"), KOTLIN_EQUALS);
add(pattern("String|Boolean|Char|Number.equals"), KOTLIN_EQUALS);
add(pattern("String.subSequence"), STRING_SUBSTRING);
add(pattern("CharSequence.subSequence"), KOTLIN_SUBSEQUENCE);
add(pattern("kotlin", "iterator").isExtensionOf(FQ_NAMES.iterator.asString()), RETURN_RECEIVER_INTRINSIC);
add(pattern("kotlin.js", "Json", "get"), ArrayFIF.GET_INTRINSIC);
@@ -63,7 +63,15 @@ fun generateDelegateCall(
args.add(JsNameRef(jsParamName))
}
val functionObject = simpleReturnFunction(context.getScopeForDescriptor(fromDescriptor), JsInvocation(overriddenMemberFunctionRef, args))
val intrinsic = context.intrinsics().getFunctionIntrinsic(toDescriptor)
val invocation = if (intrinsic.exists()) {
intrinsic.apply(thisObject, args, context)
}
else {
JsInvocation(overriddenMemberFunctionRef, args)
}
val functionObject = simpleReturnFunction(context.getScopeForDescriptor(fromDescriptor), invocation)
functionObject.parameters.addAll(parameters)
context.addFunctionToPrototype(classDescriptor, fromDescriptor, functionObject)
@@ -1,5 +1,15 @@
package foo
class CC(val s: CharSequence) : CharSequence by s, MyCharSequence {}
interface MyCharSequence {
val length: Int
operator fun get(index: Int): Char
fun subSequence(startIndex: Int, endIndex: Int): CharSequence
}
fun box(): String {
val kotlin: String = "kotlin"
@@ -11,5 +21,20 @@ fun box(): String {
val tlin = (kotlin as CharSequence).subSequence(2, 6)
if (tlin.toString() != "tlin") return "Fail 2: $tlin"
val cc: CharSequence = CC(kotlin)
if (cc.length != 6) return "Fail 3: ${cc.length}"
if (cc.subSequence(0, 3) != kot) return "Fail 4"
if (cc[2] != 't') return "Fail 5: ${cc[2]}"
val mcc: MyCharSequence = CC(kotlin)
if (mcc.length != 6) return "Fail 6: ${mcc.length}"
if (mcc.subSequence(0, 3) != kot) return "Fail 7"
if (mcc[2] != 't') return "Fail 8: ${mcc[2]}"
val ccc = CC(cc)
if (ccc.length != 6) return "Fail 6: ${ccc.length}"
if (ccc.subSequence(0, 3) != kot) return "Fail 7"
if (ccc[2] != 't') return "Fail 8: ${ccc[2]}"
return "OK"
}