Skip to content

Commit

Permalink
optin to experimentalforeignapi
Browse files Browse the repository at this point in the history
  • Loading branch information
crc-32 committed May 24, 2024
1 parent 47d62fd commit e9840c5
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/iosMain/kotlin/util/UtilFunctions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import platform.posix.memcpy
import platform.posix.size_t

actual fun runBlocking(block: suspend () -> Unit) = kotlinx.coroutines.runBlocking{block()}

@OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
internal fun isPlatformBigEndian(): Boolean {
memScoped {
val i = alloc<IntVar>()
Expand All @@ -22,10 +22,12 @@ internal fun reverseOrd(varr: UInt): UInt = ((reverseOrd((varr and 0xffffu).toUS

internal fun reverseOrd(varr: ULong): ULong = ((reverseOrd((varr and 0xffffffffu).toUInt()).toLong() shl 32) or (reverseOrd((varr shr 32).toUInt()).toLong() and 0xffffffff)).toULong()

@OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
fun ByteArray.toNative(): NSData = memScoped {
NSData.create(bytes = allocArrayOf(this@toNative), length = castToNativeSize(this@toNative.size))
}

@OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
fun KUtil.byteArrayFromNative(arr: NSData): ByteArray = ByteArray(arr.length.toInt()).apply {
if (this.isNotEmpty()) {
usePinned {
Expand All @@ -34,6 +36,7 @@ fun KUtil.byteArrayFromNative(arr: NSData): ByteArray = ByteArray(arr.length.toI
}
}

@OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
internal fun castToNativeSize(v: Int): size_t {
@Suppress("CAST_NEVER_SUCCEEDS", "USELESS_CAST") // Depending on the platform different side of if will trigger, lets ignore
return if (size_t.SIZE_BITS == 32) {
Expand Down

0 comments on commit e9840c5

Please sign in to comment.