Skip to content

Commit 74f3d2a

Browse files
authored
Merge pull request #14
async函数支持第二位的CoroutineScope参数
2 parents ce9bc9b + 63506c7 commit 74f3d2a

File tree

9 files changed

+44
-434
lines changed

9 files changed

+44
-434
lines changed

buildSrc/src/main/kotlin/IProject.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
object IProject {
2-
const val VERSION = "0.0.6"
2+
const val VERSION = "0.1.0"
33
const val GROUP = "love.forte.plugin.suspend-transform"
44
const val DESCRIPTION = "Generate platform-compatible functions for Kotlin suspend functions"
55

compiler/suspend-transform-plugin/build.gradle.kts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,35 @@ plugins {
1010
id("com.bennyhuo.kotlin.plugin.embeddable.test") version "1.7.10.0"
1111
}
1212

13+
//testWithEmbedded0()
14+
1315
dependencies {
1416
compileOnly(kotlin("stdlib"))
1517
compileOnly(kotlin("compiler"))
18+
compileOnly("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
19+
20+
// compileOnly(kotlin("compiler-embeddable"))
1621

1722
kapt("com.google.auto.service:auto-service:1.0.1")
1823
compileOnly("com.google.auto.service:auto-service-annotations:1.0.1")
1924

2025
testImplementation(kotlin("stdlib"))
2126
testImplementation(kotlin("test-junit"))
22-
// testImplementation(kotlin("compiler-embeddable"))
23-
testImplementation("org.jetbrains.kotlin:kotlin-compiler-embeddable")
27+
testImplementation(kotlin("compiler-embeddable"))
28+
testImplementation(kotlin("reflect"))
29+
// testImplementation("org.jetbrains.kotlin:kotlin-compiler-embeddable")
2430

2531
testImplementation(project(":runtime:suspend-transform-annotation"))
2632
testImplementation(project(":runtime:suspend-transform-runtime"))
2733

28-
// testImplementation("com.github.tschuchortdev:kotlin-compile-testing:1.4.9")
34+
testImplementation("com.github.tschuchortdev:kotlin-compile-testing:1.4.9")
2935
// testImplementation("org.bitbucket.mstrobel:procyon-compilertools:0.6.0")
30-
testImplementation("com.bennyhuo.kotlin:kotlin-compile-testing-extensions:1.7.10.2-SNAPSHOT")
36+
// testImplementation("com.bennyhuo.kotlin:kotlin-compile-testing-extensions:1.7.10.2-SNAPSHOT")
3137

3238
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
3339
// testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:1.6.4")
3440
}
41+
3542
val compileKotlin: KotlinCompile by tasks
3643
compileKotlin.kotlinOptions.freeCompilerArgs += listOf("-Xjvm-default=enable", "-opt-in=kotlin.RequiresOptIn")
3744

@@ -55,8 +62,3 @@ buildConfig {
5562
tasks.withType<KotlinCompile> {
5663
kotlinOptions.jvmTarget = "1.8"
5764
}
58-
59-
kotlin {
60-
// explicitApi()
61-
}
62-

compiler/suspend-transform-plugin/src/main/kotlin/love/forte/plugin/suspendtrans/SuspendTransformConfiguration.kt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,27 @@ open class SuspendTransformConfiguration @JvmOverloads constructor(var enabled:
5151
var jvmAsyncMarkAnnotation = MarkAnnotation(TO_JVM_ASYNC_ANNOTATION_NAME)
5252

5353
/**
54-
* 格式必须为
54+
* 格式必须为:
5555
*
5656
* ```kotlin
57-
* fun <T> <fun-name>(block: suspend () -> T): CompletableFuture<T> {
57+
* fun <T> <fun-name>(block: suspend () -> T[, scope: CoroutineScope = ...]): CompletableFuture<T> {
5858
* // ...
5959
* }
6060
* ```
61+
*
62+
* 其中,此异步函数可以有第二个参数,此参数格式必须为 [kotlinx.coroutines.CoroutineScope]。
63+
* 如果存在此参数,当转化函数所处类型自身实现了 [kotlinx.coroutines.CoroutineScope] 时,将会将其自身作为参数填入,类似于:
64+
*
65+
* ```kotlin
66+
* class Bar : CoroutineScope {
67+
* @JvmAsync
68+
* suspend fun foo(): Foo
69+
*
70+
* @Api4J fun fooAsync(): CompletableFuture<Foo> = runInAsync(block = { foo() }, scope = this)
71+
* }
72+
* ```
73+
* 当前类型不属于 [kotlinx.coroutines.CoroutineScope] 类型时不会使用此参数。
74+
*
6175
*/
6276
var jvmAsyncFunctionName: String = JVM_RUN_IN_ASYNC_FUNCTION_NAME
6377

compiler/suspend-transform-plugin/src/main/kotlin/love/forte/plugin/suspendtrans/ir/SuspendTransformTransformer.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import org.jetbrains.kotlin.ir.builders.irReturn
1616
import org.jetbrains.kotlin.ir.declarations.*
1717
import org.jetbrains.kotlin.ir.expressions.IrBody
1818
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
19+
import org.jetbrains.kotlin.ir.types.isSubtypeOfClass
1920
import org.jetbrains.kotlin.ir.types.typeWith
2021
import org.jetbrains.kotlin.ir.util.*
2122
import org.jetbrains.kotlin.name.FqName
@@ -212,14 +213,15 @@ private fun generateTransformBodyForFunction(
212213

213214
if (owner.valueParameters.size > 1) {
214215
val secondType = owner.valueParameters[1].type
215-
KOTLIN_SUSPEND_BUILT_IN_FUNCTION_FQ_NAME
216-
val coroutineScopeTypeName = COROUTINES_PACKAGE_FQ_NAME.child(Name.identifier("CoroutineScope"))
216+
val coroutineScopeTypeName = "kotlinx.coroutines.CoroutineScope".fqn
217217
val coroutineScopeTypeNameUnsafe = coroutineScopeTypeName.toUnsafe()
218218
if (secondType.isClassType(coroutineScopeTypeNameUnsafe)) {
219-
originFunction.dispatchReceiverParameter?.also { dispatchReceiverParameter ->
220-
if (dispatchReceiverParameter.type.isClassType(coroutineScopeTypeNameUnsafe)) {
221-
// put 'this' to second arg
222-
putValueArgument(1, irGet(dispatchReceiverParameter))
219+
function.dispatchReceiverParameter?.also { dispatchReceiverParameter ->
220+
context.referenceClass(coroutineScopeTypeName)?.also { coroutineScopeRef ->
221+
if (dispatchReceiverParameter.type.isSubtypeOfClass(coroutineScopeRef)) {
222+
// put 'this' to second arg
223+
putValueArgument(1, irGet(dispatchReceiverParameter))
224+
}
223225
}
224226
}
225227
}

compiler/suspend-transform-plugin/src/test/kotlin/love/forte/plugin/suspendtrans/test/CliOptionsTest.kt

Lines changed: 0 additions & 45 deletions
This file was deleted.

compiler/suspend-transform-plugin/src/test/kotlin/love/forte/plugin/suspendtrans/test/CpTest.kt

Lines changed: 0 additions & 122 deletions
This file was deleted.

compiler/suspend-transform-plugin/src/test/kotlin/love/forte/plugin/suspendtrans/test/SuspendTransTest.kt

Lines changed: 0 additions & 100 deletions
This file was deleted.

0 commit comments

Comments
 (0)