diff --git a/libraries/stdlib/js/src/org.w3c/org.w3c.dom.kt b/libraries/stdlib/js/src/org.w3c/org.w3c.dom.kt index 70cbd4e7c5e..db78e113095 100644 --- a/libraries/stdlib/js/src/org.w3c/org.w3c.dom.kt +++ b/libraries/stdlib/js/src/org.w3c/org.w3c.dom.kt @@ -1793,7 +1793,7 @@ public external interface ElementDefinitionOptions { } @kotlin.internal.InlineOnly -public inline fun ElementDefinitionOptions(extends: String? = null): ElementDefinitionOptions { +public inline fun ElementDefinitionOptions(extends: String? = undefined): ElementDefinitionOptions { val o = js("({})") o["extends"] = extends @@ -2109,7 +2109,7 @@ public external interface PromiseRejectionEventInit : EventInit { } @kotlin.internal.InlineOnly -public inline fun PromiseRejectionEventInit(promise: Promise?, reason: Any? = null, bubbles: Boolean? = false, cancelable: Boolean? = false, composed: Boolean? = false): PromiseRejectionEventInit { +public inline fun PromiseRejectionEventInit(promise: Promise?, reason: Any? = undefined, bubbles: Boolean? = false, cancelable: Boolean? = false, composed: Boolean? = false): PromiseRejectionEventInit { val o = js("({})") o["promise"] = promise @@ -2365,7 +2365,7 @@ public external interface ImageBitmapOptions { } @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("({})") o["imageOrientation"] = imageOrientation @@ -2416,9 +2416,7 @@ public inline fun MessageEventInit(data: Any? = null, origin: String? = "", last o["origin"] = origin o["lastEventId"] = lastEventId o["source"] = source - if (ports != null) { - o["ports"] = ports - } + o["ports"] = ports o["bubbles"] = bubbles o["cancelable"] = cancelable o["composed"] = composed @@ -2998,7 +2996,7 @@ public external interface MutationObserverInit { } @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? = 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? = undefined): MutationObserverInit { val o = js("({})") o["childList"] = childList @@ -3007,9 +3005,7 @@ public inline fun MutationObserverInit(childList: Boolean? = false, attributes: o["subtree"] = subtree o["attributeOldValue"] = attributeOldValue o["characterDataOldValue"] = characterDataOldValue - if (attributeFilter != null) { - o["attributeFilter"] = attributeFilter - } + o["attributeFilter"] = attributeFilter return o } @@ -3122,7 +3118,7 @@ public external interface ElementCreationOptions { } @kotlin.internal.InlineOnly -public inline fun ElementCreationOptions(is_: String? = null): ElementCreationOptions { +public inline fun ElementCreationOptions(is_: String? = undefined): ElementCreationOptions { val o = js("({})") o["is"] = is_ @@ -3706,7 +3702,7 @@ public external interface ScrollToOptions : ScrollOptions { } @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("({})") o["left"] = left @@ -3810,7 +3806,7 @@ public external interface BoxQuadOptions { } @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("({})") o["box"] = box diff --git a/libraries/stdlib/js/src/org.w3c/org.w3c.fetch.kt b/libraries/stdlib/js/src/org.w3c/org.w3c.fetch.kt index 7a01e68b0b6..f047eca71ae 100644 --- a/libraries/stdlib/js/src/org.w3c/org.w3c.fetch.kt +++ b/libraries/stdlib/js/src/org.w3c/org.w3c.fetch.kt @@ -112,7 +112,7 @@ public external interface RequestInit { } @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("({})") o["method"] = method @@ -171,7 +171,7 @@ public external interface ResponseInit { } @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("({})") o["status"] = status diff --git a/libraries/stdlib/js/src/org.w3c/org.w3c.files.kt b/libraries/stdlib/js/src/org.w3c/org.w3c.files.kt index 2647be2622e..f174194d0f1 100644 --- a/libraries/stdlib/js/src/org.w3c/org.w3c.files.kt +++ b/libraries/stdlib/js/src/org.w3c/org.w3c.files.kt @@ -64,7 +64,7 @@ public external interface FilePropertyBag : BlobPropertyBag { } @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("({})") o["lastModified"] = lastModified diff --git a/libraries/stdlib/js/src/org.w3c/org.w3c.notifications.kt b/libraries/stdlib/js/src/org.w3c/org.w3c.notifications.kt index cbd0e001dcd..6f66cda7460 100644 --- a/libraries/stdlib/js/src/org.w3c/org.w3c.notifications.kt +++ b/libraries/stdlib/js/src/org.w3c/org.w3c.notifications.kt @@ -111,7 +111,7 @@ public external interface NotificationOptions { } @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? = 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? = arrayOf()): NotificationOptions { val o = js("({})") o["dir"] = dir @@ -130,9 +130,7 @@ public inline fun NotificationOptions(dir: NotificationDirection? = Notification o["requireInteraction"] = requireInteraction o["sticky"] = sticky o["data"] = data - if (actions != null) { - o["actions"] = actions - } + o["actions"] = actions return o } @@ -150,7 +148,7 @@ public external interface NotificationAction { } @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("({})") o["action"] = action diff --git a/libraries/stdlib/js/src/org.w3c/org.w3c.workers.kt b/libraries/stdlib/js/src/org.w3c/org.w3c.workers.kt index e1554889e9a..4619c2d75e3 100644 --- a/libraries/stdlib/js/src/org.w3c/org.w3c.workers.kt +++ b/libraries/stdlib/js/src/org.w3c/org.w3c.workers.kt @@ -91,7 +91,7 @@ public external interface RegistrationOptions { } @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("({})") o["scope"] = scope @@ -130,16 +130,14 @@ public external interface ServiceWorkerMessageEventInit : EventInit { } @kotlin.internal.InlineOnly -public inline fun ServiceWorkerMessageEventInit(data: Any? = null, origin: String? = null, lastEventId: String? = null, source: UnionMessagePortOrServiceWorker? = null, ports: Array? = 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? = undefined, bubbles: Boolean? = false, cancelable: Boolean? = false, composed: Boolean? = false): ServiceWorkerMessageEventInit { val o = js("({})") o["data"] = data o["origin"] = origin o["lastEventId"] = lastEventId o["source"] = source - if (ports != null) { - o["ports"] = ports - } + o["ports"] = ports o["bubbles"] = bubbles o["cancelable"] = cancelable o["composed"] = composed @@ -237,12 +235,8 @@ public external interface ForeignFetchOptions { public inline fun ForeignFetchOptions(scopes: Array?, origins: Array?): ForeignFetchOptions { val o = js("({})") - if (scopes != null) { - o["scopes"] = scopes - } - if (origins != null) { - o["origins"] = origins - } + o["scopes"] = scopes + o["origins"] = origins return o } @@ -324,14 +318,12 @@ public external interface ForeignFetchResponse { } @kotlin.internal.InlineOnly -public inline fun ForeignFetchResponse(response: Response?, origin: String? = null, headers: Array? = null): ForeignFetchResponse { +public inline fun ForeignFetchResponse(response: Response?, origin: String? = undefined, headers: Array? = undefined): ForeignFetchResponse { val o = js("({})") o["response"] = response o["origin"] = origin - if (headers != null) { - o["headers"] = headers - } + o["headers"] = headers return o } @@ -366,16 +358,14 @@ public external interface ExtendableMessageEventInit : ExtendableEventInit { } @kotlin.internal.InlineOnly -public inline fun ExtendableMessageEventInit(data: Any? = null, origin: String? = null, lastEventId: String? = null, source: UnionClientOrMessagePortOrServiceWorker? = null, ports: Array? = 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? = undefined, bubbles: Boolean? = false, cancelable: Boolean? = false, composed: Boolean? = false): ExtendableMessageEventInit { val o = js("({})") o["data"] = data o["origin"] = origin o["lastEventId"] = lastEventId o["source"] = source - if (ports != null) { - o["ports"] = ports - } + o["ports"] = ports o["bubbles"] = bubbles o["cancelable"] = cancelable o["composed"] = composed @@ -412,7 +402,7 @@ public external interface CacheQueryOptions { } @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("({})") o["ignoreSearch"] = ignoreSearch @@ -439,7 +429,7 @@ public external interface CacheBatchOperation { } @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("({})") o["type"] = type diff --git a/libraries/tools/idl2k/src/main/kotlin/render.kt b/libraries/tools/idl2k/src/main/kotlin/render.kt index 49955276509..6d1879f7527 100644 --- a/libraries/tools/idl2k/src/main/kotlin/render.kt +++ b/libraries/tools/idl2k/src/main/kotlin/render.kt @@ -17,7 +17,6 @@ package org.jetbrains.idl2k import org.jetbrains.idl2k.util.mapEnumConstant -import java.io.* import java.math.BigInteger private fun 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) = 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, allTypes: Set) { 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) }