diff --git a/generators/nativeInteropRuntime/NativeInteropRuntimeGenerator.kt b/generators/nativeInteropRuntime/NativeInteropRuntimeGenerator.kt index f9b5063b4de..cc86faa8761 100644 --- a/generators/nativeInteropRuntime/NativeInteropRuntimeGenerator.kt +++ b/generators/nativeInteropRuntime/NativeInteropRuntimeGenerator.kt @@ -31,6 +31,7 @@ fun FileWriter.generateAllocWithValue(type: PrimitiveInteropType) { * Allocates variable with given value type and initializes it with given value. */ @Suppress("FINAL_UPPER_BOUND") + @ExperimentalForeignApi public fun NativePlacement.alloc(value: T): ${typeName}VarOf = alloc<${typeName}VarOf> { this.value = value } """.trimIndent() diff --git a/kotlin-native/Interop/Indexer/build.gradle.kts b/kotlin-native/Interop/Indexer/build.gradle.kts index fbc56cad0c5..90e6732f0a3 100644 --- a/kotlin-native/Interop/Indexer/build.gradle.kts +++ b/kotlin-native/Interop/Indexer/build.gradle.kts @@ -1,17 +1,6 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. */ import org.jetbrains.kotlin.tools.lib @@ -176,7 +165,12 @@ val compileKotlin: org.jetbrains.kotlin.gradle.tasks.KotlinCompile by tasks tasks.withType().configureEach { kotlinOptions { - freeCompilerArgs += listOf("-Xskip-prerelease-check") + freeCompilerArgs += listOf( + "-Xskip-prerelease-check", + "-opt-in=kotlinx.cinterop.BetaInteropApi", + "-opt-in=kotlinx.cinterop.ExperimentalForeignApi", + ) + } } diff --git a/kotlin-native/Interop/JsRuntime/src/main/kotlin/jsinterop.kt b/kotlin-native/Interop/JsRuntime/src/main/kotlin/jsinterop.kt index 9ec24a77b3f..6610ffa6ba5 100644 --- a/kotlin-native/Interop/JsRuntime/src/main/kotlin/jsinterop.kt +++ b/kotlin-native/Interop/JsRuntime/src/main/kotlin/jsinterop.kt @@ -14,7 +14,8 @@ * limitations under the License. */ -@file:Suppress("DEPRECATION") // RetainForTarget +@file:Suppress("DEPRECATION", "TYPEALIAS_EXPANSION_DEPRECATION") // Everything is scheduled for removal +@file:OptIn(ExperimentalForeignApi::class) package kotlinx.wasm.jsinterop @@ -22,49 +23,66 @@ import kotlin.native.* import kotlin.native.internal.* import kotlinx.cinterop.* +@Deprecated(WASM_TARGET_IS_DEPRECATED, level = DeprecationLevel.WARNING) typealias Arena = Int +@Deprecated(WASM_TARGET_IS_DEPRECATED, level = DeprecationLevel.WARNING) typealias Object = Int +@Deprecated(WASM_TARGET_IS_DEPRECATED, level = DeprecationLevel.WARNING) typealias Pointer = Int +private const val WASM_TARGET_IS_DEPRECATED = "K/N WASM target and all related API is deprecated for removal. " + + "See https://blog.jetbrains.com/kotlin/2023/02/update-regarding-kotlin-native-targets for additional details" + /** * @Retain annotation is required to preserve functions from internalization and DCE. */ @RetainForTarget("wasm32") @GCUnsafeCall("Konan_js_allocateArena") +@Deprecated(WASM_TARGET_IS_DEPRECATED, level = DeprecationLevel.WARNING) external public fun allocateArena(): Arena @RetainForTarget("wasm32") @GCUnsafeCall("Konan_js_freeArena") +@Deprecated(WASM_TARGET_IS_DEPRECATED, level = DeprecationLevel.WARNING) external public fun freeArena(arena: Arena) @RetainForTarget("wasm32") @GCUnsafeCall("Konan_js_pushIntToArena") +@Deprecated(WASM_TARGET_IS_DEPRECATED, level = DeprecationLevel.WARNING) external public fun pushIntToArena(arena: Arena, value: Int) +@Deprecated(WASM_TARGET_IS_DEPRECATED, level = DeprecationLevel.WARNING) const val upperWord = 0xffffffff.toLong() shl 32 @ExportForCppRuntime +@Deprecated(WASM_TARGET_IS_DEPRECATED, level = DeprecationLevel.WARNING) fun doubleUpper(value: Double): Int = ((value.toBits() and upperWord) ushr 32) .toInt() @ExportForCppRuntime +@Deprecated(WASM_TARGET_IS_DEPRECATED, level = DeprecationLevel.WARNING) fun doubleLower(value: Double): Int = (value.toBits() and 0x00000000ffffffff) .toInt() @RetainForTarget("wasm32") @GCUnsafeCall("ReturnSlot_getDouble") +@Deprecated(WASM_TARGET_IS_DEPRECATED, level = DeprecationLevel.WARNING) external public fun ReturnSlot_getDouble(): Double @RetainForTarget("wasm32") @GCUnsafeCall("Kotlin_String_utf16pointer") +@Deprecated(WASM_TARGET_IS_DEPRECATED, level = DeprecationLevel.WARNING) external public fun stringPointer(message: String): Pointer @RetainForTarget("wasm32") +@Deprecated(WASM_TARGET_IS_DEPRECATED, level = DeprecationLevel.WARNING) @GCUnsafeCall("Kotlin_String_utf16length") external public fun stringLengthBytes(message: String): Int +@Deprecated(WASM_TARGET_IS_DEPRECATED, level = DeprecationLevel.WARNING) typealias KtFunction = ((ArrayList)->R) +@Deprecated(WASM_TARGET_IS_DEPRECATED, level = DeprecationLevel.WARNING) fun wrapFunction(func: KtFunction): Int { val ptr: Long = StableRef.create(func).asCPointer().toLong() return ptr.toInt() // TODO: LP64 unsafe. @@ -72,6 +90,7 @@ fun wrapFunction(func: KtFunction): Int { @RetainForTarget("wasm32") @ExportForCppRuntime("Konan_js_runLambda") +@Deprecated(WASM_TARGET_IS_DEPRECATED, level = DeprecationLevel.WARNING) fun runLambda(pointer: Int, argumentsArena: Arena, argumentsArenaSize: Int): Int { val arguments = arrayListOf() for (i in 0 until argumentsArenaSize) { @@ -87,6 +106,7 @@ fun runLambda(pointer: Int, argumentsArena: Arena, argumentsArenaSize: Int): Int return result.index } +@Deprecated(WASM_TARGET_IS_DEPRECATED, level = DeprecationLevel.WARNING) open class JsValue(val arena: Arena, val index: Object) { fun getInt(property: String): Int { return getInt(ArenaManager.currentArena, index, stringPointer(property), stringLengthBytes(property)) @@ -96,6 +116,7 @@ open class JsValue(val arena: Arena, val index: Object) { } } +@Deprecated(WASM_TARGET_IS_DEPRECATED, level = DeprecationLevel.WARNING) open class JsArray(arena: Arena, index: Object): JsValue(arena, index) { constructor(jsValue: JsValue): this(jsValue.arena, jsValue.index) operator fun get(index: Int): JsValue { @@ -108,37 +129,46 @@ open class JsArray(arena: Arena, index: Object): JsValue(arena, index) { @RetainForTarget("wasm32") @GCUnsafeCall("Konan_js_getInt") +@Deprecated(WASM_TARGET_IS_DEPRECATED, level = DeprecationLevel.WARNING) external public fun getInt(arena: Arena, obj: Object, propertyPtr: Pointer, propertyLen: Int): Int; @RetainForTarget("wasm32") @GCUnsafeCall("Konan_js_getProperty") +@Deprecated(WASM_TARGET_IS_DEPRECATED, level = DeprecationLevel.WARNING) external public fun Konan_js_getProperty(arena: Arena, obj: Object, propertyPtr: Pointer, propertyLen: Int): Int; @RetainForTarget("wasm32") @GCUnsafeCall("Konan_js_setFunction") +@Deprecated(WASM_TARGET_IS_DEPRECATED, level = DeprecationLevel.WARNING) external public fun setFunction(arena: Arena, obj: Object, propertyName: Pointer, propertyLength: Int , function: Int) @RetainForTarget("wasm32") @GCUnsafeCall("Konan_js_setString") +@Deprecated(WASM_TARGET_IS_DEPRECATED, level = DeprecationLevel.WARNING) external public fun setString(arena: Arena, obj: Object, propertyName: Pointer, propertyLength: Int, stringPtr: Pointer, stringLength: Int ) +@Deprecated(WASM_TARGET_IS_DEPRECATED, level = DeprecationLevel.WARNING) fun setter(obj: JsValue, property: String, string: String) { setString(obj.arena, obj.index, stringPointer(property), stringLengthBytes(property), stringPointer(string), stringLengthBytes(string)) } +@Deprecated(WASM_TARGET_IS_DEPRECATED, level = DeprecationLevel.WARNING) fun setter(obj: JsValue, property: String, lambda: KtFunction) { val pointer = wrapFunction(lambda); setFunction(obj.arena, obj.index, stringPointer(property), stringLengthBytes(property), pointer) } +@Deprecated(WASM_TARGET_IS_DEPRECATED, level = DeprecationLevel.WARNING) fun JsValue.setter(property: String, lambda: KtFunction) { setter(this, property, lambda) } +@Deprecated(WASM_TARGET_IS_DEPRECATED, level = DeprecationLevel.WARNING) fun JsValue.setter(property: String, string: String) { setter(this, property, string) } +@Deprecated(WASM_TARGET_IS_DEPRECATED, level = DeprecationLevel.WARNING) object ArenaManager { val globalArena = allocateArena() diff --git a/kotlin-native/Interop/Runtime/build.gradle.kts b/kotlin-native/Interop/Runtime/build.gradle.kts index 2009a9df704..00050e4386a 100644 --- a/kotlin-native/Interop/Runtime/build.gradle.kts +++ b/kotlin-native/Interop/Runtime/build.gradle.kts @@ -1,18 +1,8 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ + import org.jetbrains.kotlin.tools.lib import org.jetbrains.kotlin.tools.solib import org.jetbrains.kotlin.* @@ -64,10 +54,13 @@ dependencies { sourceSets.main.get().java.srcDir("src/jvm/kotlin") + tasks.withType().configureEach { kotlinOptions { freeCompilerArgs += listOf( "-opt-in=kotlin.ExperimentalUnsignedTypes", + "-opt-in=kotlinx.cinterop.BetaInteropApi", + "-opt-in=kotlinx.cinterop.ExperimentalForeignApi", "-Xskip-prerelease-check" ) } diff --git a/kotlin-native/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Annotations.kt b/kotlin-native/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Annotations.kt index f6ced80c008..96e0f6ac977 100644 --- a/kotlin-native/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Annotations.kt +++ b/kotlin-native/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Annotations.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -8,11 +8,52 @@ package kotlinx.cinterop /** * Marker for declarations that depend on numeric types of different bit width on at least two platforms. * - * @param actualPlatformTypes: Contains platform types represented as `{konanTarget}: {type fqn}` + * @param actualPlatformTypes Contains platform types represented as `{konanTarget}: {type fqn}` * e.g. ["linux_x64: kotlin.Int", "linux_arm64: kotlin.Long"] */ @Suppress("unused") // Is emitted by the Commonizer @Target(AnnotationTarget.TYPEALIAS, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY) @Retention(AnnotationRetention.BINARY) @RequiresOptIn(level = RequiresOptIn.Level.WARNING) -annotation class UnsafeNumber(val actualPlatformTypes: Array) \ No newline at end of file +public annotation class UnsafeNumber(val actualPlatformTypes: Array) + +/** + * Marks Objective-C and Swift interoperability API as Beta. + * + * The marked API has official Beta stability level, is considered to be a vital part of Kotlin/Native + * and Kotlin Multiplatform, and is evolved in a backward-compatible manner with the proper migration paths for incompatible changes. + * + * It may partially lack the concise semantics, documentation, and API to interoperate with Swift and Objective-C features + * that do not have a direct Kotlin counterpart. + * + * This API is recommended to be used for interoperability purposes, but with the API availability scope confined and narrowed down. + */ +@Target(AnnotationTarget.TYPEALIAS, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY, + AnnotationTarget.ANNOTATION_CLASS, AnnotationTarget.CLASS) +@Retention(AnnotationRetention.BINARY) +@RequiresOptIn(level = RequiresOptIn.Level.WARNING) +public annotation class BetaInteropApi + +/** + * Marks foreign-memory-related API as experimental. + * + * Foreign API includes all operations and classifiers that are required for operating with + * unmanaged and foreign memory, including but not limited to such declarations as [CPointer], [CPointed], [StableRef], and `Pinned`. + * + * Such API is considered experimental and has the following known limitations and caveats: + * - It is either undocumented or lacks extensive and sound description. + * - There is no clear behavioural semantics and explicit mapping between foreign (e.g. C-pointer) concepts + * and the corresponding foreign API (e.g. [CPointer]). Such declarations might have an unsound mapping. + * - There is no clear semantic difference between similar declarations. + * - It lacks best practices and cookbook-like recommendations. + * + * ABI and API compatibilities are provided on a best-effort basis. + * We also do provide a best-effort migration path for binary/source incompatible changes, including the proper deprecation + * cycle, migration path, and introducing replacements for the API rather than API breakage. + * + * This API is recommended to be used for interoperability purposes, but with the API availability scope confined and narrowed down. + */ +@Target(AnnotationTarget.TYPEALIAS, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY, AnnotationTarget.CLASS) +@Retention(AnnotationRetention.BINARY) +@RequiresOptIn(level = RequiresOptIn.Level.ERROR) +public annotation class ExperimentalForeignApi diff --git a/kotlin-native/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Generated.kt b/kotlin-native/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Generated.kt index 323811d4e53..d52673fd5a9 100644 --- a/kotlin-native/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Generated.kt +++ b/kotlin-native/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Generated.kt @@ -1,275 +1,326 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @file:Suppress("FINAL_UPPER_BOUND", "NOTHING_TO_INLINE") package kotlinx.cinterop +@ExperimentalForeignApi @JvmName("plus\$Byte") inline operator fun > CPointer?.plus(index: Long): CPointer? = interpretCPointer(this.rawValue + index * 1) +@ExperimentalForeignApi @JvmName("plus\$Byte") inline operator fun > CPointer?.plus(index: Int): CPointer? = this + index.toLong() +@ExperimentalForeignApi @JvmName("get\$Byte") inline operator fun CPointer>.get(index: Int): T = (this + index)!!.pointed.value +@ExperimentalForeignApi @JvmName("set\$Byte") inline operator fun CPointer>.set(index: Int, value: T) { (this + index)!!.pointed.value = value } +@ExperimentalForeignApi @JvmName("get\$Byte") inline operator fun CPointer>.get(index: Long): T = (this + index)!!.pointed.value +@ExperimentalForeignApi @JvmName("set\$Byte") inline operator fun CPointer>.set(index: Long, value: T) { (this + index)!!.pointed.value = value } +@ExperimentalForeignApi @JvmName("plus\$Short") inline operator fun > CPointer?.plus(index: Long): CPointer? = interpretCPointer(this.rawValue + index * 2) +@ExperimentalForeignApi @JvmName("plus\$Short") inline operator fun > CPointer?.plus(index: Int): CPointer? = this + index.toLong() +@ExperimentalForeignApi @JvmName("get\$Short") inline operator fun CPointer>.get(index: Int): T = (this + index)!!.pointed.value +@ExperimentalForeignApi @JvmName("set\$Short") inline operator fun CPointer>.set(index: Int, value: T) { (this + index)!!.pointed.value = value } +@ExperimentalForeignApi @JvmName("get\$Short") inline operator fun CPointer>.get(index: Long): T = (this + index)!!.pointed.value +@ExperimentalForeignApi @JvmName("set\$Short") inline operator fun CPointer>.set(index: Long, value: T) { (this + index)!!.pointed.value = value } +@ExperimentalForeignApi @JvmName("plus\$Int") inline operator fun > CPointer?.plus(index: Long): CPointer? = interpretCPointer(this.rawValue + index * 4) +@ExperimentalForeignApi @JvmName("plus\$Int") inline operator fun > CPointer?.plus(index: Int): CPointer? = this + index.toLong() +@ExperimentalForeignApi @JvmName("get\$Int") inline operator fun CPointer>.get(index: Int): T = (this + index)!!.pointed.value +@ExperimentalForeignApi @JvmName("set\$Int") inline operator fun CPointer>.set(index: Int, value: T) { (this + index)!!.pointed.value = value } +@ExperimentalForeignApi @JvmName("get\$Int") inline operator fun CPointer>.get(index: Long): T = (this + index)!!.pointed.value +@ExperimentalForeignApi @JvmName("set\$Int") inline operator fun CPointer>.set(index: Long, value: T) { (this + index)!!.pointed.value = value } +@ExperimentalForeignApi @JvmName("plus\$Long") inline operator fun > CPointer?.plus(index: Long): CPointer? = interpretCPointer(this.rawValue + index * 8) +@ExperimentalForeignApi @JvmName("plus\$Long") inline operator fun > CPointer?.plus(index: Int): CPointer? = this + index.toLong() +@ExperimentalForeignApi @JvmName("get\$Long") inline operator fun CPointer>.get(index: Int): T = (this + index)!!.pointed.value +@ExperimentalForeignApi @JvmName("set\$Long") inline operator fun CPointer>.set(index: Int, value: T) { (this + index)!!.pointed.value = value } +@ExperimentalForeignApi @JvmName("get\$Long") inline operator fun CPointer>.get(index: Long): T = (this + index)!!.pointed.value +@ExperimentalForeignApi @JvmName("set\$Long") inline operator fun CPointer>.set(index: Long, value: T) { (this + index)!!.pointed.value = value } +@ExperimentalForeignApi @JvmName("plus\$UByte") inline operator fun > CPointer?.plus(index: Long): CPointer? = interpretCPointer(this.rawValue + index * 1) +@ExperimentalForeignApi @JvmName("plus\$UByte") inline operator fun > CPointer?.plus(index: Int): CPointer? = this + index.toLong() +@ExperimentalForeignApi @JvmName("get\$UByte") inline operator fun CPointer>.get(index: Int): T = (this + index)!!.pointed.value +@ExperimentalForeignApi inline operator fun CPointer>.set(index: Int, value: T) { (this + index)!!.pointed.value = value } +@ExperimentalForeignApi inline operator fun CPointer>.get(index: Long): T = (this + index)!!.pointed.value +@ExperimentalForeignApi inline operator fun CPointer>.set(index: Long, value: T) { (this + index)!!.pointed.value = value } +@ExperimentalForeignApi @JvmName("plus\$UShort") inline operator fun > CPointer?.plus(index: Long): CPointer? = interpretCPointer(this.rawValue + index * 2) +@ExperimentalForeignApi @JvmName("plus\$UShort") inline operator fun > CPointer?.plus(index: Int): CPointer? = this + index.toLong() +@ExperimentalForeignApi @JvmName("get\$UShort") inline operator fun CPointer>.get(index: Int): T = (this + index)!!.pointed.value +@ExperimentalForeignApi inline operator fun CPointer>.set(index: Int, value: T) { (this + index)!!.pointed.value = value } +@ExperimentalForeignApi @JvmName("get\$UShort") inline operator fun CPointer>.get(index: Long): T = (this + index)!!.pointed.value +@ExperimentalForeignApi inline operator fun CPointer>.set(index: Long, value: T) { (this + index)!!.pointed.value = value } +@ExperimentalForeignApi @JvmName("plus\$UInt") inline operator fun > CPointer?.plus(index: Long): CPointer? = interpretCPointer(this.rawValue + index * 4) +@ExperimentalForeignApi @JvmName("plus\$UInt") inline operator fun > CPointer?.plus(index: Int): CPointer? = this + index.toLong() +@ExperimentalForeignApi @JvmName("get\$UInt") inline operator fun CPointer>.get(index: Int): T = (this + index)!!.pointed.value +@ExperimentalForeignApi inline operator fun CPointer>.set(index: Int, value: T) { (this + index)!!.pointed.value = value } +@ExperimentalForeignApi @JvmName("get\$UInt") inline operator fun CPointer>.get(index: Long): T = (this + index)!!.pointed.value +@ExperimentalForeignApi inline operator fun CPointer>.set(index: Long, value: T) { (this + index)!!.pointed.value = value } +@ExperimentalForeignApi @JvmName("plus\$ULong") inline operator fun > CPointer?.plus(index: Long): CPointer? = interpretCPointer(this.rawValue + index * 8) +@ExperimentalForeignApi @JvmName("plus\$ULong") inline operator fun > CPointer?.plus(index: Int): CPointer? = this + index.toLong() +@ExperimentalForeignApi @JvmName("get\$ULong") inline operator fun CPointer>.get(index: Int): T = (this + index)!!.pointed.value +@ExperimentalForeignApi inline operator fun CPointer>.set(index: Int, value: T) { (this + index)!!.pointed.value = value } +@ExperimentalForeignApi @JvmName("get\$ULong") inline operator fun CPointer>.get(index: Long): T = (this + index)!!.pointed.value +@ExperimentalForeignApi inline operator fun CPointer>.set(index: Long, value: T) { (this + index)!!.pointed.value = value } +@ExperimentalForeignApi @JvmName("plus\$Float") inline operator fun > CPointer?.plus(index: Long): CPointer? = interpretCPointer(this.rawValue + index * 4) +@ExperimentalForeignApi @JvmName("plus\$Float") inline operator fun > CPointer?.plus(index: Int): CPointer? = this + index.toLong() +@ExperimentalForeignApi @JvmName("get\$Float") inline operator fun CPointer>.get(index: Int): T = (this + index)!!.pointed.value +@ExperimentalForeignApi @JvmName("set\$Float") inline operator fun CPointer>.set(index: Int, value: T) { (this + index)!!.pointed.value = value } +@ExperimentalForeignApi @JvmName("get\$Float") inline operator fun CPointer>.get(index: Long): T = (this + index)!!.pointed.value +@ExperimentalForeignApi @JvmName("set\$Float") inline operator fun CPointer>.set(index: Long, value: T) { (this + index)!!.pointed.value = value } +@ExperimentalForeignApi @JvmName("plus\$Double") inline operator fun > CPointer?.plus(index: Long): CPointer? = interpretCPointer(this.rawValue + index * 8) +@ExperimentalForeignApi @JvmName("plus\$Double") inline operator fun > CPointer?.plus(index: Int): CPointer? = this + index.toLong() +@ExperimentalForeignApi @JvmName("get\$Double") inline operator fun CPointer>.get(index: Int): T = (this + index)!!.pointed.value +@ExperimentalForeignApi @JvmName("set\$Double") inline operator fun CPointer>.set(index: Int, value: T) { (this + index)!!.pointed.value = value } +@ExperimentalForeignApi @JvmName("get\$Double") inline operator fun CPointer>.get(index: Long): T = (this + index)!!.pointed.value +@ExperimentalForeignApi @JvmName("set\$Double") inline operator fun CPointer>.set(index: Long, value: T) { (this + index)!!.pointed.value = value } -/* Generated by: +/* Seva: Used to be generated by: + +Seva: It probably means the reasoning of this API and is general applicability should be revisited #!/bin/bash diff --git a/kotlin-native/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/StableRef.kt b/kotlin-native/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/StableRef.kt index 727dbe9f469..e37cbac959e 100644 --- a/kotlin-native/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/StableRef.kt +++ b/kotlin-native/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/StableRef.kt @@ -1,24 +1,10 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ package kotlinx.cinterop -@Deprecated("Use StableRef instead", ReplaceWith("StableRef"), DeprecationLevel.ERROR) -typealias StableObjPtr = StableRef<*> - /** * This class provides a way to create a stable handle to any Kotlin object. * After [converting to CPointer][asCPointer] it can be safely passed to native code e.g. to be received @@ -28,6 +14,7 @@ typealias StableObjPtr = StableRef<*> */ @Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS") @kotlin.jvm.JvmInline +@ExperimentalForeignApi public value class StableRef @PublishedApi internal constructor( private val stablePtr: COpaquePointer ) { @@ -39,17 +26,7 @@ public value class StableRef @PublishedApi internal constructor( */ fun create(any: T) = StableRef(createStablePointer(any)) - /** - * Creates [StableRef] from given raw value. - * - * @param value must be a [value] of some [StableRef] - */ - @Deprecated("Use CPointer<*>.asStableRef() instead", ReplaceWith("ptr.asStableRef()"), - DeprecationLevel.ERROR) - fun fromValue(value: COpaquePointer) = value.asStableRef() } - @Deprecated("Use .asCPointer() instead", ReplaceWith("this.asCPointer()"), DeprecationLevel.ERROR) - val value: COpaquePointer get() = this.asCPointer() /** * Converts the handle to C pointer. @@ -75,4 +52,5 @@ public value class StableRef @PublishedApi internal constructor( /** * Converts to [StableRef] this opaque pointer produced by [StableRef.asCPointer]. */ +@ExperimentalForeignApi inline fun CPointer<*>.asStableRef(): StableRef = StableRef(this).also { it.get() } diff --git a/kotlin-native/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Types.kt b/kotlin-native/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Types.kt index e338c4928f9..7c4901ef85e 100644 --- a/kotlin-native/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Types.kt +++ b/kotlin-native/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Types.kt @@ -1,21 +1,12 @@ /* - * Copyright 2010-2019 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ package kotlinx.cinterop +import kotlin.native.* + /** * The entity which has an associated native pointer. * Subtypes are supposed to represent interpretations of the pointed data or code. @@ -24,12 +15,14 @@ package kotlinx.cinterop * * TODO: the behavior of [equals], [hashCode] and [toString] differs on Native and JVM backends. */ +@ExperimentalForeignApi public open class NativePointed internal constructor(rawPtr: NonNullNativePtr) { var rawPtr = rawPtr.toNativePtr() internal set } // `null` value of `NativePointed?` is mapped to `nativeNullPtr`. +@ExperimentalForeignApi public val NativePointed?.rawPtr: NativePtr get() = if (this != null) this.rawPtr else nativeNullPtr @@ -38,21 +31,27 @@ public val NativePointed?.rawPtr: NativePtr * * @param T must not be abstract */ +@ExperimentalForeignApi public inline fun interpretPointed(ptr: NativePtr): T = interpretNullablePointed(ptr)!! +@ExperimentalForeignApi private class OpaqueNativePointed(rawPtr: NativePtr) : NativePointed(rawPtr.toNonNull()) +@ExperimentalForeignApi public fun interpretOpaquePointed(ptr: NativePtr): NativePointed = interpretPointed(ptr) +@ExperimentalForeignApi public fun interpretNullableOpaquePointed(ptr: NativePtr): NativePointed? = interpretNullablePointed(ptr) /** * Changes the interpretation of the pointed data or code. */ +@ExperimentalForeignApi public inline fun NativePointed.reinterpret(): T = interpretPointed(this.rawPtr) /** * C data or code. */ +@ExperimentalForeignApi public abstract class CPointed(rawPtr: NativePtr) : NativePointed(rawPtr.toNonNull()) /** @@ -64,9 +63,10 @@ public abstract class CPointed(rawPtr: NativePtr) : NativePointed(rawPtr.toNonNu * Passing [CValues] has nearly the same semantics as passing by value: the C function receives * the pointer to the temporary copy of these values, and the caller can't observe the modifications to this copy. * The copy is valid until the C function returns. - * There are also other implementations of [CValuesRef] that provide temporary pointer, + * There are other implementations of [CValuesRef] that provide temporary pointer, * e.g. Kotlin Native specific [refTo] functions to pass primitive arrays directly to native. */ +@ExperimentalForeignApi public abstract class CValuesRef { /** * If this reference is [CPointer], returns this pointer, otherwise @@ -79,6 +79,7 @@ public abstract class CValuesRef { * The (possibly empty) sequence of immutable C values. * It is self-contained and doesn't depend on native memory. */ +@ExperimentalForeignApi public abstract class CValues : CValuesRef() { /** * Copies the values to [placement] and returns the pointer to the copy. @@ -126,6 +127,7 @@ public abstract class CValues : CValuesRef() { public abstract fun place(placement: CPointer): CPointer } +@ExperimentalForeignApi public fun CValues.placeTo(scope: AutofreeScope) = this.getPointer(scope) /** @@ -134,11 +136,13 @@ public fun CValues.placeTo(scope: AutofreeScope) = this.getPo * * TODO: consider providing an adapter instead of subtyping [CValues]. */ +@ExperimentalForeignApi public abstract class CValue : CValues() /** * C pointer. */ +@ExperimentalForeignApi public class CPointer internal constructor(@PublishedApi internal val value: NonNullNativePtr) : CValuesRef() { // TODO: replace by [value]. @@ -165,6 +169,7 @@ public class CPointer internal constructor(@PublishedApi internal /** * Returns the pointer to this data or code. */ +@ExperimentalForeignApi public val T.ptr: CPointer get() = interpretCPointer(this.rawPtr)!! @@ -173,32 +178,47 @@ public val T.ptr: CPointer * * @param T must not be abstract */ +@ExperimentalForeignApi public inline val CPointer.pointed: T get() = interpretPointed(this.rawValue) // `null` value of `CPointer?` is mapped to `nativeNullPtr` +@ExperimentalForeignApi public val CPointer<*>?.rawValue: NativePtr get() = if (this != null) this.rawValue else nativeNullPtr +@ExperimentalForeignApi public fun CPointer<*>.reinterpret(): CPointer = interpretCPointer(this.rawValue)!! +@ExperimentalForeignApi public fun CPointer?.toLong() = this.rawValue.toLong() +@ExperimentalForeignApi public fun Long.toCPointer(): CPointer? = interpretCPointer(nativeNullPtr + this) /** * The [CPointed] without any specified interpretation. */ +@ExperimentalForeignApi public abstract class COpaque(rawPtr: NativePtr) : CPointed(rawPtr) // TODO: should it correspond to COpaquePointer? /** * The pointer with an opaque type. */ -public typealias COpaquePointer = CPointer // FIXME +@OptIn(ExperimentalForeignApi::class) +/* + * This typealias should be marked as @ExperimentalForeignApi + * but such marking starts crashing the compiler (due to typealias handling bug). + * We are not blocked by this behaviour because any declaration mentioning + * `COpaquePointer` will trigger `ExperimentalForeignApi` error due to typealias expansion + * in opt-in propagation mechanism. + */ +public typealias COpaquePointer = CPointer // FIXME (the comment is about the typealias, not its opt-in annotation) /** * The variable containing a [COpaquePointer]. */ +@ExperimentalForeignApi public typealias COpaquePointerVar = CPointerVarOf /** @@ -207,6 +227,7 @@ public typealias COpaquePointerVar = CPointerVarOf * The non-abstract subclasses should represent the (complete) C data type and thus specify size and alignment. * Each such subclass must have a companion object which is a [Type]. */ +@ExperimentalForeignApi public abstract class CVariable(rawPtr: NativePtr) : CPointed(rawPtr) { /** @@ -227,18 +248,22 @@ public abstract class CVariable(rawPtr: NativePtr) : CPointed(rawPtr) { } @Suppress("DEPRECATION") +@ExperimentalForeignApi public inline fun sizeOf() = typeOf().size @Suppress("DEPRECATION") +@ExperimentalForeignApi public inline fun alignOf() = typeOf().align /** * Returns the member of this [CStructVar] which is located by given offset in bytes. */ +@ExperimentalForeignApi public inline fun CStructVar.memberAt(offset: Long): T { return interpretPointed(this.rawPtr + offset) } +@ExperimentalForeignApi public inline fun CStructVar.arrayMemberAt(offset: Long): CArrayPointer { return interpretCPointer(this.rawPtr + offset)!! } @@ -246,6 +271,7 @@ public inline fun CStructVar.arrayMemberAt(offset: Long) /** * The C struct-typed variable located in memory. */ +@ExperimentalForeignApi public abstract class CStructVar(rawPtr: NativePtr) : CVariable(rawPtr) { @Deprecated("Use sizeOf() or alignOf() instead.") @Suppress("DEPRECATION") @@ -255,6 +281,7 @@ public abstract class CStructVar(rawPtr: NativePtr) : CVariable(rawPtr) { /** * The C primitive-typed variable located in memory. */ +@ExperimentalForeignApi sealed class CPrimitiveVar(rawPtr: NativePtr) : CVariable(rawPtr) { // aligning by size is obviously enough @Deprecated("Use sizeOf() or alignOf() instead.") @@ -267,11 +294,13 @@ public interface CEnum { public val value: Any } +@ExperimentalForeignApi public abstract class CEnumVar(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) // generics below are used for typedef support // these classes are not supposed to be used directly, instead the typealiases are provided. +@ExperimentalForeignApi @Suppress("FINAL_UPPER_BOUND") public class BooleanVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) { @Deprecated("Use sizeOf() or alignOf() instead.") @@ -279,6 +308,7 @@ public class BooleanVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr companion object : Type(1) } +@ExperimentalForeignApi @Suppress("FINAL_UPPER_BOUND") public class ByteVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) { @Deprecated("Use sizeOf() or alignOf() instead.") @@ -286,6 +316,7 @@ public class ByteVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) { companion object : Type(1) } +@ExperimentalForeignApi @Suppress("FINAL_UPPER_BOUND") public class ShortVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) { @Deprecated("Use sizeOf() or alignOf() instead.") @@ -293,6 +324,7 @@ public class ShortVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) { companion object : Type(2) } +@ExperimentalForeignApi @Suppress("FINAL_UPPER_BOUND") public class IntVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) { @Deprecated("Use sizeOf() or alignOf() instead.") @@ -300,6 +332,7 @@ public class IntVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) { companion object : Type(4) } +@ExperimentalForeignApi @Suppress("FINAL_UPPER_BOUND") public class LongVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) { @Deprecated("Use sizeOf() or alignOf() instead.") @@ -307,6 +340,7 @@ public class LongVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) { companion object : Type(8) } +@ExperimentalForeignApi @Suppress("FINAL_UPPER_BOUND") public class UByteVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) { @Deprecated("Use sizeOf() or alignOf() instead.") @@ -314,6 +348,7 @@ public class UByteVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) { companion object : Type(1) } +@ExperimentalForeignApi @Suppress("FINAL_UPPER_BOUND") public class UShortVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) { @Deprecated("Use sizeOf() or alignOf() instead.") @@ -321,6 +356,7 @@ public class UShortVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) companion object : Type(2) } +@ExperimentalForeignApi @Suppress("FINAL_UPPER_BOUND") public class UIntVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) { @Deprecated("Use sizeOf() or alignOf() instead.") @@ -328,6 +364,7 @@ public class UIntVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) { companion object : Type(4) } +@ExperimentalForeignApi @Suppress("FINAL_UPPER_BOUND") public class ULongVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) { @Deprecated("Use sizeOf() or alignOf() instead.") @@ -335,6 +372,7 @@ public class ULongVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) { companion object : Type(8) } +@ExperimentalForeignApi @Suppress("FINAL_UPPER_BOUND") public class FloatVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) { @Deprecated("Use sizeOf() or alignOf() instead.") @@ -342,6 +380,7 @@ public class FloatVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) { companion object : Type(4) } +@ExperimentalForeignApi @Suppress("FINAL_UPPER_BOUND") public class DoubleVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) { @Deprecated("Use sizeOf() or alignOf() instead.") @@ -349,18 +388,30 @@ public class DoubleVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) companion object : Type(8) } +@ExperimentalForeignApi public typealias BooleanVar = BooleanVarOf +@ExperimentalForeignApi public typealias ByteVar = ByteVarOf +@ExperimentalForeignApi public typealias ShortVar = ShortVarOf +@ExperimentalForeignApi public typealias IntVar = IntVarOf +@ExperimentalForeignApi public typealias LongVar = LongVarOf +@ExperimentalForeignApi public typealias UByteVar = UByteVarOf +@ExperimentalForeignApi public typealias UShortVar = UShortVarOf +@ExperimentalForeignApi public typealias UIntVar = UIntVarOf +@ExperimentalForeignApi public typealias ULongVar = ULongVarOf +@ExperimentalForeignApi public typealias FloatVar = FloatVarOf +@ExperimentalForeignApi public typealias DoubleVar = DoubleVarOf +@ExperimentalForeignApi @Suppress("FINAL_UPPER_BOUND", "UNCHECKED_CAST") public var BooleanVarOf.value: T get() { @@ -369,47 +420,59 @@ public var BooleanVarOf.value: T } set(value) = nativeMemUtils.putByte(this, value.toByte()) +// TODO remove these boolean <-> byte declarations + @Suppress("NOTHING_TO_INLINE") +@ExperimentalForeignApi public inline fun Boolean.toByte(): Byte = if (this) 1 else 0 @Suppress("NOTHING_TO_INLINE") +@ExperimentalForeignApi public inline fun Byte.toBoolean() = (this.toInt() != 0) +@ExperimentalForeignApi @Suppress("FINAL_UPPER_BOUND", "UNCHECKED_CAST") public var ByteVarOf.value: T get() = nativeMemUtils.getByte(this) as T set(value) = nativeMemUtils.putByte(this, value) +@ExperimentalForeignApi @Suppress("FINAL_UPPER_BOUND", "UNCHECKED_CAST") public var ShortVarOf.value: T get() = nativeMemUtils.getShort(this) as T set(value) = nativeMemUtils.putShort(this, value) +@ExperimentalForeignApi @Suppress("FINAL_UPPER_BOUND", "UNCHECKED_CAST") public var IntVarOf.value: T get() = nativeMemUtils.getInt(this) as T set(value) = nativeMemUtils.putInt(this, value) +@ExperimentalForeignApi @Suppress("FINAL_UPPER_BOUND", "UNCHECKED_CAST") public var LongVarOf.value: T get() = nativeMemUtils.getLong(this) as T set(value) = nativeMemUtils.putLong(this, value) +@ExperimentalForeignApi @Suppress("FINAL_UPPER_BOUND", "UNCHECKED_CAST") public var UByteVarOf.value: T get() = nativeMemUtils.getByte(this).toUByte() as T set(value) = nativeMemUtils.putByte(this, value.toByte()) +@ExperimentalForeignApi @Suppress("FINAL_UPPER_BOUND", "UNCHECKED_CAST") public var UShortVarOf.value: T get() = nativeMemUtils.getShort(this).toUShort() as T set(value) = nativeMemUtils.putShort(this, value.toShort()) +@ExperimentalForeignApi @Suppress("FINAL_UPPER_BOUND", "UNCHECKED_CAST") public var UIntVarOf.value: T get() = nativeMemUtils.getInt(this).toUInt() as T set(value) = nativeMemUtils.putInt(this, value.toInt()) +@ExperimentalForeignApi @Suppress("FINAL_UPPER_BOUND", "UNCHECKED_CAST") public var ULongVarOf.value: T get() = nativeMemUtils.getLong(this).toULong() as T @@ -417,17 +480,20 @@ public var ULongVarOf.value: T // TODO: ensure native floats have the appropriate binary representation +@ExperimentalForeignApi @Suppress("FINAL_UPPER_BOUND", "UNCHECKED_CAST") public var FloatVarOf.value: T get() = nativeMemUtils.getFloat(this) as T set(value) = nativeMemUtils.putFloat(this, value) +@ExperimentalForeignApi @Suppress("FINAL_UPPER_BOUND", "UNCHECKED_CAST") public var DoubleVarOf.value: T get() = nativeMemUtils.getDouble(this) as T set(value) = nativeMemUtils.putDouble(this, value) +@ExperimentalForeignApi public class CPointerVarOf>(rawPtr: NativePtr) : CVariable(rawPtr) { @Deprecated("Use sizeOf() or alignOf() instead.") @Suppress("DEPRECATION") @@ -437,11 +503,13 @@ public class CPointerVarOf>(rawPtr: NativePtr) : CVariable(rawPt /** * The C data variable containing the pointer to `T`. */ +@ExperimentalForeignApi public typealias CPointerVar = CPointerVarOf> /** * The value of this variable. */ +@ExperimentalForeignApi @Suppress("UNCHECKED_CAST") public inline var

> CPointerVarOf

.value: P? get() = interpretCPointer(nativeMemUtils.getNativePtr(this)) as P? @@ -449,15 +517,17 @@ public inline var

> CPointerVarOf

.value: P? /** * The code or data pointed by the value of this variable. - * + * * @param T must not be abstract */ +@ExperimentalForeignApi public inline var > CPointerVarOf

.pointed: T? get() = this.value?.pointed set(value) { this.value = value?.ptr as P? } +@ExperimentalForeignApi public inline operator fun CPointer.get(index: Long): T { val offset = if (index == 0L) { 0L // optimization for JVM impl which uses reflection for now. @@ -467,40 +537,50 @@ public inline operator fun CPointer.get(index: Long): return interpretPointed(this.rawValue + offset) } +@ExperimentalForeignApi public inline operator fun CPointer.get(index: Int): T = this.get(index.toLong()) +@ExperimentalForeignApi @Suppress("NOTHING_TO_INLINE") @JvmName("plus\$CPointer") public inline operator fun > CPointer?.plus(index: Long): CPointer? = interpretCPointer(this.rawValue + index * pointerSize) +@ExperimentalForeignApi @Suppress("NOTHING_TO_INLINE") @JvmName("plus\$CPointer") public inline operator fun > CPointer?.plus(index: Int): CPointer? = this + index.toLong() +@ExperimentalForeignApi @Suppress("NOTHING_TO_INLINE") public inline operator fun > CPointer>.get(index: Int): T? = (this + index)!!.pointed.value +@ExperimentalForeignApi @Suppress("NOTHING_TO_INLINE") public inline operator fun > CPointer>.set(index: Int, value: T?) { (this + index)!!.pointed.value = value } +@ExperimentalForeignApi @Suppress("NOTHING_TO_INLINE") public inline operator fun > CPointer>.get(index: Long): T? = (this + index)!!.pointed.value +@ExperimentalForeignApi @Suppress("NOTHING_TO_INLINE") public inline operator fun > CPointer>.set(index: Long, value: T?) { (this + index)!!.pointed.value = value } +@ExperimentalForeignApi public typealias CArrayPointer = CPointer +@ExperimentalForeignApi public typealias CArrayPointerVar = CPointerVar /** * The C function. */ +@ExperimentalForeignApi public class CFunction>(rawPtr: NativePtr) : CPointed(rawPtr) diff --git a/kotlin-native/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Utils.kt b/kotlin-native/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Utils.kt index 9f6e9b67e33..b049b292b4a 100644 --- a/kotlin-native/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Utils.kt +++ b/kotlin-native/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Utils.kt @@ -1,21 +1,11 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ package kotlinx.cinterop +@ExperimentalForeignApi public interface NativePlacement { public fun alloc(size: Long, align: Int): NativePointed @@ -23,21 +13,27 @@ public interface NativePlacement { public fun alloc(size: Int, align: Int): NativePointed = alloc(size.toLong(), align) } +@ExperimentalForeignApi public interface NativeFreeablePlacement : NativePlacement { public fun free(mem: NativePtr) } +@ExperimentalForeignApi public fun NativeFreeablePlacement.free(pointer: CPointer<*>) = this.free(pointer.rawValue) +@ExperimentalForeignApi public fun NativeFreeablePlacement.free(pointed: NativePointed) = this.free(pointed.rawPtr) +@ExperimentalForeignApi public object nativeHeap : NativeFreeablePlacement { override fun alloc(size: Long, align: Int) = nativeMemUtils.alloc(size, align) override fun free(mem: NativePtr) = nativeMemUtils.free(mem) } +@ExperimentalForeignApi private typealias Deferred = () -> Unit +@ExperimentalForeignApi public open class DeferScope { @PublishedApi @@ -65,10 +61,12 @@ public open class DeferScope { } } +@ExperimentalForeignApi public abstract class AutofreeScope : DeferScope(), NativePlacement { abstract override fun alloc(size: Long, align: Int): NativePointed } +@ExperimentalForeignApi public open class ArenaBase(private val parent: NativeFreeablePlacement = nativeHeap) : AutofreeScope() { private var lastChunk: NativePointed? = null @@ -97,6 +95,7 @@ public open class ArenaBase(private val parent: NativeFreeablePlacement = native } +@ExperimentalForeignApi public class Arena(parent: NativeFreeablePlacement = nativeHeap) : ArenaBase(parent) { fun clear() = this.clearImpl() } @@ -106,12 +105,14 @@ public class Arena(parent: NativeFreeablePlacement = nativeHeap) : ArenaBase(par * * @param T must not be abstract */ +@ExperimentalForeignApi public inline fun NativePlacement.alloc(): T = @Suppress("DEPRECATION") alloc(typeOf()).reinterpret() @PublishedApi @Suppress("DEPRECATION") +@ExperimentalForeignApi internal fun NativePlacement.alloc(type: CVariable.Type): NativePointed = alloc(type.size, type.align) @@ -120,6 +121,7 @@ internal fun NativePlacement.alloc(type: CVariable.Type): NativePointed = * * @param T must not be abstract */ +@ExperimentalForeignApi public inline fun NativePlacement.alloc(initialize: T.() -> Unit): T = alloc().also { it.initialize() } @@ -128,6 +130,7 @@ public inline fun NativePlacement.alloc(initialize: T.() * * @param T must not be abstract */ +@ExperimentalForeignApi public inline fun NativePlacement.allocArray(length: Long): CArrayPointer = alloc(sizeOf() * length, alignOf()).reinterpret().ptr @@ -136,6 +139,7 @@ public inline fun NativePlacement.allocArray(length: Lon * * @param T must not be abstract */ +@ExperimentalForeignApi public inline fun NativePlacement.allocArray(length: Int): CArrayPointer = allocArray(length.toLong()) @@ -144,6 +148,7 @@ public inline fun NativePlacement.allocArray(length: Int * * @param T must not be abstract */ +@ExperimentalForeignApi public inline fun NativePlacement.allocArray(length: Long, initializer: T.(index: Long)->Unit): CArrayPointer { val res = allocArray(length) @@ -160,6 +165,7 @@ public inline fun NativePlacement.allocArray(length: Lon * * @param T must not be abstract */ +@ExperimentalForeignApi public inline fun NativePlacement.allocArray( length: Int, initializer: T.(index: Int)->Unit): CArrayPointer = allocArray(length.toLong()) { index -> this.initializer(index.toInt()) @@ -169,6 +175,7 @@ public inline fun NativePlacement.allocArray( /** * Allocates C array of pointers to given elements. */ +@ExperimentalForeignApi public fun NativePlacement.allocArrayOfPointersTo(elements: List): CArrayPointer> { val res = allocArray>(elements.size) elements.forEachIndexed { index, value -> @@ -180,12 +187,14 @@ public fun NativePlacement.allocArrayOfPointersTo(elements: List< /** * Allocates C array of pointers to given elements. */ +@ExperimentalForeignApi public fun NativePlacement.allocArrayOfPointersTo(vararg elements: T?) = allocArrayOfPointersTo(listOf(*elements)) /** * Allocates C array of given values. */ +@ExperimentalForeignApi public inline fun > NativePlacement.allocArrayOf(vararg elements: T?): CArrayPointer> { return allocArrayOf(listOf(*elements)) @@ -194,6 +203,7 @@ public inline fun > /** * Allocates C array of given values. */ +@ExperimentalForeignApi public inline fun > NativePlacement.allocArrayOf(elements: List): CArrayPointer> { @@ -206,12 +216,14 @@ public inline fun > return res } +@ExperimentalForeignApi public fun NativePlacement.allocArrayOf(elements: ByteArray): CArrayPointer { val result = allocArray(elements.size) nativeMemUtils.putByteArray(elements, result.pointed, elements.size) return result } +@ExperimentalForeignApi public fun NativePlacement.allocArrayOf(vararg elements: Float): CArrayPointer { val res = allocArray(elements.size) var index = 0 @@ -222,10 +234,12 @@ public fun NativePlacement.allocArrayOf(vararg elements: Float): CArrayPointer NativePlacement.allocPointerTo() = alloc>() @PublishedApi -internal class ZeroValue(private val sizeBytes: Int, private val alignBytes: Int): CValue() { +@ExperimentalForeignApi +internal class ZeroValue(private val sizeBytes: Int, private val alignBytes: Int) : CValue() { // Optimization to avoid unneeded virtual calls in base class implementation. override fun getPointer(scope: AutofreeScope): CPointer { return place(interpretCPointer(scope.alloc(size, align).rawPtr)!!) @@ -241,12 +255,16 @@ internal class ZeroValue(private val sizeBytes: Int, private val a } @Suppress("NOTHING_TO_INLINE") +@ExperimentalForeignApi public inline fun zeroValue(size: Int, align: Int): CValue = ZeroValue(size, align) +@ExperimentalForeignApi public inline fun zeroValue(): CValue = zeroValue(sizeOf().toInt(), alignOf()) +@ExperimentalForeignApi public inline fun cValue(): CValue = zeroValue() +@ExperimentalForeignApi public fun CPointed.readValues(size: Int, align: Int): CValues { val bytes = ByteArray(size) nativeMemUtils.getByteArray(this, bytes, size) @@ -265,9 +283,11 @@ public fun CPointed.readValues(size: Int, align: Int): CValues T.readValues(count: Int): CValues = this.readValues(size = count * sizeOf().toInt(), align = alignOf()) +@ExperimentalForeignApi public fun CPointed.readValue(size: Long, align: Int): CValue { val bytes = ByteArray(size.toInt()) nativeMemUtils.getByteArray(this, bytes, size.toInt()) @@ -287,19 +307,24 @@ public fun CPointed.readValue(size: Long, align: Int): CValue } @Suppress("DEPRECATION") -@PublishedApi internal fun CPointed.readValue(type: CVariable.Type): CValue = - readValue(type.size, type.align) +@PublishedApi +@ExperimentalForeignApi +internal fun CPointed.readValue(type: CVariable.Type): CValue = + readValue(type.size, type.align) // Note: can't be declared as property due to possible clash with a struct field. // TODO: find better name. @Suppress("DEPRECATION") +@ExperimentalForeignApi public inline fun T.readValue(): CValue = this.readValue(typeOf()) -public fun CValue.write(location: NativePtr) { +@ExperimentalForeignApi +public fun CValue.write(location: NativePtr) { this.place(interpretCPointer(location)!!) } // TODO: optimize +@ExperimentalForeignApi public fun CValues.getBytes(): ByteArray = memScoped { val result = ByteArray(size) nativeMemUtils.getByteArray( @@ -313,18 +338,22 @@ public fun CValues.getBytes(): ByteArray = memScoped { /** * Calls the [block] with temporary copy of this value as receiver. */ +@ExperimentalForeignApi public inline fun CValue.useContents(block: T.() -> R): R = memScoped { this@useContents.placeTo(memScope).pointed.block() } +@ExperimentalForeignApi public inline fun CValue.copy(modify: T.() -> Unit): CValue = useContents { this.modify() this.readValue() } +@ExperimentalForeignApi public inline fun cValue(initialize: T.() -> Unit): CValue = zeroValue().copy(modify = initialize) +@ExperimentalForeignApi public inline fun createValues(count: Int, initializer: T.(index: Int) -> Unit) = memScoped { val array = allocArray(count, initializer) array[0].readValues(count) @@ -334,7 +363,8 @@ public inline fun createValues(count: Int, initializer: /** * Returns sequence of immutable values [CValues] to pass them to C code. */ -fun cValuesOf(vararg elements: Byte): CValues = object : CValues() { +@ExperimentalForeignApi +public fun cValuesOf(vararg elements: Byte): CValues = object : CValues() { // Optimization to avoid unneeded virtual calls in base class implementation. override fun getPointer(scope: AutofreeScope): CPointer { return place(interpretCPointer(scope.alloc(size, align).rawPtr)!!) @@ -348,34 +378,49 @@ fun cValuesOf(vararg elements: Byte): CValues = object : CValues = createValues(elements.size) { index -> this.value = elements[index] } +@ExperimentalForeignApi public fun cValuesOf(vararg elements: Int): CValues = createValues(elements.size) { index -> this.value = elements[index] } +@ExperimentalForeignApi public fun cValuesOf(vararg elements: Long): CValues = createValues(elements.size) { index -> this.value = elements[index] } +@ExperimentalForeignApi public fun cValuesOf(vararg elements: Float): CValues = createValues(elements.size) { index -> this.value = elements[index] } +@ExperimentalForeignApi public fun cValuesOf(vararg elements: Double): CValues = createValues(elements.size) { index -> this.value = elements[index] } +@ExperimentalForeignApi public fun cValuesOf(vararg elements: CPointer?): CValues> = createValues(elements.size) { index -> this.value = elements[index] } +@ExperimentalForeignApi public fun ByteArray.toCValues() = cValuesOf(*this) +@ExperimentalForeignApi public fun ShortArray.toCValues() = cValuesOf(*this) +@ExperimentalForeignApi public fun IntArray.toCValues() = cValuesOf(*this) +@ExperimentalForeignApi public fun LongArray.toCValues() = cValuesOf(*this) +@ExperimentalForeignApi public fun FloatArray.toCValues() = cValuesOf(*this) +@ExperimentalForeignApi public fun DoubleArray.toCValues() = cValuesOf(*this) +@ExperimentalForeignApi public fun Array?>.toCValues() = cValuesOf(*this) +@ExperimentalForeignApi public fun List?>.toCValues() = this.toTypedArray().toCValues() -private class CString(val bytes: ByteArray): CValues() { +@ExperimentalForeignApi +private class CString(val bytes: ByteArray) : CValues() { override val size get() = bytes.size + 1 override val align get() = 1 @@ -390,7 +435,8 @@ private class CString(val bytes: ByteArray): CValues() { } } -private object EmptyCString: CValues() { +@ExperimentalForeignApi +private object EmptyCString : CValues() { override val size get() = 1 override val align get() = 1 @@ -402,6 +448,7 @@ private object EmptyCString: CValues() { override fun getPointer(scope: AutofreeScope): CPointer { return placement } + override fun place(placement: CPointer): CPointer { placement[0] = 0.toByte() return placement @@ -411,12 +458,14 @@ private object EmptyCString: CValues() { /** * @return the value of zero-terminated UTF-8-encoded C string constructed from given [kotlin.String]. */ +@ExperimentalForeignApi public val String.cstr: CValues get() = if (isEmpty()) EmptyCString else CString(encodeToUtf8(this)) /** * @return the value of zero-terminated UTF-8-encoded C string constructed from given [kotlin.String]. */ +@ExperimentalForeignApi public val String.utf8: CValues get() = CString(encodeToUtf8(this)) @@ -424,6 +473,7 @@ public val String.utf8: CValues * Convert this list of Kotlin strings to C array of C strings, * allocating memory for the array and C strings with given [AutofreeScope]. */ +@ExperimentalForeignApi public fun List.toCStringArray(autofreeScope: AutofreeScope): CPointer> = autofreeScope.allocArrayOf(this.map { it.cstr.getPointer(autofreeScope) }) @@ -431,10 +481,12 @@ public fun List.toCStringArray(autofreeScope: AutofreeScope): CPointer.toCStringArray(autofreeScope: AutofreeScope): CPointer> = autofreeScope.allocArrayOf(this.map { it.cstr.getPointer(autofreeScope) }) +@ExperimentalForeignApi private class U16CString(val chars: CharArray): CValues() { override val size get() = 2 * (chars.size + 1) @@ -456,16 +508,19 @@ private class U16CString(val chars: CharArray): CValues() { /** * @return the value of zero-terminated UTF-16-encoded C string constructed from given [kotlin.String]. */ +@ExperimentalForeignApi public val String.wcstr: CValues get() = U16CString(this.toCharArray()) /** * @return the value of zero-terminated UTF-16-encoded C string constructed from given [kotlin.String]. */ +@ExperimentalForeignApi public val String.utf16: CValues get() = U16CString(this.toCharArray()) -private class U32CString(val chars: CharArray): CValues() { +@ExperimentalForeignApi +private class U32CString(val chars: CharArray) : CValues() { override val size get() = 4 * (chars.size + 1) override val align get() = 4 @@ -499,6 +554,7 @@ private class U32CString(val chars: CharArray): CValues() { /** * @return the value of zero-terminated UTF-32-encoded C string constructed from given [kotlin.String]. */ +@ExperimentalForeignApi public val String.utf32: CValues get() = U32CString(this.toCharArray()) @@ -507,16 +563,19 @@ public val String.utf32: CValues /** * @return the [kotlin.String] decoded from given zero-terminated UTF-8-encoded C string. */ +@ExperimentalForeignApi public fun CPointer.toKStringFromUtf8(): String = this.toKStringFromUtf8Impl() /** * @return the [kotlin.String] decoded from given zero-terminated UTF-8-encoded C string. */ +@ExperimentalForeignApi public fun CPointer.toKString(): String = this.toKStringFromUtf8() /** * @return the [kotlin.String] decoded from given zero-terminated UTF-16-encoded C string. */ +@ExperimentalForeignApi public fun CPointer.toKStringFromUtf16(): String { val nativeBytes = this @@ -536,6 +595,7 @@ public fun CPointer.toKStringFromUtf16(): String { /** * @return the [kotlin.String] decoded from given zero-terminated UTF-32-encoded C string. */ +@ExperimentalForeignApi public fun CPointer.toKStringFromUtf32(): String { val nativeBytes = this @@ -565,6 +625,7 @@ public fun CPointer.toKStringFromUtf32(): String { return chars.concatToString() } + /** * Decodes a string from the bytes in UTF-8 encoding in this array. * Bytes following the first occurrence of `0` byte, if it occurs, are not decoded. @@ -572,6 +633,7 @@ public fun CPointer.toKStringFromUtf32(): String { * Malformed byte sequences are replaced by the replacement char `\uFFFD`. */ @SinceKotlin("1.3") +@ExperimentalForeignApi public fun ByteArray.toKString() : String { val realEndIndex = realEndIndex(this, 0, this.size) return decodeToString(0, realEndIndex) @@ -590,6 +652,7 @@ public fun ByteArray.toKString() : String { * @throws CharacterCodingException if the byte array contains malformed UTF-8 byte sequence and [throwOnInvalidSequence] is true. */ @SinceKotlin("1.3") +@ExperimentalForeignApi public fun ByteArray.toKString( startIndex: Int = 0, endIndex: Int = this.size, @@ -617,6 +680,7 @@ private fun checkBoundsIndexes(startIndex: Int, endIndex: Int, size: Int) { } } +@ExperimentalForeignApi public class MemScope : ArenaBase() { val memScope: MemScope @@ -632,6 +696,7 @@ public class MemScope : ArenaBase() { * Runs given [block] providing allocation of memory * which will be automatically disposed at the end of this scope. */ +@ExperimentalForeignApi public inline fun memScoped(block: MemScope.()->R): R { val memScope = MemScope() try { @@ -641,6 +706,7 @@ public inline fun memScoped(block: MemScope.()->R): R { } } +@ExperimentalForeignApi public fun COpaquePointer.readBytes(count: Int): ByteArray { val result = ByteArray(count) nativeMemUtils.getByteArray(this.reinterpret().pointed, result, count) diff --git a/kotlin-native/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/_UtilsGenerated.kt b/kotlin-native/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/_UtilsGenerated.kt index 555c294abd3..9f6c2da4b51 100644 --- a/kotlin-native/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/_UtilsGenerated.kt +++ b/kotlin-native/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/_UtilsGenerated.kt @@ -12,6 +12,7 @@ package kotlinx.cinterop * Allocates variable with given value type and initializes it with given value. */ @Suppress("FINAL_UPPER_BOUND") +@ExperimentalForeignApi public fun NativePlacement.alloc(value: T): BooleanVarOf = alloc> { this.value = value } @@ -19,6 +20,7 @@ public fun NativePlacement.alloc(value: T): BooleanVarOf = * Allocates variable with given value type and initializes it with given value. */ @Suppress("FINAL_UPPER_BOUND") +@ExperimentalForeignApi public fun NativePlacement.alloc(value: T): ByteVarOf = alloc> { this.value = value } @@ -26,6 +28,7 @@ public fun NativePlacement.alloc(value: T): ByteVarOf = * Allocates variable with given value type and initializes it with given value. */ @Suppress("FINAL_UPPER_BOUND") +@ExperimentalForeignApi public fun NativePlacement.alloc(value: T): ShortVarOf = alloc> { this.value = value } @@ -33,6 +36,7 @@ public fun NativePlacement.alloc(value: T): ShortVarOf = * Allocates variable with given value type and initializes it with given value. */ @Suppress("FINAL_UPPER_BOUND") +@ExperimentalForeignApi public fun NativePlacement.alloc(value: T): IntVarOf = alloc> { this.value = value } @@ -40,6 +44,7 @@ public fun NativePlacement.alloc(value: T): IntVarOf = * Allocates variable with given value type and initializes it with given value. */ @Suppress("FINAL_UPPER_BOUND") +@ExperimentalForeignApi public fun NativePlacement.alloc(value: T): LongVarOf = alloc> { this.value = value } @@ -47,6 +52,7 @@ public fun NativePlacement.alloc(value: T): LongVarOf = * Allocates variable with given value type and initializes it with given value. */ @Suppress("FINAL_UPPER_BOUND") +@ExperimentalForeignApi public fun NativePlacement.alloc(value: T): UByteVarOf = alloc> { this.value = value } @@ -54,6 +60,7 @@ public fun NativePlacement.alloc(value: T): UByteVarOf = * Allocates variable with given value type and initializes it with given value. */ @Suppress("FINAL_UPPER_BOUND") +@ExperimentalForeignApi public fun NativePlacement.alloc(value: T): UShortVarOf = alloc> { this.value = value } @@ -61,6 +68,7 @@ public fun NativePlacement.alloc(value: T): UShortVarOf = * Allocates variable with given value type and initializes it with given value. */ @Suppress("FINAL_UPPER_BOUND") +@ExperimentalForeignApi public fun NativePlacement.alloc(value: T): UIntVarOf = alloc> { this.value = value } @@ -68,6 +76,7 @@ public fun NativePlacement.alloc(value: T): UIntVarOf = * Allocates variable with given value type and initializes it with given value. */ @Suppress("FINAL_UPPER_BOUND") +@ExperimentalForeignApi public fun NativePlacement.alloc(value: T): ULongVarOf = alloc> { this.value = value } @@ -75,6 +84,7 @@ public fun NativePlacement.alloc(value: T): ULongVarOf = * Allocates variable with given value type and initializes it with given value. */ @Suppress("FINAL_UPPER_BOUND") +@ExperimentalForeignApi public fun NativePlacement.alloc(value: T): FloatVarOf = alloc> { this.value = value } @@ -82,6 +92,7 @@ public fun NativePlacement.alloc(value: T): FloatVarOf = * Allocates variable with given value type and initializes it with given value. */ @Suppress("FINAL_UPPER_BOUND") +@ExperimentalForeignApi public fun NativePlacement.alloc(value: T): DoubleVarOf = alloc> { this.value = value } diff --git a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ForeignException.kt b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ForeignException.kt index db94c707f08..160f3abb419 100644 --- a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ForeignException.kt +++ b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ForeignException.kt @@ -1,13 +1,15 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ + package kotlinx.cinterop import kotlin.native.internal.ExportForCppRuntime import kotlin.native.internal.GCUnsafeCall -public class ForeignException internal constructor(val nativeException: Any?): Exception() { +@BetaInteropApi +public class ForeignException internal constructor(val nativeException: Any?) : Exception() { override val message: String = nativeException?.let { kotlin_ObjCExport_ExceptionDetails(nativeException) }?: "" @@ -18,5 +20,7 @@ public class ForeignException internal constructor(val nativeException: Any?): E } @ExportForCppRuntime +@BetaInteropApi +@ExperimentalForeignApi internal fun CreateForeignException(payload: NativePtr): Throwable = ForeignException(interpretObjCPointerOrNull(payload)) diff --git a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/FunctionPointers.kt b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/FunctionPointers.kt index b59df85afcc..31465b7d49c 100644 --- a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/FunctionPointers.kt +++ b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/FunctionPointers.kt @@ -1,17 +1,6 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ package kotlinx.cinterop @@ -20,48 +9,71 @@ import kotlin.native.internal.TypedIntrinsic import kotlin.native.internal.IntrinsicType import kotlin.native.internal.ExportForCompiler +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_FUNPTR_INVOKE) external operator fun CPointer R>>.invoke(): R +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_FUNPTR_INVOKE) external operator fun CPointer R>>.invoke(p1: P1): R +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_FUNPTR_INVOKE) external operator fun CPointer R>>.invoke(p1: P1, p2: P2): R +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_FUNPTR_INVOKE) external operator fun CPointer R>>.invoke(p1: P1, p2: P2, p3: P3): R +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_FUNPTR_INVOKE) external operator fun CPointer R>>.invoke(p1: P1, p2: P2, p3: P3, p4: P4): R +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_FUNPTR_INVOKE) external operator fun CPointer R>>.invoke(p1: P1, p2: P2, p3: P3, p4: P4, p5: P5): R +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_FUNPTR_INVOKE) external operator fun CPointer R>>.invoke(p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6): R +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_FUNPTR_INVOKE) external operator fun CPointer R>>.invoke(p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7): R +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_FUNPTR_INVOKE) external operator fun CPointer R>>.invoke(p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8): R +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_FUNPTR_INVOKE) external operator fun CPointer R>>.invoke(p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8, p9: P9): R +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_FUNPTR_INVOKE) external operator fun CPointer R>>.invoke(p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8, p9: P9, p10: P10): R +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_FUNPTR_INVOKE) external operator fun CPointer R>>.invoke(p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8, p9: P9, p10: P10, p11: P11): R +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_FUNPTR_INVOKE) external operator fun CPointer R>>.invoke(p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8, p9: P9, p10: P10, p11: P11, p12: P12): R +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_FUNPTR_INVOKE) external operator fun CPointer R>>.invoke(p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8, p9: P9, p10: P10, p11: P11, p12: P12, p13: P13): R +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_FUNPTR_INVOKE) external operator fun CPointer R>>.invoke(p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8, p9: P9, p10: P10, p11: P11, p12: P12, p13: P13, p14: P14): R +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_FUNPTR_INVOKE) external operator fun CPointer R>>.invoke(p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8, p9: P9, p10: P10, p11: P11, p12: P12, p13: P13, p14: P14, p15: P15): R +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_FUNPTR_INVOKE) external operator fun CPointer R>>.invoke(p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8, p9: P9, p10: P10, p11: P11, p12: P12, p13: P13, p14: P14, p15: P15, p16: P16): R +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_FUNPTR_INVOKE) external operator fun CPointer R>>.invoke(p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8, p9: P9, p10: P10, p11: P11, p12: P12, p13: P13, p14: P14, p15: P15, p16: P16, p17: P17): R +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_FUNPTR_INVOKE) external operator fun CPointer R>>.invoke(p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8, p9: P9, p10: P10, p11: P11, p12: P12, p13: P13, p14: P14, p15: P15, p16: P16, p17: P17, p18: P18): R +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_FUNPTR_INVOKE) external operator fun CPointer R>>.invoke(p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8, p9: P9, p10: P10, p11: P11, p12: P12, p13: P13, p14: P14, p15: P15, p16: P16, p17: P17, p18: P18, p19: P19): R +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_FUNPTR_INVOKE) external operator fun CPointer R>>.invoke(p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8, p9: P9, p10: P10, p11: P11, p12: P12, p13: P13, p14: P14, p15: P15, p16: P16, p17: P17, p18: P18, p19: P19, p20: P20): R +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_FUNPTR_INVOKE) external operator fun CPointer R>>.invoke(p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8, p9: P9, p10: P10, p11: P11, p12: P12, p13: P13, p14: P14, p15: P15, p16: P16, p17: P17, p18: P18, p19: P19, p20: P20, p21: P21): R +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_FUNPTR_INVOKE) external operator fun CPointer R>>.invoke(p1: P1, p2: P2, p3: P3, p4: P4, p5: P5, p6: P6, p7: P7, p8: P8, p9: P9, p10: P10, p11: P11, p12: P12, p13: P13, p14: P14, p15: P15, p16: P16, p17: P17, p18: P18, p19: P19, p20: P20, p21: P21, p22: P22): R diff --git a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ManagedType.kt b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ManagedType.kt index 1d9630d0d2a..a6119e7063a 100644 --- a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ManagedType.kt +++ b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ManagedType.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -7,9 +7,14 @@ package kotlinx.cinterop import kotlinx.cinterop.CStructVar +@ExperimentalForeignApi interface SkiaRefCnt + +@ExperimentalForeignApi interface CPlusPlusClass +@ExperimentalForeignApi abstract class ManagedType(val cpp: T) +@ExperimentalForeignApi val ManagedType.ptr: CPointer get() = this.cpp.ptr diff --git a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeMem.kt b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeMem.kt index 6697a37844c..3a00eed1663 100644 --- a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeMem.kt +++ b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeMem.kt @@ -1,19 +1,8 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ - +@file:OptIn(ExperimentalForeignApi::class) package kotlinx.cinterop import kotlin.native.* @@ -139,6 +128,7 @@ internal object nativeMemUtils { } } +@ExperimentalForeignApi public fun CPointer.toKStringFromUtf16(): String { val nativeBytes = this @@ -155,8 +145,10 @@ public fun CPointer.toKStringFromUtf16(): String { return chars.concatToString() } +@ExperimentalForeignApi public fun CPointer.toKString(): String = this.toKStringFromUtf16() +@ExperimentalForeignApi public fun CPointer.toKString(): String = this.toKStringFromUtf16() @GCUnsafeCall("Kotlin_interop_malloc") @@ -165,7 +157,10 @@ private external fun malloc(size: Long, align: Int): NativePtr @GCUnsafeCall("Kotlin_interop_free") private external fun cfree(ptr: NativePtr) +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_READ_BITS) external fun readBits(ptr: NativePtr, offset: Long, size: Int, signed: Boolean): Long + +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_WRITE_BITS) external fun writeBits(ptr: NativePtr, offset: Long, size: Int, value: Long) diff --git a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeStableRef.kt b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeStableRef.kt index eded7d5a25c..9652444137d 100644 --- a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeStableRef.kt +++ b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeStableRef.kt @@ -1,20 +1,10 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ - +@file:OptIn(ExperimentalForeignApi::class) package kotlinx.cinterop + import kotlin.native.* import kotlin.native.internal.GCUnsafeCall diff --git a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeTypes.kt b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeTypes.kt index 8a99919937f..f4431b9d34c 100644 --- a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeTypes.kt +++ b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeTypes.kt @@ -1,19 +1,7 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ - package kotlinx.cinterop import kotlin.native.internal.reinterpret @@ -22,17 +10,21 @@ import kotlin.native.internal.VolatileLambda import kotlin.native.internal.TypedIntrinsic import kotlin.native.internal.IntrinsicType +@ExperimentalForeignApi typealias NativePtr = kotlin.native.internal.NativePtr internal typealias NonNullNativePtr = kotlin.native.internal.NonNullNativePtr @Suppress("NOTHING_TO_INLINE") +@ExperimentalForeignApi internal inline fun NativePtr.toNonNull() = this.reinterpret() +@ExperimentalForeignApi inline val nativeNullPtr: NativePtr get() = NativePtr.NULL @Deprecated("Use sizeOf() or alignOf() instead.") @Suppress("DEPRECATION") +@ExperimentalForeignApi fun typeOf(): CVariable.Type = throw Error("typeOf() is called with erased argument") /** @@ -40,23 +32,29 @@ fun typeOf(): CVariable.Type = throw Error("typeOf() is called w * * @param T must not be abstract */ +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.IDENTITY) external fun interpretNullablePointed(ptr: NativePtr): T? /** * Performs type cast of the [CPointer] from the given raw pointer. */ +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.IDENTITY) external fun interpretCPointer(rawValue: NativePtr): CPointer? +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.IDENTITY) external fun NativePointed.getRawPointer(): NativePtr +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.IDENTITY) external fun CPointer<*>.getRawValue(): NativePtr +@ExperimentalForeignApi internal fun CPointer<*>.cPointerToString() = "CPointer(raw=$rawValue)" +@ExperimentalForeignApi @Suppress("FINAL_UPPER_BOUND") public class Vector128VarOf(rawPtr: NativePtr) : CVariable(rawPtr) { @Deprecated("Use sizeOf() or alignOf() instead.") @@ -64,8 +62,10 @@ public class Vector128VarOf(rawPtr: NativePtr) : CVariable(rawPtr companion object : Type(size = 16, align = 16) } +@ExperimentalForeignApi public typealias Vector128Var = Vector128VarOf +@ExperimentalForeignApi @Suppress("FINAL_UPPER_BOUND", "UNCHECKED_CAST") public var Vector128VarOf.value: T get() = nativeMemUtils.getVector(this) as T @@ -77,48 +77,71 @@ public var Vector128VarOf.value: T * @param function must be *static*, i.e. an (unbound) reference to a Kotlin function or * a closure which doesn't capture any variable */ +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_STATIC_C_FUNCTION) external fun staticCFunction(@VolatileLambda function: () -> R): CPointer R>> +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_STATIC_C_FUNCTION) external fun staticCFunction(@VolatileLambda function: (P1) -> R): CPointer R>> +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_STATIC_C_FUNCTION) external fun staticCFunction(@VolatileLambda function: (P1, P2) -> R): CPointer R>> +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_STATIC_C_FUNCTION) external fun staticCFunction(@VolatileLambda function: (P1, P2, P3) -> R): CPointer R>> +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_STATIC_C_FUNCTION) external fun staticCFunction(@VolatileLambda function: (P1, P2, P3, P4) -> R): CPointer R>> +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_STATIC_C_FUNCTION) external fun staticCFunction(@VolatileLambda function: (P1, P2, P3, P4, P5) -> R): CPointer R>> +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_STATIC_C_FUNCTION) external fun staticCFunction(@VolatileLambda function: (P1, P2, P3, P4, P5, P6) -> R): CPointer R>> +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_STATIC_C_FUNCTION) external fun staticCFunction(@VolatileLambda function: (P1, P2, P3, P4, P5, P6, P7) -> R): CPointer R>> +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_STATIC_C_FUNCTION) external fun staticCFunction(@VolatileLambda function: (P1, P2, P3, P4, P5, P6, P7, P8) -> R): CPointer R>> +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_STATIC_C_FUNCTION) external fun staticCFunction(@VolatileLambda function: (P1, P2, P3, P4, P5, P6, P7, P8, P9) -> R): CPointer R>> +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_STATIC_C_FUNCTION) external fun staticCFunction(@VolatileLambda function: (P1, P2, P3, P4, P5, P6, P7, P8, P9, P10) -> R): CPointer R>> +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_STATIC_C_FUNCTION) external fun staticCFunction(@VolatileLambda function: (P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11) -> R): CPointer R>> +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_STATIC_C_FUNCTION) external fun staticCFunction(@VolatileLambda function: (P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12) -> R): CPointer R>> +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_STATIC_C_FUNCTION) external fun staticCFunction(@VolatileLambda function: (P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13) -> R): CPointer R>> +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_STATIC_C_FUNCTION) external fun staticCFunction(@VolatileLambda function: (P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14) -> R): CPointer R>> +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_STATIC_C_FUNCTION) external fun staticCFunction(@VolatileLambda function: (P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15) -> R): CPointer R>> +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_STATIC_C_FUNCTION) external fun staticCFunction(@VolatileLambda function: (P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16) -> R): CPointer R>> +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_STATIC_C_FUNCTION) external fun staticCFunction(@VolatileLambda function: (P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17) -> R): CPointer R>> +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_STATIC_C_FUNCTION) external fun staticCFunction(@VolatileLambda function: (P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18) -> R): CPointer R>> +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_STATIC_C_FUNCTION) external fun staticCFunction(@VolatileLambda function: (P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19) -> R): CPointer R>> +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_STATIC_C_FUNCTION) external fun staticCFunction(@VolatileLambda function: (P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19, P20) -> R): CPointer R>> +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_STATIC_C_FUNCTION) external fun staticCFunction(@VolatileLambda function: (P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19, P20, P21) -> R): CPointer R>> +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_STATIC_C_FUNCTION) external fun staticCFunction(@VolatileLambda function: (P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19, P20, P21, P22) -> R): CPointer R>> diff --git a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeUtils.kt b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeUtils.kt index 0eebf60f2ad..f5f5bd9d4d0 100644 --- a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeUtils.kt +++ b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeUtils.kt @@ -1,17 +1,6 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ package kotlinx.cinterop @@ -24,48 +13,67 @@ import kotlin.native.internal.IntrinsicType internal fun encodeToUtf8(str: String): ByteArray = str.encodeToByteArray() @GCUnsafeCall("Kotlin_CString_toKStringFromUtf8Impl") +@ExperimentalForeignApi internal external fun CPointer.toKStringFromUtf8Impl(): String +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_BITS_TO_FLOAT) external fun bitsToFloat(bits: Int): Float +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_BITS_TO_DOUBLE) external fun bitsToDouble(bits: Long): Double -// TODO: deprecate. +@Deprecated("Deprecated without replacement as part of the obsolete interop API", level = DeprecationLevel.WARNING) @TypedIntrinsic(IntrinsicType.INTEROP_SIGN_EXTEND) external inline fun Number.signExtend(): R -// TODO: deprecate. +@Deprecated("Deprecated without replacement as part of the obsolete interop API", level = DeprecationLevel.WARNING) @TypedIntrinsic(IntrinsicType.INTEROP_NARROW) external inline fun Number.narrow(): R +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_CONVERT) external inline fun Byte.convert(): R +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_CONVERT) external inline fun Short.convert(): R +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_CONVERT) external inline fun Int.convert(): R +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_CONVERT) external inline fun Long.convert(): R +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_CONVERT) external inline fun UByte.convert(): R +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_CONVERT) external inline fun UShort.convert(): R +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_CONVERT) external inline fun UInt.convert(): R +@ExperimentalForeignApi @TypedIntrinsic(IntrinsicType.INTEROP_CONVERT) external inline fun ULong.convert(): R @Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER, AnnotationTarget.FILE) @Retention(AnnotationRetention.SOURCE) internal annotation class JvmName(val name: String) +@ExperimentalForeignApi fun cValuesOf(vararg elements: UByte): CValues = createValues(elements.size) { index -> this.value = elements[index] } +@ExperimentalForeignApi fun cValuesOf(vararg elements: UShort): CValues = createValues(elements.size) { index -> this.value = elements[index] } +@ExperimentalForeignApi fun cValuesOf(vararg elements: UInt): CValues = createValues(elements.size) { index -> this.value = elements[index] } +@ExperimentalForeignApi fun cValuesOf(vararg elements: ULong): CValues = createValues(elements.size) { index -> this.value = elements[index] } +@ExperimentalForeignApi fun UByteArray.toCValues() = cValuesOf(*this) +@ExperimentalForeignApi fun UShortArray.toCValues() = cValuesOf(*this) +@ExperimentalForeignApi fun UIntArray.toCValues() = cValuesOf(*this) -fun ULongArray.toCValues() = cValuesOf(*this) \ No newline at end of file +@ExperimentalForeignApi +fun ULongArray.toCValues() = cValuesOf(*this) diff --git a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCImpl.kt b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCImpl.kt index f32cf4bf175..fee555316ed 100644 --- a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCImpl.kt +++ b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCImpl.kt @@ -1,30 +1,27 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @file:Suppress("NOTHING_TO_INLINE") +@file:OptIn(ExperimentalForeignApi::class, BetaInteropApi::class) package kotlinx.cinterop + import kotlin.native.* import kotlin.native.internal.* +import kotlin.native.internal.InternalForKotlinNative +@BetaInteropApi interface ObjCObject +@BetaInteropApi interface ObjCClass : ObjCObject +@BetaInteropApi interface ObjCClassOf : ObjCClass // TODO: T should be added to ObjCClass and all meta-classes instead. +@BetaInteropApi typealias ObjCObjectMeta = ObjCClass +@BetaInteropApi interface ObjCProtocol : ObjCObject @ExportTypeInfo("theForeignObjCObjectTypeInfo") @@ -32,13 +29,17 @@ interface ObjCProtocol : ObjCObject @kotlin.native.internal.Frozen internal open class ForeignObjCObject : kotlin.native.internal.ObjCObjectWrapper +@BetaInteropApi abstract class ObjCObjectBase protected constructor() : ObjCObject { @Target(AnnotationTarget.CONSTRUCTOR) @Retention(AnnotationRetention.SOURCE) annotation class OverrideInit } + +@BetaInteropApi abstract class ObjCObjectBaseMeta protected constructor() : ObjCObjectBase(), ObjCObjectMeta {} +@BetaInteropApi fun optional(): Nothing = throw RuntimeException("Do not call me!!!") @Deprecated( @@ -48,6 +49,7 @@ fun optional(): Nothing = throw RuntimeException("Do not call me!!!") @TypedIntrinsic(IntrinsicType.OBJC_INIT_BY) external fun T.initBy(constructorCall: T): T +@BetaInteropApi @kotlin.native.internal.ExportForCompiler private fun ObjCObjectBase.superInitCheck(superInitCallResult: ObjCObject?) { if (superInitCallResult == null) @@ -60,20 +62,25 @@ private fun ObjCObjectBase.superInitCheck(superInitCallResult: ObjCObject?) { internal fun Any?.uncheckedCast(): T = @Suppress("UNCHECKED_CAST") (this as T) // Note: if this is called for non-frozen object on a wrong worker, the program will terminate. +@ExperimentalForeignApi @GCUnsafeCall("Kotlin_Interop_refFromObjC") external fun interpretObjCPointerOrNull(objcPtr: NativePtr): T? @ExportForCppRuntime +@ExperimentalForeignApi inline fun interpretObjCPointer(objcPtr: NativePtr): T = interpretObjCPointerOrNull(objcPtr)!! @GCUnsafeCall("Kotlin_Interop_refToObjC") -external fun Any?.objcPtr(): NativePtr +@ExperimentalForeignApi +public external fun Any?.objcPtr(): NativePtr @GCUnsafeCall("Kotlin_Interop_createKotlinObjectHolder") -external fun createKotlinObjectHolder(any: Any?): NativePtr +@ExperimentalForeignApi +public external fun createKotlinObjectHolder(any: Any?): NativePtr // Note: if this is called for non-frozen underlying ref on a wrong worker, the program will terminate. -inline fun unwrapKotlinObjectHolder(holder: Any?): T { +@BetaInteropApi +public inline fun unwrapKotlinObjectHolder(holder: Any?): T { return unwrapKotlinObjectHolderImpl(holder!!.objcPtr()) as T } @@ -81,23 +88,28 @@ inline fun unwrapKotlinObjectHolder(holder: Any?): T { @GCUnsafeCall("Kotlin_Interop_unwrapKotlinObjectHolder") external internal fun unwrapKotlinObjectHolderImpl(ptr: NativePtr): Any +@ExperimentalForeignApi class ObjCObjectVar(rawPtr: NativePtr) : CVariable(rawPtr) { @Deprecated("Use sizeOf() or alignOf() instead.") @Suppress("DEPRECATION") companion object : CVariable.Type(pointerSize.toLong(), pointerSize) } +@ExperimentalForeignApi class ObjCNotImplementedVar(rawPtr: NativePtr) : CVariable(rawPtr) { @Deprecated("Use sizeOf() or alignOf() instead.") @Suppress("DEPRECATION") companion object : CVariable.Type(pointerSize.toLong(), pointerSize) } +@ExperimentalForeignApi var ObjCNotImplementedVar.value: T get() = TODO() set(_) = TODO() +@ExperimentalForeignApi typealias ObjCStringVarOf = ObjCNotImplementedVar +@ExperimentalForeignApi typealias ObjCBlockVar = ObjCNotImplementedVar @TypedIntrinsic(IntrinsicType.OBJC_CREATE_SUPER_STRUCT) @@ -106,27 +118,33 @@ internal external fun createObjCSuperStruct(receiver: NativePtr, superClass: Nat @Target(AnnotationTarget.CLASS) @Retention(AnnotationRetention.BINARY) -annotation class ExternalObjCClass(val protocolGetter: String = "", val binaryName: String = "") +@InternalForKotlinNative +public annotation class ExternalObjCClass(val protocolGetter: String = "", val binaryName: String = "") @Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER) @Retention(AnnotationRetention.BINARY) -annotation class ObjCMethod(val selector: String, val encoding: String, val isStret: Boolean = false) +@InternalForKotlinNative +public annotation class ObjCMethod(val selector: String, val encoding: String, val isStret: Boolean = false) @Target(AnnotationTarget.FUNCTION) @Retention(AnnotationRetention.BINARY) -annotation class ObjCDirect(val symbol: String) +@InternalForKotlinNative +public annotation class ObjCDirect(val symbol: String) @Target(AnnotationTarget.CONSTRUCTOR) @Retention(AnnotationRetention.BINARY) -annotation class ObjCConstructor(val initSelector: String, val designated: Boolean) +@InternalForKotlinNative +public annotation class ObjCConstructor(val initSelector: String, val designated: Boolean) @Target(AnnotationTarget.FUNCTION) @Retention(AnnotationRetention.BINARY) -annotation class ObjCFactory(val selector: String, val encoding: String, val isStret: Boolean = false) +@InternalForKotlinNative +public annotation class ObjCFactory(val selector: String, val encoding: String, val isStret: Boolean = false) @Target(AnnotationTarget.FILE) @Retention(AnnotationRetention.BINARY) -annotation class InteropStubs() +@InternalForKotlinNative +public annotation class InteropStubs() @PublishedApi @Target(AnnotationTarget.FUNCTION) @@ -193,20 +211,25 @@ internal external fun createObjCObjectHolder(ptr: NativePtr): Any? // Objective-C runtime: @GCUnsafeCall("objc_retainAutoreleaseReturnValue") -external fun objc_retainAutoreleaseReturnValue(ptr: NativePtr): NativePtr +@ExperimentalForeignApi +public external fun objc_retainAutoreleaseReturnValue(ptr: NativePtr): NativePtr @GCUnsafeCall("Kotlin_objc_autoreleasePoolPush") -external fun objc_autoreleasePoolPush(): NativePtr +@ExperimentalForeignApi +public external fun objc_autoreleasePoolPush(): NativePtr @GCUnsafeCall("Kotlin_objc_autoreleasePoolPop") -external fun objc_autoreleasePoolPop(ptr: NativePtr) +@ExperimentalForeignApi +public external fun objc_autoreleasePoolPop(ptr: NativePtr) @GCUnsafeCall("Kotlin_objc_allocWithZone") @FilterExceptions private external fun objc_allocWithZone(clazz: NativePtr): NativePtr @GCUnsafeCall("Kotlin_objc_retain") -external fun objc_retain(ptr: NativePtr): NativePtr +@ExperimentalForeignApi +public external fun objc_retain(ptr: NativePtr): NativePtr @GCUnsafeCall("Kotlin_objc_release") -external fun objc_release(ptr: NativePtr) +@ExperimentalForeignApi +public external fun objc_release(ptr: NativePtr) diff --git a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCKClassSupport.kt b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCKClassSupport.kt index 880f357680d..8e4eb1e2379 100644 --- a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCKClassSupport.kt +++ b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCKClassSupport.kt @@ -1,8 +1,8 @@ /* - * Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license - * that can be found in the LICENSE file. + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ - +@file:OptIn(ExperimentalForeignApi::class) package kotlinx.cinterop import kotlin.native.internal.KClassImpl @@ -15,7 +15,8 @@ import kotlin.reflect.KClass * * Otherwise returns `null`. */ -fun getOriginalKotlinClass(objCClass: ObjCClass): KClass<*>? { +@BetaInteropApi +public fun getOriginalKotlinClass(objCClass: ObjCClass): KClass<*>? { val typeInfo = getTypeInfoForClass(objCClass.objcPtr()) if (typeInfo.isNull()) return null @@ -28,7 +29,8 @@ fun getOriginalKotlinClass(objCClass: ObjCClass): KClass<*>? { * * Otherwise returns `null`. */ -fun getOriginalKotlinClass(objCProtocol: ObjCProtocol): KClass<*>? { +@BetaInteropApi +public fun getOriginalKotlinClass(objCProtocol: ObjCProtocol): KClass<*>? { val typeInfo = getTypeInfoForProtocol(objCProtocol.objcPtr()) if (typeInfo.isNull()) return null @@ -39,4 +41,5 @@ fun getOriginalKotlinClass(objCProtocol: ObjCProtocol): KClass<*>? { private external fun getTypeInfoForClass(ptr: NativePtr): NativePtr @GCUnsafeCall("Kotlin_ObjCInterop_getTypeInfoForProtocol") -private external fun getTypeInfoForProtocol(ptr: NativePtr): NativePtr \ No newline at end of file +private external fun getTypeInfoForProtocol(ptr: NativePtr): NativePtr + diff --git a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCUtils.kt b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCUtils.kt index b3b76ea1672..0e39256f50b 100644 --- a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCUtils.kt +++ b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCUtils.kt @@ -1,22 +1,13 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ package kotlinx.cinterop -inline fun autoreleasepool(block: () -> R): R { +@BetaInteropApi +@OptIn(ExperimentalForeignApi::class) +public inline fun autoreleasepool(block: () -> R): R { val pool = objc_autoreleasePoolPush() return try { block() @@ -25,11 +16,10 @@ inline fun autoreleasepool(block: () -> R): R { } } -@Deprecated("Use plain Kotlin cast", ReplaceWith("this as T"), DeprecationLevel.ERROR) -fun ObjCObject.reinterpret() = @Suppress("DEPRECATION") this.uncheckedCast() - // TODO: null checks -var ObjCObjectVar.value: T +@BetaInteropApi +@ExperimentalForeignApi +public var ObjCObjectVar.value: T @Suppress("DEPRECATION") get() = interpretObjCPointerOrNull(nativeMemUtils.getNativePtr(this)).uncheckedCast() @@ -41,7 +31,8 @@ var ObjCObjectVar.value: T */ @Target(AnnotationTarget.FUNCTION) @Retention(AnnotationRetention.SOURCE) -annotation class ObjCAction +@BetaInteropApi +public annotation class ObjCAction /** * Makes Kotlin property in Objective-C class settable through Objective-C dispatch @@ -49,7 +40,8 @@ annotation class ObjCAction */ @Target(AnnotationTarget.PROPERTY) @Retention(AnnotationRetention.SOURCE) -annotation class ObjCOutlet +@BetaInteropApi +public annotation class ObjCOutlet /** * Makes Kotlin subclass of Objective-C class visible for runtime lookup @@ -60,4 +52,5 @@ annotation class ObjCOutlet */ @Target(AnnotationTarget.CLASS) @Retention(AnnotationRetention.SOURCE) -annotation class ExportObjCClass(val name: String = "") +@BetaInteropApi +public annotation class ExportObjCClass(val name: String = "") diff --git a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/Pinning.kt b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/Pinning.kt index d8ce8a1ad8e..e1ed911eba3 100644 --- a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/Pinning.kt +++ b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/Pinning.kt @@ -1,23 +1,14 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ - +@file:OptIn(ExperimentalForeignApi::class) package kotlinx.cinterop + import kotlin.native.* import kotlin.native.internal.GCUnsafeCall +@ExperimentalForeignApi data class Pinned internal constructor(private val stablePtr: COpaquePointer) { /** @@ -34,8 +25,10 @@ data class Pinned internal constructor(private val stablePtr: COpaq } +@ExperimentalForeignApi fun T.pin() = Pinned(createStablePointer(this)) +@ExperimentalForeignApi inline fun T.usePinned(block: (Pinned) -> R): R { val pinned = this.pin() return try { @@ -45,43 +38,68 @@ inline fun T.usePinned(block: (Pinned) -> R): R { } } +@ExperimentalForeignApi fun Pinned.addressOf(index: Int): CPointer = this.get().addressOfElement(index) +@ExperimentalForeignApi fun ByteArray.refTo(index: Int): CValuesRef = this.usingPinned { addressOf(index) } +@ExperimentalForeignApi fun Pinned.addressOf(index: Int): CPointer = this.get().addressOfElement(index) +@ExperimentalForeignApi fun String.refTo(index: Int): CValuesRef = this.usingPinned { addressOf(index) } +@ExperimentalForeignApi fun Pinned.addressOf(index: Int): CPointer = this.get().addressOfElement(index) +@ExperimentalForeignApi fun CharArray.refTo(index: Int): CValuesRef = this.usingPinned { addressOf(index) } +@ExperimentalForeignApi fun Pinned.addressOf(index: Int): CPointer = this.get().addressOfElement(index) +@ExperimentalForeignApi fun ShortArray.refTo(index: Int): CValuesRef = this.usingPinned { addressOf(index) } +@ExperimentalForeignApi fun Pinned.addressOf(index: Int): CPointer = this.get().addressOfElement(index) +@ExperimentalForeignApi fun IntArray.refTo(index: Int): CValuesRef = this.usingPinned { addressOf(index) } +@ExperimentalForeignApi fun Pinned.addressOf(index: Int): CPointer = this.get().addressOfElement(index) +@ExperimentalForeignApi fun LongArray.refTo(index: Int): CValuesRef = this.usingPinned { addressOf(index) } // TODO: pinning of unsigned arrays involves boxing as they are inline classes wrapping signed arrays. +@ExperimentalForeignApi fun Pinned.addressOf(index: Int): CPointer = this.get().addressOfElement(index) +@ExperimentalForeignApi fun UByteArray.refTo(index: Int): CValuesRef = this.usingPinned { addressOf(index) } +@ExperimentalForeignApi fun Pinned.addressOf(index: Int): CPointer = this.get().addressOfElement(index) +@ExperimentalForeignApi fun UShortArray.refTo(index: Int): CValuesRef = this.usingPinned { addressOf(index) } +@ExperimentalForeignApi fun Pinned.addressOf(index: Int): CPointer = this.get().addressOfElement(index) +@ExperimentalForeignApi fun UIntArray.refTo(index: Int): CValuesRef = this.usingPinned { addressOf(index) } +@ExperimentalForeignApi fun Pinned.addressOf(index: Int): CPointer = this.get().addressOfElement(index) +@ExperimentalForeignApi fun ULongArray.refTo(index: Int): CValuesRef = this.usingPinned { addressOf(index) } +@ExperimentalForeignApi fun Pinned.addressOf(index: Int): CPointer = this.get().addressOfElement(index) +@ExperimentalForeignApi fun FloatArray.refTo(index: Int): CValuesRef = this.usingPinned { addressOf(index) } +@ExperimentalForeignApi fun Pinned.addressOf(index: Int): CPointer = this.get().addressOfElement(index) +@ExperimentalForeignApi fun DoubleArray.refTo(index: Int): CValuesRef = this.usingPinned { addressOf(index) } +@ExperimentalForeignApi private inline fun T.usingPinned( crossinline block: Pinned.() -> CPointer

) = object : CValuesRef

() { diff --git a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/internal/Annotations.kt b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/internal/Annotations.kt index b81351e2099..840cb4c704b 100644 --- a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/internal/Annotations.kt +++ b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/internal/Annotations.kt @@ -1,5 +1,9 @@ + package kotlinx.cinterop.internal +import kotlin.native.internal.InternalForKotlinNative + +@InternalForKotlinNative @Target(AnnotationTarget.CLASS) @Retention(AnnotationRetention.BINARY) annotation class CStruct(val spelling: String) { @@ -38,6 +42,7 @@ annotation class CStruct(val spelling: String) { AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER ) +@InternalForKotlinNative @Retention(AnnotationRetention.BINARY) public annotation class CCall(val id: String) { @Target(AnnotationTarget.VALUE_PARAMETER) @@ -77,6 +82,7 @@ public annotation class CCall(val id: String) { * Collection of annotations that allow to store * constant values. */ +@InternalForKotlinNative public object ConstantValue { @Retention(AnnotationRetention.BINARY) annotation class Byte(val value: kotlin.Byte) @@ -106,6 +112,7 @@ public object ConstantValue { * Denotes property that is an alias to some enum entry. */ @Target(AnnotationTarget.CLASS) +@InternalForKotlinNative @Retention(AnnotationRetention.BINARY) public annotation class CEnumEntryAlias(val entryName: String) @@ -113,5 +120,6 @@ public annotation class CEnumEntryAlias(val entryName: String) * Stores instance size of the type T: CEnumVar. */ @Target(AnnotationTarget.CLASS) +@InternalForKotlinNative @Retention(AnnotationRetention.BINARY) public annotation class CEnumVarTypeSize(val size: Int) diff --git a/kotlin-native/Interop/Skia/build.gradle b/kotlin-native/Interop/Skia/build.gradle index b1d2b5f30b7..8509adafbeb 100644 --- a/kotlin-native/Interop/Skia/build.gradle +++ b/kotlin-native/Interop/Skia/build.gradle @@ -1,17 +1,6 @@ /* - * Copyright 2010-2021 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. */ buildscript { @@ -24,3 +13,10 @@ dependencies { implementation project(":kotlin-native:Interop:Indexer") implementation project(":kotlin-native:Interop:StubGenerator") } + +compileKotlin { + kotlinOptions.freeCompilerArgs = [ + "-opt-in=kotlinx.cinterop.BetaInteropApi", + "-opt-in=kotlinx.cinterop.ExperimentalForeignApi", + ] +} diff --git a/kotlin-native/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIrTextEmitter.kt b/kotlin-native/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIrTextEmitter.kt index 1871ea0887e..bcb79e733b0 100644 --- a/kotlin-native/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIrTextEmitter.kt +++ b/kotlin-native/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIrTextEmitter.kt @@ -4,6 +4,7 @@ */ package org.jetbrains.kotlin.native.interop.gen +import kotlinx.cinterop.ExperimentalForeignApi import org.jetbrains.kotlin.native.interop.gen.jvm.KotlinPlatform import org.jetbrains.kotlin.native.interop.indexer.* import org.jetbrains.kotlin.utils.addIfNotNull @@ -114,6 +115,7 @@ class StubIrTextEmitter( } out("@file:Suppress(${suppress.joinToString { it.quoteAsKotlinLiteral() }})") + out("@file:OptIn(ExperimentalForeignApi::class)") if (pkgName != "") { out("package ${context.validPackageName}") out("") diff --git a/kotlin-native/backend.native/build.gradle b/kotlin-native/backend.native/build.gradle index c99b0eecbe5..d8c1a94d528 100644 --- a/kotlin-native/backend.native/build.gradle +++ b/kotlin-native/backend.native/build.gradle @@ -1,11 +1,14 @@ +/* + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ + import org.jetbrains.kotlin.cpp.CppConsumerPlugin import org.jetbrains.kotlin.cpp.CppUsage import org.jetbrains.kotlin.cpp.DependencyHandlerExKt import org.jetbrains.kotlin.tools.NativePluginKt -/* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license - * that can be found in the LICENSE file. - */ + + buildscript { apply from: "../../kotlin-native/gradle/kotlinGradlePlugin.gradle" apply plugin: 'project-report' @@ -31,7 +34,12 @@ sourceSets { compileCompilerKotlin { kotlinOptions.jvmTarget = "1.8" - kotlinOptions.freeCompilerArgs += ['-opt-in=kotlin.RequiresOptIn', '-opt-in=org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI', '-Xskip-prerelease-check'] + kotlinOptions.freeCompilerArgs += [ + '-opt-in=kotlin.RequiresOptIn', + '-opt-in=org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI', + "-opt-in=kotlinx.cinterop.BetaInteropApi", + "-opt-in=kotlinx.cinterop.ExperimentalForeignApi", + '-Xskip-prerelease-check'] } compileCli_bcKotlin { diff --git a/kotlin-native/backend.native/tests/build.gradle b/kotlin-native/backend.native/tests/build.gradle index 2487b6b9511..7d7f506bcdd 100644 --- a/kotlin-native/backend.native/tests/build.gradle +++ b/kotlin-native/backend.native/tests/build.gradle @@ -116,6 +116,7 @@ if (!isExperimentalMM) { tasks.withType(KonanCompileNativeBinary).configureEach { extraOpts "-XXLanguage:+ImplicitSignedToUnsignedIntegerConversion" + extraOpts "-opt-in=kotlinx.cinterop.ExperimentalForeignApi" } allprojects { diff --git a/kotlin-native/backend.native/tests/codegen/bridges/nativePointed.kt b/kotlin-native/backend.native/tests/codegen/bridges/nativePointed.kt index 00151feb2a4..c2a815119d6 100644 --- a/kotlin-native/backend.native/tests/codegen/bridges/nativePointed.kt +++ b/kotlin-native/backend.native/tests/codegen/bridges/nativePointed.kt @@ -1,8 +1,8 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ - +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) package codegen.bridges.nativePointed import kotlinx.cinterop.* diff --git a/kotlin-native/backend.native/tests/codegen/funInterface/kt43887.kt b/kotlin-native/backend.native/tests/codegen/funInterface/kt43887.kt index e296bee93e5..3565bc145f2 100644 --- a/kotlin-native/backend.native/tests/codegen/funInterface/kt43887.kt +++ b/kotlin-native/backend.native/tests/codegen/funInterface/kt43887.kt @@ -1,8 +1,8 @@ /* - * Copyright 2010-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ - +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) package codegen.funInterface.kt43887 import kotlin.test.* diff --git a/kotlin-native/backend.native/tests/codegen/intrinsics/interop_convert.kt b/kotlin-native/backend.native/tests/codegen/intrinsics/interop_convert.kt index 722428b53b6..e26605c138a 100644 --- a/kotlin-native/backend.native/tests/codegen/intrinsics/interop_convert.kt +++ b/kotlin-native/backend.native/tests/codegen/intrinsics/interop_convert.kt @@ -1,5 +1,6 @@ -package codegen.intrinsics.interop_convert +@file:OptIn(ExperimentalForeignApi::class) +package codegen.intrinsics.interop_convert import kotlin.test.* import kotlinx.cinterop.* diff --git a/kotlin-native/backend.native/tests/codegen/intrinsics/interop_sourceCodeStruct.kt b/kotlin-native/backend.native/tests/codegen/intrinsics/interop_sourceCodeStruct.kt index be891194f47..9ce1bd29db1 100644 --- a/kotlin-native/backend.native/tests/codegen/intrinsics/interop_sourceCodeStruct.kt +++ b/kotlin-native/backend.native/tests/codegen/intrinsics/interop_sourceCodeStruct.kt @@ -1,3 +1,6 @@ +@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class, kotlin.native.internal.InternalForKotlinNative::class) + package codegen.intrinsics.interop_sourceCodeStruct import kotlinx.cinterop.* @@ -5,8 +8,8 @@ import kotlinx.cinterop.internal.* import kotlin.test.* // Just making sure this doesn't get accidentally forbidden or otherwise broken. -// (however defining structs this way is still strongly discouraged, please define -// structs in C headers or .def files instead). +// Used by auto-generated code, user-defined structs should be declared via +// structs in C headers or .def files instead. @CStruct("struct { int p0; int p1; }") class S(rawPtr: NativePtr) : CStructVar(rawPtr) { diff --git a/kotlin-native/backend.native/tests/filecheck/signext_zeroext_interop.kt b/kotlin-native/backend.native/tests/filecheck/signext_zeroext_interop.kt index 16c6f0ce184..fc64e1548a9 100644 --- a/kotlin-native/backend.native/tests/filecheck/signext_zeroext_interop.kt +++ b/kotlin-native/backend.native/tests/filecheck/signext_zeroext_interop.kt @@ -1,7 +1,10 @@ /* - * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use + * of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) + import signext_zeroext_interop_input.* import kotlinx.cinterop.* diff --git a/kotlin-native/backend.native/tests/interop/auxiliary_sources/main.kt b/kotlin-native/backend.native/tests/interop/auxiliary_sources/main.kt index 69873293e18..2bba4e61263 100644 --- a/kotlin-native/backend.native/tests/interop/auxiliary_sources/main.kt +++ b/kotlin-native/backend.native/tests/interop/auxiliary_sources/main.kt @@ -1,7 +1,9 @@ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) + import auxiliaryCppSources.* import kotlin.test.* import kotlinx.cinterop.* fun main() { assertEquals(name()!!.toKString(), "Hello from C++") -} \ No newline at end of file +} diff --git a/kotlin-native/backend.native/tests/interop/basics/0.kt b/kotlin-native/backend.native/tests/interop/basics/0.kt index ffcda1a9244..2b6f4397d55 100644 --- a/kotlin-native/backend.native/tests/interop/basics/0.kt +++ b/kotlin-native/backend.native/tests/interop/basics/0.kt @@ -1,7 +1,8 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) import sysstat.* import kotlinx.cinterop.* @@ -11,4 +12,4 @@ fun main(args: Array) { val res = stat("/", statBuf.ptr) println(res) println(statBuf.st_uid) -} \ No newline at end of file +} diff --git a/kotlin-native/backend.native/tests/interop/basics/1.kt b/kotlin-native/backend.native/tests/interop/basics/1.kt index 5abf6cdce93..1738fc1b39e 100644 --- a/kotlin-native/backend.native/tests/interop/basics/1.kt +++ b/kotlin-native/backend.native/tests/interop/basics/1.kt @@ -1,7 +1,8 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) import cstdlib.* import kotlinx.cinterop.* @@ -14,4 +15,4 @@ fun main(args: Array) { println(quot) println(rem) -} \ No newline at end of file +} diff --git a/kotlin-native/backend.native/tests/interop/basics/3.kt b/kotlin-native/backend.native/tests/interop/basics/3.kt index 7f3cb8d81f4..3d38cf2db9d 100644 --- a/kotlin-native/backend.native/tests/interop/basics/3.kt +++ b/kotlin-native/backend.native/tests/interop/basics/3.kt @@ -1,7 +1,8 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) import kotlinx.cinterop.* diff --git a/kotlin-native/backend.native/tests/interop/basics/4.kt b/kotlin-native/backend.native/tests/interop/basics/4.kt index f76c55105f1..135465f7199 100644 --- a/kotlin-native/backend.native/tests/interop/basics/4.kt +++ b/kotlin-native/backend.native/tests/interop/basics/4.kt @@ -1,7 +1,8 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) import cstdio.* import kotlinx.cinterop.* @@ -19,4 +20,4 @@ fun main(args: Array) { val sscanfResult = sscanf("42", "%d%d", aVar.ptr, bVar.ptr) printf("%d %d\n", sscanfResult, aVar.value) } -} \ No newline at end of file +} diff --git a/kotlin-native/backend.native/tests/interop/basics/5.kt b/kotlin-native/backend.native/tests/interop/basics/5.kt index a4b15a66c44..193dbbb6913 100644 --- a/kotlin-native/backend.native/tests/interop/basics/5.kt +++ b/kotlin-native/backend.native/tests/interop/basics/5.kt @@ -1,7 +1,8 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) import platform.posix.printf diff --git a/kotlin-native/backend.native/tests/interop/basics/arrayPointers.kt b/kotlin-native/backend.native/tests/interop/basics/arrayPointers.kt index 06ea7067b47..f33cdb3613d 100644 --- a/kotlin-native/backend.native/tests/interop/basics/arrayPointers.kt +++ b/kotlin-native/backend.native/tests/interop/basics/arrayPointers.kt @@ -1,3 +1,5 @@ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) + import carrayPointers.* import kotlin.test.* import kotlinx.cinterop.* @@ -13,4 +15,4 @@ fun main() { struct.arrayPointer = globalArray assertEquals(globalArray[0], struct.arrayPointer!![0]) } -} \ No newline at end of file +} diff --git a/kotlin-native/backend.native/tests/interop/basics/available_processors.kt b/kotlin-native/backend.native/tests/interop/basics/available_processors.kt index 8edab2948b1..d1a48d348c4 100644 --- a/kotlin-native/backend.native/tests/interop/basics/available_processors.kt +++ b/kotlin-native/backend.native/tests/interop/basics/available_processors.kt @@ -1,7 +1,8 @@ /* - * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) import kotlin.native.* import kotlin.test.* diff --git a/kotlin-native/backend.native/tests/interop/basics/bf.kt b/kotlin-native/backend.native/tests/interop/basics/bf.kt index 3729fd97349..ef6739c61e3 100644 --- a/kotlin-native/backend.native/tests/interop/basics/bf.kt +++ b/kotlin-native/backend.native/tests/interop/basics/bf.kt @@ -1,7 +1,8 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) import bitfields.* import kotlinx.cinterop.* diff --git a/kotlin-native/backend.native/tests/interop/basics/callbacksAndVarargs.kt b/kotlin-native/backend.native/tests/interop/basics/callbacksAndVarargs.kt index 5ac6aa863aa..30276377e53 100644 --- a/kotlin-native/backend.native/tests/interop/basics/callbacksAndVarargs.kt +++ b/kotlin-native/backend.native/tests/interop/basics/callbacksAndVarargs.kt @@ -1,7 +1,8 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) import kotlin.test.* import kotlinx.cinterop.* diff --git a/kotlin-native/backend.native/tests/interop/basics/echo_server.kt b/kotlin-native/backend.native/tests/interop/basics/echo_server.kt index 347711e580d..fd73b1f125b 100644 --- a/kotlin-native/backend.native/tests/interop/basics/echo_server.kt +++ b/kotlin-native/backend.native/tests/interop/basics/echo_server.kt @@ -1,7 +1,8 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) import kotlinx.cinterop.* import sockets.* diff --git a/kotlin-native/backend.native/tests/interop/basics/enums.kt b/kotlin-native/backend.native/tests/interop/basics/enums.kt index e9569757171..b147bdab53e 100644 --- a/kotlin-native/backend.native/tests/interop/basics/enums.kt +++ b/kotlin-native/backend.native/tests/interop/basics/enums.kt @@ -1,7 +1,8 @@ /* - * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) import cenums.* import kotlinx.cinterop.* @@ -27,4 +28,4 @@ fun main() { // assertEquals(entries[0], E.A) // assertEquals(entries[1], E.B) // assertEquals(entries[2], E.C) -} \ No newline at end of file +} diff --git a/kotlin-native/backend.native/tests/interop/basics/funptr.kt b/kotlin-native/backend.native/tests/interop/basics/funptr.kt index 6e1e677ba1c..b5e81736818 100644 --- a/kotlin-native/backend.native/tests/interop/basics/funptr.kt +++ b/kotlin-native/backend.native/tests/interop/basics/funptr.kt @@ -1,7 +1,8 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) import kotlinx.cinterop.* import cfunptr.* @@ -43,4 +44,4 @@ fun main(args: Array) { printIntPtr(notSoLongSignatureFunction(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) } -fun Boolean.ifThenOneElseZero() = if (this) 1 else 0 \ No newline at end of file +fun Boolean.ifThenOneElseZero() = if (this) 1 else 0 diff --git a/kotlin-native/backend.native/tests/interop/basics/globals.kt b/kotlin-native/backend.native/tests/interop/basics/globals.kt index 9a48e87db72..dbb6696d925 100644 --- a/kotlin-native/backend.native/tests/interop/basics/globals.kt +++ b/kotlin-native/backend.native/tests/interop/basics/globals.kt @@ -1,7 +1,8 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) import kotlinx.cinterop.* import cglobals.* diff --git a/kotlin-native/backend.native/tests/interop/basics/macros.kt b/kotlin-native/backend.native/tests/interop/basics/macros.kt index 6497b3124f5..accb2730d87 100644 --- a/kotlin-native/backend.native/tests/interop/basics/macros.kt +++ b/kotlin-native/backend.native/tests/interop/basics/macros.kt @@ -1,7 +1,8 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) import kotlin.test.* import cmacros.* diff --git a/kotlin-native/backend.native/tests/interop/basics/opengl_teapot.kt b/kotlin-native/backend.native/tests/interop/basics/opengl_teapot.kt index 5c88879c897..c52795deafd 100644 --- a/kotlin-native/backend.native/tests/interop/basics/opengl_teapot.kt +++ b/kotlin-native/backend.native/tests/interop/basics/opengl_teapot.kt @@ -1,7 +1,8 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) import kotlinx.cinterop.* import platform.GLUT.* @@ -113,4 +114,4 @@ fun main(args: Array) { // run GLUT mainloop glutMainLoop() -} \ No newline at end of file +} diff --git a/kotlin-native/backend.native/tests/interop/basics/pinning.kt b/kotlin-native/backend.native/tests/interop/basics/pinning.kt index ba1836dea0a..07842ee9cab 100644 --- a/kotlin-native/backend.native/tests/interop/basics/pinning.kt +++ b/kotlin-native/backend.native/tests/interop/basics/pinning.kt @@ -1,7 +1,8 @@ /* - * Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) import kotlin.test.* import kotlinx.cinterop.* diff --git a/kotlin-native/backend.native/tests/interop/basics/structAnonym.kt b/kotlin-native/backend.native/tests/interop/basics/structAnonym.kt index ed0bc9afda5..38bdc271685 100644 --- a/kotlin-native/backend.native/tests/interop/basics/structAnonym.kt +++ b/kotlin-native/backend.native/tests/interop/basics/structAnonym.kt @@ -1,3 +1,6 @@ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) + + import kotlinx.cinterop.* import kotlin.test.* import structAnonym.* diff --git a/kotlin-native/backend.native/tests/interop/basics/structs.kt b/kotlin-native/backend.native/tests/interop/basics/structs.kt index 7beaed45590..ec56c26533c 100644 --- a/kotlin-native/backend.native/tests/interop/basics/structs.kt +++ b/kotlin-native/backend.native/tests/interop/basics/structs.kt @@ -1,3 +1,5 @@ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) + import cstructs.* import kotlinx.cinterop.* import kotlin.test.* @@ -116,4 +118,4 @@ fun checkEnumSubTyping(e: T) = memScoped { fun checkIntSubTyping(x: T) = memScoped { val s = alloc() s.i = x -} \ No newline at end of file +} diff --git a/kotlin-native/backend.native/tests/interop/basics/toKString.kt b/kotlin-native/backend.native/tests/interop/basics/toKString.kt index 69a49b763e6..c085b7d2d4b 100644 --- a/kotlin-native/backend.native/tests/interop/basics/toKString.kt +++ b/kotlin-native/backend.native/tests/interop/basics/toKString.kt @@ -1,3 +1,5 @@ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) + import ctoKString.* import kotlinx.cinterop.* import kotlin.native.* diff --git a/kotlin-native/backend.native/tests/interop/basics/types.kt b/kotlin-native/backend.native/tests/interop/basics/types.kt index 46300f26cb5..68609c2c63c 100644 --- a/kotlin-native/backend.native/tests/interop/basics/types.kt +++ b/kotlin-native/backend.native/tests/interop/basics/types.kt @@ -1,3 +1,5 @@ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) + import kotlinx.cinterop.* import kotlin.native.* import kotlin.test.* diff --git a/kotlin-native/backend.native/tests/interop/basics/union.kt b/kotlin-native/backend.native/tests/interop/basics/union.kt index 9eb21d51921..ec115ff1766 100644 --- a/kotlin-native/backend.native/tests/interop/basics/union.kt +++ b/kotlin-native/backend.native/tests/interop/basics/union.kt @@ -1,3 +1,5 @@ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) + import cunion.* import kotlinx.cinterop.* import kotlin.native.* @@ -33,4 +35,4 @@ fun main() { union.i = 0u assertEquals(0u, union.b) } -} \ No newline at end of file +} diff --git a/kotlin-native/backend.native/tests/interop/basics/values.kt b/kotlin-native/backend.native/tests/interop/basics/values.kt index 8a6e03941c8..36f409bf6bc 100644 --- a/kotlin-native/backend.native/tests/interop/basics/values.kt +++ b/kotlin-native/backend.native/tests/interop/basics/values.kt @@ -1,3 +1,5 @@ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) + import kotlinx.cinterop.* import kotlin.test.* import cvalues.* @@ -7,4 +9,4 @@ fun main() { assertTrue(isNullWString(null)) assertFalse(isNullString("a")) assertFalse(isNullWString("b")) -} \ No newline at end of file +} diff --git a/kotlin-native/backend.native/tests/interop/basics/vectors.kt b/kotlin-native/backend.native/tests/interop/basics/vectors.kt index 60c0d019cc9..d7ec77daf07 100644 --- a/kotlin-native/backend.native/tests/interop/basics/vectors.kt +++ b/kotlin-native/backend.native/tests/interop/basics/vectors.kt @@ -1,3 +1,5 @@ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) + import kotlinx.cinterop.* import kotlin.native.* import kotlin.test.* diff --git a/kotlin-native/backend.native/tests/interop/basics/withSpaces.kt b/kotlin-native/backend.native/tests/interop/basics/withSpaces.kt index 71d8bee4a17..8a06281757f 100644 --- a/kotlin-native/backend.native/tests/interop/basics/withSpaces.kt +++ b/kotlin-native/backend.native/tests/interop/basics/withSpaces.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ diff --git a/kotlin-native/backend.native/tests/interop/cpp/cppClass.kt b/kotlin-native/backend.native/tests/interop/cpp/cppClass.kt index ff5c014bb8d..f5ae5bf6245 100644 --- a/kotlin-native/backend.native/tests/interop/cpp/cppClass.kt +++ b/kotlin-native/backend.native/tests/interop/cpp/cppClass.kt @@ -1,3 +1,5 @@ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) + import kotlinx.cinterop.* import kotlin.test.* @@ -132,4 +134,4 @@ fun test2() { println("x.useContents {iPub} = ${x.useContents {iPub}}" ) } -*/ \ No newline at end of file +*/ diff --git a/kotlin-native/backend.native/tests/interop/cpp/skia.kt b/kotlin-native/backend.native/tests/interop/cpp/skia.kt index 51f53492ee6..6eaae5734a4 100644 --- a/kotlin-native/backend.native/tests/interop/cpp/skia.kt +++ b/kotlin-native/backend.native/tests/interop/cpp/skia.kt @@ -1,3 +1,4 @@ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) @file:Suppress("OPT_IN_USAGE_ERROR") import kotlinx.cinterop.* diff --git a/kotlin-native/backend.native/tests/interop/cpp/skiaSignature.kt b/kotlin-native/backend.native/tests/interop/cpp/skiaSignature.kt index 426e0d54436..611f37d5f33 100644 --- a/kotlin-native/backend.native/tests/interop/cpp/skiaSignature.kt +++ b/kotlin-native/backend.native/tests/interop/cpp/skiaSignature.kt @@ -1,3 +1,4 @@ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) @file:Suppress("OPT_IN_USAGE_ERROR") import kotlinx.cinterop.* diff --git a/kotlin-native/backend.native/tests/interop/cpp/types.kt b/kotlin-native/backend.native/tests/interop/cpp/types.kt index 348144e94f0..15580e24292 100644 --- a/kotlin-native/backend.native/tests/interop/cpp/types.kt +++ b/kotlin-native/backend.native/tests/interop/cpp/types.kt @@ -1,3 +1,5 @@ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) + import kotlinx.cinterop.* import kotlin.test.* import kotlin.random.* diff --git a/kotlin-native/backend.native/tests/interop/exceptions/cCallback.kt b/kotlin-native/backend.native/tests/interop/exceptions/cCallback.kt index 4a02d9e676f..c51d38e297c 100644 --- a/kotlin-native/backend.native/tests/interop/exceptions/cCallback.kt +++ b/kotlin-native/backend.native/tests/interop/exceptions/cCallback.kt @@ -1,3 +1,5 @@ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) + import kotlinx.cinterop.staticCFunction import cCallback.runAndCatch @@ -8,4 +10,4 @@ fun throwingCallback() { fun main() { runAndCatch(staticCFunction(::throwingCallback)) -} \ No newline at end of file +} diff --git a/kotlin-native/backend.native/tests/interop/forwardDeclarations/forwardDeclarations.kt b/kotlin-native/backend.native/tests/interop/forwardDeclarations/forwardDeclarations.kt index 312ae42befe..e38cde37f27 100644 --- a/kotlin-native/backend.native/tests/interop/forwardDeclarations/forwardDeclarations.kt +++ b/kotlin-native/backend.native/tests/interop/forwardDeclarations/forwardDeclarations.kt @@ -1,4 +1,5 @@ // This test mostly checks frontend behaviour. +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) import cForwardDeclarations.* import cnames.structs.StructDeclared diff --git a/kotlin-native/backend.native/tests/interop/forwardDeclarationsTwoLibs/forwardDeclarationsTwoLibs.kt b/kotlin-native/backend.native/tests/interop/forwardDeclarationsTwoLibs/forwardDeclarationsTwoLibs.kt index 814e38a1cfb..24ba5123abd 100644 --- a/kotlin-native/backend.native/tests/interop/forwardDeclarationsTwoLibs/forwardDeclarationsTwoLibs.kt +++ b/kotlin-native/backend.native/tests/interop/forwardDeclarationsTwoLibs/forwardDeclarationsTwoLibs.kt @@ -1,4 +1,5 @@ // This test mostly checks frontend behaviour. +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) import cForwardDeclarationsTwoLibs1.* import cForwardDeclarationsTwoLibs2.* diff --git a/kotlin-native/backend.native/tests/interop/incomplete_types/main.kt b/kotlin-native/backend.native/tests/interop/incomplete_types/main.kt index 11ac83e16d1..80d53c32a02 100644 --- a/kotlin-native/backend.native/tests/interop/incomplete_types/main.kt +++ b/kotlin-native/backend.native/tests/interop/incomplete_types/main.kt @@ -1,3 +1,5 @@ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) + import library.* import kotlinx.cinterop.* import kotlin.test.* @@ -23,4 +25,4 @@ fun main() { for (i in 0 until arrayLength()) { assertEquals(0x1, array[i]) } -} \ No newline at end of file +} diff --git a/kotlin-native/backend.native/tests/interop/kt51925/kt51925_lib.kt b/kotlin-native/backend.native/tests/interop/kt51925/kt51925_lib.kt index 5113481f17b..2751bc4d06c 100644 --- a/kotlin-native/backend.native/tests/interop/kt51925/kt51925_lib.kt +++ b/kotlin-native/backend.native/tests/interop/kt51925/kt51925_lib.kt @@ -1,7 +1,8 @@ /* - * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) import kt51925.* import kotlinx.cinterop.* @@ -20,4 +21,4 @@ inline fun foo2(): Int { s.d = 42 return bar2(s) } -} \ No newline at end of file +} diff --git a/kotlin-native/backend.native/tests/runtime/atomics/atomic_smoke.kt b/kotlin-native/backend.native/tests/runtime/atomics/atomic_smoke.kt index d730a6a11ed..bd5483f8fee 100644 --- a/kotlin-native/backend.native/tests/runtime/atomics/atomic_smoke.kt +++ b/kotlin-native/backend.native/tests/runtime/atomics/atomic_smoke.kt @@ -3,7 +3,7 @@ * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ -@file:OptIn(FreezingIsDeprecated::class) +@file:OptIn(FreezingIsDeprecated::class, kotlinx.cinterop.ExperimentalForeignApi::class) package runtime.atomics.atomic_smoke import kotlin.test.* diff --git a/kotlin-native/backend.native/tests/runtime/basic/cleaner_basic.kt b/kotlin-native/backend.native/tests/runtime/basic/cleaner_basic.kt index 8aa90a0ac48..1b2a752337d 100644 --- a/kotlin-native/backend.native/tests/runtime/basic/cleaner_basic.kt +++ b/kotlin-native/backend.native/tests/runtime/basic/cleaner_basic.kt @@ -2,7 +2,8 @@ * Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ -@file:OptIn(ExperimentalStdlibApi::class, FreezingIsDeprecated::class, kotlin.native.runtime.NativeRuntimeApi::class) +@file:OptIn(ExperimentalStdlibApi::class, FreezingIsDeprecated::class, + kotlin.native.runtime.NativeRuntimeApi::class, kotlinx.cinterop.ExperimentalForeignApi::class) package runtime.basic.cleaner_basic diff --git a/kotlin-native/backend.native/tests/runtime/basic/simd.kt b/kotlin-native/backend.native/tests/runtime/basic/simd.kt index 65b2ee3e064..d1310dc0298 100644 --- a/kotlin-native/backend.native/tests/runtime/basic/simd.kt +++ b/kotlin-native/backend.native/tests/runtime/basic/simd.kt @@ -1,7 +1,8 @@ /* - * Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) package runtime.basic.simd diff --git a/kotlin-native/backend.native/tests/runtime/collections/array5.kt b/kotlin-native/backend.native/tests/runtime/collections/array5.kt index 6b930a461e1..0128eaa0db0 100644 --- a/kotlin-native/backend.native/tests/runtime/collections/array5.kt +++ b/kotlin-native/backend.native/tests/runtime/collections/array5.kt @@ -1,7 +1,8 @@ /* - * Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) package runtime.collections.array5 diff --git a/kotlin-native/backend.native/tests/runtime/interop/interop_alloc_value.kt b/kotlin-native/backend.native/tests/runtime/interop/interop_alloc_value.kt index 65380eb5004..01f3bafd42b 100644 --- a/kotlin-native/backend.native/tests/runtime/interop/interop_alloc_value.kt +++ b/kotlin-native/backend.native/tests/runtime/interop/interop_alloc_value.kt @@ -1,3 +1,5 @@ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) + package runtime.interop.interop_alloc_value import kotlinx.cinterop.* diff --git a/kotlin-native/backend.native/tests/runtime/memory/gcStats.kt b/kotlin-native/backend.native/tests/runtime/memory/gcStats.kt index f457695bd2f..158265e2c25 100644 --- a/kotlin-native/backend.native/tests/runtime/memory/gcStats.kt +++ b/kotlin-native/backend.native/tests/runtime/memory/gcStats.kt @@ -3,7 +3,7 @@ * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ -@file:OptIn(kotlin.ExperimentalStdlibApi::class, kotlin.native.runtime.NativeRuntimeApi::class) +@file:OptIn(kotlin.ExperimentalStdlibApi::class, kotlin.native.runtime.NativeRuntimeApi::class, kotlinx.cinterop.ExperimentalForeignApi::class) import kotlin.native.runtime.GC import kotlin.test.* diff --git a/kotlin-native/backend.native/tests/runtime/memory/stable_ref_cross_thread_check.kt b/kotlin-native/backend.native/tests/runtime/memory/stable_ref_cross_thread_check.kt index 71f6d9caede..388d16a7e71 100644 --- a/kotlin-native/backend.native/tests/runtime/memory/stable_ref_cross_thread_check.kt +++ b/kotlin-native/backend.native/tests/runtime/memory/stable_ref_cross_thread_check.kt @@ -2,7 +2,7 @@ * Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ -@file:OptIn(FreezingIsDeprecated::class, ObsoleteWorkersApi::class) +@file:OptIn(FreezingIsDeprecated::class, ObsoleteWorkersApi::class, kotlinx.cinterop.ExperimentalForeignApi::class) package runtime.memory.stable_ref_cross_thread_check diff --git a/kotlin-native/backend.native/tests/runtime/text/utf8.kt b/kotlin-native/backend.native/tests/runtime/text/utf8.kt index ffbeabf8ccc..426e80e5b37 100644 --- a/kotlin-native/backend.native/tests/runtime/text/utf8.kt +++ b/kotlin-native/backend.native/tests/runtime/text/utf8.kt @@ -1,7 +1,8 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) package runtime.text.utf8 diff --git a/kotlin-native/backend.native/tests/runtime/workers/worker10.kt b/kotlin-native/backend.native/tests/runtime/workers/worker10.kt index 28ddd7e105f..8da022d9ccc 100644 --- a/kotlin-native/backend.native/tests/runtime/workers/worker10.kt +++ b/kotlin-native/backend.native/tests/runtime/workers/worker10.kt @@ -1,4 +1,5 @@ -@file:OptIn(FreezingIsDeprecated::class, kotlin.experimental.ExperimentalNativeApi::class, kotlin.native.runtime.NativeRuntimeApi::class, ObsoleteWorkersApi::class) +@file:OptIn(FreezingIsDeprecated::class, kotlin.experimental.ExperimentalNativeApi::class, kotlin.native.runtime.NativeRuntimeApi::class, ObsoleteWorkersApi::class, kotlinx.cinterop.ExperimentalForeignApi::class) + package runtime.workers.worker10 import kotlin.test.* diff --git a/kotlin-native/backend.native/tests/runtime/workers/worker8.kt b/kotlin-native/backend.native/tests/runtime/workers/worker8.kt index 3703795c4a2..b9b17d89151 100644 --- a/kotlin-native/backend.native/tests/runtime/workers/worker8.kt +++ b/kotlin-native/backend.native/tests/runtime/workers/worker8.kt @@ -2,7 +2,7 @@ * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ -@file:OptIn(FreezingIsDeprecated::class, ObsoleteWorkersApi::class) +@file:OptIn(FreezingIsDeprecated::class, ObsoleteWorkersApi::class, kotlinx.cinterop.ExperimentalForeignApi::class) package runtime.workers.worker8 diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/Throwable.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/Throwable.kt index 2850b8f93b9..53636335463 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/Throwable.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/Throwable.kt @@ -1,7 +1,8 @@ /* - * Copyright 2010-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ +@file:OptIn(ExperimentalForeignApi::class) package kotlin @@ -12,6 +13,7 @@ import kotlin.native.internal.ExportForCppRuntime import kotlin.native.internal.ExportTypeInfo import kotlin.native.internal.GCUnsafeCall import kotlin.native.internal.NativePtrArray +import kotlinx.cinterop.ExperimentalForeignApi /** * The base class for all errors and exceptions. Only instances of this class can be thrown or caught. diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/concurrent/Atomics.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/concurrent/Atomics.kt index 526984df52e..a7a330c5ce7 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/concurrent/Atomics.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/concurrent/Atomics.kt @@ -6,6 +6,7 @@ package kotlin.concurrent import kotlinx.cinterop.NativePtr +import kotlinx.cinterop.ExperimentalForeignApi import kotlin.native.internal.* import kotlin.reflect.* import kotlin.concurrent.* @@ -306,6 +307,7 @@ public class AtomicReference { @Frozen @OptIn(FreezingIsDeprecated::class, ExperimentalStdlibApi::class) @SinceKotlin("1.9") +@ExperimentalForeignApi public class AtomicNativePtr(@Volatile public var value: NativePtr) { /** * Atomically sets the value to the given [new value][newValue] and returns the old value. diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/native/Blob.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/native/Blob.kt index ba7db889dc1..6408962fd78 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/native/Blob.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/native/Blob.kt @@ -1,7 +1,8 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ +@file:OptIn(ExperimentalForeignApi::class) package kotlin.native import kotlin.native.internal.* diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/Atomics.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/Atomics.kt index e5ee27fab32..79c6dcc9b56 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/Atomics.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/Atomics.kt @@ -1,10 +1,12 @@ /* - * Copyright 2010-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ +@file:OptIn(ExperimentalForeignApi::class) package kotlin.native.concurrent +import kotlinx.cinterop.ExperimentalForeignApi import kotlinx.cinterop.NativePtr import kotlin.native.internal.* import kotlin.reflect.* diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/Continuation.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/Continuation.kt index 35d6aa69d02..e6898f1787f 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/Continuation.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/Continuation.kt @@ -1,7 +1,8 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ +@file:OptIn(ExperimentalForeignApi::class) package kotlin.native.concurrent @@ -9,6 +10,9 @@ import kotlin.experimental.ExperimentalNativeApi import kotlin.native.internal.* import kotlinx.cinterop.* +private const val DEPRECATED_API_MESSAGE = "This API is deprecated without replacement" + +@Deprecated(DEPRECATED_API_MESSAGE, level = DeprecationLevel.WARNING) @OptIn(FreezingIsDeprecated::class, ExperimentalNativeApi::class) public class Continuation0(block: () -> Unit, private val invoker: CPointer Unit>>, @@ -33,6 +37,7 @@ public class Continuation0(block: () -> Unit, } } +@Deprecated(DEPRECATED_API_MESSAGE, level = DeprecationLevel.WARNING) @OptIn(FreezingIsDeprecated::class, ExperimentalNativeApi::class) public class Continuation1( block: (p1: T1) -> Unit, @@ -65,6 +70,7 @@ public class Continuation1( } } +@Deprecated(DEPRECATED_API_MESSAGE, level = DeprecationLevel.WARNING) @OptIn(FreezingIsDeprecated::class, ExperimentalNativeApi::class) public class Continuation2( block: (p1: T1, p2: T2) -> Unit, @@ -98,16 +104,20 @@ public class Continuation2( } } + +@Deprecated(DEPRECATED_API_MESSAGE, level = DeprecationLevel.WARNING) public fun COpaquePointer.callContinuation0() { val single = this.asStableRef<() -> Unit>() single.get()() } +@Deprecated(DEPRECATED_API_MESSAGE, level = DeprecationLevel.WARNING) public fun COpaquePointer.callContinuation1() { val pair = this.asStableRef Unit>, T1>>().get() pair.first.get()(pair.second) } +@Deprecated(DEPRECATED_API_MESSAGE, level = DeprecationLevel.WARNING) public fun COpaquePointer.callContinuation2() { val triple = this.asStableRef Unit>, T1, T2>>().get() triple.first.get()(triple.second, triple.third) diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/Internal.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/Internal.kt index 0bb956517e9..001dedf9eee 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/Internal.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/Internal.kt @@ -1,8 +1,8 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ - +@file:OptIn(ExperimentalForeignApi::class) package kotlin.native.concurrent import kotlin.experimental.ExperimentalNativeApi diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/Lazy.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/Lazy.kt index a342be2eea0..b67fd698191 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/Lazy.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/Lazy.kt @@ -1,16 +1,19 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ @file:Suppress("DEPRECATION") +@file:OptIn(ExperimentalForeignApi::class) package kotlin.native.concurrent import kotlin.experimental.ExperimentalNativeApi import kotlin.native.internal.Frozen import kotlin.concurrent.AtomicReference +import kotlinx.cinterop.ExperimentalForeignApi @FreezingIsDeprecated + internal class FreezeAwareLazyImpl(initializer: () -> T) : Lazy { private val value_ = FreezableAtomicReference(UNINITIALIZED) // This cannot be made atomic because of the legacy MM. See https://github.com/JetBrains/kotlin-native/pull/3944 diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/MutableData.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/MutableData.kt index 2592d7394a2..cddc0508942 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/MutableData.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/MutableData.kt @@ -1,7 +1,8 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ +@file:OptIn(ExperimentalForeignApi::class) package kotlin.native.concurrent diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/ObjectTransfer.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/ObjectTransfer.kt index 794400c0a54..345837bc9fb 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/ObjectTransfer.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/ObjectTransfer.kt @@ -1,8 +1,8 @@ /* - * Copyright 2010-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ - +@file:OptIn(ExperimentalForeignApi::class) package kotlin.native.concurrent import kotlinx.cinterop.* @@ -77,6 +77,7 @@ public class DetachedObjectGraph internal constructor(pointer: NativePtr) { /** * Returns raw C pointer value, usable for interoperability with C scenarious. */ + @ExperimentalForeignApi public fun asCPointer(): COpaquePointer? = interpretCPointer(stable.value) } diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/Worker.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/Worker.kt index 55dfd02de4d..37b0687d190 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/Worker.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/Worker.kt @@ -1,8 +1,8 @@ /* - * Copyright 2010-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ - +@file:OptIn(ExperimentalForeignApi::class) package kotlin.native.concurrent import kotlin.experimental.ExperimentalNativeApi diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/WorkerBoundReference.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/WorkerBoundReference.kt index 7c8124cbcdb..58a47c07f0e 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/WorkerBoundReference.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/native/concurrent/WorkerBoundReference.kt @@ -1,11 +1,13 @@ /* - * Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ +@file:OptIn(ExperimentalForeignApi::class) package kotlin.native.concurrent import kotlin.native.internal.* +import kotlinx.cinterop.ExperimentalForeignApi @GCUnsafeCall("Kotlin_WorkerBoundReference_create") @ObsoleteWorkersApi @@ -35,6 +37,7 @@ external private fun describeWorkerBoundReference(ref: NativePtr): String @HasFreezeHook @FreezingIsDeprecated @ObsoleteWorkersApi +@OptIn(ExperimentalForeignApi::class) public class WorkerBoundReference(value: T) { private var ptr = NativePtr.NULL diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/Intrinsics.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/Intrinsics.kt index 946662ba452..ab5dfac0703 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/Intrinsics.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/Intrinsics.kt @@ -1,13 +1,15 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ +@file:OptIn(ExperimentalForeignApi::class) package kotlin.native.internal import kotlinx.cinterop.CPointer import kotlinx.cinterop.NativePointed import kotlinx.cinterop.NativePtr +import kotlinx.cinterop.ExperimentalForeignApi import kotlin.native.internal.TypedIntrinsic import kotlin.native.internal.IntrinsicType @@ -32,4 +34,4 @@ import kotlin.native.internal.IntrinsicType @TypedIntrinsic(IntrinsicType.IDENTITY) @PublishedApi external internal fun T.reinterpret(): R -@TypedIntrinsic(IntrinsicType.THE_UNIT_INSTANCE) @ExportForCompiler external internal fun theUnitInstance(): Unit \ No newline at end of file +@TypedIntrinsic(IntrinsicType.THE_UNIT_INSTANCE) @ExportForCompiler external internal fun theUnitInstance(): Unit diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/KClassImpl.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/KClassImpl.kt index df75b3821a1..4e37bd65c37 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/KClassImpl.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/KClassImpl.kt @@ -1,11 +1,13 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ +@file:OptIn(ExperimentalForeignApi::class) package kotlin.native.internal import kotlin.reflect.KClass +import kotlinx.cinterop.* @ExportForCompiler internal class KClassImpl(private val typeInfo: NativePtr) : KClass { diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/NativePtr.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/NativePtr.kt index 6b836fb525e..1862865b0d4 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/NativePtr.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/NativePtr.kt @@ -1,16 +1,19 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ - +@file:OptIn(ExperimentalForeignApi::class) @file:Suppress("RESERVED_MEMBER_INSIDE_VALUE_CLASS") package kotlin.native.internal +import kotlinx.cinterop.* + @TypedIntrinsic(IntrinsicType.INTEROP_GET_NATIVE_NULL_PTR) @PublishedApi internal external fun getNativeNullPtr(): NativePtr +@ExperimentalForeignApi class NativePtr @PublishedApi internal constructor(private val value: NonNullNativePtr?) { companion object { // TODO: make it properly precreated, maybe use an intrinsic for that. diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/ObjCExportCoroutines.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/ObjCExportCoroutines.kt index 5fae512f7b5..5ded07f0ab7 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/ObjCExportCoroutines.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/ObjCExportCoroutines.kt @@ -1,11 +1,12 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ - +@file:OptIn(ExperimentalForeignApi::class) package kotlin.native.internal import kotlin.experimental.ExperimentalNativeApi +import kotlinx.cinterop.ExperimentalForeignApi import kotlin.coroutines.* import kotlin.coroutines.intrinsics.* import kotlin.coroutines.native.internal.* diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/RuntimeUtils.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/RuntimeUtils.kt index e2d199073b4..338e3593494 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/RuntimeUtils.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/RuntimeUtils.kt @@ -1,7 +1,8 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ +@file:OptIn(ExperimentalForeignApi::class) @file:Suppress("DEPRECATION", "DEPRECATION_ERROR") // Char.toInt() package kotlin.native.internal @@ -10,6 +11,7 @@ import kotlin.experimental.ExperimentalNativeApi import kotlin.internal.getProgressionLastElement import kotlin.reflect.KClass import kotlin.native.concurrent.freeze +import kotlinx.cinterop.* import kotlin.native.concurrent.FreezableAtomicReference @ExportForCppRuntime diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/Utils.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/Utils.kt index a977c41d1b6..95d7e185d6e 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/Utils.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/native/internal/Utils.kt @@ -1,11 +1,13 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ +@file:OptIn(ExperimentalForeignApi::class) package kotlin.native.internal import kotlin.reflect.KClass +import kotlinx.cinterop.* @ExportForCppRuntime internal fun DescribeObjectForDebugging(typeInfo: NativePtr, address: NativePtr): String { @@ -51,4 +53,4 @@ public fun Any.collectReferenceFieldValues() : List { getObjectReferenceFieldByIndex(this@collectReferenceFieldValues, it) } } -} \ No newline at end of file +} diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/native/ref/Cleaner.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/native/ref/Cleaner.kt index a4e76320d0d..7653a9423c8 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/native/ref/Cleaner.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/native/ref/Cleaner.kt @@ -3,12 +3,14 @@ * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ +@file:OptIn(ExperimentalForeignApi::class) package kotlin.native.ref import kotlin.experimental.ExperimentalNativeApi import kotlin.native.concurrent.* import kotlin.native.internal.* import kotlinx.cinterop.NativePtr +import kotlinx.cinterop.* /** * The marker interface for objects that have a cleanup action associated with them. diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/native/ref/WeakPrivate.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/native/ref/WeakPrivate.kt index fc3ee149ad8..22254aa2c11 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/native/ref/WeakPrivate.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/native/ref/WeakPrivate.kt @@ -1,11 +1,11 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ - +@file:OptIn(ExperimentalForeignApi::class) package kotlin.native.ref -import kotlinx.cinterop.COpaquePointer +import kotlinx.cinterop.* import kotlin.native.internal.* /** diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/native/runtime/GCInfo.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/native/runtime/GCInfo.kt index 1ea9764c492..92d22b72c32 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/native/runtime/GCInfo.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/native/runtime/GCInfo.kt @@ -3,6 +3,7 @@ * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ +@file:OptIn(ExperimentalForeignApi::class) package kotlin.native.runtime import kotlin.native.internal.* diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/native/simd.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/native/simd.kt index 866d8e4c2a5..d2ca6a3d3f5 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/native/simd.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/native/simd.kt @@ -1,5 +1,5 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ package kotlin.native @@ -7,8 +7,10 @@ package kotlin.native import kotlin.native.internal.GCUnsafeCall import kotlin.native.internal.TypedIntrinsic import kotlin.native.internal.IntrinsicType +import kotlinx.cinterop.ExperimentalForeignApi +@ExperimentalForeignApi public final class Vector128 private constructor() { @TypedIntrinsic(IntrinsicType.EXTRACT_ELEMENT) external fun getByteAt(index: Int): Byte @@ -52,8 +54,10 @@ public final class Vector128 private constructor() { } } +@ExperimentalForeignApi @GCUnsafeCall("Kotlin_Vector4f_of") external fun vectorOf(f0: Float, f1: Float, f2: Float, f3: Float): Vector128 +@ExperimentalForeignApi @GCUnsafeCall("Kotlin_Vector4i32_of") external fun vectorOf(f0: Int, f1: Int, f2: Int, f3: Int): Vector128