days
1
4
hours
1
8
minutes
5
2
seconds
0
2
search
Christmas edition

Kotlin/Native 0.5 adds support for calling Kotlin code from Swift

Gabriela Motroc
Kotlin/Native

© Shutterstock / larryrains

Santa came early this year. Kotlin/Native 0.5 is here — if support for calling Kotlin code from Swift and Objective-C was on your wish list, consider it granted.

JetBrains recently released Kotlin/Native v0.5, Christmas edition. There’s support for using Kotlin/Native code from C, Objective-C APIs and Swift.

Version 0.5 also supports development using iOS simulator, along with LLVM 5 support and creating WebAssembly from Linux and Windows hosts.

Kotlin/Native 0.5 highlights:

Support for calling Kotlin code from Swift and Objective-C

JetBrains previously introduced calling Apple frameworks from Kotlin/Native, assuming they provide Objective-C headers. Now it’s time to go the other way around and adds support for calling Kotlin code from Swift and Objective-C.

According to the blog post announcing the new version, a new compiler option, -produce framework, has been implemented in order to turn this idea into reality. This compiler option generates a self-contained framework which can be used from other parts of your application as if it was written in Swift.

SEE ALSO: Kotlin/Native IDE support: CLion 2017.3 EAP now available for download

Reverse interoperability for other platforms

Kotlin/Native code can also be called from the outside world. Since the lowest common denominator of modern languages is C, the team decided that this is going to be the language they 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. They went one step further and demonstrated this interoperability by creating Python extension. Python calls to C implementations and they call Kotlin implementation like this:

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. In short, Python launcher code calls Kotlin/Native objects via C bridge and properly converts both object and primitive types.

Kotlin/Native 0.5’s goody bag also contains LLVM 5.0.0 support and improved Workers API.

Read the blog post announcing Kotlin/Native v0.5 here.  

Author
Gabriela Motroc
Gabriela Motroc is editor of JAXenter.com and JAX Magazine. Before working at Software & Support Media Group, she studied International Communication Management at the Hague University of Applied Sciences.

Leave a Reply

Be the First to Comment!

avatar
400
  Subscribe  
Notify of