Weāre happy to announce the release of Kotlin/Native v0.5, Christmas edition! This release adds support for using Kotlin/Native code from C, Objective-C APIs and Swift, supports development using iOS simulator, along with LLVM 5 support and creating WebAssembly from Linux and Windows hosts.
Reverse interop from Objective-C and Swift
In the previous release of Kotlin/Native we introduced calling Apple frameworks from Kotlin/Native, assuming they provide Objective-C headers. Now we go another way around, and add support for calling Kotlin code from Swift and Objective-C. For that, a new compiler option, -produce framework
, has been implemented. It generates a self-contained framework, which could be used from other parts of your application, as if it was written in Swift. Letās take a look at the calculator example. It has UI written in Swift along with calculator logic written in Kotlin. Swift code intermixes with Kotlin transparently. For example, this line of Swift code:
1 |
private let parser = KAPPartialParser(composer: KAPCalculator(), partialComposer: PartialRenderer()) |
creates an instance of the Kotlin class PartialParser, and gives it an instance of a Swift class PartialRenderer implementing a Kotlin interface ExpressionComposer.
Note, that basic types like numbers and strings are transparently mapped between Swift and Kotlin worlds.
To build the project, just open it in XCode and compile it for either a real device or the simulator, see the README.md for details.
And Kotlin code in IntelliJ IDEA:
Reverse interop from C
For other platforms we also support reverse interoperability, allowing to call Kotlin/Native code from the outside world. The lowest common denominator of modern languages is C, so this was the language we interoperate with. Compiler option -produce dynamic
produces a dynamic library (such as .dylib
on macOS, .so
on Linux and .dll
on Windows) containing everything needed to work with Kotlin/Native code. To make things fancy we decided to demonstrate this interoperability by creating Python extension. Python calls to C implementations and they call Kotlin implementation like this:
1 2 3 4 5 6 7 8 9 |
if (PyArg_ParseTuple(args, "Lss", &session_arg, &string_arg1, &string_arg2)) { T_(Server) server = getServer(); T_(Session) session = { (void*)(uintptr_t)session_arg }; const char* string = __ kotlin.demo.Server.concat(server, session, string_arg1, string_arg2); result = Py_BuildValue("s", string); __ DisposeString(string); } else { result = Py_BuildValue("s", NULL); } |
The Kotlin/Native compiler produces a dynamic library, and then Python distutils build tool produces another dynamic library, depending on that one. So Python launcher code calls Kotlin/Native objects via C bridge and gets both object and primitive types properly converted.
Other improvements
- In Kotlin 1.2, the
kotlin.math
package was added to the Kotlin standard library. With v0.5 Kotlin/Native supports operations available inkotlin.math
package as well - LLVM 5.0.0 is supported with this release, as both clang toolchain and bitcode codegenerator and optimizer
- Code for WebAssembly targets (
-target wasm32
) now can be produced from Linux and Windows hosts (before only macOS hosts were supported) - Workers API was improved by allowing easier consumption of worker execution result and adding ability to pass primitive values from and to worker
- Bugfixes and improvements
Getting the bits
Binaries could be downloaded below:
Feedback
Please report bugs and issues in the Kotlin bug tracker. Questions are welcome on #kotlin-native channel on Slack (get invite here).
I wonder if thatās mean kotlin native could now make high-performance cross apps than react native?
You may try and see, how well it behaves for your usecases.
Hey, does it support passing a lambda/closure from Swift to Kotlin and vice-versa? Thanks
Yes, see https://github.com/JetBrains/kotlin-native/blob/6ee6f5907d808f21af06436e6c44b6fe2b9bb6b5/backend.native/tests/interop/objc/smoke.kt#L13 for example.
tl;dr How do you deal with kotlin native code being called from different platform threads?
I did read in the comments of v0.4 release that object heaps are not shared between threads and explicit ownership has to be transferred. Now in v0.5 the calculator shows a framework implemented in Kotlin called from objc/swift, but there are no thread jumps involved. In the past Iāve tried to use a language with similar memory characteristics unsuccessfully in the following scenario:
Main UI thread calls framework method to initialise some globals. As such, these globals are attached to the UI thread.
User clicks a button to perform a long operation. iOS side shows a HUD, switches to a background thread and calls a framework method to do calculations. This method requires modifying previously initialised globals.
In this scenario, the global state initialised early is not accessible from the different background thread and the language crashed. How does kotlin/native work in this situation? Will the background thread crash in the kotlin code presuming to access those early globals/state?