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
+9 -13
View File
@@ -1793,7 +1793,7 @@ public external interface ElementDefinitionOptions {
} }
@kotlin.internal.InlineOnly @kotlin.internal.InlineOnly
public inline fun ElementDefinitionOptions(extends: String? = null): ElementDefinitionOptions { public inline fun ElementDefinitionOptions(extends: String? = undefined): ElementDefinitionOptions {
val o = js("({})") val o = js("({})")
o["extends"] = extends o["extends"] = extends
@@ -2109,7 +2109,7 @@ public external interface PromiseRejectionEventInit : EventInit {
} }
@kotlin.internal.InlineOnly @kotlin.internal.InlineOnly
public inline fun PromiseRejectionEventInit(promise: Promise<Any?>?, reason: Any? = null, bubbles: Boolean? = false, cancelable: Boolean? = false, composed: Boolean? = false): PromiseRejectionEventInit { public inline fun PromiseRejectionEventInit(promise: Promise<Any?>?, reason: Any? = undefined, bubbles: Boolean? = false, cancelable: Boolean? = false, composed: Boolean? = false): PromiseRejectionEventInit {
val o = js("({})") val o = js("({})")
o["promise"] = promise o["promise"] = promise
@@ -2365,7 +2365,7 @@ public external interface ImageBitmapOptions {
} }
@kotlin.internal.InlineOnly @kotlin.internal.InlineOnly
public inline fun ImageBitmapOptions(imageOrientation: ImageOrientation? = ImageOrientation.NONE, premultiplyAlpha: PremultiplyAlpha? = PremultiplyAlpha.DEFAULT, colorSpaceConversion: ColorSpaceConversion? = ColorSpaceConversion.DEFAULT, resizeWidth: Int? = null, resizeHeight: Int? = null, resizeQuality: ResizeQuality? = ResizeQuality.LOW): ImageBitmapOptions { public inline fun ImageBitmapOptions(imageOrientation: ImageOrientation? = ImageOrientation.NONE, premultiplyAlpha: PremultiplyAlpha? = PremultiplyAlpha.DEFAULT, colorSpaceConversion: ColorSpaceConversion? = ColorSpaceConversion.DEFAULT, resizeWidth: Int? = undefined, resizeHeight: Int? = undefined, resizeQuality: ResizeQuality? = ResizeQuality.LOW): ImageBitmapOptions {
val o = js("({})") val o = js("({})")
o["imageOrientation"] = imageOrientation o["imageOrientation"] = imageOrientation
@@ -2416,9 +2416,7 @@ public inline fun MessageEventInit(data: Any? = null, origin: String? = "", last
o["origin"] = origin o["origin"] = origin
o["lastEventId"] = lastEventId o["lastEventId"] = lastEventId
o["source"] = source o["source"] = source
if (ports != null) { o["ports"] = ports
o["ports"] = ports
}
o["bubbles"] = bubbles o["bubbles"] = bubbles
o["cancelable"] = cancelable o["cancelable"] = cancelable
o["composed"] = composed o["composed"] = composed
@@ -2998,7 +2996,7 @@ public external interface MutationObserverInit {
} }
@kotlin.internal.InlineOnly @kotlin.internal.InlineOnly
public inline fun MutationObserverInit(childList: Boolean? = false, attributes: Boolean? = null, characterData: Boolean? = null, subtree: Boolean? = false, attributeOldValue: Boolean? = null, characterDataOldValue: Boolean? = null, attributeFilter: Array<String>? = null): MutationObserverInit { public inline fun MutationObserverInit(childList: Boolean? = false, attributes: Boolean? = undefined, characterData: Boolean? = undefined, subtree: Boolean? = false, attributeOldValue: Boolean? = undefined, characterDataOldValue: Boolean? = undefined, attributeFilter: Array<String>? = undefined): MutationObserverInit {
val o = js("({})") val o = js("({})")
o["childList"] = childList o["childList"] = childList
@@ -3007,9 +3005,7 @@ public inline fun MutationObserverInit(childList: Boolean? = false, attributes:
o["subtree"] = subtree o["subtree"] = subtree
o["attributeOldValue"] = attributeOldValue o["attributeOldValue"] = attributeOldValue
o["characterDataOldValue"] = characterDataOldValue o["characterDataOldValue"] = characterDataOldValue
if (attributeFilter != null) { o["attributeFilter"] = attributeFilter
o["attributeFilter"] = attributeFilter
}
return o return o
} }
@@ -3122,7 +3118,7 @@ public external interface ElementCreationOptions {
} }
@kotlin.internal.InlineOnly @kotlin.internal.InlineOnly
public inline fun ElementCreationOptions(is_: String? = null): ElementCreationOptions { public inline fun ElementCreationOptions(is_: String? = undefined): ElementCreationOptions {
val o = js("({})") val o = js("({})")
o["is"] = is_ o["is"] = is_
@@ -3706,7 +3702,7 @@ public external interface ScrollToOptions : ScrollOptions {
} }
@kotlin.internal.InlineOnly @kotlin.internal.InlineOnly
public inline fun ScrollToOptions(left: Double? = null, top: Double? = null, behavior: ScrollBehavior? = ScrollBehavior.AUTO): ScrollToOptions { public inline fun ScrollToOptions(left: Double? = undefined, top: Double? = undefined, behavior: ScrollBehavior? = ScrollBehavior.AUTO): ScrollToOptions {
val o = js("({})") val o = js("({})")
o["left"] = left o["left"] = left
@@ -3810,7 +3806,7 @@ public external interface BoxQuadOptions {
} }
@kotlin.internal.InlineOnly @kotlin.internal.InlineOnly
public inline fun BoxQuadOptions(box: CSSBoxType? = CSSBoxType.BORDER, relativeTo: dynamic = null): BoxQuadOptions { public inline fun BoxQuadOptions(box: CSSBoxType? = CSSBoxType.BORDER, relativeTo: dynamic = undefined): BoxQuadOptions {
val o = js("({})") val o = js("({})")
o["box"] = box o["box"] = box
@@ -112,7 +112,7 @@ public external interface RequestInit {
} }
@kotlin.internal.InlineOnly @kotlin.internal.InlineOnly
public inline fun RequestInit(method: String? = null, headers: dynamic = null, body: dynamic = null, referrer: String? = null, referrerPolicy: dynamic = null, mode: RequestMode? = null, credentials: RequestCredentials? = null, cache: RequestCache? = null, redirect: RequestRedirect? = null, integrity: String? = null, keepalive: Boolean? = null, window: Any? = null): RequestInit { public inline fun RequestInit(method: String? = undefined, headers: dynamic = undefined, body: dynamic = undefined, referrer: String? = undefined, referrerPolicy: dynamic = undefined, mode: RequestMode? = undefined, credentials: RequestCredentials? = undefined, cache: RequestCache? = undefined, redirect: RequestRedirect? = undefined, integrity: String? = undefined, keepalive: Boolean? = undefined, window: Any? = undefined): RequestInit {
val o = js("({})") val o = js("({})")
o["method"] = method o["method"] = method
@@ -171,7 +171,7 @@ public external interface ResponseInit {
} }
@kotlin.internal.InlineOnly @kotlin.internal.InlineOnly
public inline fun ResponseInit(status: Short? = 200, statusText: String? = "OK", headers: dynamic = null): ResponseInit { public inline fun ResponseInit(status: Short? = 200, statusText: String? = "OK", headers: dynamic = undefined): ResponseInit {
val o = js("({})") val o = js("({})")
o["status"] = status o["status"] = status
@@ -64,7 +64,7 @@ public external interface FilePropertyBag : BlobPropertyBag {
} }
@kotlin.internal.InlineOnly @kotlin.internal.InlineOnly
public inline fun FilePropertyBag(lastModified: Int? = null, type: String? = ""): FilePropertyBag { public inline fun FilePropertyBag(lastModified: Int? = undefined, type: String? = ""): FilePropertyBag {
val o = js("({})") val o = js("({})")
o["lastModified"] = lastModified o["lastModified"] = lastModified
@@ -111,7 +111,7 @@ public external interface NotificationOptions {
} }
@kotlin.internal.InlineOnly @kotlin.internal.InlineOnly
public inline fun NotificationOptions(dir: NotificationDirection? = NotificationDirection.AUTO, lang: String? = "", body: String? = "", tag: String? = "", image: String? = null, icon: String? = null, badge: String? = null, sound: String? = null, vibrate: dynamic = null, timestamp: Number? = null, renotify: Boolean? = false, silent: Boolean? = false, noscreen: Boolean? = false, requireInteraction: Boolean? = false, sticky: Boolean? = false, data: Any? = null, actions: Array<NotificationAction>? = arrayOf()): NotificationOptions { public inline fun NotificationOptions(dir: NotificationDirection? = NotificationDirection.AUTO, lang: String? = "", body: String? = "", tag: String? = "", image: String? = undefined, icon: String? = undefined, badge: String? = undefined, sound: String? = undefined, vibrate: dynamic = undefined, timestamp: Number? = undefined, renotify: Boolean? = false, silent: Boolean? = false, noscreen: Boolean? = false, requireInteraction: Boolean? = false, sticky: Boolean? = false, data: Any? = null, actions: Array<NotificationAction>? = arrayOf()): NotificationOptions {
val o = js("({})") val o = js("({})")
o["dir"] = dir o["dir"] = dir
@@ -130,9 +130,7 @@ public inline fun NotificationOptions(dir: NotificationDirection? = Notification
o["requireInteraction"] = requireInteraction o["requireInteraction"] = requireInteraction
o["sticky"] = sticky o["sticky"] = sticky
o["data"] = data o["data"] = data
if (actions != null) { o["actions"] = actions
o["actions"] = actions
}
return o return o
} }
@@ -150,7 +148,7 @@ public external interface NotificationAction {
} }
@kotlin.internal.InlineOnly @kotlin.internal.InlineOnly
public inline fun NotificationAction(action: String?, title: String?, icon: String? = null): NotificationAction { public inline fun NotificationAction(action: String?, title: String?, icon: String? = undefined): NotificationAction {
val o = js("({})") val o = js("({})")
o["action"] = action o["action"] = action
@@ -91,7 +91,7 @@ public external interface RegistrationOptions {
} }
@kotlin.internal.InlineOnly @kotlin.internal.InlineOnly
public inline fun RegistrationOptions(scope: String? = null, type: WorkerType? = WorkerType.CLASSIC): RegistrationOptions { public inline fun RegistrationOptions(scope: String? = undefined, type: WorkerType? = WorkerType.CLASSIC): RegistrationOptions {
val o = js("({})") val o = js("({})")
o["scope"] = scope o["scope"] = scope
@@ -130,16 +130,14 @@ public external interface ServiceWorkerMessageEventInit : EventInit {
} }
@kotlin.internal.InlineOnly @kotlin.internal.InlineOnly
public inline fun ServiceWorkerMessageEventInit(data: Any? = null, origin: String? = null, lastEventId: String? = null, source: UnionMessagePortOrServiceWorker? = null, ports: Array<MessagePort>? = null, bubbles: Boolean? = false, cancelable: Boolean? = false, composed: Boolean? = false): ServiceWorkerMessageEventInit { public inline fun ServiceWorkerMessageEventInit(data: Any? = undefined, origin: String? = undefined, lastEventId: String? = undefined, source: UnionMessagePortOrServiceWorker? = undefined, ports: Array<MessagePort>? = undefined, bubbles: Boolean? = false, cancelable: Boolean? = false, composed: Boolean? = false): ServiceWorkerMessageEventInit {
val o = js("({})") val o = js("({})")
o["data"] = data o["data"] = data
o["origin"] = origin o["origin"] = origin
o["lastEventId"] = lastEventId o["lastEventId"] = lastEventId
o["source"] = source o["source"] = source
if (ports != null) { o["ports"] = ports
o["ports"] = ports
}
o["bubbles"] = bubbles o["bubbles"] = bubbles
o["cancelable"] = cancelable o["cancelable"] = cancelable
o["composed"] = composed o["composed"] = composed
@@ -237,12 +235,8 @@ public external interface ForeignFetchOptions {
public inline fun ForeignFetchOptions(scopes: Array<String>?, origins: Array<String>?): ForeignFetchOptions { public inline fun ForeignFetchOptions(scopes: Array<String>?, origins: Array<String>?): ForeignFetchOptions {
val o = js("({})") val o = js("({})")
if (scopes != null) { o["scopes"] = scopes
o["scopes"] = scopes o["origins"] = origins
}
if (origins != null) {
o["origins"] = origins
}
return o return o
} }
@@ -324,14 +318,12 @@ public external interface ForeignFetchResponse {
} }
@kotlin.internal.InlineOnly @kotlin.internal.InlineOnly
public inline fun ForeignFetchResponse(response: Response?, origin: String? = null, headers: Array<String>? = null): ForeignFetchResponse { public inline fun ForeignFetchResponse(response: Response?, origin: String? = undefined, headers: Array<String>? = undefined): ForeignFetchResponse {
val o = js("({})") val o = js("({})")
o["response"] = response o["response"] = response
o["origin"] = origin o["origin"] = origin
if (headers != null) { o["headers"] = headers
o["headers"] = headers
}
return o return o
} }
@@ -366,16 +358,14 @@ public external interface ExtendableMessageEventInit : ExtendableEventInit {
} }
@kotlin.internal.InlineOnly @kotlin.internal.InlineOnly
public inline fun ExtendableMessageEventInit(data: Any? = null, origin: String? = null, lastEventId: String? = null, source: UnionClientOrMessagePortOrServiceWorker? = null, ports: Array<MessagePort>? = null, bubbles: Boolean? = false, cancelable: Boolean? = false, composed: Boolean? = false): ExtendableMessageEventInit { public inline fun ExtendableMessageEventInit(data: Any? = undefined, origin: String? = undefined, lastEventId: String? = undefined, source: UnionClientOrMessagePortOrServiceWorker? = undefined, ports: Array<MessagePort>? = undefined, bubbles: Boolean? = false, cancelable: Boolean? = false, composed: Boolean? = false): ExtendableMessageEventInit {
val o = js("({})") val o = js("({})")
o["data"] = data o["data"] = data
o["origin"] = origin o["origin"] = origin
o["lastEventId"] = lastEventId o["lastEventId"] = lastEventId
o["source"] = source o["source"] = source
if (ports != null) { o["ports"] = ports
o["ports"] = ports
}
o["bubbles"] = bubbles o["bubbles"] = bubbles
o["cancelable"] = cancelable o["cancelable"] = cancelable
o["composed"] = composed o["composed"] = composed
@@ -412,7 +402,7 @@ public external interface CacheQueryOptions {
} }
@kotlin.internal.InlineOnly @kotlin.internal.InlineOnly
public inline fun CacheQueryOptions(ignoreSearch: Boolean? = false, ignoreMethod: Boolean? = false, ignoreVary: Boolean? = false, cacheName: String? = null): CacheQueryOptions { public inline fun CacheQueryOptions(ignoreSearch: Boolean? = false, ignoreMethod: Boolean? = false, ignoreVary: Boolean? = false, cacheName: String? = undefined): CacheQueryOptions {
val o = js("({})") val o = js("({})")
o["ignoreSearch"] = ignoreSearch o["ignoreSearch"] = ignoreSearch
@@ -439,7 +429,7 @@ public external interface CacheBatchOperation {
} }
@kotlin.internal.InlineOnly @kotlin.internal.InlineOnly
public inline fun CacheBatchOperation(type: String? = null, request: Request? = null, response: Response? = null, options: CacheQueryOptions? = null): CacheBatchOperation { public inline fun CacheBatchOperation(type: String? = undefined, request: Request? = undefined, response: Response? = undefined, options: CacheQueryOptions? = undefined): CacheBatchOperation {
val o = js("({})") val o = js("({})")
o["type"] = type o["type"] = type
@@ -17,7 +17,6 @@
package org.jetbrains.idl2k package org.jetbrains.idl2k
import org.jetbrains.idl2k.util.mapEnumConstant import org.jetbrains.idl2k.util.mapEnumConstant
import java.io.*
import java.math.BigInteger import java.math.BigInteger
private fun <O : Appendable> O.indent(commented: Boolean = false, level: Int) { 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.parse() = if (this.startsWith("0x")) BigInteger(this.substring(2), 16) else BigInteger(this)
private fun String.replaceWrongConstants(type: Type) = when { 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" 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" type is SimpleType && type.type == "Double" && this.matches("[0-9]+".toRegex()) -> "${this}.0"
else -> this 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.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>) { fun Appendable.renderBuilderFunction(dictionary: GenerateTraitOrClass, allSuperTypes: List<GenerateTraitOrClass>, allTypes: Set<String>) {
val fields = (dictionary.memberAttributes + allSuperTypes.flatMap { it.memberAttributes }) val fields = (dictionary.memberAttributes + allSuperTypes.flatMap { it.memberAttributes })
.distinctBy { it.signature } .distinctBy { it.signature }
.map { it.copy(kind = AttributeKind.ARGUMENT) } .map { it.copy(kind = AttributeKind.ARGUMENT) }
.dynamicIfUnknownType(allTypes) .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") appendln("@kotlin.internal.InlineOnly")
append("public inline fun ${dictionary.name}") append("public inline fun ${dictionary.name}")
@@ -306,10 +308,10 @@ fun Appendable.renderBuilderFunction(dictionary: GenerateTraitOrClass, allSuperT
indent(level = 1) indent(level = 1)
val escapedFieldName = field.name.replaceKeywords() val escapedFieldName = field.name.replaceKeywords()
val nullGuardedAssignment = field.type is ArrayType val nullGuardedAssignment = field.hasNoDefaultValue()
if (nullGuardedAssignment) { if (nullGuardedAssignment) {
appendln("if ($escapedFieldName != null) {") appendln("if ($escapedFieldName !== undefined) {")
indent(level = 2) indent(level = 2)
} }