Leave undefined values for undefined params in idl-generated code unassigned

https://upsource.jetbrains.com/kotlin/review/KOTLIN-CR-2535
This commit is contained in:
Shagen Ogandzhanian
2018-12-07 16:02:13 +01:00
parent 1ad2434fa0
commit 1f0bca12e1
6 changed files with 33 additions and 47 deletions
@@ -17,7 +17,6 @@
package org.jetbrains.idl2k
import org.jetbrains.idl2k.util.mapEnumConstant
import java.io.*
import java.math.BigInteger
private fun <O : Appendable> O.indent(commented: Boolean = false, level: Int) {
@@ -88,7 +87,7 @@ private val keywords = setOf("interface", "is", "as")
private fun String.parse() = if (this.startsWith("0x")) BigInteger(this.substring(2), 16) else BigInteger(this)
private fun String.replaceWrongConstants(type: Type) = when {
this == "null" && type.nullable -> "null"
this == "undefined" && type.nullable -> "undefined"
this == "definedExternally" || type is SimpleType && type.type == "Int" && parse() > BigInteger.valueOf(Int.MAX_VALUE.toLong()) -> "definedExternally"
type is SimpleType && type.type == "Double" && this.matches("[0-9]+".toRegex()) -> "${this}.0"
else -> this
@@ -286,12 +285,15 @@ private fun GenerateAttribute.kindNotChanged(superAttributesByName: Map<String,
private fun GenerateAttribute.hasSuperImplementation(allSuperTypes: List<GenerateTraitOrClass>) = allSuperTypes.any { st -> st.kind != GenerateDefinitionKind.INTERFACE && st.memberAttributes.any { it.signature == signature } }
private fun GenerateAttribute.hasNoDefaultValue() =
this.initializer == null && (this.type.nullable || this.type == DynamicType) && !this.required
fun Appendable.renderBuilderFunction(dictionary: GenerateTraitOrClass, allSuperTypes: List<GenerateTraitOrClass>, allTypes: Set<String>) {
val fields = (dictionary.memberAttributes + allSuperTypes.flatMap { it.memberAttributes })
.distinctBy { it.signature }
.map { it.copy(kind = AttributeKind.ARGUMENT) }
.dynamicIfUnknownType(allTypes)
.map { if (it.initializer == null && (it.type.nullable || it.type == DynamicType) && !it.required) it.copy(initializer = "null") else it }
.map { if (it.hasNoDefaultValue()) it.copy(initializer = "undefined") else it }
appendln("@kotlin.internal.InlineOnly")
append("public inline fun ${dictionary.name}")
@@ -306,10 +308,10 @@ fun Appendable.renderBuilderFunction(dictionary: GenerateTraitOrClass, allSuperT
indent(level = 1)
val escapedFieldName = field.name.replaceKeywords()
val nullGuardedAssignment = field.type is ArrayType
val nullGuardedAssignment = field.hasNoDefaultValue()
if (nullGuardedAssignment) {
appendln("if ($escapedFieldName != null) {")
appendln("if ($escapedFieldName !== undefined) {")
indent(level = 2)
}