From 563f38c312f425dc8fb71ddb799d3a98998b67cd Mon Sep 17 00:00:00 2001 From: "dima.avdeev" Date: Fri, 10 Nov 2023 21:37:06 +0400 Subject: [PATCH 1/4] Fix problem with conversion of Kotlin String to C char * --- skiko/RELEASE.md | 1 + .../org/jetbrains/skia/impl/Native.native.kt | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/skiko/RELEASE.md b/skiko/RELEASE.md index 91be26906..60d522e11 100644 --- a/skiko/RELEASE.md +++ b/skiko/RELEASE.md @@ -21,6 +21,7 @@ build configuration. ```bash ./gradlew publishToMavenLocal -Pskiko.native.enabled=true -Pskiko.wasm.enabled=true -Pskiko.android.enabled=true ``` +Use flag `-Pskiko.debug=true` to build with debug build type. ##### Publish to `build/repo` directory ```bash diff --git a/skiko/src/nativeMain/kotlin/org/jetbrains/skia/impl/Native.native.kt b/skiko/src/nativeMain/kotlin/org/jetbrains/skia/impl/Native.native.kt index 859061532..da32c070c 100644 --- a/skiko/src/nativeMain/kotlin/org/jetbrains/skia/impl/Native.native.kt +++ b/skiko/src/nativeMain/kotlin/org/jetbrains/skia/impl/Native.native.kt @@ -66,10 +66,7 @@ internal actual inline fun interopScope(block: InteropScope.() -> T): T { internal actual class InteropScope actual constructor() { actual fun toInterop(string: String?): InteropPointer { return if (string != null) { - // encodeToByteArray encodes to utf8 - val utf8 = string.encodeToByteArray() - // TODO Remove array copy, use `skString(data, length)` instead of `skString(data)` - val pinned = utf8.copyOf(utf8.size + 1).pin() + val pinned = convertToZeroTerminatedString(string).pin() elements.add(pinned) val result = pinned.addressOf(0).rawValue result @@ -186,7 +183,7 @@ internal actual class InteropScope actual constructor() { if (stringArray == null || stringArray.isEmpty()) return NativePtr.NULL val pins = stringArray.toList() - .map { it.encodeToByteArray().pin() } + .map { convertToZeroTerminatedString(it).pin() } val nativePointerArray = NativePointerArray(stringArray.size) pins.forEachIndexed { index, pin -> @@ -284,3 +281,13 @@ private external fun initCallbacks( callVoid: COpaquePointer, dispose: COpaquePointer ) + +/** + * C++ needs char* with zero byte at the end. So, we need to increase original string and write 0 at end. + */ +private fun convertToZeroTerminatedString(string: String): ByteArray { + // encodeToByteArray encodes to utf8 + val utf8 = string.encodeToByteArray() + // TODO Remove array copy, use `skString(data, length)` instead of `skString(data)` + return utf8.copyOf(utf8.size + 1) +} From ec684990fe6e8ff67681c556494e8481d399fe90 Mon Sep 17 00:00:00 2001 From: "dima.avdeev" Date: Fri, 10 Nov 2023 21:58:37 +0400 Subject: [PATCH 2/4] remove unnecessary convertToZeroTerminatedString function comment --- .../nativeMain/kotlin/org/jetbrains/skia/impl/Native.native.kt | 3 --- 1 file changed, 3 deletions(-) diff --git a/skiko/src/nativeMain/kotlin/org/jetbrains/skia/impl/Native.native.kt b/skiko/src/nativeMain/kotlin/org/jetbrains/skia/impl/Native.native.kt index da32c070c..af8fa53e4 100644 --- a/skiko/src/nativeMain/kotlin/org/jetbrains/skia/impl/Native.native.kt +++ b/skiko/src/nativeMain/kotlin/org/jetbrains/skia/impl/Native.native.kt @@ -282,9 +282,6 @@ private external fun initCallbacks( dispose: COpaquePointer ) -/** - * C++ needs char* with zero byte at the end. So, we need to increase original string and write 0 at end. - */ private fun convertToZeroTerminatedString(string: String): ByteArray { // encodeToByteArray encodes to utf8 val utf8 = string.encodeToByteArray() From 1bc53bb00c1a510bc7086191fdd5119575ac5a32 Mon Sep 17 00:00:00 2001 From: "dima.avdeev" Date: Fri, 10 Nov 2023 22:10:48 +0400 Subject: [PATCH 3/4] added comment back --- .../nativeMain/kotlin/org/jetbrains/skia/impl/Native.native.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/skiko/src/nativeMain/kotlin/org/jetbrains/skia/impl/Native.native.kt b/skiko/src/nativeMain/kotlin/org/jetbrains/skia/impl/Native.native.kt index af8fa53e4..da32c070c 100644 --- a/skiko/src/nativeMain/kotlin/org/jetbrains/skia/impl/Native.native.kt +++ b/skiko/src/nativeMain/kotlin/org/jetbrains/skia/impl/Native.native.kt @@ -282,6 +282,9 @@ private external fun initCallbacks( dispose: COpaquePointer ) +/** + * C++ needs char* with zero byte at the end. So, we need to increase original string and write 0 at end. + */ private fun convertToZeroTerminatedString(string: String): ByteArray { // encodeToByteArray encodes to utf8 val utf8 = string.encodeToByteArray() From d91ead0924d72afd6e642c6eacebd847065bde04 Mon Sep 17 00:00:00 2001 From: "dima.avdeev" Date: Fri, 10 Nov 2023 22:22:57 +0400 Subject: [PATCH 4/4] A little bit change a comment --- .../kotlin/org/jetbrains/skia/impl/Native.native.kt | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/skiko/src/nativeMain/kotlin/org/jetbrains/skia/impl/Native.native.kt b/skiko/src/nativeMain/kotlin/org/jetbrains/skia/impl/Native.native.kt index da32c070c..ffd54ae11 100644 --- a/skiko/src/nativeMain/kotlin/org/jetbrains/skia/impl/Native.native.kt +++ b/skiko/src/nativeMain/kotlin/org/jetbrains/skia/impl/Native.native.kt @@ -283,11 +283,12 @@ private external fun initCallbacks( ) /** - * C++ needs char* with zero byte at the end. So, we need to increase original string and write 0 at end. + * Converts String to zero-terminated utf-8 byte array. */ private fun convertToZeroTerminatedString(string: String): ByteArray { - // encodeToByteArray encodes to utf8 - val utf8 = string.encodeToByteArray() + // C++ needs char* with zero byte at the end. So we need to copy array with an extra zero byte. + + val utf8 = string.encodeToByteArray() // encodeToByteArray encodes to utf8 // TODO Remove array copy, use `skString(data, length)` instead of `skString(data)` return utf8.copyOf(utf8.size + 1) }