toadzky
11/21/2024, 4:08 AMtoadzky
11/21/2024, 3:58 PMdmitriy.novozhilov
11/21/2024, 4:00 PMtoadzky
11/21/2024, 4:10 PMtoadzky
11/21/2024, 4:10 PMudalov
toadzky
11/22/2024, 5:46 PMudalov
toadzky
11/22/2024, 5:50 PMrunBlocking
or using the jax-rs AsyncResponse
pattern. i'm trying to write a compiler plugin that will automatically transform annotated suspend jax-rs methods to be non-suspend methods that have an additional parameter and wraps the entire function body contents with a coroutine async
block.toadzky
11/22/2024, 5:51 PMasync
launch with the trailing lambda block that contains all the statements from the original functionudalov
toadzky
11/22/2024, 5:58 PMtoadzky
11/22/2024, 5:59 PMudalov
toadzky
11/22/2024, 6:00 PMCoroutineScope(Dispatchers.Default).async
toadzky
11/22/2024, 6:00 PMtoadzky
11/22/2024, 6:00 PM@GET
suspend fun myRouteHandler(): MyResponseType {
// handle the rest request
}
becomes
@GET
fun myRouteHandler(response: AsyncResponse): Unit {
CoroutineScope(Dispatchers.Default).async {
// handle the rest request
// take the original response and pipe it into the `AsyncResponse`
}
}
udalov
toadzky
11/22/2024, 6:04 PMtoadzky
11/22/2024, 6:04 PMtoadzky
11/22/2024, 6:05 PMIrBlockBody
or an anonymous function or a few other thingsudalov
-Xphases-to-dump-before=IrLowering
, and then do the same with your plugin's code, and compare them.toadzky
11/22/2024, 6:05 PMtoadzky
11/22/2024, 6:06 PMtoadzky
11/23/2024, 5:09 AMTYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
toadzky
11/23/2024, 5:46 AMblock: FUN_EXPR type=@[ExtensionFunctionType] kotlin.coroutines.SuspendFunction1<kotlinx.coroutines.CoroutineScope, kotlin.Unit> origin=LAMBDA
i've managed to get to look like this:
block: FUN_EXPR type=kotlin.coroutines.SuspendFunction0<kotlin.Unit> origin=LAMBDA
but that's as close as i've managed to cometoadzky
11/23/2024, 5:47 AMudalov
It's anTYPE_OP type=kotlin.Unit origin=IMPLICIT_COERCION_TO_UNIT typeOperand=kotlin.Unit
IrTypeOperatorCall
with IrTypeOperator.IMPLICIT_COERCION_TO_UNIT
udalov
@[ExtensionFunctionType]
is not important, however it seems weird that SuspendFunction0
works for you where SuspendFunction1
is neededtoadzky
11/25/2024, 3:58 PMSuspendFunction0
or if i switched it. i probably switched it because you are right, it shouldn't work with a 0
aritytoadzky
11/28/2024, 9:26 PMSuspendFunction1
to make it work.toadzky
11/28/2024, 9:27 PMtoadzky
11/28/2024, 9:28 PM! java.lang.NullPointerException: null
! at kotlin.coroutines.jvm.internal.ContinuationImpl.getContext(ContinuationImpl.kt:105)
! at kotlin.coroutines.jvm.internal.ContinuationImpl.intercepted(ContinuationImpl.kt:112)
! at kotlin.coroutines.intrinsics.IntrinsicsKt__IntrinsicsJvmKt.intercepted(IntrinsicsJvm.kt:182)
! at kotlinx.coroutines.DelayKt.delay(Delay.kt:172)
! at dev.skytag.jaxrs.suspend.test.Api.doTheThing(Api.kt:31)
toadzky
11/28/2024, 9:29 PM@GET
suspend fun doTheThing(): String {
delay(50) // this is line 31 in the source
return getResponse()
}
toadzky
11/28/2024, 9:29 PM@GET
fun doTheThing(@Suspended asyncResponse: AsyncResponse) {
CoroutineScope(Dispatchers.Default).launch {
try {
delay(50)
asyncResponse.resume(getResponse())
} catch (ex: Exception) {
asyncResponse.resume(ex)
}
}
}