mirror of
https://github.com/jlengrand/kotlin.git
synced 2026-05-15 08:31:35 +00:00
The main idea is the following: since we need to generate (fake)continuations before inlining, we move IrClasses of suspend lambdas and continuation classes of named functions into the functions. Thus, it allows the codegen to generate them prior to inlining and the inliner will happily transform them for us. Because of that, lowerings which transform call-site function are likely to change reference to lowered suspend lambdas or functions. Hence, do not rely on references to lowered suspend lambdas or functions, instead, rely on attributes. Do not generate continuation for inline suspend lambdas. Previously, inline suspend lambdas were treated like suspend functions, thus we generated continuations for them. Now we just do not treat them as suspend functions or lambdas during AddContinuationLowering. We should add continuation parameter to them, however. Do not generate secondary constructor for suspend lambdas, otherwise, the inliner is unable to transform them (it requires only one constructor to be present). Generate continuation classes for suspend functions as first statement inside the function. This enables suspend functions in local object inside inline functions. Since we already have attributes inside suspend named functions, we just reuse them to generate continuation class names. This allows us to close the gap between code generated by old back-end and the new one. If a suspend named function captures crossinline lambda, we should generate a template for inliner: a copy of the function without state-machine and a continuation constructor call. The call is needed so the inliner transforms the continuation as well. Refactor CoroutineTransformerMethodVisitor, so it no longer depends on PSI.
61 lines
1.6 KiB
Kotlin
Vendored
61 lines
1.6 KiB
Kotlin
Vendored
// IGNORE_BACKEND: JVM_IR
|
|
// TARGET_BACKEND: JVM
|
|
// FILE: flow.kt
|
|
// COMMON_COROUTINES_TEST
|
|
// FULL_JDK
|
|
// WITH_RUNTIME
|
|
// WITH_COROUTINES
|
|
|
|
package flow
|
|
|
|
interface FlowCollector<T> {
|
|
suspend fun emit(value: T)
|
|
}
|
|
|
|
interface Flow<T> {
|
|
suspend fun collect(collector: FlowCollector<T>)
|
|
}
|
|
|
|
public inline fun <T> flow(crossinline block: suspend FlowCollector<T>.() -> Unit) = object : Flow<T> {
|
|
override suspend fun collect(collector: FlowCollector<T>) = collector.block()
|
|
}
|
|
|
|
suspend inline fun <T> Flow<T>.collect(crossinline action: suspend (T) -> Unit): Unit =
|
|
collect(object : FlowCollector<T> {
|
|
override suspend fun emit(value: T) = action(value)
|
|
})
|
|
|
|
public inline fun <T, R> Flow<T>.transform(crossinline transformer: suspend FlowCollector<R>.(value: T) -> Unit): Flow<R> {
|
|
return flow {
|
|
collect { value ->
|
|
transformer(value)
|
|
}
|
|
}
|
|
}
|
|
|
|
public inline fun <T, R> Flow<T>.map(crossinline transformer: suspend (value: T) -> R): Flow<R> = transform { value -> emit(transformer(value)) }
|
|
|
|
inline fun decorate() = suspend {
|
|
flow<Int> {
|
|
emit(1)
|
|
}.map { it + 1 }
|
|
.collect {
|
|
}
|
|
}
|
|
|
|
// FILE: box.kt
|
|
// COMMON_COROUTINES_TEST
|
|
|
|
import flow.*
|
|
|
|
fun box() : String {
|
|
decorate()
|
|
val enclosingMethod = try {
|
|
Class.forName("flow.FlowKt\$decorate\$1\$invokeSuspend\$\$inlined\$map\$1\$1").enclosingMethod
|
|
} catch (ignore: ClassNotFoundException) {
|
|
Class.forName("flow.FlowKt\$decorate\$1\$doResume\$\$inlined\$map\$1\$1").enclosingMethod
|
|
}
|
|
if ("$enclosingMethod".contains("\$\$forInline")) return "$enclosingMethod"
|
|
return "OK"
|
|
}
|