Skip to content

Commit 46bd845

Browse files
committed
feat: 针对 CoroutineScope 类型的参数的填充优化
1 parent 94d2817 commit 46bd845

File tree

5 files changed

+67
-73
lines changed

5 files changed

+67
-73
lines changed

.run/PublishAllToLocal.run.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="PublishAllToLocal" type="GradleRunConfiguration" factoryName="Gradle">
3+
<ExternalSystemSettings>
4+
<option name="env">
5+
<map>
6+
<entry key="SIMBOT_LOCAL" value="true" />
7+
</map>
8+
</option>
9+
<option name="executionName" />
10+
<option name="externalProjectPath" value="$PROJECT_DIR$" />
11+
<option name="externalSystemIdString" value="GRADLE" />
12+
<option name="scriptParameters" value="" />
13+
<option name="taskDescriptions">
14+
<list />
15+
</option>
16+
<option name="taskNames">
17+
<list>
18+
<option value="publishAllPublicationsToMavenLocalRepository" />
19+
</list>
20+
</option>
21+
<option name="vmOptions" />
22+
</ExternalSystemSettings>
23+
<ExternalSystemDebugServerProcess>true</ExternalSystemDebugServerProcess>
24+
<ExternalSystemReattachDebugProcess>true</ExternalSystemReattachDebugProcess>
25+
<DebugAllEnabled>false</DebugAllEnabled>
26+
<ForceTestExec>false</ForceTestExec>
27+
<method v="2" />
28+
</configuration>
29+
</component>

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

