Skip to content

Commit

Permalink
IDL2K: KT-15436 js stdlib: org.w3c.fetch.RequestInit has 12 parameter…
Browse files Browse the repository at this point in the history
…s, all required
  • Loading branch information
Sergey Mashkov committed Jan 11, 2017
1 parent 4816474 commit 099cd81
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
8 changes: 5 additions & 3 deletions libraries/tools/idl2k/src/main/kotlin/gen.kt
Expand Up @@ -40,7 +40,8 @@ fun generateFunction(repository: Repository, function: Operation, functionName:
override = false,
kind = AttributeKind.ARGUMENT,
vararg = it.vararg,
static = it.static
static = it.static,
required = it.required
)
},
nativeGetterOrSetter = nativeGetterOrSetter,
Expand Down Expand Up @@ -100,7 +101,8 @@ fun generateAttribute(putNoImpl: Boolean, repository: Repository, attribute: Att
kind = if (attribute.readOnly) AttributeKind.VAL else AttributeKind.VAR,
override = false,
vararg = attribute.vararg,
static = attribute.static
static = attribute.static,
required = attribute.required
)

private fun InterfaceDefinition.superTypes(repository: Repository) = superTypes.map { repository.interfaces[it] }.filterNotNull()
Expand All @@ -118,7 +120,7 @@ private fun resolveDefinitionKind(repository: Repository, iface: InterfaceDefini
private fun InterfaceDefinition.mapAttributes(repository: Repository)
= attributes.map { generateAttribute(putNoImpl = dictionary, repository = repository, attribute = it, nullableAttributes = dictionary) }
private fun InterfaceDefinition.mapOperations(repository: Repository) = operations.flatMap { generateFunctions(repository, it) }
private fun Constant.mapConstant(repository : Repository) = GenerateAttribute(name, mapType(repository, type), null, false, AttributeKind.VAL, false, false, true)
private fun Constant.mapConstant(repository : Repository) = GenerateAttribute(name, mapType(repository, type), null, false, AttributeKind.VAL, false, false, true, false)
private val EMPTY_CONSTRUCTOR = ExtendedAttribute(null, "Constructor", emptyList())

fun generateTrait(repository: Repository, iface: InterfaceDefinition): GenerateTraitOrClass {
Expand Down
20 changes: 16 additions & 4 deletions libraries/tools/idl2k/src/main/kotlin/idl.kt
Expand Up @@ -29,7 +29,7 @@ import java.util.ArrayList

data class ExtendedAttribute(val name: String?, val call: String, val arguments: List<Attribute>)
data class Operation(val name: String, val returnType: Type, val parameters: List<Attribute>, val attributes: List<ExtendedAttribute>, val static: Boolean)
data class Attribute(val name: String, val type: Type, val readOnly: Boolean = true, val defaultValue: String? = null, val vararg: Boolean, val static: Boolean)
data class Attribute(val name: String, val type: Type, val readOnly: Boolean = true, val defaultValue: String? = null, val vararg: Boolean, val static: Boolean, val required: Boolean)
data class Constant(val name: String, val type: Type, val value: String?)

enum class DefinitionKind {
Expand Down Expand Up @@ -102,7 +102,7 @@ class ExtendedAttributeParser(private val namespace: String) : WebIDLBaseVisitor
object : WebIDLBaseVisitor<Unit>() {
override fun visitTerminal(node: TerminalNode) {
if (node.symbol.type == WebIDLLexer.IDENTIFIER_WEBIDL) {
arguments.add(Attribute(node.text, AnyType(), true, vararg = false, static = false))
arguments.add(Attribute(node.text, AnyType(), true, vararg = false, static = false, required = false))
}
}
}.visitChildren(ctx)
Expand Down Expand Up @@ -202,8 +202,9 @@ class AttributeVisitor(private val readOnly: Boolean = false, private val static
private var name: String = ""
private var defaultValue: String? = null
private var vararg: Boolean = false
private var required: Boolean = false

override fun defaultResult(): Attribute = Attribute(name, type, readOnly, defaultValue, vararg, static)
override fun defaultResult(): Attribute = Attribute(name, type, readOnly, defaultValue, vararg, static, required)

override fun visitType(ctx: WebIDLParser.TypeContext): Attribute {
type = TypeVisitor(namespace).visit(ctx)
Expand All @@ -214,6 +215,9 @@ class AttributeVisitor(private val readOnly: Boolean = false, private val static
if (ctx.children?.any { it is TerminalNode && it.text == "optional" } ?: false) {
defaultValue = "noImpl"
}
if (ctx.children?.any { it is TerminalNode && it.text == "required" } ?: false) {
required = true
}
return visitChildren(ctx)
}

Expand Down Expand Up @@ -367,6 +371,7 @@ class DefinitionVisitor(val extendedAttributes: List<ExtendedAttribute>, val nam
?.text

val type = TypeVisitor(namespace).visit(ctx.children.first { it is TypeContext })
var required = false
val defaultValue = object : WebIDLBaseVisitor<String?>() {
private var value: String? = null

Expand All @@ -376,9 +381,16 @@ class DefinitionVisitor(val extendedAttributes: List<ExtendedAttribute>, val nam
value = ctx2.text
return value
}

override fun visitRequired(ctx: RequiredContext?): String? {
if (ctx?.children?.any { it is TerminalNode && it.text == "required" } ?: false) {
required = true
}
return super.visitRequired(ctx)
}
}.visit(ctx)

attributes.add(Attribute(name ?: "", type, false, defaultValue, false, static))
attributes.add(Attribute(name ?: "", type, false, defaultValue, false, static, required))

return defaultResult()
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/tools/idl2k/src/main/kotlin/model.kt
Expand Up @@ -28,7 +28,7 @@ data class Repository(
enum class AttributeKind {
VAL, VAR, ARGUMENT
}
data class GenerateAttribute(val name: String, val type: Type, val initializer: String?, val getterSetterNoImpl: Boolean, val kind: AttributeKind, val override: Boolean, var vararg: Boolean, val static: Boolean)
data class GenerateAttribute(val name: String, val type: Type, val initializer: String?, val getterSetterNoImpl: Boolean, val kind: AttributeKind, val override: Boolean, var vararg: Boolean, val static: Boolean, val required: Boolean)

val GenerateAttribute.getterNoImpl: Boolean
get() = getterSetterNoImpl
Expand Down
10 changes: 8 additions & 2 deletions libraries/tools/idl2k/src/main/kotlin/render.kt
Expand Up @@ -86,6 +86,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 == "noImpl" || type is SimpleType && type.type == "Int" && parse() > BigInteger.valueOf(Int.MAX_VALUE.toLong()) -> "noImpl"
type is SimpleType && type.type == "Double" && this.matches("[0-9]+".toRegex()) -> "${this}.0"
else -> this
Expand Down Expand Up @@ -242,7 +243,11 @@ fun Appendable.render(allTypes: Map<String, GenerateTraitOrClass>, typeNamesToUn
}

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)
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 }

appendln("@Suppress(\"NOTHING_TO_INLINE\")")
append("public inline fun ${dictionary.name}")
Expand Down Expand Up @@ -308,7 +313,8 @@ private fun merge(a: GenerateAttribute, b: GenerateAttribute): GenerateAttribute
merge(a.kind, b.kind),
a.override,
a.vararg,
a.static
a.static,
a.required || b.required
)
}

Expand Down

0 comments on commit 099cd81

Please sign in to comment.