KT-16252 IDL2K: Add ItemArrayLike interface implementation to collection-like classes

This commit is contained in:
Sergey Mashkov
2017-03-10 18:26:16 +03:00
parent a795313c7d
commit a5d6541f1e
4 changed files with 31 additions and 4 deletions
@@ -76,4 +76,17 @@ val requiredArguments = setOf(
val inheritanceExclude = mapOf(
"SVGAElement" to setOf("HTMLHyperlinkElementUtils")
)
val kotlinBuiltinInterfaces = mapOf(
"ItemArrayLike" to GenerateTraitOrClass("ItemArrayLike", "org.w3c.dom", GenerateDefinitionKind.INTERFACE, emptyList(),
memberAttributes = mutableListOf(GenerateAttribute("length", SimpleType("Int", false), null, false, AttributeKind.VAL, false, false, false, false)),
memberFunctions = mutableListOf(GenerateFunction("item", DynamicType, listOf(
GenerateAttribute("index", SimpleType("Int", false), null, false, AttributeKind.ARGUMENT, false, false, false, false)
), NativeGetterOrSetter.NONE, false, false)),
constants = emptyList(),
generateBuilderFunction = false,
primaryConstructor = null,
secondaryConstructors = emptyList()
)
)
+14 -1
View File
@@ -161,7 +161,7 @@ fun generateTrait(repository: Repository, iface: InterfaceDefinition): GenerateT
ConstructorWithSuperTypeCall(constructorAsFunction, secondaryConstructor)
}
return GenerateTraitOrClass(iface.name, iface.namespace, entityKind, (iface.superTypes + extensions.map { it.name }).distinct(),
val result = GenerateTraitOrClass(iface.name, iface.namespace, entityKind, (iface.superTypes + extensions.map { it.name }).distinct(),
memberAttributes = iface.mapAttributes(repository).toMutableList(),
memberFunctions = iface.mapOperations(repository).toMutableList(),
constants = (iface.constants.map { it.mapConstant(repository) } + extensions.flatMap { it.constants.map { it.mapConstant(repository) } }.distinct().toList()),
@@ -169,6 +169,19 @@ fun generateTrait(repository: Repository, iface: InterfaceDefinition): GenerateT
secondaryConstructors = secondaryConstructorsWithCall,
generateBuilderFunction = iface.dictionary
)
return markAsArrayLikeIfApplicable(result)
}
fun markAsArrayLikeIfApplicable(iface: GenerateTraitOrClass): GenerateTraitOrClass {
fun isInt(type: Type) = type is SimpleType && type.type == "Int"
val lengthProperty = iface.memberAttributes.singleOrNull { it.name == "length" && isInt(it.type) }
val itemAccessFunction = iface.memberFunctions.singleOrNull { it.name == "item" && it.arguments.map { isInt(it.type) } == listOf(true) && it.returnType != UnitType }
if (lengthProperty == null || itemAccessFunction == null) return iface
return iface.copy(superTypes = iface.superTypes + "ItemArrayLike<${itemAccessFunction.returnType.dropNullable().render()}>")
}
fun generateConstructorAsFunction(repository: Repository, constructor: ExtendedAttribute) = generateFunction(
@@ -159,7 +159,7 @@ fun Appendable.render(allTypes: Map<String, GenerateTraitOrClass>, enums: List<E
GenerateDefinitionKind.INTERFACE -> append("interface ")
}
val allSuperTypes = iface.allSuperTypes(allTypes)
val allSuperTypes = iface.allSuperTypes(allTypes + kotlinBuiltinInterfaces)
val allSuperTypesNames = allSuperTypes.map { it.name }.toSet()
append(iface.name)
@@ -171,7 +171,8 @@ fun Appendable.render(allTypes: Map<String, GenerateTraitOrClass>, enums: List<E
val superTypesExclude = inheritanceExclude[iface.name] ?: emptySet()
val superTypesWithCalls =
iface.superTypes.filter { it in allSuperTypesNames }.filter { it !in superTypesExclude } +
(typeNamesToUnions[iface.name] ?: emptyList())
(typeNamesToUnions[iface.name] ?: emptyList()) +
(iface.superTypes.filter { it.substringBefore("<") in kotlinBuiltinInterfaces }) // TODO in theory we have to parse type but for now it is the only place needs it so let's just cut string
if (superTypesWithCalls.isNotEmpty()) {
superTypesWithCalls.joinTo(this, ", ", " : ")
@@ -50,7 +50,7 @@ fun GenerateTraitOrClass.allSuperTypes(all: Map<String, GenerateTraitOrClass>) =
tailrec fun allSuperTypesImpl(roots: List<GenerateTraitOrClass>, all: Map<String, GenerateTraitOrClass>, result: MutableSet<GenerateTraitOrClass>) {
if (roots.isNotEmpty()) {
allSuperTypesImpl(roots.flatMap { it.superTypes }.map { all[it] }.filterNotNull().filter { result.add(it) }, all, result)
allSuperTypesImpl(roots.flatMap { it.superTypes }.map { all[it] ?: all[it.substringBefore("<")] }.filterNotNull().filter { result.add(it) }, all, result)
}
}