Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix problem with conversion of Kotlin String to C char * #827

Merged
merged 4 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions skiko/RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ internal actual inline fun <T> 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
Expand Down Expand Up @@ -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 ->
Expand Down Expand Up @@ -284,3 +281,14 @@ private external fun initCallbacks(
callVoid: COpaquePointer,
dispose: COpaquePointer
)

/**
* Converts String to zero-terminated utf-8 byte array.
*/
private fun convertToZeroTerminatedString(string: String): ByteArray {
// 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)`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there still no way to avoid double-copy in K/N?

Copy link
Contributor Author

@dima-avdeev-jb dima-avdeev-jb Nov 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do it, with this TODO comment. But for now it is safe to stay double copy

// TODO Remove array copy, use skString(data, length) instead of skString(data)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An unfortunately there is no way to encode to preallocated byte array.

return utf8.copyOf(utf8.size + 1)
}
Loading