Lines changed: 33 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -210,52 +210,14 @@ private fun generateTransformBodyForFunction(
210210
val owner = transformTargetFunctionCall.owner
211211

212212
// CoroutineScope
213-
if (owner.valueParameters.size > 1) {
214-
owner.valueParameters.forEachIndexed { index, valueParameter ->
215-
if (index == 0) {
216-
return@forEachIndexed
217-
}
213+
val ownerValueParameters = owner.valueParameters
218214

215+
if (ownerValueParameters.size > 1) {
216+
for (index in 1..ownerValueParameters.lastIndex) {
217+
val valueParameter = ownerValueParameters[index]
219218
val type = valueParameter.type
220219
tryResolveCoroutineScopeValueParameter(type, context, function, owner, this@irBlockBody, index)
221220
}
222-
223-
// val secondType = owner.valueParameters[1].type
224-
// val coroutineScopeTypeName = "kotlinx.coroutines.CoroutineScope".fqn
225-
// val coroutineScopeTypeClassId = ClassId.topLevel("kotlinx.coroutines.CoroutineScope".fqn)
226-
// val coroutineScopeTypeNameUnsafe = coroutineScopeTypeName.toUnsafe()
227-
// if (secondType.isClassType(coroutineScopeTypeNameUnsafe)) {
228-
// function.dispatchReceiverParameter?.also { dispatchReceiverParameter ->
229-
// context.referenceClass(coroutineScopeTypeClassId)?.also { coroutineScopeRef ->
230-
//// irGet(dispatchReceiverParameter)
231-
//// irAs(irGet(dispatchReceiverParameter), coroutineScopeRef).safeAs<>()
232-
// if (dispatchReceiverParameter.type.isSubtypeOfClass(coroutineScopeRef)) {
233-
// // put 'this' to second arg
234-
// putValueArgument(1, irGet(dispatchReceiverParameter))
235-
// } else {
236-
// // scope safe cast
237-
// val scopeType = coroutineScopeRef.defaultType
238-
//
239-
// val scopeParameter = owner.valueParameters.getOrNull(1)
240-
//
241-
// if (scopeParameter?.type?.isNullable() == true) {
242-
// val irSafeAs = IrTypeOperatorCallImpl(
243-
// startOffset,
244-
// endOffset,
245-
// scopeType,
246-
// IrTypeOperator.SAFE_CAST,
247-
// scopeType,
248-
// irGet(dispatchReceiverParameter)
249-
// )
250-
//
251-
// putValueArgument(1, irSafeAs)
252-
// }
253-
//// irAs(irGet(dispatchReceiverParameter), coroutineScopeRef.defaultType)
254-
// }
255-
// }
256-
// }
257-
// }
258-
259221
}
260222

261223
})
@@ -269,8 +231,9 @@ private val coroutineScopeTypeNameUnsafe = coroutineScopeTypeName.toUnsafe()
269231
/**
270232
* 解析类型为 CoroutineScope 的参数。
271233
* 如果当前参数类型为 CoroutineScope:
272-
* - 如果当前 dispatcher 即为 CoroutineScope 类型,将其填充
273-
* - 如果当前 dispatcher 不是 CoroutineScope 类型,但是此参数可以为 null,则使用 safe-cast 将 dispatcher 转化为 CoroutineScope ( `dispatcher as? CoroutineScope` )
234+
* - 如果当前 receiver 即为 CoroutineScope 类型,将其填充
235+
* - 如果当前 receiver 不是 CoroutineScope 类型,但是此参数可以为 null,
236+
* 则使用 safe-cast 将 receiver 转化为 CoroutineScope ( `dispatcher as? CoroutineScope` )
274237
* - 其他情况忽略此参数(适用于此参数有默认值的情况)
275238
*/
276239
private fun IrCall.tryResolveCoroutineScopeValueParameter(
@@ -281,34 +244,33 @@ private fun IrCall.tryResolveCoroutineScopeValueParameter(
281244
builderWithScope: IrBuilderWithScope,
282245
index: Int
283246
) {
284-
if (type.isClassType(coroutineScopeTypeNameUnsafe)) {
285-
function.dispatchReceiverParameter?.also { dispatchReceiverParameter ->
286-
context.referenceClass(coroutineScopeTypeClassId)?.also { coroutineScopeRef ->
287-
// irGet(dispatchReceiverParameter)
288-
// irAs(irGet(dispatchReceiverParameter), coroutineScopeRef).safeAs<>()
289-
if (dispatchReceiverParameter.type.isSubtypeOfClass(coroutineScopeRef)) {
290-
// put 'this' to second arg
291-
putValueArgument(index, builderWithScope.irGet(dispatchReceiverParameter))
292-
} else {
293-
// scope safe cast
294-
val scopeType = coroutineScopeRef.defaultType
295-
296-
val scopeParameter = owner.valueParameters.getOrNull(1)
297-
298-
if (scopeParameter?.type?.isNullable() == true) {
299-
val irSafeAs = IrTypeOperatorCallImpl(
300-
startOffset,
301-
endOffset,
302-
scopeType,
303-
IrTypeOperator.SAFE_CAST,
304-
scopeType,
305-
builderWithScope.irGet(dispatchReceiverParameter)
306-
)
307-
308-
putValueArgument(index, irSafeAs)
309-
}
310-
// irAs(irGet(dispatchReceiverParameter), coroutineScopeRef.defaultType)
247+
if (!type.isClassType(coroutineScopeTypeNameUnsafe)) {
248+
return
249+
}
250+
251+
function.dispatchReceiverParameter?.also { dispatchReceiverParameter ->
252+
context.referenceClass(coroutineScopeTypeClassId)?.also { coroutineScopeRef ->
253+
if (dispatchReceiverParameter.type.isSubtypeOfClass(coroutineScopeRef)) {
254+
// put 'this' to the arg
255+
putValueArgument(index, builderWithScope.irGet(dispatchReceiverParameter))
256+
} else {
257+
val scopeType = coroutineScopeRef.defaultType
258+
259+
val scopeParameter = owner.valueParameters.getOrNull(1)
260+
261+
if (scopeParameter?.type?.isNullable() == true) {
262+
val irSafeAs = IrTypeOperatorCallImpl(
263+
startOffset,
264+
endOffset,
265+
scopeType,
266+
IrTypeOperator.SAFE_CAST,
267+
scopeType,
268+
builderWithScope.irGet(dispatchReceiverParameter)
269+
)
270+
271+
putValueArgument(index, irSafeAs)
311272
}
273+
// irAs(irGet(dispatchReceiverParameter), coroutineScopeRef.defaultType)
312274
}
313275
}
314276
}

runtime/suspend-transform-runtime/src/jvmMain/kotlin/love/forte/plugin/suspendtrans/runtime/RunInSuspendJvm.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private val transformer: FutureTransformer =
7474
@Suppress("FunctionName")
7575
public fun <T> `$runInAsync$`(
7676
block: suspend () -> T,
77-
scope: CoroutineScope? = null //`$CoroutineScope4J$`
77+
scope: CoroutineScope? = null
7878
): CompletableFuture<T> {
7979
return transformer.trans(scope ?: `$CoroutineScope4J$`, block)
8080
}

settings.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ include(":runtime:suspend-transform-runtime")
88

99
include(":plugins:suspend-transform-plugin-gradle")
1010

11-
include(":suspend-transform-plugin-sample")
11+
//include(":suspend-transform-plugin-sample")
1212
// include(":plugins:ide:suspend-transform-plugin-idea")

suspend-transform-plugin-sample/src/main/kotlin/love/forte/plugin/suspendtrans/sample/ForteScarlet.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package love.forte.plugin.suspendtrans.sample
33
import kotlinx.coroutines.CoroutineScope
44
import kotlinx.coroutines.delay
55
import love.forte.plugin.suspendtrans.annotation.JvmAsync
6+
import love.forte.plugin.suspendtrans.annotation.JvmBlocking
67
import kotlin.coroutines.CoroutineContext
78
import kotlin.coroutines.EmptyCoroutineContext
89

910

1011
abstract class IForteScarlet {
1112
@JvmAsync
13+
@JvmBlocking
1214
abstract suspend fun stringToInt(value: String): Int
1315
}
1416

@@ -22,6 +24,7 @@ class ForteScarlet : CoroutineScope, IForteScarlet() {
2224
get() = EmptyCoroutineContext
2325

2426
@JvmAsync
27+
@JvmBlocking
2528
override suspend fun stringToInt(value: String): Int {
2629
delay(5)
2730
return value.toInt()

0 commit comments

Comments
 (0)