f4e8ae5191
* @ExperimentalForeignApi for all declarations that operate on
unmanaged memory (i.e. pointers and references)
* @BetaInteropApi for the rest of the interoperability declarations,
namely Swift/CInterop-specific interfaces and convenience-functions
### Implementation details
* Some typealiases are not marked explicitly because it crashes the compiler,
yet their experimentality is properly propagated
* License header is adjusted where it previously had been existing
* Deprecated with ERROR interop declarations that are deprecated for more than
two years are removed
* WASM target interop declarations are deprecated
* Deliberately make Boolean.toByte and Byte.toBoolean foreign-experimental to scare
people away
^KT-57728 fixed
Merge-request: KT-MR-9788
Merged-by: Vsevolod Tolstopyatov <qwwdfsad@gmail.com>
57 lines
1.7 KiB
Kotlin
57 lines
1.7 KiB
Kotlin
/*
|
|
* Copyright 2010-2022 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 org.jetbrains.kotlin.generators.native.interopRuntime
|
|
|
|
import java.io.File
|
|
import java.io.FileWriter
|
|
|
|
fun FileWriter.generateHeader() {
|
|
appendLine(File("license/COPYRIGHT_HEADER.txt").readText())
|
|
appendLine("package kotlinx.cinterop")
|
|
appendLine()
|
|
appendLine("//")
|
|
appendLine("// NOTE: THIS FILE IS AUTO-GENERATED by the generators/nativeInteropRuntime/NativeInteropRuntimeGenerator.kt")
|
|
appendLine("//")
|
|
appendLine()
|
|
}
|
|
|
|
enum class PrimitiveInteropType {
|
|
Boolean, Byte, Short, Int, Long, UByte, UShort, UInt, ULong, Float, Double;
|
|
}
|
|
|
|
fun FileWriter.generateAllocWithValue(type: PrimitiveInteropType) {
|
|
val typeName = type.name
|
|
|
|
appendLine(
|
|
"""
|
|
/**
|
|
* Allocates variable with given value type and initializes it with given value.
|
|
*/
|
|
@Suppress("FINAL_UPPER_BOUND")
|
|
@ExperimentalForeignApi
|
|
public fun <T : $typeName> NativePlacement.alloc(value: T): ${typeName}VarOf<T> =
|
|
alloc<${typeName}VarOf<T>> { this.value = value }
|
|
""".trimIndent()
|
|
)
|
|
}
|
|
|
|
fun generateUtils(targetDir: File) {
|
|
FileWriter(targetDir.resolve("_UtilsGenerated.kt")).use { writer ->
|
|
writer.generateHeader()
|
|
|
|
for (type in PrimitiveInteropType.values()) {
|
|
writer.generateAllocWithValue(type)
|
|
writer.appendLine()
|
|
}
|
|
}
|
|
}
|
|
|
|
fun main() {
|
|
val targetDir = File("kotlin-native/Interop/Runtime/src/main/kotlin/kotlinx/cinterop")
|
|
|
|
generateUtils(targetDir)
|
|
}
|