Slide 1

Slide 1 text

Kotlin/Native CA.kt #3 @stormcat24

Slide 2

Slide 2 text

stormcat24 ‣ CyberAgent, Inc. ‣ FRESH! https://freshlive.tv ‣ https://blog.stormcat.io ‣ Docker Comedian

Slide 3

Slide 3 text

Ice Break?

Slide 4

Slide 4 text

Written by @ngsw_taro Congratulations!

Slide 5

Slide 5 text

I reviewed 2nd, 3rd parts. ‣SparkFramework with Kotlin ‣Spring Boot with Kotlin

Slide 6

Slide 6 text

Kotlin/Native

Slide 7

Slide 7 text

What?

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

“Kotlin/Native that compiles Kotlin directly to machine.”

Slide 10

Slide 10 text

“without any virtual machine.” (standalone executables)

Slide 11

Slide 11 text

Kotlin/Native is ‣Compiles directly to native code via LLVM ‣Call native libraries ‣Cross Platform ‣Windows/Linux/macOS/iOS/Android/Raspberry Pi ‣Version 0.3

Slide 12

Slide 12 text

Setup $ git clone [email protected]:JetBrains/kotlin-native.git $ cd kotlin-native (kotlin-native)$ git checkout v0.3.4 Get Kotlin/Native Build (kotlin-native)$ ./gradlew dependencies:update (kotlin-native)$ ./gradlew dist (kotlin-native)$ ./gradlew cross_dist Path $ export PATH=$PATH:path-to-path/kotlin-native/dist/bin

Slide 13

Slide 13 text

Try Quickly fun main(args: Array) { // Not System.out.println() println(“Hello! Kotlin/Native“) } hello.kt compile $ kotlinc hello.kt -o hello KtFile: hello.kt Execute $ ./hello.kexe Hello! Kotlin/Native

Slide 14

Slide 14 text

build.gradle buildscript { repositories { mavenCentral() maven { url "https://dl.bintray.com/jetbrains/kotlin-native-dependencies" } } dependencies { classpath "org.jetbrains.kotlin:kotlin-native-gradle-plugin:0.3.3" } } apply plugin: 'konan' konanArtifacts { KonanExample { } }

Slide 15

Slide 15 text

Native Library

Slide 16

Slide 16 text

Today’s Example HTTP GET Request By using libcurl

Slide 17

Slide 17 text

Create *.def file ‣Library definition file ‣Link Kotlin/Native to Native libraries ‣Generate interoperability stubs ‣Define in build.gradle

Slide 18

Slide 18 text

Define *.def file headers = curl/curl.h compilerOpts=-I/usr/local/include linkerOpts.osx = -L/opt/local/lib -L/usr/local/opt/curl/lib -lcurl src/main/c_interop/libcurl.def build.gradle apply plugin: 'konan' konanInterop { Libcurl { includeDirs '/usr/local/opt/curl/include', '.' } } konanArtifacts { KonanExample { useInterop 'libcurl' } }

Slide 19

Slide 19 text

Implementation curl.kt import kotlinx.cinterop.* import libcurl.* fun main(args: Array): Unit { if (args.size == 0) { return } val url = args.first() val curl = curl_easy_init() curl_easy_setopt(curl, CURLOPT_URL, url) val res = curl_easy_perform(curl) when (res) { CURLE_OK -> println("HTTP Request OK") else -> println("HTTP Request NG") } }

Slide 20

Slide 20 text

Call Library In C #include CURLcode curl_easy_perform(CURL * easy_handle); fun curl_easy_perform(curl: COpaquePointer?): CURLcode { return kniBridge40(curl.rawValue) } typealias CURLcode = kotlin.Int val CURLE_OK: CURLcode = 0 val CURLE_UNSUPPORTED_PROTOCOL: CURLcode = 1 val CURLE_FAILED_INIT: CURLcode = 2 … Stub in Kotlin/Native

Slide 21

Slide 21 text

Execute Build Execute $ ./gradlew build $ ls build/konan/bin Curl.kexe Curl.kt.bc $ build/konan/bin/Curl.kexe https://blog.stormcat.io (...) HTTP Request OK

Slide 22

Slide 22 text

Interoperability

Slide 23

Slide 23 text

Interop types ‣Int => kotlin.Int ‣T* => CPointer ‣void* => COpaquePointer? ‣

Slide 24

Slide 24 text

Memory Management ‣Automated memory management by Cycle Collector ‣https://github.com/JetBrains/kotlin-native/blob/master/ runtime/src/main/cpp/Memory.cpp ‣When using Native Library…?

Slide 25

Slide 25 text

Memory allocation Allocate memory val buffer = nativeHeap.allocArray(size) ... nativeHeap.free(buffer) memScoped val fileSize = memScoped { val statBuf = alloc() val error = stat("/", statBuf.ptr) statBuf.st_size }

Slide 26

Slide 26 text

Use IDEA

Slide 27

Slide 27 text

<- Uncheck Java

Slide 28

Slide 28 text

Code Completion

Slide 29

Slide 29 text

Impressions

Slide 30

Slide 30 text

Impressions ‣Very experimental and challenging ‣Interoperability is difficult ‣Lack of Ecosystem, fundamental libraries ‣Not enough code completion ‣If gRPC support Kotlin/Native?

Slide 31

Slide 31 text

Thanks✋