Interoperability between kotlin-native and kotlin-jvm


#1

In one of my current projects I have to implement two-sided interaction between JVM and native code. Basically, I have to use JNI or JNA and it requires a lot of work. Now, we have Kotlin native which have some great tools for interop with native code, so it would seem reasonable to create a kotlin-native bridge which will work as a wrapper for native library. The problem is that there seem to be no way to directly interface kotlin-native code with kotlin-jvm without explicit use of JNI. Such interface probably could be generated automatically since both kotlin-native and kotlin-jvm share the same type system. Also it would be good to have such mechanism for multi-platform support, since the situation when part of the code is native and part of the code is JVM is quite frequent.

Are there any plans to create such an interface?


#2

Try cinterop -flavor=jvm


#3

Sorry, but I do not understand, what should I try. I have not found any documentation for the option you mentioned.

While searching, I found another similar question on this forum.


#4

There is no documentation - it used internally in K/N compiler for building stubs to interact with LLVM.
Compiler is in Kotlin/JVM, LLVM is native. Exactly what you want, if I understood correctly.


#5

Indeed. But it will be very hard to do anything without at least a bit documentation. I will try to look through the sources. Thanks for suggestion.


#6

Any news here? I have tried to modify the compiler to pass the flavor jvm in but I can’t make any progress…


#7

What’s about using kotlin-native just to optimize Kotlin execution speed?
This would mean that the kotlin-native code still uses the kotlin-jvm stdlib.


#8

At this time (and probably for quite a while into the future), HotSpot is far better at optimizing Kotlin execution speed than Kotlin/Native. Also, Kotlin/Native does not support compiling Java bytecode to native code, which would be necessary to use the kotlin-jvm stdlib.


#9

@yole I meant that just the Kotlin code is compiled and then loaded via JNI into a standard VM.
It would still use the Java standard library of that VM.


#10

Calling all JVM APIs through JNI is definitely going to be much, much, much slower than running Kotlin-compiled code under HotSpot (which can inline calls to JVM APIs and perform lots and lots of optimizations on the resulting code).


#11

The problem is not the speed, at least my problem is not about it, but interoperability with native libraries. Usually, connecting native libraries to java requires a lot of painful work with something like SWIG or JNA. Direct interface with JNI is even harder.
Kotlin native seems to provide solution since it allows to create easy interface with native libraries and then creating automatic interop between kotlin native and kotlin jvm seems much easier since the language model is the same.
I do not know about other fields, but for scientific and engineering applications, it would be tremendous achievement. It would allow to easily write top level applications in Kotlin/JVM and forget about C++.


#12

Dear all,

I’m looking into the same problem as @darksnake - binding a C library to Kotlin on the JVM.
So far, I was going to delve into JNI or SWIG, but the JVM version of K/N Interop seems more attractive.
As @msink has mentioned this is used inside K/N, but there is no documentation (I just saw this project use it https://github.com/msink/kotlin-ui and give some indications)…

My question: is this something the K/N developers just needed for themselves and want to keep hidden, or will it be officially supported one day as part of K/N?

Best regards,
Vito