From 31f27fb246e307d90635160a8f11c409df0cc155 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Thu, 16 Aug 2018 18:34:31 +0300 Subject: [PATCH] Improve zlib test and ImmutableBinaryBlob API --- backend.native/tests/interop/platform_zlib.kt | 27 ++++++++++--------- .../main/kotlin/kotlin/native/BinaryBlob.kt | 3 ++- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/backend.native/tests/interop/platform_zlib.kt b/backend.native/tests/interop/platform_zlib.kt index aeb91a05a25..07230bdf9a6 100644 --- a/backend.native/tests/interop/platform_zlib.kt +++ b/backend.native/tests/interop/platform_zlib.kt @@ -1,21 +1,22 @@ import kotlinx.cinterop.* + import platform.zlib.* -val source = immutableBinaryBlobOf(0xF3, 0x48, 0xCD, 0xC9, 0xC9, 0x57, 0x04, 0x00).asCPointer(0)!! -val golden = immutableBinaryBlobOf(0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x21, 0x00).asCPointer(0)!! +val source = immutableBinaryBlobOf(0xF3, 0x48, 0xCD, 0xC9, 0xC9, 0x57, 0x04, 0x00).asCPointer() +val golden = immutableBinaryBlobOf(0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x21, 0x00).asCPointer() fun main(args: Array) = memScoped { - val buffer = allocArray(32) - - val z = alloc().apply { - next_in = source - avail_in = 8 - next_out = buffer - avail_out = 32 - }.ptr - - if (inflateInit2(z, -15) == Z_OK && inflate(z, Z_FINISH) == Z_STREAM_END && inflateEnd(z) == Z_OK) - println(buffer.toKString()) + val buffer = ByteArray(32) + buffer.usePinned { pinned -> + val z = alloc().apply { + next_in = source + avail_in = 8 + next_out = pinned.addressOf(0) + avail_out = buffer.size + }.ptr + if (inflateInit2(z, -15) == Z_OK && inflate(z, Z_FINISH) == Z_STREAM_END && inflateEnd(z) == Z_OK) + println(buffer.stringFromUtf8()) + } println(golden.toKString()) } diff --git a/runtime/src/main/kotlin/kotlin/native/BinaryBlob.kt b/runtime/src/main/kotlin/kotlin/native/BinaryBlob.kt index b75dbd15f2c..0ce81a432f0 100644 --- a/runtime/src/main/kotlin/kotlin/native/BinaryBlob.kt +++ b/runtime/src/main/kotlin/kotlin/native/BinaryBlob.kt @@ -61,7 +61,8 @@ public fun ImmutableBinaryBlob.toByteArray() = toByteArray(0, size) // Returns stable C pointer to data at certain offset, useful as a way to pass resource // to C API. -public fun ImmutableBinaryBlob.asCPointer(offset: Int) = interpretCPointer(asCPointerImpl(offset)) +public fun ImmutableBinaryBlob.asCPointer(offset: Int = 0) = + interpretCPointer(asCPointerImpl(offset))!! @SymbolName("Kotlin_ImmutableBinaryBlob_asCPointerImpl") private external fun ImmutableBinaryBlob.asCPointerImpl(offset: Int): kotlin.native.internal.NativePtr