Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin/Native

stormcat24
October 19, 2017

 Kotlin/Native

2017/10/19 CA.kt #3

stormcat24

October 19, 2017
Tweet

More Decks by stormcat24

Other Decks in Programming

Transcript

  1. Kotlin/Native
    CA.kt #3 @stormcat24

    View Slide

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

    View Slide

  3. Ice Break?

    View Slide

  4. Written by @ngsw_taro
    Congratulations!

    View Slide

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

    View Slide

  6. Kotlin/Native

    View Slide

  7. What?

    View Slide

  8. View Slide

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

    View Slide

  10. “without any virtual machine.”
    (standalone executables)

    View Slide

  11. 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

    View Slide

  12. 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

    View Slide

  13. 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

    View Slide

  14. 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 {
    }
    }

    View Slide

  15. Native Library

    View Slide

  16. Today’s Example
    HTTP GET Request By using libcurl

    View Slide

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

    View Slide

  18. 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'
    }
    }

    View Slide

  19. 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")
    }
    }

    View Slide

  20. 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

    View Slide

  21. 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

    View Slide

  22. Interoperability

    View Slide

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

    View Slide

  24. 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…?

    View Slide

  25. 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
    }

    View Slide

  26. Use IDEA

    View Slide

  27. <- Uncheck Java

    View Slide

  28. Code Completion


    View Slide

  29. Impressions

    View Slide

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

    View Slide

  31. Thanks✋

    View Slide