KT-2752: add draft implementation of JsName annotation. Fix some tests using the annotation
This commit is contained in:
@@ -23,7 +23,8 @@ enum class PredefinedAnnotation(fqName: String) {
|
||||
NATIVE("kotlin.js.native"),
|
||||
NATIVE_INVOKE("kotlin.js.nativeInvoke"),
|
||||
NATIVE_GETTER("kotlin.js.nativeGetter"),
|
||||
NATIVE_SETTER("kotlin.js.nativeSetter");
|
||||
NATIVE_SETTER("kotlin.js.nativeSetter"),
|
||||
JS_NAME("kotlin.js.JsName");
|
||||
|
||||
val fqName: FqName = FqName(fqName)
|
||||
|
||||
|
||||
@@ -135,11 +135,12 @@ class FQNGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
return if (needsStableMangling(resolvedDescriptor)) {
|
||||
Pair(getStableMangledName(baseName, getArgumentTypesAsString(resolvedDescriptor)), true)
|
||||
}
|
||||
else {
|
||||
Pair(baseName, false)
|
||||
val explicitName = getJsName(descriptor)
|
||||
return when {
|
||||
explicitName != null -> Pair(explicitName, true)
|
||||
needsStableMangling(resolvedDescriptor) ->
|
||||
Pair(getStableMangledName(baseName, getArgumentTypesAsString(resolvedDescriptor)), true)
|
||||
else -> Pair(baseName, false)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -167,8 +168,7 @@ class FQNGenerator {
|
||||
}
|
||||
|
||||
private fun getStableMangledName(suggestedName: String, forCalculateId: String): String {
|
||||
val absHashCode = Math.abs(forCalculateId.hashCode())
|
||||
val suffix = if (absHashCode == 0) "" else "_" + Integer.toString(absHashCode, Character.MAX_RADIX) + "$"
|
||||
val suffix = if (forCalculateId.isEmpty()) "" else "_${mangledId(forCalculateId)}\$"
|
||||
return suggestedName + suffix
|
||||
}
|
||||
|
||||
@@ -187,7 +187,9 @@ class FQNGenerator {
|
||||
return when (containingDeclaration) {
|
||||
is PackageFragmentDescriptor -> descriptor.visibility.isPublicAPI
|
||||
is ClassDescriptor -> {
|
||||
if (descriptor.modality == Modality.OPEN || descriptor.modality == Modality.ABSTRACT) return true
|
||||
if (descriptor.modality == Modality.OPEN || descriptor.modality == Modality.ABSTRACT) {
|
||||
return descriptor.visibility.isPublicAPI
|
||||
}
|
||||
|
||||
// valueOf() is created in the library with a mangled name for every enum class
|
||||
if (descriptor is FunctionDescriptor && descriptor.isEnumValueOfMethod()) return true
|
||||
@@ -210,4 +212,11 @@ class FQNGenerator {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
@JvmStatic fun mangledId(forCalculateId: String): String {
|
||||
val absHashCode = Math.abs(forCalculateId.hashCode())
|
||||
return if (absHashCode != 0) Integer.toString(absHashCode, Character.MAX_RADIX) else ""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,6 +126,19 @@ public final class AnnotationsUtils {
|
||||
return hasAnnotationOrInsideAnnotatedClass(descriptor, PredefinedAnnotation.LIBRARY);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String getJsName(@NotNull DeclarationDescriptor descriptor) {
|
||||
AnnotationDescriptor annotation = getAnnotationByName(descriptor, PredefinedAnnotation.JS_NAME.getFqName());
|
||||
if (annotation == null) return null;
|
||||
|
||||
ConstantValue<?> value = annotation.getAllValueArguments().values().iterator().next();
|
||||
assert value != null : "JsName annotation should always declare string parameter";
|
||||
|
||||
Object result = value.getValue();
|
||||
assert result instanceof String : "Parameter of JsName annotation should be string";
|
||||
return (String) result;
|
||||
}
|
||||
|
||||
public static boolean isPredefinedObject(@NotNull DeclarationDescriptor descriptor) {
|
||||
for (PredefinedAnnotation annotation : PredefinedAnnotation.values()) {
|
||||
if (hasAnnotationOrInsideAnnotatedClass(descriptor, annotation)) {
|
||||
|
||||
Reference in New Issue
Block a user