Maven Central Gradle Plugin Portal
English | ็ฎไฝไธญๆ
This is a Kotlin compiler plugin for generating platform-compatible functions for suspend functions.
class Foo { @JvmBlocking @JvmAsync suspend fun waitAndGet(): String { delay(5) return "Hello" } }
compiled ๐
class Foo { // Hide from Java @JvmSynthetic suspend fun waitAndGet(): String { delay(5) return "Hello" } @Api4J // RequiresOptIn annotation, provide warnings to Kotlin fun waitAndGetBlocking(): String = runInBlocking { waitAndGet() } // 'runInBlocking' from the runtime provided by the plugin @Api4J // RequiresOptIn annotation, provide warnings to Kotlin fun waitAndGetAsync(): CompletableFuture<out String> = runInAsync { waitAndGet() } // 'runInAsync' from the runtime provided by the plugin }
class Foo { @JsPromise suspend fun waitAndGet(): String { delay(5) return "Hello" } }
compiled ๐
class Foo { suspend fun waitAndGet(): String { delay(5) return "Hello" } @Api4Js // RequiresOptIn annotation, provide warnings to Kotlin fun waitAndGetAsync(): Promise<String> = runInAsync { waitAndGet() } // 'runInAsync' from the runtime provided by the plugin }
(ๅ้ค) JS platform target not supported yet. see: KT-53993 (ๅ้คใใใพใง)JS has been supported since 0.6.0! See the process at KT-53993, and the final winning shot at #39!
Warning
Since v0.6.0, In experiments, immature and unstable
class Foo { @JsPromise suspend fun waitAndGet(): String { delay(5) return "Hello" } } // Some functions or types customised by **you**... // They are not included in the runtime. // Since there are a lot of restrictions on the use of various types in WasmJS... // so I'm not sure how to handle them perfectly yet. // Until then, you can customise functions and types to control the behaviour of the compiler plugin yourself. // just like you can customise other platforms. fun <T> runInAsync(block: suspend () -> T): AsyncResult<T> = AsyncResult(block) class AsyncResult<T>(val block: suspend () -> T) { @OptIn(DelicateCoroutinesApi::class) fun toPromise(): Promise<JsAny?> { return GlobalScope.promise { block() } } }
compiled ๐
class Foo { suspend fun waitAndGet(): String { delay(5) return "Hello" } @Api4Js // RequiresOptIn annotation, provide warnings to Kotlin fun waitAndGetAsync(): AsyncResult<String> = runInAsync { waitAndGet() } // 'runInAsync' from the runtime provided by the plugin // AsyncResult is a custom type by **you** }
since v0.13.0, #96
You can use markName to add a name mark annotation (e.g. @JvmName, @JsName) to the generated synthetic function.
For example the JVM:
class Foo { @JvmBlocking(markName = "namedWaitAndGet") suspend fun waitAndGet(): String { delay(5) return "Hello" } }
compiled ๐
class Foo { // Hide from Java @JvmSynthetic suspend fun waitAndGet(): String { delay(5) return "Hello" } @Api4J // RequiresOptIn annotation, provide warnings to Kotlin @JvmName("namedWaitAndGet") // From the `markName`'s value fun waitAndGetBlocking(): String = runInBlocking { waitAndGet() } // 'runInBlocking' from the runtime provided by the plugin }
Note: @JvmName has limitations on non-final functions, and even the compiler may prevent compilation.
For example the JS:
class Foo { @JsPromise(markName = "namedWaitAndGet") suspend fun waitAndGet(): String { delay(5) return "Hello" } }
compiled ๐
class Foo { suspend fun waitAndGet(): String { delay(5) return "Hello" } @Api4Js // RequiresOptIn annotation, provide warnings to Kotlin @JsName("namedWaitAndGet") // From the `markName`'s value fun waitAndGetAsync(): Promise<String> = runInAsync { waitAndGet() } // 'runInAsync' from the runtime provided by the plugin }
This is the documentation.
Note
If you notice any issues or omissions in the documentation, feel free to provide feedback anytime!
- Simple Robot Frameworks (Fully customized)
see LICENSE .
Copyright (c) 2022 ForteScarlet
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.