diff --git a/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeUtils.kt b/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeUtils.kt index 10d39259606..846bd3650f4 100644 --- a/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeUtils.kt +++ b/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeUtils.kt @@ -31,20 +31,20 @@ external fun bitsToDouble(bits: Long): Double // TODO: deprecate. @TypedIntrinsic(IntrinsicType.INTEROP_SIGN_EXTEND) -external fun Number.signExtend(): R +external inline fun Number.signExtend(): R // TODO: deprecate. @TypedIntrinsic(IntrinsicType.INTEROP_NARROW) -external fun Number.narrow(): R +external inline fun Number.narrow(): R -@TypedIntrinsic(IntrinsicType.INTEROP_CONVERT) external fun Byte.convert(): R -@TypedIntrinsic(IntrinsicType.INTEROP_CONVERT) external fun Short.convert(): R -@TypedIntrinsic(IntrinsicType.INTEROP_CONVERT) external fun Int.convert(): R -@TypedIntrinsic(IntrinsicType.INTEROP_CONVERT) external fun Long.convert(): R -@TypedIntrinsic(IntrinsicType.INTEROP_CONVERT) external fun UByte.convert(): R -@TypedIntrinsic(IntrinsicType.INTEROP_CONVERT) external fun UShort.convert(): R -@TypedIntrinsic(IntrinsicType.INTEROP_CONVERT) external fun UInt.convert(): R -@TypedIntrinsic(IntrinsicType.INTEROP_CONVERT) external fun ULong.convert(): R +@TypedIntrinsic(IntrinsicType.INTEROP_CONVERT) external inline fun Byte.convert(): R +@TypedIntrinsic(IntrinsicType.INTEROP_CONVERT) external inline fun Short.convert(): R +@TypedIntrinsic(IntrinsicType.INTEROP_CONVERT) external inline fun Int.convert(): R +@TypedIntrinsic(IntrinsicType.INTEROP_CONVERT) external inline fun Long.convert(): R +@TypedIntrinsic(IntrinsicType.INTEROP_CONVERT) external inline fun UByte.convert(): R +@TypedIntrinsic(IntrinsicType.INTEROP_CONVERT) external inline fun UShort.convert(): R +@TypedIntrinsic(IntrinsicType.INTEROP_CONVERT) external inline fun UInt.convert(): R +@TypedIntrinsic(IntrinsicType.INTEROP_CONVERT) external inline fun ULong.convert(): R @Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER, AnnotationTarget.FILE) @Retention(AnnotationRetention.SOURCE) diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index fc56bbe2e6b..5cfbc482ba9 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -3533,6 +3533,10 @@ interopTest("interop_withSpaces") { } } +task interop_convert(type: KonanLocalTest) { + source = "codegen/intrinsics/interop_convert.kt" +} + /* TODO: This test isn't run automatically task interop_echo_server(type: RunInteropKonanTest) { diff --git a/backend.native/tests/codegen/intrinsics/interop_convert.kt b/backend.native/tests/codegen/intrinsics/interop_convert.kt new file mode 100644 index 00000000000..df1259b761c --- /dev/null +++ b/backend.native/tests/codegen/intrinsics/interop_convert.kt @@ -0,0 +1,20 @@ +package codegen.intrinsics.interop_convert + +import kotlin.test.* +import kotlinx.cinterop.* + +fun convertIntToShortOrNull(i: Int, b: Boolean): Short? = if (b) i.convert() else null +fun narrowIntToShortOrNull(i: Int, b: Boolean): Short? = if (b) i.narrow() else null +fun signExtendShortToIntOrNull(i: Short, b: Boolean): Int? = if (b) i.signExtend() else null + +@Test +fun testNI() { + assertNull(convertIntToShortOrNull(0, false)) + assertEquals(1, convertIntToShortOrNull(1, true)) + + assertNull(narrowIntToShortOrNull(2, false)) + assertEquals(3, narrowIntToShortOrNull(3, true)) + + assertNull(signExtendShortToIntOrNull(4, false)) + assertEquals(5, signExtendShortToIntOrNull(5, true)) +} \ No newline at end of file