11package godot.codegen.services.impl
22
3+ import com.squareup.kotlinpoet.ANY
34import com.squareup.kotlinpoet.AnnotationSpec
45import com.squareup.kotlinpoet.ClassName
56import com.squareup.kotlinpoet.FileSpec
67import com.squareup.kotlinpoet.FunSpec
78import com.squareup.kotlinpoet.KModifier
9+ import com.squareup.kotlinpoet.LIST
810import com.squareup.kotlinpoet.MemberName
911import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
12+ import com.squareup.kotlinpoet.TypeName
1013import com.squareup.kotlinpoet.TypeVariableName
1114import com.squareup.kotlinpoet.UNIT
1215import godot.codegen.services.IAwaitGenerationService
@@ -29,7 +32,6 @@ object AwaitGenerationService : IAwaitGenerationService {
2932 }.toList()
3033
3134 for (argCount in 0 .. maxArgumentCount) {
32-
3335 val parameters = allParameters.take(argCount)
3436
3537 val baseReceiver = ClassName (godotCorePackage, signal + argCount)
@@ -39,6 +41,14 @@ object AwaitGenerationService : IAwaitGenerationService {
3941 baseReceiver
4042 }
4143
44+ val returnType = when (argCount) {
45+ 1 -> parameters[0 ]
46+ 2 -> ClassName (" kotlin" , " Pair" ).parameterizedBy(parameters)
47+ 3 -> ClassName (" kotlin" , " Triple" ).parameterizedBy(parameters)
48+ in 4 .. maxArgumentCount -> LIST .parameterizedBy(ANY .copy(nullable = true ))
49+ else -> UNIT
50+ }
51+
4252 awaitFile.addFunction(
4353 FunSpec .builder(" await" )
4454 .addModifiers(KModifier .SUSPEND , KModifier .INLINE )
@@ -48,7 +58,8 @@ object AwaitGenerationService : IAwaitGenerationService {
4858 addTypeVariables(parameters)
4959 }
5060 }
51- .generateBody(argCount)
61+ .generateBody(argCount, returnType)
62+ .returns(returnType)
5263 .build()
5364 )
5465 }
@@ -63,7 +74,8 @@ object AwaitGenerationService : IAwaitGenerationService {
6374 }
6475
6576
66- private fun FunSpec.Builder.generateBody (argCount : Int ): FunSpec .Builder {
77+ private fun FunSpec.Builder.generateBody (argCount : Int , returnType : TypeName ): FunSpec .Builder {
78+ // Build `p0, p1, ..., px`
6779 val lambdaParameters = buildString {
6880 for (i in 0 until argCount) {
6981 if (i != 0 ) {
@@ -73,12 +85,21 @@ object AwaitGenerationService : IAwaitGenerationService {
7385 }
7486 }
7587
88+ // Build what is inserted into the `resume()` method : `Unit`, `po`, `Pair(p0, P1)`, `Triple(p0, p1, p2)`, `listOf(p0, p1, p2, p3), etc..`
89+ val resumeParameters = when (argCount) {
90+ 0 -> " Unit"
91+ 1 -> lambdaParameters
92+ 2 -> " Pair($lambdaParameters )"
93+ 3 -> " Triple($lambdaParameters )"
94+ in 4 .. Int .MAX_VALUE -> " listOf($lambdaParameters )"
95+ else -> " "
96+ }
97+
7698 return this
77- .addStatement(" %M { cont: %T<%T> ->" , suspendCancellableCoroutine, cancellableContinuationClass, UNIT )
78- .addStatement(" %M(%T.ConnectFlags.CONNECT_ONE_SHOT.id.toInt()) { $lambdaParameters ->" , connect, GODOT_OBJECT )
79- .addStatement(" cont.%M(%T)" , resume, UNIT )
80- .addStatement(" }" )
81- .addStatement(" }" )
82- .returns(UNIT )
99+ .beginControlFlow(" return %M { cont: %T<%T> ->" , suspendCancellableCoroutine, cancellableContinuationClass, returnType)
100+ .beginControlFlow(" %M(%T.ConnectFlags.CONNECT_ONE_SHOT.id.toInt()) { $lambdaParameters ->" , connect, GODOT_OBJECT )
101+ .addStatement(" cont.%M($resumeParameters )" , resume)
102+ .endControlFlow()
103+ .endControlFlow()
83104 }
84105}
0 commit comments