Support for Unsigned Types in swift export #KT-65668 fixed
Merge-request: KT-MR-14300 Merged-by: Artem Olkov <artem.olkov@jetbrains.com>
This commit is contained in:
@@ -12,4 +12,20 @@ fun fooInt(): Int {
|
|||||||
|
|
||||||
fun fooLong(): Long {
|
fun fooLong(): Long {
|
||||||
return -1
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
fun fooUByte(): UByte {
|
||||||
|
return 0u
|
||||||
|
}
|
||||||
|
|
||||||
|
fun fooUShort(): UShort {
|
||||||
|
return 0u
|
||||||
|
}
|
||||||
|
|
||||||
|
fun fooUInt(): UInt {
|
||||||
|
return 0u
|
||||||
|
}
|
||||||
|
|
||||||
|
fun fooULong(): ULong {
|
||||||
|
return 0u
|
||||||
}
|
}
|
||||||
@@ -14,6 +14,24 @@ func smoke() throws {
|
|||||||
|
|
||||||
try assertEquals(actual: org.kotlin.plus(a: 1, b: 2, c: 3), expected: 1 + 2 + 3)
|
try assertEquals(actual: org.kotlin.plus(a: 1, b: 2, c: 3), expected: 1 + 2 + 3)
|
||||||
|
|
||||||
|
// we expect strange stuff for plus and minus if UInt8 and UInt16 because kotlin behaves that way:
|
||||||
|
// https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-u-byte/minus.html
|
||||||
|
try assertEquals(actual: fooUByte(), expected: UInt8.min)
|
||||||
|
try assertEquals(actual: org.kotlin.minus(a: fooUByte(), b: UInt8(1)), expected: UInt32.max)
|
||||||
|
try assertEquals(actual: org.kotlin.plus(a: UInt8.max, b: UInt8(1)), expected: UInt32(256))
|
||||||
|
|
||||||
|
try assertEquals(actual: fooUShort(), expected: UInt16.min)
|
||||||
|
try assertEquals(actual: org.kotlin.minus(a: fooUShort(), b: UInt16(1)), expected: UInt32.max)
|
||||||
|
try assertEquals(actual: org.kotlin.plus(a: UInt16.max, b: UInt16(1)), expected: UInt32(65536))
|
||||||
|
|
||||||
|
try assertEquals(actual: fooUInt(), expected: UInt32.min)
|
||||||
|
try assertEquals(actual: org.kotlin.minus(a: fooUInt(), b: UInt32(1)), expected: UInt32.max)
|
||||||
|
try assertEquals(actual: org.kotlin.plus(a: UInt32.max, b: UInt32(1)), expected: UInt32.min)
|
||||||
|
|
||||||
|
try assertEquals(actual: fooULong(), expected: UInt64.min)
|
||||||
|
try assertEquals(actual: org.kotlin.minus(a: fooULong(), b: UInt64(1)), expected: UInt64.max)
|
||||||
|
try assertEquals(actual: org.kotlin.plus(a: UInt64.max, b: UInt64(1)), expected: UInt64.min)
|
||||||
|
|
||||||
try assertEquals(actual: org.kotlin.logicalOr(a: true, b: true), expected: true || true)
|
try assertEquals(actual: org.kotlin.logicalOr(a: true, b: true), expected: true || true)
|
||||||
try assertEquals(actual: org.kotlin.logicalOr(a: true, b: false), expected: true || false)
|
try assertEquals(actual: org.kotlin.logicalOr(a: true, b: false), expected: true || false)
|
||||||
try assertEquals(actual: org.kotlin.logicalOr(a: false, b: false), expected: false || false)
|
try assertEquals(actual: org.kotlin.logicalOr(a: false, b: false), expected: false || false)
|
||||||
|
|||||||
+13
-1
@@ -5,4 +5,16 @@ fun plus(a: Int, b: Int, c: Int) =
|
|||||||
|
|
||||||
fun logicalOr(a: Boolean, b: Boolean) = a || b
|
fun logicalOr(a: Boolean, b: Boolean) = a || b
|
||||||
|
|
||||||
fun xor(a: Boolean, b: Boolean) = a xor b
|
fun xor(a: Boolean, b: Boolean) = a xor b
|
||||||
|
|
||||||
|
fun plus(a: UByte, b: UByte) = a + b
|
||||||
|
fun minus(a: UByte, b: UByte) = a - b
|
||||||
|
|
||||||
|
fun plus(a: UShort, b: UShort) = a + b
|
||||||
|
fun minus(a: UShort, b: UShort) = a - b
|
||||||
|
|
||||||
|
fun plus(a: UInt, b: UInt) = a + b
|
||||||
|
fun minus(a: UInt, b: UInt) = a - b
|
||||||
|
|
||||||
|
fun plus(a: ULong, b: ULong) = a + b
|
||||||
|
fun minus(a: ULong, b: ULong) = a - b
|
||||||
@@ -5,7 +5,9 @@ plugins {
|
|||||||
tasks.register("sirAllTests") {
|
tasks.register("sirAllTests") {
|
||||||
dependsOn(
|
dependsOn(
|
||||||
":native:swift:sir-compiler-bridge:test",
|
":native:swift:sir-compiler-bridge:test",
|
||||||
":native:swift:sir-printer:test",
|
":native:swift:sir-printer:test"
|
||||||
":native:swift:swift-export-standalone:test",
|
|
||||||
)
|
)
|
||||||
|
if (kotlinBuildProperties.isKotlinNativeEnabled) {
|
||||||
|
dependsOn(":native:swift:swift-export-standalone:test")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,9 +41,8 @@ sourceSets {
|
|||||||
|
|
||||||
val testDataDir = projectDir.resolve("testData")
|
val testDataDir = projectDir.resolve("testData")
|
||||||
|
|
||||||
projectTest(jUnitMode = JUnitMode.JUnit5) {
|
val test by nativeTest("test", null) {
|
||||||
inputs.dir(testDataDir)
|
inputs.dir(testDataDir)
|
||||||
dependsOn(":dist")
|
|
||||||
workingDir = rootDir
|
workingDir = rootDir
|
||||||
useJUnitPlatform { }
|
useJUnitPlatform { }
|
||||||
}
|
}
|
||||||
|
|||||||
+16
-6
@@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.swiftexport.standalone
|
package org.jetbrains.kotlin.swiftexport.standalone
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.konan.target.Distribution
|
||||||
import org.jetbrains.kotlin.swiftexport.standalone.SwiftExportConfig.Companion.BRIDGE_MODULE_NAME
|
import org.jetbrains.kotlin.swiftexport.standalone.SwiftExportConfig.Companion.BRIDGE_MODULE_NAME
|
||||||
import org.jetbrains.kotlin.swiftexport.standalone.SwiftExportConfig.Companion.DEBUG_MODE_ENABLED
|
import org.jetbrains.kotlin.swiftexport.standalone.SwiftExportConfig.Companion.DEBUG_MODE_ENABLED
|
||||||
import org.jetbrains.kotlin.swiftexport.standalone.SwiftExportConfig.Companion.DEFAULT_BRIDGE_MODULE_NAME
|
import org.jetbrains.kotlin.swiftexport.standalone.SwiftExportConfig.Companion.DEFAULT_BRIDGE_MODULE_NAME
|
||||||
@@ -12,11 +13,13 @@ import org.jetbrains.kotlin.swiftexport.standalone.builders.buildFunctionBridges
|
|||||||
import org.jetbrains.kotlin.swiftexport.standalone.builders.buildSwiftModule
|
import org.jetbrains.kotlin.swiftexport.standalone.builders.buildSwiftModule
|
||||||
import org.jetbrains.kotlin.swiftexport.standalone.transformation.transformToSwift
|
import org.jetbrains.kotlin.swiftexport.standalone.transformation.transformToSwift
|
||||||
import org.jetbrains.kotlin.swiftexport.standalone.writer.dumpResultToFiles
|
import org.jetbrains.kotlin.swiftexport.standalone.writer.dumpResultToFiles
|
||||||
|
import org.jetbrains.kotlin.utils.KotlinNativePaths
|
||||||
import java.nio.file.Path
|
import java.nio.file.Path
|
||||||
|
|
||||||
public data class SwiftExportConfig(
|
public data class SwiftExportConfig(
|
||||||
val settings: Map<String, String>,
|
val settings: Map<String, String> = emptyMap(),
|
||||||
val logger: SwiftExportLogger,
|
val logger: SwiftExportLogger = createDummyLogger(),
|
||||||
|
val distribution: Distribution = Distribution(KotlinNativePaths.homePath.absolutePath)
|
||||||
) {
|
) {
|
||||||
public companion object {
|
public companion object {
|
||||||
public const val DEBUG_MODE_ENABLED: String = "DEBUG_MODE_ENABLED"
|
public const val DEBUG_MODE_ENABLED: String = "DEBUG_MODE_ENABLED"
|
||||||
@@ -73,18 +76,25 @@ public fun createDummyLogger(): SwiftExportLogger = object : SwiftExportLogger {
|
|||||||
*/
|
*/
|
||||||
public fun runSwiftExport(
|
public fun runSwiftExport(
|
||||||
input: SwiftExportInput,
|
input: SwiftExportInput,
|
||||||
config: SwiftExportConfig = SwiftExportConfig(emptyMap(), createDummyLogger()),
|
config: SwiftExportConfig = SwiftExportConfig(),
|
||||||
output: SwiftExportOutput,
|
output: SwiftExportOutput,
|
||||||
) {
|
) {
|
||||||
val isDebugModeEnabled = config.settings.containsKey(DEBUG_MODE_ENABLED)
|
val isDebugModeEnabled = config.settings.containsKey(DEBUG_MODE_ENABLED)
|
||||||
val bridgeModuleName = config.settings.getOrElse(BRIDGE_MODULE_NAME) {
|
val bridgeModuleName = config.settings.getOrElse(BRIDGE_MODULE_NAME) {
|
||||||
config.logger.report(SwiftExportLogger.Severity.Warning,
|
config.logger.report(
|
||||||
"Bridging header is not set. Using $DEFAULT_BRIDGE_MODULE_NAME instead")
|
SwiftExportLogger.Severity.Warning,
|
||||||
|
"Bridging header is not set. Using $DEFAULT_BRIDGE_MODULE_NAME instead"
|
||||||
|
)
|
||||||
DEFAULT_BRIDGE_MODULE_NAME
|
DEFAULT_BRIDGE_MODULE_NAME
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
val module = buildSwiftModule(input, isDebugModeEnabled, bridgeModuleName)
|
val module = buildSwiftModule(
|
||||||
|
input,
|
||||||
|
config.distribution,
|
||||||
|
isDebugModeEnabled,
|
||||||
|
bridgeModuleName
|
||||||
|
)
|
||||||
.transformToSwift()
|
.transformToSwift()
|
||||||
val bridgeRequests = module.buildFunctionBridges()
|
val bridgeRequests = module.buildFunctionBridges()
|
||||||
module.dumpResultToFiles(bridgeRequests, output)
|
module.dumpResultToFiles(bridgeRequests, output)
|
||||||
|
|||||||
+13
@@ -10,7 +10,9 @@ import org.jetbrains.kotlin.analysis.api.analyze
|
|||||||
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeTokenProvider
|
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeTokenProvider
|
||||||
import org.jetbrains.kotlin.analysis.api.standalone.KtAlwaysAccessibleLifetimeTokenProvider
|
import org.jetbrains.kotlin.analysis.api.standalone.KtAlwaysAccessibleLifetimeTokenProvider
|
||||||
import org.jetbrains.kotlin.analysis.api.standalone.buildStandaloneAnalysisAPISession
|
import org.jetbrains.kotlin.analysis.api.standalone.buildStandaloneAnalysisAPISession
|
||||||
|
import org.jetbrains.kotlin.analysis.project.structure.builder.buildKtLibraryModule
|
||||||
import org.jetbrains.kotlin.analysis.project.structure.builder.buildKtSourceModule
|
import org.jetbrains.kotlin.analysis.project.structure.builder.buildKtSourceModule
|
||||||
|
import org.jetbrains.kotlin.konan.target.Distribution
|
||||||
import org.jetbrains.kotlin.platform.konan.NativePlatforms
|
import org.jetbrains.kotlin.platform.konan.NativePlatforms
|
||||||
import org.jetbrains.kotlin.psi.KtFile
|
import org.jetbrains.kotlin.psi.KtFile
|
||||||
import org.jetbrains.kotlin.sir.SirModule
|
import org.jetbrains.kotlin.sir.SirModule
|
||||||
@@ -18,10 +20,12 @@ import org.jetbrains.kotlin.sir.builder.buildImport
|
|||||||
import org.jetbrains.kotlin.sir.builder.buildModule
|
import org.jetbrains.kotlin.sir.builder.buildModule
|
||||||
import org.jetbrains.kotlin.swiftexport.standalone.SwiftExportInput
|
import org.jetbrains.kotlin.swiftexport.standalone.SwiftExportInput
|
||||||
import org.jetbrains.sir.passes.builder.buildSirDeclarationList
|
import org.jetbrains.sir.passes.builder.buildSirDeclarationList
|
||||||
|
import kotlin.io.path.Path
|
||||||
|
|
||||||
@OptIn(KtAnalysisApiInternals::class)
|
@OptIn(KtAnalysisApiInternals::class)
|
||||||
internal fun buildSwiftModule(
|
internal fun buildSwiftModule(
|
||||||
input: SwiftExportInput,
|
input: SwiftExportInput,
|
||||||
|
kotlinDistribution: Distribution,
|
||||||
shouldSortInputFiles: Boolean,
|
shouldSortInputFiles: Boolean,
|
||||||
bridgeModuleName: String,
|
bridgeModuleName: String,
|
||||||
): SirModule {
|
): SirModule {
|
||||||
@@ -31,11 +35,20 @@ internal fun buildSwiftModule(
|
|||||||
buildKtModuleProvider {
|
buildKtModuleProvider {
|
||||||
platform = NativePlatforms.unspecifiedNativePlatform
|
platform = NativePlatforms.unspecifiedNativePlatform
|
||||||
|
|
||||||
|
val stdlib = addModule(
|
||||||
|
buildKtLibraryModule {
|
||||||
|
addBinaryRoot(Path(kotlinDistribution.stdlib))
|
||||||
|
platform = NativePlatforms.unspecifiedNativePlatform
|
||||||
|
libraryName = "stdlib"
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
addModule(
|
addModule(
|
||||||
buildKtSourceModule {
|
buildKtSourceModule {
|
||||||
addSourceRoot(input.sourceRoot)
|
addSourceRoot(input.sourceRoot)
|
||||||
platform = NativePlatforms.unspecifiedNativePlatform
|
platform = NativePlatforms.unspecifiedNativePlatform
|
||||||
moduleName = "main"
|
moduleName = "main"
|
||||||
|
addRegularDependency(stdlib)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -4,7 +4,7 @@ void namespace1_local_functions_foo();
|
|||||||
|
|
||||||
int32_t namespace1_main_foobar__TypesOfArguments__int32_t__(int32_t param);
|
int32_t namespace1_main_foobar__TypesOfArguments__int32_t__(int32_t param);
|
||||||
|
|
||||||
void namespace1_main_all_args__TypesOfArguments___Bool_int8_t_int16_t_int32_t_int64_t_float_double__(_Bool arg1, int8_t arg2, int16_t arg3, int32_t arg4, int64_t arg5, float arg10, double arg11);
|
void namespace1_main_all_args__TypesOfArguments___Bool_int8_t_int16_t_int32_t_int64_t_uint8_t_uint16_t_uint32_t_uint64_t_float_double__(_Bool arg1, int8_t arg2, int16_t arg3, int32_t arg4, int64_t arg5, uint8_t arg6, uint16_t arg7, uint32_t arg8, uint64_t arg9, float arg10, double arg11);
|
||||||
|
|
||||||
int32_t namespace1_bar();
|
int32_t namespace1_bar();
|
||||||
|
|
||||||
|
|||||||
+3
-3
@@ -12,9 +12,9 @@ public fun namespace1_main_foobar(param: Int): Int {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExportedBridge("namespace1_main_all_args__TypesOfArguments___Bool_int8_t_int16_t_int32_t_int64_t_float_double__")
|
@ExportedBridge("namespace1_main_all_args__TypesOfArguments___Bool_int8_t_int16_t_int32_t_int64_t_uint8_t_uint16_t_uint32_t_uint64_t_float_double__")
|
||||||
public fun namespace1_main_all_args(arg1: Boolean, arg2: Byte, arg3: Short, arg4: Int, arg5: Long, arg10: Float, arg11: Double): Unit {
|
public fun namespace1_main_all_args(arg1: Boolean, arg2: Byte, arg3: Short, arg4: Int, arg5: Long, arg6: UByte, arg7: UShort, arg8: UInt, arg9: ULong, arg10: Float, arg11: Double): Unit {
|
||||||
val result = namespace1.main.all_args(arg1, arg2, arg3, arg4, arg5, arg10, arg11)
|
val result = namespace1.main.all_args(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+5
-1
@@ -18,10 +18,14 @@ public enum namespace1 {
|
|||||||
arg3: Swift.Int16,
|
arg3: Swift.Int16,
|
||||||
arg4: Swift.Int32,
|
arg4: Swift.Int32,
|
||||||
arg5: Swift.Int64,
|
arg5: Swift.Int64,
|
||||||
|
arg6: Swift.UInt8,
|
||||||
|
arg7: Swift.UInt16,
|
||||||
|
arg8: Swift.UInt32,
|
||||||
|
arg9: Swift.UInt64,
|
||||||
arg10: Swift.Float,
|
arg10: Swift.Float,
|
||||||
arg11: Swift.Double
|
arg11: Swift.Double
|
||||||
) -> Swift.Void {
|
) -> Swift.Void {
|
||||||
namespace1_main_all_args__TypesOfArguments___Bool_int8_t_int16_t_int32_t_int64_t_float_double__(arg1, arg2, arg3, arg4, arg5, arg10, arg11)
|
namespace1_main_all_args__TypesOfArguments___Bool_int8_t_int16_t_int32_t_int64_t_uint8_t_uint16_t_uint32_t_uint64_t_float_double__(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public static func bar() -> Swift.Int32 {
|
public static func bar() -> Swift.Int32 {
|
||||||
|
|||||||
@@ -13,11 +13,10 @@ fun all_args(
|
|||||||
arg4: Int,
|
arg4: Int,
|
||||||
arg5: Long,
|
arg5: Long,
|
||||||
|
|
||||||
// TODO: support kotlin Unsigned types - KT-65668
|
arg6: UByte,
|
||||||
// arg6: UByte,
|
arg7: UShort,
|
||||||
// arg7: UShort,
|
arg8: UInt,
|
||||||
// arg8: UInt,
|
arg9: ULong,
|
||||||
// arg9: ULong,
|
|
||||||
|
|
||||||
arg10: Float,
|
arg10: Float,
|
||||||
arg11: Double,
|
arg11: Double,
|
||||||
|
|||||||
+11
-1
@@ -6,6 +6,7 @@
|
|||||||
package org.jetbrains.kotlin.swiftexport.standalone
|
package org.jetbrains.kotlin.swiftexport.standalone
|
||||||
|
|
||||||
import com.intellij.openapi.util.io.FileUtil
|
import com.intellij.openapi.util.io.FileUtil
|
||||||
|
import org.jetbrains.kotlin.konan.target.Distribution
|
||||||
import org.jetbrains.kotlin.test.KotlinTestUtils
|
import org.jetbrains.kotlin.test.KotlinTestUtils
|
||||||
import kotlin.io.path.*
|
import kotlin.io.path.*
|
||||||
|
|
||||||
@@ -41,7 +42,8 @@ abstract class AbstractSwiftRunnerTestBase {
|
|||||||
SwiftExportConfig.DEBUG_MODE_ENABLED to "true",
|
SwiftExportConfig.DEBUG_MODE_ENABLED to "true",
|
||||||
SwiftExportConfig.BRIDGE_MODULE_NAME to SwiftExportConfig.DEFAULT_BRIDGE_MODULE_NAME,
|
SwiftExportConfig.BRIDGE_MODULE_NAME to SwiftExportConfig.DEFAULT_BRIDGE_MODULE_NAME,
|
||||||
),
|
),
|
||||||
logger = createDummyLogger()
|
logger = createDummyLogger(),
|
||||||
|
distribution = Distribution(KonanHome.konanHomePath),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -50,3 +52,11 @@ abstract class AbstractSwiftRunnerTestBase {
|
|||||||
KotlinTestUtils.assertEqualsToFile(expectedKotlinBridge, output.kotlinBridges.readText())
|
KotlinTestUtils.assertEqualsToFile(expectedKotlinBridge, output.kotlinBridges.readText())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private object KonanHome {
|
||||||
|
private const val KONAN_HOME_PROPERTY_KEY = "kotlin.internal.native.test.nativeHome"
|
||||||
|
|
||||||
|
val konanHomePath: String
|
||||||
|
get() = System.getProperty(KONAN_HOME_PROPERTY_KEY)
|
||||||
|
?: error("Missing System property: '$KONAN_HOME_PROPERTY_KEY'")
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user