Skip to content

Commit eddc946

Browse files
authored
update: minor c interop updates (#4893)
1 parent 3b31ce2 commit eddc946

File tree

4 files changed

+18
-17
lines changed

4 files changed

+18
-17
lines changed

docs/topics/native/mapping-function-pointers-from-c.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ As you can see, C function pointers are represented in Kotlin using `CPointer<CF
8888
takes an optional function pointer as a parameter, while `supply_fun()` returns a function pointer.
8989

9090
`CFunction<(Int) -> Int>` represents the function signature, and `CPointer<CFunction<...>>?` represents a nullable
91-
function pointer. There is an `invoke` operator extension function available for all `CPointer<CFunction<...>>` types,
91+
function pointer. There is an [`.invoke()`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlinx.cinterop/invoke.html)
92+
operator extension function available for all `CPointer<CFunction<...>>` types,
9293
allowing you to call function pointers as if they were regular Kotlin functions.
9394

9495
## Pass Kotlin function as a C function pointer

docs/topics/native/mapping-strings-from-c.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ fun passStringToC() {
154154
}
155155
```
156156

157-
Here, the `.toKString()` extension function converts a C string returned from the `return_string()` function
158-
into a Kotlin string.
157+
Here, the [`.toKString()`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlinx.cinterop/to-k-string.html) extension
158+
function converts a C string returned from the `return_string()` function into a Kotlin string.
159159

160160
Kotlin provides several extension functions for converting C `char *` strings into Kotlin strings,
161161
depending on the encoding:
@@ -195,9 +195,9 @@ fun sendString() {
195195
}
196196
```
197197

198-
Here, a native pointer is passed to the C function first. The `.usePinned` extension function temporarily
199-
pins the native memory address of the byte array. The C function fills in the byte array with data. Another extension
200-
function, `ByteArray.decodeToString()`, turns the byte array into a Kotlin string, assuming UTF-8 encoding.
198+
Here, a native pointer is passed to the C function first. The [`.usePinned()`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlinx.cinterop/use-pinned.html)
199+
extension function temporarily pins the native memory address of the byte array. The C function fills in the byte array with data.
200+
Another extension function, `ByteArray.decodeToString()`, turns the byte array into a Kotlin string, assuming UTF-8 encoding.
201201

202202
## Update Kotlin code
203203

docs/topics/native/native-app-with-c-and-libcurl.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ Kotlin/Native can use the [Gradle](https://gradle.org) build system through the
6060

6161
* Targets are defined using `macosArm64`, `macosX64`, `linuxArm64`, `linuxX64`, and `mingwX64` for macOS, Linux,
6262
and Windows. See the complete list of [supported platforms](native-target-support.md).
63-
* The entry itself defines a series of properties to indicate how the binary is generated and the entry
64-
point of the applications. These can be left as default values.
63+
* The `binaries {}` block defines how the binary is generated and the entry point of the application.
64+
These can be left as default values.
6565
* C interoperability is configured as an additional step in the build. By default, all the symbols from C are
6666
imported to the `interop` package. You may want to import the whole package in `.kt` files. Learn more about
6767
[how to configure](gradle-configure-project.md#targeting-multiple-platforms) it.
@@ -100,7 +100,7 @@ In this app, you'll need the libcurl library to make some HTTP calls. To create
100100
linkerOpts.linux = -L/usr/lib/x86_64-linux-gnu -lcurl
101101
```
102102
103-
* `headers` is the list of header files to generate Kotlin stubs for. You can add multiple files to this entry,
103+
* `headers` is the list of header files to generate Kotlin stubs for. You can add multiple files here,
104104
separating each with a space. In this case, it's only `curl.h`. The referenced files need to be available
105105
on the specified path (in this case, it's `/usr/include/curl`).
106106
* `headerFilter` shows what exactly is included. In C, all the headers are also included when one file references
@@ -127,7 +127,7 @@ In this app, you'll need the libcurl library to make some HTTP calls. To create
127127
## Add interoperability to the build process
128128

129129
To use header files, make sure they are generated as a part of the build process. For this, add the following
130-
entry to the `build.gradle.kts` file:
130+
`compilations {}` block to the `build.gradle.kts` file:
131131

132132
```kotlin
133133
nativeTarget.apply {

docs/topics/native/native-c-interop.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
This document covers general aspects of Kotlin's interoperability with C. Kotlin/Native comes with a cinterop tool,
1313
which you can use to quickly generate everything you need to interact with an external C library.
1414

15-
The tool analyzes C headers and produces a straightforward mapping of C types, functions, and constants into Kotlin.
15+
The tool analyzes C headers and produces a straightforward mapping of C types, functions, and strings into Kotlin.
1616
The generated stubs then can be imported into an IDE to enable code completion and navigation.
1717

1818
> Kotlin also provides interoperability with Objective-C. Objective-C libraries are imported through the cinterop tool
@@ -401,7 +401,7 @@ and pointers to such objects' inner data could be passed to C functions.
401401

402402
There's a couple of approaches you can take:
403403

404-
* Use the [`usePinned`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlinx.cinterop/use-pinned.html) service function
404+
* Use the [`.usePinned()`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlinx.cinterop/use-pinned.html) extension function
405405
that pins an object, executes a block, and unpins it on normal and exception paths:
406406

407407
```kotlin
@@ -423,10 +423,10 @@ There's a couple of approaches you can take:
423423
}
424424
```
425425

426-
Here, `pinned` is an object of a special type `Pinned<T>`. It provides useful extensions like `addressOf`, which allows
427-
getting the address of a pinned array body.
426+
Here, `pinned` is an object of a special type `Pinned<T>`. It provides useful extensions like [`.addressOf()`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlinx.cinterop/address-of.html),
427+
which allows getting the address of a pinned array body.
428428

429-
* Use the [`refTo()`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlinx.cinterop/ref-to.html) function that has
429+
* Use the [`.refTo()`](https://kotlinlang.org/api/core/kotlin-stdlib/kotlinx.cinterop/ref-to.html) extension function that has
430430
similar functionality under the hood but, in certain cases, may help you reduce boilerplate code:
431431

432432
```kotlin
@@ -498,9 +498,9 @@ fun test() {
498498

499499
## What's next
500500

501-
Learn how types, functions, and constants are mapped between Kotlin and C by completing the following tutorials:
501+
Learn how types, functions, and strings are mapped between Kotlin and C by completing the following tutorials:
502502

503503
* [Mapping primitive data types from C](mapping-primitive-data-types-from-c.md)
504-
* [Mapping struct and union types from C](mapping-function-pointers-from-c.md)
504+
* [Mapping struct and union types from C](mapping-struct-union-types-from-c.md)
505505
* [Mapping function pointers from C](mapping-function-pointers-from-c.md)
506506
* [Mapping strings from C](mapping-strings-from-c.md)

0 commit comments

Comments
 (0)