Oleg Yukhnevich
03/02/2023, 9:22 AMOleg Yukhnevich
03/02/2023, 9:22 AMOleg Yukhnevich
03/02/2023, 9:23 AMsvyatoslav.scherbina
03/08/2023, 3:11 PMkotlinx.cinterop inspired common APIWhile kotlinx.cinterop is a fine baseline in a sense (because it is known to be able to represent many of the required concepts, and the unsupported concepts are more or less known), it might make sense to start from scratch, just to avoid being affected by the current (arguable and opinionated) design and to properly take platform specifics into account.
PanamaPanama includes https://openjdk.org/jeps/424, which somewhat restricts operations with unsafe memory segments (see “Unsafe memory segments” and “Safety” sections). In particular, memory access operations on arbitrary
CPointer
can only be implemented using restricted FFM API.
Do you use FFM API? How do you overcome those restrictions?
• requires compiler to build JNI library - how to set up it properly to support different OSs?
• is it possible to use K/N provided clang with cross-compile possibilities here?(from https://github.com/whyoleg/ffi-kotlin#jvmandroidjni) You can start with
run_konan clang clang $target
from the Kotlin/Native distribution, e.g. this
~/.konan/kotlin-native-prebuilt-macos-x86_64-1.8.10/bin/run_konan clang clang linux_x64 -c 1.c
compiles 1.c
to 1.o
(and prints the whole clang
command as well). Doesn’t play nicely with a linker, but you can fix this by adding -fuse-ld=lld
compiler flag:
~/.konan/kotlin-native-prebuilt-macos-x86_64-1.8.10/bin/run_konan clang clang linux_x64 1.c -fuse-ld=lld
Oleg Yukhnevich
03/08/2023, 4:16 PMsvyatoslav.scherbina
03/08/2023, 5:41 PMcan you explain more about what’s wrong with cinterop API, and what would you change if something? Overall only working with strings and structs is somehow different per platforms, while everything else is super similar and fit well into cinterop apiOverall, cinterop was designed 1. with language limitations in mind, for example, lack of value types, implicit conversions etc. 2. driven by really peculiar examples, like POSIX. 3. with attempts on making simple examples shorter, without real scalability here; this also made it inconsistent to some extent. 4. targeting people who know well both C and Kotlin 5. with attempts to be able to represent C concepts as correctly as possible (to the degree where it became unnatural to both C and Kotlin) cinterop introduces really convoluted concepts (often to workaround 1, like CValuesRef). As a result, cinterop is pretty hard to use. It is not intuitive: with a specific goal in mind (like use a C library in Kotlin, having C examples for it), one might have a hard time figuring out how to achieve it. It cinterop doesn’t provide a way to write some of the bindings manually, which could have made things simpler (by allowing auto-generator to be imprecise or non-universal in some cases). I believe some of these things can be avoided even in a third-party project.
I use FFM API, but Im not sure of what restrictions specifically you are talking, can you give an example?Quote from the JEP:
The FFM API can also produce unsafe segments, that is, memory segments whose spatial and temporal bounds are user-provided and cannot be verified by the Java runtime (see).MemorySegment::ofAddress
The unsafe methods in the FFM API do not pose the same risks as JNI functions; they cannot, e.g., change the values offields in Java objects. On the other hand, the unsafe methods in the FFM API are easy to call from Java code. For this reason, the use of unsafe methods in the FFM API is restricted: Their use is permitted but, by default, every such use causes a warning to be issued at run time.final
CPointer
is basically an unsafe segment (e.g. when it is returned from a C function).
if using konan, is it possible to download it from gradle without adding native targets, f.e. if there is no konan on user PC, and ffi used only for jvm?I’m not aware of any stable easy approach for this.
Oleg Yukhnevich
03/08/2023, 7:20 PMsvyatoslav.scherbina
03/10/2023, 11:50 AMCan we chat/make c call sometime to discuss ideas on how it will be possible to improve ffi? Dart BTW has similar APIYes, sure. I’ve sent you a DM.
Manual bindings for multiplatform will be even harder, as there are much more things involvedA compiler plugin might help.
When CPointer returned from function, it can be exposed via Unbounded(Unsafe) segment,Exactly. And the JEP says that unsafe segments are restricted and trigger a warning.