Kotlin/Native v0.5 released: calling Kotlin from Swift and C, LLVM 5 and more

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:

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.

Screen Shot 2017-12-18 at 18.40.21

And Kotlin code in IntelliJ IDEA:

Screen Shot 2017-12-18 at 19.44.38

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:

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 in kotlin.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).

About Nikolay Igotti

Kotlin/Native Tech Lead
This entry was posted in iOS, Native, Releases and tagged . Bookmark the permalink.

5 Responses to Kotlin/Native v0.5 released: calling Kotlin from Swift and C, LLVM 5 and more

  1. Amr Eniou says:

    I wonder if that’s mean kotlin native could now make high-performance cross apps than react native?

  2. Bruno says:

    Hey, does it support passing a lambda/closure from Swift to Kotlin and vice-versa? Thanks

  3. kotlinikov says:

    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?

Leave a Reply

Your email address will not be published. Required fields are marked *