From 8ae5ecf4ff0396bcd17faeba101799092ece78eb Mon Sep 17 00:00:00 2001 From: Sergey Mashkov Date: Fri, 19 Aug 2016 14:32:10 +0300 Subject: [PATCH] KT-12810 IDL: support special modifiers(e.g. getter and setter) --- .../src/generated/org.khronos.webgl.kt | 63 +++++++---- .../src/generated/org.w3c.dom.css.kt | 12 ++- js/js.libraries/src/generated/org.w3c.dom.kt | 102 ++++++++++++------ .../src/generated/org.w3c.files.kt | 3 +- libraries/tools/idl2k/src/main/kotlin/gen.kt | 9 +- .../tools/idl2k/src/main/kotlin/render.kt | 17 ++- .../tools/idl2k/src/main/kotlin/types.kt | 12 +++ 7 files changed, 155 insertions(+), 63 deletions(-) diff --git a/js/js.libraries/src/generated/org.khronos.webgl.kt b/js/js.libraries/src/generated/org.khronos.webgl.kt index 0f548b9cd82..242d3ff4d2b 100644 --- a/js/js.libraries/src/generated/org.khronos.webgl.kt +++ b/js/js.libraries/src/generated/org.khronos.webgl.kt @@ -595,8 +595,11 @@ public inline fun WebGLContextEventInit(statusMessage: String, bubbles: Boolean get() = noImpl open val byteLength: Int get() = noImpl - fun get(index: Int): Byte = noImpl - fun set(index: Int, value: Byte): Unit = noImpl + @Suppress("NATIVE_GETTER_RETURN_TYPE_SHOULD_BE_NULLABLE") + @nativeGetter + operator fun get(index: Int): Byte = noImpl + @nativeSetter + operator fun set(index: Int, value: Byte): Unit = noImpl fun set(array: Int8Array, offset: Int = noImpl): Unit = noImpl fun set(array: Array, offset: Int = noImpl): Unit = noImpl fun subarray(start: Int, end: Int): Int8Array = noImpl @@ -619,8 +622,11 @@ public inline fun WebGLContextEventInit(statusMessage: String, bubbles: Boolean get() = noImpl open val byteLength: Int get() = noImpl - fun get(index: Int): Byte = noImpl - fun set(index: Int, value: Byte): Unit = noImpl + @Suppress("NATIVE_GETTER_RETURN_TYPE_SHOULD_BE_NULLABLE") + @nativeGetter + operator fun get(index: Int): Byte = noImpl + @nativeSetter + operator fun set(index: Int, value: Byte): Unit = noImpl fun set(array: Uint8Array, offset: Int = noImpl): Unit = noImpl fun set(array: Array, offset: Int = noImpl): Unit = noImpl fun subarray(start: Int, end: Int): Uint8Array = noImpl @@ -643,8 +649,11 @@ public inline fun WebGLContextEventInit(statusMessage: String, bubbles: Boolean get() = noImpl open val byteLength: Int get() = noImpl - fun get(index: Int): Byte = noImpl - fun set(index: Int, value: Byte): Unit = noImpl + @Suppress("NATIVE_GETTER_RETURN_TYPE_SHOULD_BE_NULLABLE") + @nativeGetter + operator fun get(index: Int): Byte = noImpl + @nativeSetter + operator fun set(index: Int, value: Byte): Unit = noImpl fun set(array: Uint8ClampedArray, offset: Int = noImpl): Unit = noImpl fun set(array: Array, offset: Int = noImpl): Unit = noImpl fun subarray(start: Int, end: Int): Uint8ClampedArray = noImpl @@ -667,8 +676,11 @@ public inline fun WebGLContextEventInit(statusMessage: String, bubbles: Boolean get() = noImpl open val byteLength: Int get() = noImpl - fun get(index: Int): Short = noImpl - fun set(index: Int, value: Short): Unit = noImpl + @Suppress("NATIVE_GETTER_RETURN_TYPE_SHOULD_BE_NULLABLE") + @nativeGetter + operator fun get(index: Int): Short = noImpl + @nativeSetter + operator fun set(index: Int, value: Short): Unit = noImpl fun set(array: Int16Array, offset: Int = noImpl): Unit = noImpl fun set(array: Array, offset: Int = noImpl): Unit = noImpl fun subarray(start: Int, end: Int): Int16Array = noImpl @@ -691,8 +703,11 @@ public inline fun WebGLContextEventInit(statusMessage: String, bubbles: Boolean get() = noImpl open val byteLength: Int get() = noImpl - fun get(index: Int): Short = noImpl - fun set(index: Int, value: Short): Unit = noImpl + @Suppress("NATIVE_GETTER_RETURN_TYPE_SHOULD_BE_NULLABLE") + @nativeGetter + operator fun get(index: Int): Short = noImpl + @nativeSetter + operator fun set(index: Int, value: Short): Unit = noImpl fun set(array: Uint16Array, offset: Int = noImpl): Unit = noImpl fun set(array: Array, offset: Int = noImpl): Unit = noImpl fun subarray(start: Int, end: Int): Uint16Array = noImpl @@ -715,8 +730,11 @@ public inline fun WebGLContextEventInit(statusMessage: String, bubbles: Boolean get() = noImpl open val byteLength: Int get() = noImpl - fun get(index: Int): Int = noImpl - fun set(index: Int, value: Int): Unit = noImpl + @Suppress("NATIVE_GETTER_RETURN_TYPE_SHOULD_BE_NULLABLE") + @nativeGetter + operator fun get(index: Int): Int = noImpl + @nativeSetter + operator fun set(index: Int, value: Int): Unit = noImpl fun set(array: Int32Array, offset: Int = noImpl): Unit = noImpl fun set(array: Array, offset: Int = noImpl): Unit = noImpl fun subarray(start: Int, end: Int): Int32Array = noImpl @@ -739,8 +757,11 @@ public inline fun WebGLContextEventInit(statusMessage: String, bubbles: Boolean get() = noImpl open val byteLength: Int get() = noImpl - fun get(index: Int): Int = noImpl - fun set(index: Int, value: Int): Unit = noImpl + @Suppress("NATIVE_GETTER_RETURN_TYPE_SHOULD_BE_NULLABLE") + @nativeGetter + operator fun get(index: Int): Int = noImpl + @nativeSetter + operator fun set(index: Int, value: Int): Unit = noImpl fun set(array: Uint32Array, offset: Int = noImpl): Unit = noImpl fun set(array: Array, offset: Int = noImpl): Unit = noImpl fun subarray(start: Int, end: Int): Uint32Array = noImpl @@ -763,8 +784,11 @@ public inline fun WebGLContextEventInit(statusMessage: String, bubbles: Boolean get() = noImpl open val byteLength: Int get() = noImpl - fun get(index: Int): Float = noImpl - fun set(index: Int, value: Float): Unit = noImpl + @Suppress("NATIVE_GETTER_RETURN_TYPE_SHOULD_BE_NULLABLE") + @nativeGetter + operator fun get(index: Int): Float = noImpl + @nativeSetter + operator fun set(index: Int, value: Float): Unit = noImpl fun set(array: Float32Array, offset: Int = noImpl): Unit = noImpl fun set(array: Array, offset: Int = noImpl): Unit = noImpl fun subarray(start: Int, end: Int): Float32Array = noImpl @@ -787,8 +811,11 @@ public inline fun WebGLContextEventInit(statusMessage: String, bubbles: Boolean get() = noImpl open val byteLength: Int get() = noImpl - fun get(index: Int): Double = noImpl - fun set(index: Int, value: Double): Unit = noImpl + @Suppress("NATIVE_GETTER_RETURN_TYPE_SHOULD_BE_NULLABLE") + @nativeGetter + operator fun get(index: Int): Double = noImpl + @nativeSetter + operator fun set(index: Int, value: Double): Unit = noImpl fun set(array: Float64Array, offset: Int = noImpl): Unit = noImpl fun set(array: Array, offset: Int = noImpl): Unit = noImpl fun subarray(start: Int, end: Int): Float64Array = noImpl diff --git a/js/js.libraries/src/generated/org.w3c.dom.css.kt b/js/js.libraries/src/generated/org.w3c.dom.css.kt index 1e2c04e8466..2e3b2b2005e 100644 --- a/js/js.libraries/src/generated/org.w3c.dom.css.kt +++ b/js/js.libraries/src/generated/org.w3c.dom.css.kt @@ -707,7 +707,8 @@ import org.w3c.xhr.* get() = noImpl set(value) = noImpl fun item(index: Int): String = noImpl - operator @nativeGetter fun get(index: Int): String? = noImpl + @nativeGetter + operator fun get(index: Int): String? = noImpl fun getPropertyValue(property: String): String = noImpl fun getPropertyPriority(property: String): String = noImpl fun setProperty(property: String, value: String, priority: String = ""): Unit = noImpl @@ -723,7 +724,8 @@ import org.w3c.xhr.* val length: Int get() = noImpl fun item(index: Int): String? = noImpl - operator @nativeGetter fun get(index: Int): String? = noImpl + @nativeGetter + operator fun get(index: Int): String? = noImpl fun appendMedium(medium: String): Unit = noImpl fun deleteMedium(medium: String): Unit = noImpl } @@ -759,14 +761,16 @@ import org.w3c.xhr.* val length: Int get() = noImpl fun item(index: Int): StyleSheet? = noImpl - operator @nativeGetter fun get(index: Int): StyleSheet? = noImpl + @nativeGetter + operator fun get(index: Int): StyleSheet? = noImpl } @native public interface CSSRuleList { val length: Int get() = noImpl fun item(index: Int): CSSRule? = noImpl - operator @nativeGetter fun get(index: Int): CSSRule? = noImpl + @nativeGetter + operator fun get(index: Int): CSSRule? = noImpl } @native public interface CSSRule { diff --git a/js/js.libraries/src/generated/org.w3c.dom.kt b/js/js.libraries/src/generated/org.w3c.dom.kt index f9daa0d13e5..ae333bd1641 100644 --- a/js/js.libraries/src/generated/org.w3c.dom.kt +++ b/js/js.libraries/src/generated/org.w3c.dom.kt @@ -334,7 +334,8 @@ import org.w3c.xhr.* open val childElementCount: Int get() = noImpl fun exitFullscreen(): Unit = noImpl - operator @nativeGetter fun get(name: String): dynamic = noImpl + @nativeGetter + operator fun get(name: String): dynamic = noImpl fun getElementsByName(elementName: String): NodeList = noImpl fun getItems(typeNames: String = ""): NodeList = noImpl fun open(type: String = "text/html", replace: String = ""): Document = noImpl @@ -704,8 +705,10 @@ import org.w3c.xhr.* fun focus(): Unit = noImpl fun blur(): Unit = noImpl fun open(url: String = "about:blank", target: String = "_blank", features: String = "", replace: Boolean = false): Window = noImpl - operator @nativeGetter fun get(index: Int): Window? = noImpl - operator @nativeGetter fun get(name: String): dynamic = noImpl + @nativeGetter + operator fun get(index: Int): Window? = noImpl + @nativeGetter + operator fun get(name: String): dynamic = noImpl fun alert(): Unit = noImpl fun alert(message: String): Unit = noImpl fun confirm(message: String = ""): Boolean = noImpl @@ -854,12 +857,14 @@ import org.w3c.xhr.* @native public interface HTMLAllCollection : HTMLCollection { fun item(name: String): UnionElementOrHTMLCollection? = noImpl // override fun namedItem(name: String): UnionElementOrHTMLCollection? = noImpl -// operator @nativeGetter override fun get(name: String): UnionElementOrHTMLCollection? = noImpl +// @nativeGetter +// operator override fun get(name: String): UnionElementOrHTMLCollection? = noImpl } @native public interface HTMLFormControlsCollection : HTMLCollection { // override fun namedItem(name: String): UnionElementOrRadioNodeList? = noImpl -// operator @nativeGetter override fun get(name: String): UnionElementOrRadioNodeList? = noImpl +// @nativeGetter +// operator override fun get(name: String): UnionElementOrRadioNodeList? = noImpl } @native public interface RadioNodeList : NodeList, UnionElementOrRadioNodeList { @@ -875,7 +880,8 @@ import org.w3c.xhr.* var selectedIndex: Int get() = noImpl set(value) = noImpl - operator @nativeSetter fun set(index: Int, option: HTMLOptionElement?): Unit = noImpl + @nativeSetter + operator fun set(index: Int, option: HTMLOptionElement?): Unit = noImpl fun add(element: UnionHTMLOptGroupElementOrHTMLOptionElement, before: dynamic = null): Unit = noImpl fun remove(index: Int): Unit = noImpl } @@ -884,7 +890,8 @@ import org.w3c.xhr.* val names: Array get() = noImpl // override fun namedItem(name: String): PropertyNodeList? = noImpl -// operator @nativeGetter override fun get(name: String): PropertyNodeList? = noImpl +// @nativeGetter +// operator override fun get(name: String): PropertyNodeList? = noImpl } @native public interface PropertyNodeList : NodeList { @@ -892,13 +899,17 @@ import org.w3c.xhr.* } @native public interface DOMStringMap { - operator @nativeGetter fun get(name: String): String? = noImpl - operator @nativeSetter fun set(name: String, value: String): Unit = noImpl + @nativeGetter + operator fun get(name: String): String? = noImpl + @nativeSetter + operator fun set(name: String, value: String): Unit = noImpl } @native public interface DOMElementMap { - operator @nativeGetter fun get(name: String): Element? = noImpl - operator @nativeSetter fun set(name: String, value: Element): Unit = noImpl + @nativeGetter + operator fun get(name: String): Element? = noImpl + @nativeSetter + operator fun set(name: String, value: Element): Unit = noImpl } @native public open class XMLDocument : Document() { @@ -1869,7 +1880,8 @@ import org.w3c.xhr.* var onremovetrack: ((Event) -> dynamic)? get() = noImpl set(value) = noImpl - operator @nativeGetter fun get(index: Int): AudioTrack? = noImpl + @nativeGetter + operator fun get(index: Int): AudioTrack? = noImpl fun getTrackById(id: String): AudioTrack? = noImpl } @@ -1901,7 +1913,8 @@ import org.w3c.xhr.* var onremovetrack: ((Event) -> dynamic)? get() = noImpl set(value) = noImpl - operator @nativeGetter fun get(index: Int): VideoTrack? = noImpl + @nativeGetter + operator fun get(index: Int): VideoTrack? = noImpl fun getTrackById(id: String): VideoTrack? = noImpl } @@ -2008,7 +2021,8 @@ import org.w3c.xhr.* var onremovetrack: ((Event) -> dynamic)? get() = noImpl set(value) = noImpl - operator @nativeGetter fun get(index: Int): TextTrack? = noImpl + @nativeGetter + operator fun get(index: Int): TextTrack? = noImpl fun getTrackById(id: String): TextTrack? = noImpl } @@ -2040,7 +2054,8 @@ import org.w3c.xhr.* @native public interface TextTrackCueList { val length: Int get() = noImpl - operator @nativeGetter fun get(index: Int): TextTrackCue? = noImpl + @nativeGetter + operator fun get(index: Int): TextTrackCue? = noImpl fun getCueById(id: String): TextTrackCue? = noImpl } @@ -2393,8 +2408,10 @@ public inline fun TrackEventInit(track: UnionAudioTrackOrTextTrackOrVideoTrack?, get() = noImpl val length: Int get() = noImpl - operator @nativeGetter fun get(index: Int): Element? = noImpl - operator @nativeGetter fun get(name: String): UnionElementOrRadioNodeList? = noImpl + @nativeGetter + operator fun get(index: Int): Element? = noImpl + @nativeGetter + operator fun get(name: String): UnionElementOrRadioNodeList? = noImpl fun submit(): Unit = noImpl fun reset(): Unit = noImpl fun checkValidity(): Boolean = noImpl @@ -2661,11 +2678,13 @@ public inline fun TrackEventInit(track: UnionAudioTrackOrTextTrackOrVideoTrack?, val labels: NodeList get() = noImpl fun item(index: Int): Element? = noImpl - operator @nativeGetter fun get(index: Int): Element? = noImpl + @nativeGetter + operator fun get(index: Int): Element? = noImpl fun namedItem(name: String): HTMLOptionElement? = noImpl fun add(element: UnionHTMLOptGroupElementOrHTMLOptionElement, before: dynamic = null): Unit = noImpl fun remove(index: Int): Unit = noImpl - operator @nativeSetter fun set(index: Int, option: HTMLOptionElement?): Unit = noImpl + @nativeSetter + operator fun set(index: Int, option: HTMLOptionElement?): Unit = noImpl fun checkValidity(): Boolean = noImpl fun reportValidity(): Boolean = noImpl fun setCustomValidity(error: String): Unit = noImpl @@ -3389,7 +3408,8 @@ public inline fun HitRegionOptions(path: Path2D? = null, fillRule: String = "non @native public interface DataTransferItemList { val length: Int get() = noImpl - operator @nativeGetter fun get(index: Int): DataTransferItem? = noImpl + @nativeGetter + operator fun get(index: Int): DataTransferItem? = noImpl fun add(data: String, type: String): DataTransferItem? = noImpl fun add(data: File): DataTransferItem? = noImpl fun remove(index: Int): Unit = noImpl @@ -3704,18 +3724,22 @@ public inline fun ErrorEventInit(message: String, filename: String, lineno: Int, get() = noImpl fun refresh(reload: Boolean = false): Unit = noImpl fun item(index: Int): Plugin? = noImpl - operator @nativeGetter fun get(index: Int): Plugin? = noImpl + @nativeGetter + operator fun get(index: Int): Plugin? = noImpl fun namedItem(name: String): Plugin? = noImpl - operator @nativeGetter fun get(name: String): Plugin? = noImpl + @nativeGetter + operator fun get(name: String): Plugin? = noImpl } @native public interface MimeTypeArray { val length: Int get() = noImpl fun item(index: Int): MimeType? = noImpl - operator @nativeGetter fun get(index: Int): MimeType? = noImpl + @nativeGetter + operator fun get(index: Int): MimeType? = noImpl fun namedItem(name: String): MimeType? = noImpl - operator @nativeGetter fun get(name: String): MimeType? = noImpl + @nativeGetter + operator fun get(name: String): MimeType? = noImpl } @native public interface Plugin { @@ -3728,9 +3752,11 @@ public inline fun ErrorEventInit(message: String, filename: String, lineno: Int, val length: Int get() = noImpl fun item(index: Int): MimeType? = noImpl - operator @nativeGetter fun get(index: Int): MimeType? = noImpl + @nativeGetter + operator fun get(index: Int): MimeType? = noImpl fun namedItem(name: String): MimeType? = noImpl - operator @nativeGetter fun get(name: String): MimeType? = noImpl + @nativeGetter + operator fun get(name: String): MimeType? = noImpl } @native public interface MimeType { @@ -4057,9 +4083,11 @@ public inline fun CloseEventInit(wasClean: Boolean, code: Short, reason: String, get() = noImpl fun key(index: Int): String? = noImpl fun getItem(key: String): String? = noImpl - operator @nativeGetter fun get(key: String): String? = noImpl + @nativeGetter + operator fun get(key: String): String? = noImpl fun setItem(key: String, value: String): Unit = noImpl - operator @nativeSetter fun set(key: String, value: String): Unit = noImpl + @nativeSetter + operator fun set(key: String, value: String): Unit = noImpl fun removeItem(key: String): Unit = noImpl fun clear(): Unit = noImpl } @@ -4380,16 +4408,19 @@ public inline fun CustomEventInit(detail: Any? = null, bubbles: Boolean = false, val length: Int get() = noImpl fun item(index: Int): Node? = noImpl - operator @nativeGetter fun get(index: Int): Node? = noImpl + @nativeGetter + operator fun get(index: Int): Node? = noImpl } @native public interface HTMLCollection : UnionElementOrHTMLCollection { val length: Int get() = noImpl fun item(index: Int): Element? = noImpl - operator @nativeGetter fun get(index: Int): Element? = noImpl + @nativeGetter + operator fun get(index: Int): Element? = noImpl fun namedItem(name: String): Element? = noImpl - operator @nativeGetter fun get(name: String): Element? = noImpl + @nativeGetter + operator fun get(name: String): Element? = noImpl } @native public open class MutationObserver(callback: (Array, MutationObserver) -> Unit) { @@ -4551,9 +4582,11 @@ public inline fun MutationObserverInit(childList: Boolean = false, attributes: B val length: Int get() = noImpl fun item(index: Int): Attr? = noImpl - operator @nativeGetter fun get(index: Int): Attr? = noImpl + @nativeGetter + operator fun get(index: Int): Attr? = noImpl fun getNamedItem(name: String): Attr? = noImpl - operator @nativeGetter fun get(name: String): Attr? = noImpl + @nativeGetter + operator fun get(name: String): Attr? = noImpl fun getNamedItemNS(namespace: String?, localName: String): Attr? = noImpl fun setNamedItem(attr: Attr): Attr? = noImpl fun setNamedItemNS(attr: Attr): Attr? = noImpl @@ -4733,7 +4766,8 @@ public inline fun MutationObserverInit(childList: Boolean = false, attributes: B val length: Int get() = noImpl fun item(index: Int): String? = noImpl - operator @nativeGetter fun get(index: Int): String? = noImpl + @nativeGetter + operator fun get(index: Int): String? = noImpl fun contains(token: String): Boolean = noImpl fun add(vararg tokens: String): Unit = noImpl fun remove(vararg tokens: String): Unit = noImpl diff --git a/js/js.libraries/src/generated/org.w3c.files.kt b/js/js.libraries/src/generated/org.w3c.files.kt index 6f7a04ea15f..a12e0031f83 100644 --- a/js/js.libraries/src/generated/org.w3c.files.kt +++ b/js/js.libraries/src/generated/org.w3c.files.kt @@ -71,7 +71,8 @@ public inline fun FilePropertyBag(type: String = "", lastModified: Int): FilePro val length: Int get() = noImpl fun item(index: Int): File? = noImpl - operator @nativeGetter fun get(index: Int): File? = noImpl + @nativeGetter + operator fun get(index: Int): File? = noImpl } @native public open class FileReader : EventTarget { diff --git a/libraries/tools/idl2k/src/main/kotlin/gen.kt b/libraries/tools/idl2k/src/main/kotlin/gen.kt index ad683832d76..b8deaf82d0e 100644 --- a/libraries/tools/idl2k/src/main/kotlin/gen.kt +++ b/libraries/tools/idl2k/src/main/kotlin/gen.kt @@ -12,7 +12,7 @@ fun generateFunction(repository: Repository, function: Operation, functionName: function.attributes.map { it.call }.toSet().let { attributes -> GenerateFunction( name = functionName, - returnType = mapType(repository, function.returnType).let { mapped -> if (nativeGetterOrSetter == NativeGetterOrSetter.GETTER) mapped.toNullable() else mapped }, + returnType = mapType(repository, function.returnType).let { mapped -> if (nativeGetterOrSetter == NativeGetterOrSetter.GETTER) mapped.toNullableIfNonPrimitive() else mapped }, arguments = function.parameters.map { GenerateAttribute( name = it.name, @@ -31,7 +31,12 @@ fun generateFunction(repository: Repository, function: Operation, functionName: } fun generateFunctions(repository: Repository, function: Operation): List { - val realFunction = if (function.name == "") null else generateFunction(repository, function, function.name, NativeGetterOrSetter.NONE) + val realFunction = when { + function.name == "" -> null + function.getterOrSetter() == NativeGetterOrSetter.NONE -> generateFunction(repository, function, function.name, NativeGetterOrSetter.NONE) + function.name == "get" || function.name == "set" -> null + else -> generateFunction(repository, function, function.name, NativeGetterOrSetter.NONE) + } val getterOrSetterFunction = when (function.getterOrSetter()) { NativeGetterOrSetter.NONE -> null NativeGetterOrSetter.GETTER -> generateFunction(repository, function, "get") diff --git a/libraries/tools/idl2k/src/main/kotlin/render.kt b/libraries/tools/idl2k/src/main/kotlin/render.kt index 06395d425c7..897e5a424e1 100644 --- a/libraries/tools/idl2k/src/main/kotlin/render.kt +++ b/libraries/tools/idl2k/src/main/kotlin/render.kt @@ -63,14 +63,21 @@ private fun Appendable.renderArgumentsDeclaration(args: List, StringBuilder().apply { renderAttributeDeclaration(it, it.override, false, omitDefaults) } } -private fun renderCall(call: GenerateFunctionCall) = "${call.name.replaceKeywords()}(${call.arguments.joinToString(", ") { it.replaceKeywords() }})" +private fun renderCall(call: GenerateFunctionCall) = "${call.name.replaceKeywords()}(${call.arguments.joinToString(separator = ", ", transform = String::replaceKeywords)})" private fun Appendable.renderFunctionDeclaration(f: GenerateFunction, override: Boolean, commented: Boolean, level: Int = 1) { indent(commented, level) + if (f.nativeGetterOrSetter == NativeGetterOrSetter.GETTER + && !f.returnType.nullable + && f.returnType != DynamicType) { + appendln("@Suppress(\"NATIVE_GETTER_RETURN_TYPE_SHOULD_BE_NULLABLE\")") + indent(commented, level) + } + when (f.nativeGetterOrSetter) { - NativeGetterOrSetter.GETTER -> append("operator @nativeGetter ") - NativeGetterOrSetter.SETTER -> append("operator @nativeSetter ") + NativeGetterOrSetter.GETTER -> { appendln("@nativeGetter"); indent(commented, level); append("operator ") } + NativeGetterOrSetter.SETTER -> { appendln("@nativeSetter"); indent(commented, level); append("operator ") } NativeGetterOrSetter.NONE -> {} } @@ -217,7 +224,9 @@ fun betterFunction(f1: GenerateFunction, f2: GenerateFunction): GenerateFunction f1.copy( arguments = f1.arguments .zip(f2.arguments) - .map { it.first.copy(type = it.map { it.type }.betterType(), name = it.map { it.name }.betterName()) } + .map { it.first.copy(type = it.map { it.type }.betterType(), name = it.map { it.name }.betterName()) }, + nativeGetterOrSetter = listOf(f1.nativeGetterOrSetter, f2.nativeGetterOrSetter) + .firstOrNull { it != NativeGetterOrSetter.NONE } ?: NativeGetterOrSetter.NONE ) private fun Pair.map(block: (F) -> T) = block(first) to block(second) diff --git a/libraries/tools/idl2k/src/main/kotlin/types.kt b/libraries/tools/idl2k/src/main/kotlin/types.kt index b61d324a08d..3e552d38689 100644 --- a/libraries/tools/idl2k/src/main/kotlin/types.kt +++ b/libraries/tools/idl2k/src/main/kotlin/types.kt @@ -74,3 +74,15 @@ private fun T.withNullabilityImpl(nullable: Boolean): T = if (this.nul fun T.withNullability(nullable: Boolean): T = withNullabilityImpl(this.nullable or nullable) fun T.toNullable(): T = withNullabilityImpl(true) fun T.dropNullable(): T = withNullabilityImpl(false) + +@Suppress("UNCHECKED_CAST") +fun T.toNullableIfNonPrimitive(): T = when (this) { + is UnitType -> UnitType + is DynamicType -> DynamicType + + is SimpleType -> when (this.type) { + "Int", "Short", "Byte", "Float", "Double", "Boolean", "Long" -> this + else -> this.toNullable() + } + else -> this.toNullable() +} as T \ No newline at end of file