Giter Site home page Giter Site logo

89jd / kotlin-native-suspend-function-callback Goto Github PK

View Code? Open in Web Editor NEW

This project forked from feilfeilundfeil/kotlin-native-suspend-function-callback

0.0 0.0 0.0 4.51 MB

Kotlin Multiplatform compiler plugin to generate a callback implementation for suspended functions so they are visible from Kotlin Native

License: Apache License 2.0

Kotlin 100.00%

kotlin-native-suspend-function-callback's Introduction

Native Suspended Functions - Generates a wrapper with a callback for suspended functions for Kotlin Native to consume

Kotlin License Download PRs Welcome Tweet

Introduction

As suspended functions aren't visible from Kotlin Native I created this plugin that adds support for annotating existing functions with @NativeSuspendedFunction. The plugin will find these methods and generate a Kotlin source code file that uses callbacks that can be used from Kotlin Native.

Additionally you may use @NativeFlowFunction to generate a wrapper around a function that returns a Flow - something that Kotlin Native also cannot handle right now.

This plugin uses mpapt-runtime from Jens Klingenberg: https://github.com/Foso/MpApt

Example

Class with annotated suspended function:

class CommonAnnotated {

    @NativeSuspendedFunction
    internal suspend fun firstFunction2(id: Datum, type: Double?): Int {
        return 0
    }
    
    @ExperimentalCoroutinesApi
    @NativeFlowFunction
    fun subscribeToMowerChanges(test: Int): Flow<CoroutineScope> {
        return callbackFlow {
            offer(GlobalScope)
        }
    }

}

Generated extension:

internal fun CommonAnnotated.firstFunction2(
    id: Datum,
    type: Double?,
    callback: (SuspendResult<Int>) -> Unit
) = mainScope.launch {
    callback(suspendRunCatching<Int> { firstFunction2(id, type) })
}

@ExperimentalCoroutinesApi
fun CommonAnnotated.testFlowFunction(test: Int, callback: (CoroutineScope) -> Unit) =
    GlobalScope.launch {
        testFlowFunction(test).collect {
            callback(it)
        }
    }

The class SuspendResult is an implementation of https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-result/index.html that was made compatible with Kotlin Native (no inline functions etc.)

Currently the plugin only supports:

  • copying over annotations
  • parameters + their nullability (no default values)

Please check other limitations on https://github.com/Foso/MpApt

Feel free to open tickets for new features.

Usage

Inside your projects build.gradle(.kts) add the following plugin:

repositories {
    jcenter()
}

plugins {
   id("native-suspend-function") version "1.0.18"
    // or as dependency: "de.ffuf.kotlin.multiplatform.processor:nativesuspendfunction:1.0.18"
}

// also add the annotations as dependency:
dependencies {
    // multiplatform binaries are ready - please have a look at the example project
    implementation("de.ffuf.kotlin.multiplatform.annotations:annotations:1.0.18")
}

and in your pluginManagement:

pluginManagement {
    repositories {
        // ....
        jcenter()
    }
    // ....
    resolutionStrategy {
        eachPlugin {
            // ....
            if (requested.id.id == "native-suspend-function") {
                useModule("de.ffuf.kotlin.multiplatform.processor:nativesuspendfunction:${requested.version}")
            }
        }
    }
}

Configuration

The compiler plugin unfortunately won't be able to resolve all needed imports but you may add them in the configuration:

nativeSuspendExtension {
    outputDirectory = "src/commonMain/kotlin" // this is the default output directory for the generated extension file (without package)
    scopeName = "mainScope" // in our case we use a CoroutineScope called "mainScope" - don#t forget to import that location 
    imports = listOf("test.import.package") // additional imports for the generated file
    packageName = "de.ffuf.kotlin.extensions" //allows configuring the package name of the generated file
}

Release notes

For unknown reasons, uploading the kotlin-plugin doesn't upload the buildSrc package as well. So, publish it to mavenLocal first, then manually upload those files to: /de/ffuf/kotlin/multiplatform/processor/nativesuspendfunction/1.0.**

License

This project is licensed under the Apache License, Version 2.0 - see the LICENSE.md file for details


Copyright 2019 FFUF

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

kotlin-native-suspend-function-callback's People

Contributors

foso avatar jonasbark avatar ronntor avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.