Skip to content

Commit

Permalink
Fix runtime SIR when coroutines have Flow-interop disabled.
Browse files Browse the repository at this point in the history
  • Loading branch information
TadeasKriz committed May 16, 2024
1 parent 776d5d1 commit 79b357f
Showing 1 changed file with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ object SupportedFlowRuntimeGenerator {

context(SirPhase.Context)
fun generate(skieSwiftFlowIterator: SirClass) {
generateBridgeSubscriptionCountWorkaroundFunctions()

val classesForVariants = SupportedFlow.allVariants.associateWith { flowVariant ->
createSwiftFlowClass(flowVariant)
}
Expand All @@ -24,6 +26,22 @@ object SupportedFlowRuntimeGenerator {
}
}

context(SirPhase.Context)
private fun generateBridgeSubscriptionCountWorkaroundFunctions() {
// If Flow-interop is disabled globally (or even just for `kotlinx.coroutines.core`),
// `subscriptionCount` property falls back to Kotlin's `StateFlow` protocol.
// These two functions make sure our Flow runtime code still compiles as expected.
namespaceProvider.getSkieNamespaceWrittenSourceFile("bridgeSubscriptionCount").content = """
internal func bridgeSubscriptionCount(_ subscriptionCount: SkieSwiftStateFlow<KotlinInt>) -> SkieSwiftStateFlow<KotlinInt> {
return subscriptionCount
}
internal func bridgeSubscriptionCount(_ subscriptionCount: any Skie.org_jetbrains_kotlinx__kotlinx_coroutines_core.StateFlow.__Kotlin) -> SkieSwiftStateFlow<KotlinInt> {
return SkieSwiftStateFlow(internal: subscriptionCount)
}
""".trimIndent()
}

context(SirPhase.Context)
private fun createSwiftFlowClass(flowVariant: SupportedFlow.Variant): SirClass {
return namespaceProvider.getSkieNamespaceFile(flowVariant.swiftSimpleName).run {
Expand Down Expand Up @@ -161,6 +179,9 @@ object SupportedFlowRuntimeGenerator {
CustomPassthroughDeclaration.Property(
identifier = "subscriptionCount",
type = sirProvider.getClassByFqName(SirFqName(sirProvider.skieModule, "SkieSwiftStateFlow")).toType(kirBuiltins.nsNumberDeclarationsByFqName["kotlin.Int"]!!.originalSirClass.defaultType),
transformGetter = {
CodeBlock.of("bridgeSubscriptionCount(%L)", it)
}
),
CustomPassthroughDeclaration.SimpleFunction(
identifier = "emit",
Expand Down

0 comments on commit 79b357f

Please sign in to comment.