### ObjCName Annotation Example Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/annotations.fir.ir.txt Demonstrates the usage of the @ObjCName annotation to provide specific names for Objective-C and Swift. ```kotlin PROPERTY GENERATED[com.rickclephas.kmp.nativecoroutines.compiler.fir.utils.NativeCoroutinesDeclarationKey] name:objCNameProperty2Value visibility:public modality:FINAL [val] annotations: ObjCName(name = "objCNameProperty2ObjC", swiftName = , exact = ) FUN GENERATED[com.rickclephas.kmp.nativecoroutines.compiler.fir.utils.NativeCoroutinesDeclarationKey] name: visibility:public modality:FINAL returnType:kotlin.String correspondingProperty: PROPERTY GENERATED[com.rickclephas.kmp.nativecoroutines.compiler.fir.utils.NativeCoroutinesDeclarationKey] name:objCNameProperty2Value visibility:public modality:FINAL [val] BLOCK_BODY VAR IR_TEMPORARY_VARIABLE name:tmp_21 type:kotlinx.coroutines.flow.StateFlow [val] CALL 'public final fun (): kotlinx.coroutines.flow.StateFlow declared in ' type=kotlinx.coroutines.flow.StateFlow origin=null RETURN type=kotlin.Nothing from='public final fun (): kotlin.String declared in ' CALL 'public abstract fun (): T of kotlinx.coroutines.flow.StateFlow declared in kotlinx.coroutines.flow.StateFlow' type=kotlin.String origin=null ARG : GET_VAR 'val tmp_21: kotlinx.coroutines.flow.StateFlow declared in .' type=kotlinx.coroutines.flow.StateFlow origin=null ``` -------------------------------- ### ObjCName Property for Value Access (Second Example) Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/annotations.fir.kt.txt A second example demonstrating @ObjCName for accessing StateFlow values, reinforcing its utility for Objective-C interoperability. ```kotlin @ObjCName(name = "objCNameProperty2ObjC") val objCNameProperty2Value: String get(): String { val tmp_21: StateFlow = () return tmp_21.() } ``` -------------------------------- ### Swift 5: Function Returning Null (Second Instance) Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/swift5/functions.box.txt Another example demonstrating handling a null return from a generated Swift function. ```swift func testNull2(){ let result = NativeCoroutinesSwift.null2() XCTAssertNil(result) } ``` -------------------------------- ### Swift Codegen for Flow with flowOf Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/swift13/functions.fir.ir.txt This example shows the Swift representation of a Kotlin Flow initialized using `flowOf`. It highlights how the compiler handles Flow creation and delegation. ```swift private let $$delegate_0: Flow ``` -------------------------------- ### ObjCName Annotation with Both Names Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/swift5/annotations.fir.ir.txt This example shows how to provide both Objective-C and Swift names using the ObjCName annotation. This offers granular control over how the function is exposed in both environments. ```kotlin native_coroutines.ObjCName(name = "objCNameFunction3ObjC", swiftName = "objCNameFunction3Swift", exact = ) ``` -------------------------------- ### Using nativeSuspend with a non-nullable CoroutineScope Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/coroutinescope.fir.ir.txt This example demonstrates the use of `nativeSuspend` with a non-nullable `CoroutineScope`. The function generates a callback-based interface for managing coroutine execution and its outcomes. ```kotlin FILE fqName: fileName:/__GENERATED__CALLABLES__.kt FUN GENERATED[com.rickclephas.kmp.nativecoroutines.compiler.fir.utils.NativeCoroutinesDeclarationKey] name:returnSuspendValueNative visibility:public modality:FINAL returnType:kotlin.Function3<@[ParameterName(name = "onResult")] kotlin.Function2, @[ParameterName(name = "onError")] kotlin.Function2, @[ParameterName(name = "onCancelled")] kotlin.Function2, kotlin.Function0> annotations: ObjCName(name = "returnSuspendValue", swiftName = , exact = ) BLOCK_BODY VAR IR_TEMPORARY_VARIABLE name:tmp_1 type:kotlinx.coroutines.CoroutineScope [val] CALL 'internal final fun (): kotlinx.coroutines.CoroutineScope declared in ' type=kotlinx.coroutines.CoroutineScope origin=null RETURN type=kotlin.Nothing from='public final fun returnSuspendValueNative (): kotlin.Function3<@[ParameterName(name = "onResult")] kotlin.Function2, @[ParameterName(name = "onError")] kotlin.Function2, @[ParameterName(name = "onCancelled")] kotlin.Function2, kotlin.Function0> declared in ' ``` -------------------------------- ### Flow Creation with flowOf Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/coroutinescope.fir.ir.txt This example demonstrates creating a Kotlin Flow from a single value using the `flowOf` function. The generated code indicates the return type and the value used. ```kotlin RETURN type=kotlin.Nothing from='public final fun (: .MyClass3): kotlinx.coroutines.flow.Flow declared in ' CALL 'public final fun flowOf (value: T of kotlinx.coroutines.flow.flowOf): kotlinx.coroutines.flow.Flow declared in kotlinx.coroutines.flow' type=kotlinx.coroutines.flow.Flow origin=null TYPE_ARG T: kotlin.String ARG value: CONST String type=kotlin.String value="OK6" ``` -------------------------------- ### Swift Collection of Top-Level Flow Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/swift5/properties.fir.ir.txt Illustrates collecting a top-level Flow in Swift using the `runBoxTest` utility. This example shows how to consume a Flow directly within a Swift test context. ```swift runBoxTest { collect(topLevelFlowNative()) collect(topLevelSharedFlowNative(), maxValues = 1) values(listOf("1", "2", "3")) } ``` -------------------------------- ### Collect Flow in Swift (Second Instance) Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/swift5/coroutinescope.fir.ir.txt Another example of collecting from a Kotlin Flow in Swift. This demonstrates the repeated usage pattern for processing stream data. ```kotlin CALL 'public final fun collect (flow:kotlinx.coroutines.flow.Flow, maxValues: kotlin.Int?): kotlin.Unit declared in com.rickclephas.kmp.nativecoroutines.BoxTest' type=kotlin.Unit origin=null TYPE_ARG T: kotlin.String ``` -------------------------------- ### Box Test Execution Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/swift3/functions.fir.txt This snippet is part of a test setup for verifying the behavior of KMP-NativeCoroutines, specifically demonstrating the execution of a `runBoxTest` function which involves awaiting suspend functions. ```kotlin public final fun box(): R|kotlin/String| { ^box R|com/rickclephas/kmp/nativecoroutines/runBoxTest|( = runBoxTest@fun R|com/rickclephas/kmp/nativecoroutines/BoxTest|.(): R|kotlin/Unit| { this@R|special/anonymous|.R|com/rickclephas/kmp/nativecoroutines/BoxTest.await|( = await@fun (): R|kotlin/String| { ^ R|/returnSuspendValueNative|() } ) this@R|special/anonymous|.R|com/rickclephas/kmp/nativecoroutines/BoxTest.await|( = await@fun (): R|kotlin/String?| { } ) } ) ``` -------------------------------- ### Swift Codegen: Flow Conversion Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/swift1/coroutinescope.fir.kt.txt Illustrates the conversion of a Kotlin Flow to a Swift publisher using @NativeCoroutines. This example shows a Flow of Strings exposed as a native property. ```kotlin @NativeCoroutines val MyClass3.flowExtProperty2: Flow get(): Flow { return flowOf(value = "OK8") } ``` -------------------------------- ### Swift: Flow Implementation with `flowOf` Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/swift1/functions.fir.ir.txt This example shows a `Flow` implementation in Kotlin that uses `flowOf` to create a flow from a single value. This is intended for Swift interoperability. ```kotlin class MyFlow23 (value1: T1, value2: T2) : Flow by flowOf(value2) { // ... } ``` -------------------------------- ### Box Test Execution with Suspend Functions Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/swift3/functions.fir.ir.txt This example demonstrates how the `runBoxTest` function is used to execute suspend functions within a test environment. It utilizes `await` to handle suspend calls. ```swift internal fun box(): String { return runBoxTest { await { returnSuspendValueNative() } await { returnNullableSuspendValueNative() } } } ``` -------------------------------- ### Generic Class Example Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/swift1/properties.fir.txt Illustrates a generic data class 'MyGenericClass1' with a type parameter 'T'. It includes a constructor, a value property, a component1 operator, and a copy function. ```kotlin public final data class MyGenericClass1 : R|kotlin/Any| { public constructor(value: R|T|): R|MyGenericClass1| { super() } public final val value: R|T| = R|/value| public get(): R|T| public final operator fun component1(): R|T| public final fun copy(value: R|T| = this@R|/MyGenericClass1|.R|/MyGenericClass1.value|): R|MyGenericClass1| ``` -------------------------------- ### Kotlin Class Constructor and Initializers Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/swift3/properties.fir.ir.txt Illustrates the primary constructor and instance initializer for a class, including delegating to the superclass constructor and initializing the class itself. ```kotlin CONSTRUCTOR visibility:public returnType:.MyClass28 [primary] BLOCK_BODY DELEGATING_CONSTRUCTOR_CALL 'public constructor () declared in kotlin.Any' INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:MyClass28 modality:FINAL visibility:public superTypes:[.MyInterface28]' ``` -------------------------------- ### ViewModel Suspend Function Example Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/swift1/viewmodelscope.fir.txt Example of a suspend function within a ViewModel that returns a String. This is a standard Kotlin coroutine function. ```kotlin @R|com/rickclephas/kmp/nativecoroutines/NativeCoroutines|() public final suspend fun R|MyObservableViewModel2|.returnSuspendValue2(): R|kotlin/String| { ^returnSuspendValue2 String(OK8) } ``` -------------------------------- ### Swift Codegen Example with Annotations Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/swift1/annotations.fir.ir.txt Demonstrates the use of various annotations within a Swift codegen test case. This includes property backing fields, deprecated functions, and flow collection. ```kotlin GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:objCNameProperty2 type:kotlinx.coroutines.flow.StateFlow visibility:private [final,static]' FUN name:box visibility:public modality:FINAL returnType:kotlin.String BLOCK_BODY RETURN type=kotlin.Nothing from='public final fun box (): kotlin.String declared in ' CALL 'public final fun runBoxTest (action: @[ExtensionFunctionType] kotlin.coroutines.SuspendFunction1): kotlin.String declared in com.rickclephas.kmp.nativecoroutines' ARG action: FUN_EXPR type=@[ExtensionFunctionType] kotlin.coroutines.SuspendFunction1 origin=LAMBDA FUN LOCAL_FUNCTION_FOR_LAMBDA name: visibility:local modality:FINAL returnType:kotlin.Unit [suspend] VALUE_PARAMETER kind:ExtensionReceiver name:$this$runBoxTest index:0 type:com.rickclephas.kmp.nativecoroutines.BoxTest BLOCK_BODY CALL 'public final fun await (result: kotlin.coroutines.SuspendFunction0): kotlin.Unit declared in com.rickclephas.kmp.nativecoroutines.BoxTest' TYPE_ARG T: kotlin.String ARG : GET_VAR '$this$runBoxTest: com.rickclephas.kmp.nativecoroutines.BoxTest declared in .box.' ARG result: FUN_EXPR type=kotlin.coroutines.SuspendFunction0 origin=LAMBDA FUN LOCAL_FUNCTION_FOR_LAMBDA name: visibility:local modality:FINAL returnType:kotlin.String [suspend] BLOCK_BODY RETURN type=kotlin.Nothing from='local final fun (): kotlin.String declared in .box.' CALL 'public final fun deprecatedFunction1Native (): kotlin.String declared in ' CALL 'public final fun await (result: kotlin.coroutines.SuspendFunction0): kotlin.Unit declared in com.rickclephas.kmp.nativecoroutines.BoxTest' TYPE_ARG T: kotlin.String ARG : GET_VAR '$this$runBoxTest: com.rickclephas.kmp.nativecoroutines.BoxTest declared in .box.' ARG result: FUN_EXPR type=kotlin.coroutines.SuspendFunction0 origin=LAMBDA FUN LOCAL_FUNCTION_FOR_LAMBDA name: visibility:local modality:FINAL returnType:kotlin.String [suspend] BLOCK_BODY RETURN type=kotlin.Nothing from='local final fun (): kotlin.String declared in .box.' CALL 'public final fun deprecatedFunction2Native (): kotlin.String declared in ' CALL 'public final fun collect (flow: kotlinx.coroutines.flow.Flow, maxValues: kotlin.Int?): kotlin.Unit declared in com.rickclephas.kmp.nativecoroutines.BoxTest' TYPE_ARG T: kotlin.String ARG : GET_VAR '$this$runBoxTest: com.rickclephas.kmp.nativecoroutines.BoxTest declared in .box.' ARG flow: CALL 'public final fun ():kotlinx.coroutines.flow.Flow declared in ' CALL 'public final fun collect (flow: kotlinx.coroutines.flow.Flow, maxValues: kotlin.Int?): kotlin.Unit declared in com.rickclephas.kmp.nativecoroutines.BoxTest' TYPE_ARG T: kotlin.String ARG : GET_VAR '$this$runBoxTest: com.rickclephas.kmp.nativecoroutines.BoxTest declared in .box.' ARG flow: CALL 'public final fun ():kotlinx.coroutines.flow.Flow declared in ' ``` -------------------------------- ### Native Coroutines await Function Usage Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/annotations.fir.ir.txt Illustrates the usage of the `await` function from `com.rickclephas.kmp.nativecoroutines.BoxTest`. This example shows how to call a native suspend function and handle its result, error, and cancellation. ```kotlin import com.rickclephas.kmp.nativecoroutines.BoxTest // Assume BoxTest is available and has an await function // The following is a representation of the IR code structure // CALL 'public final fun await (nativeSuspend: kotlin.Function0, @[ParameterName(name = "onError")] kotlin.Function2, @[ParameterName(name = "onCancelled")] kotlin.Function2, kotlin.Function0>>): kotlin.Unit declared in com.rickclephas.kmp.nativecoroutines.BoxTest' // TYPE_ARG T: kotlin.String // ARG : GET_VAR '$this$runBoxTest: com.rickclephas.kmp.nativecoroutines.BoxTest declared in .box.' // ARG nativeSuspend: FUN_EXPR type=kotlin.Function0, @[ParameterName(name = "onError")] kotlin.Function2, @[ParameterName(name = "onCancelled")] kotlin.Function2, kotlin.Function0>> origin=LAMBDA // FUN LOCAL_FUNCTION_FOR_LAMBDA name: visibility:local modality:FINAL returnType:kotlin.Function3<@[ParameterName(name = "onResult")] kotlin.Function2, @[ParameterName(name = "onError")] kotlin.Function2, @[ParameterName(name = "onCancelled")] kotlin.Function2, kotlin.Function0> // BLOCK_BODY // RETURN type=kotlin.Nothing from='local final fun (): kotlin.Function3<@[ParameterName(name = "onResult")] kotlin.Function2, @[ParameterName(name = "onError")] kotlin.Function2, @[ParameterName(name = "onCancelled")] kotlin.Function2, kotlin.Function0> declared in .box.' // CALL 'public final fun objCNameFunctionParameterNative (value: kotlin.String): kotlin.Function3<@[ParameterName(name = "onResult")] kotlin.Function2, @[ParameterName(name = "onError")] kotlin.Function2, @[ParameterName(name = "onCancelled")] kotlin.Function2, kotlin.Function0> declared in ' type=kotlin.Function3<@[ParameterName(name = "onResult")] kotlin.Function2, @[ParameterName(name = "onError")] kotlin.Function2, @[ParameterName(name = "onCancelled")] kotlin.Function2, kotlin.Function0> origin=null // ARG value: CONST String type=kotlin.String value="OK13" // This is a simplified representation of the actual Kotlin code that would generate the above IR. // The actual code would involve calling a suspend function and providing callbacks. // For example: // BoxTest().await { onResult, onError, onCancelled -> // // Call a function that returns the required Function3 // objCNameFunctionParameterNative("OK13") // } // Placeholder for the actual code that would be compiled. // The provided text is an IR dump, not direct Kotlin source. // To make this a valid snippet, we'll represent a conceptual call. // Conceptual Kotlin code that might lead to the IR: // suspend fun exampleUsage(boxTest: BoxTest) { // boxTest.await { onResult, onError, onCancelled -> // // This lambda would typically call another function or perform an action // // that returns the Function3 required by the await function. // // For demonstration, let's assume a function `createCallbackHandler` exists: // // createCallbackHandler() // // The IR shows a direct call to `objCNameFunctionParameterNative`: // objCNameFunctionParameterNative("OK13") // } // } // Since the source provides an IR dump, we cannot provide exact Kotlin source. // The following is a placeholder to represent the context. fun conceptualAwaitCall(boxTest: BoxTest) { // This is a placeholder and not actual compilable code from the source. // The source provided an IR dump, not direct Kotlin source code. println("Conceptual call to await") } ``` -------------------------------- ### Generic Suspend Function Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/swift13/functions.fir.kt.txt Example of a suspend function with generic type parameters exposed to Swift. ```kotlin @NativeCoroutines suspend fun functionWithGenericValues(value1: T1, value2: T2): String { return toString(/* = value1 */).plus(other = toString(/* = value2 */)) } ``` -------------------------------- ### Flow Implementation in Swift Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/swift5/properties.fir.ir.txt Demonstrates the implementation of a Flow in Swift, including the collect function and delegation. ```swift class MyFlow29 : Flow { private val $$delegate_0: Flow = TODO() override suspend fun collect(collector: FlowCollector) { $$delegate_0.collect(collector) } override fun equals(other: Any?): Boolean = TODO() override fun hashCode(): Int = TODO() override fun toString(): String = TODO() } ``` -------------------------------- ### ViewModel with NativeCoroutines and Custom Scope Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/viewmodelscope.fir.txt Example of a ViewModel using NativeCoroutines for suspending functions and a custom CoroutineScope. ```kotlin public final class MyObservableViewModel2 : R|com/rickclephas/kmp/observableviewmodel/ViewModel| { public constructor(): R|MyObservableViewModel2| { super() } @R|com/rickclephas/kmp/nativecoroutines/NativeCoroutineScope|() internal final val customCoroutineScope: R|kotlinx/coroutines/CoroutineScope| = R|kotlinx/coroutines/CoroutineScope|(Q|kotlinx/coroutines/Dispatchers|.R|kotlinx/coroutines/Dispatchers.Default|) internal get(): R|kotlinx/coroutines/CoroutineScope| @R|com/rickclephas/kmp/nativecoroutines/NativeCoroutines|() public final suspend fun returnSuspendValue1(): R|kotlin/String| { ^returnSuspendValue1 String(OK7) } @R|kotlin/native/ObjCName|(name = String(returnSuspendValue1)) public final fun returnSuspendValue1Native(): R|com/rickclephas/kmp/nativecoroutines/NativeSuspend| { ::R|/MyObservableViewModel2.returnSuspendValue1| R|kotlin/TODO|(String(KMP-NativeCoroutines generated declaration must be implemented in IR)) } } @R|com/rickclephas/kmp/nativecoroutines/NativeCoroutines|() public final suspend fun R|MyObservableViewModel2|.returnSuspendValue2(): R|kotlin/String| { ^returnSuspendValue2 String(OK8) } ``` -------------------------------- ### Constructor and Initializer Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/swift13/coroutinescope.fir.ir.txt Shows the primary constructor for MyClass2, which delegates to MyClass1's constructor and includes an instance initializer. ```kotlin CONSTRUCTOR visibility:public returnType:.MyClass2 [primary] BLOCK_BODY DELEGATING_CONSTRUCTOR_CALL 'public constructor () declared in .MyClass1' INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:MyClass2 modality:FINAL visibility:public superTypes:[.MyClass1]' type=kotlin.Unit ``` -------------------------------- ### Box Test Execution with Native Coroutines Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/swift1/annotations.fir.txt Illustrates the execution of a box test, demonstrating the usage of await, collect, and value functions with various native coroutine calls. ```kotlin public final fun box(): R|kotlin/String| { ^box R|com/rickclephas/kmp/nativecoroutines/runBoxTest|( = runBoxTest@fun R|com/rickclephas/kmp/nativecoroutines/BoxTest|.(): R|kotlin/Unit| { this@R|special/anonymous|.R|com/rickclephas/kmp/nativecoroutines/BoxTest.await|( = await@fun (): R|kotlin/String| { ^ R|/deprecatedFunction1Native|() } ) this@R|special/anonymous|.R|com/rickclephas/kmp/nativecoroutines/BoxTest.await|( = await@fun (): R|kotlin/String| { ^ R|/deprecatedFunction2Native|() } ) this@R|special/anonymous|.R|com/rickclephas/kmp/nativecoroutines/BoxTest.collect|(R|/deprecatedProperty1Native|) this@R|special/anonymous|.R|com/rickclephas/kmp/nativecoroutines/BoxTest.collect|(R|/deprecatedProperty2Native|) this@R|special/anonymous|.R|com/rickclephas/kmp/nativecoroutines/BoxTest.collect|(R|/deprecatedProperty4Native|, Int(1)) this@R|special/anonymous|.R|com/rickclephas/kmp/nativecoroutines/BoxTest.value|(R|/deprecatedProperty4Value|) this@R|special/anonymous|.R|com/rickclephas/kmp/nativecoroutines/BoxTest.await|( = await@fun (): R|kotlin/String| { ^ R|/objCNameFunction1Native|() } ) this@R|special/anonymous|.R|com/rickclephas/kmp/nativecoroutines/BoxTest.await|( = await@fun (): R|kotlin/String| { ^ R|/objCNameFunction2Native|() } ) this@R|special/anonymous|.R|com/rickclephas/kmp/nativecoroutines/BoxTest.await|( = await@fun (): R|kotlin/String| { ^ R|/objCNameFunction3Native|() } ) this@R|special/anonymous|.R|com/rickclephas/kmp/nativecoroutines/BoxTest.collect|(R|/objCNameProperty1Native|, Int(1)) this@R|special/anonymous|.R|com/rickclephas/kmp/nativecoroutines/BoxTest.value|(R|/objCNameProperty1Value|) this@R|special/anonymous|.R|com/rickclephas/kmp/nativecoroutines/BoxTest.collect|(R|/objCNameProperty2Flow|, Int(1)) this@R|special/anonymous|.R|com/rickclephas/kmp/nativecoroutines/BoxTest.value|(R|/objCNameProperty2Value|) this@R|special/anonymous|.R|com/rickclephas/kmp/nativecoroutines/BoxTest.await|( = await@fun (): R|kotlin/String| { ^ R|/objCNameFunctionParameterNative|(String(OK13)) } ) } ) } ``` -------------------------------- ### Get Refined State Value from StateFlow Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/properties.fir.ir.txt Retrieves the current value from a StateFlow, annotated for refinement in Swift. ```kotlin PROPERTY GENERATED[com.rickclephas.kmp.nativecoroutines.compiler.fir.utils.NativeCoroutinesDeclarationKey] name:refinedStateValue visibility:public modality:FINAL [val] annotations: ObjCName(name = "refinedState", swiftName = , exact = ) ShouldRefineInSwift FUN GENERATED[com.rickclephas.kmp.nativecoroutines.compiler.fir.utils.NativeCoroutinesDeclarationKey] name: visibility:public modality:FINAL returnType:kotlin.String correspondingProperty: PROPERTY GENERATED[com.rickclephas.kmp.nativecoroutines.compiler.fir.utils.NativeCoroutinesDeclarationKey] name:refinedStateValue visibility:public modality:FINAL [val] BLOCK_BODY VAR IR_TEMPORARY_VARIABLE name:tmp_53 type:kotlinx.coroutines.flow.StateFlow [val] CALL 'public final fun ():kotlinx.coroutines.flow.StateFlow declared in ' type=kotlinx.coroutines.flow.StateFlow origin=null RETURN type=kotlin.Nothing from='public final fun (): kotlin.String declared in ' CALL 'public abstract fun (): T of kotlinx.coroutines.flow.StateFlow declared in kotlinx.coroutines.flow.StateFlow' type=kotlin.String origin=null ARG : GET_VAR 'val tmp_53:kotlinx.coroutines.flow.StateFlow declared in .' type=kotlinx.coroutines.flow.StateFlow origin=null ``` -------------------------------- ### Box Function Entry Point Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/swift5/functions.fir.ir.txt This snippet represents the main entry point function 'box' which is typically used for testing or bootstrapping an application. It returns a String. ```swift FUN name:box visibility:public modality:FINAL returnType:kotlin.String BLOCK_BODY RETURN type=kotlin.Nothing from='public final fun box (): kotlin.String declared in ' ``` -------------------------------- ### AndroidX ViewModel with Native Coroutines Annotation Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/viewmodelscope.fir.kt.txt Example of a suspend function within an AndroidX ViewModel annotated with @NativeCoroutines. ```kotlin class MyAndroidXViewModel1 : ViewModel { constructor() /* primary */ { super/*ViewModel*/() /* () */ } @ObjCName(name = "returnSuspendValue1") fun returnSuspendValue1Native(): Function3<@ParameterName(name = "onResult") Function2, @ParameterName(name = "onError") Function2, @ParameterName(name = "onCancelled") Function2, Function0> { val tmp_0: CoroutineScope = (/* = */) return nativeSuspend(scope = tmp_0, block = local suspend fun (): String { return .returnSuspendValue1() } ) } @NativeCoroutines suspend fun returnSuspendValue1(): String { return "OK1" } } ``` -------------------------------- ### Custom ViewModel extending AndroidX ViewModel Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/swift5/viewmodelscope.fir.txt An example of a custom ViewModel that extends `androidx.lifecycle.ViewModel` and utilizes the default `viewModelScope`. ```kotlin public final class MyAndroidXViewModel1 : R|androidx/lifecycle/ViewModel| { public constructor(): R|MyAndroidXViewModel1| { super() } @R|com/rickclephas/kmp/nativecoroutines/NativeCoroutines|() public final suspend fun returnSuspendValue1(): R|kotlin/String| { ^returnSuspendValue1 String(OK1) } @R|kotlin/native/ObjCName|(name = String(returnSuspendValue1)) @R|kotlin/Throws|(exceptionClasses = vararg((Q|kotlin/Exception|))) public final suspend fun returnSuspendValue1Native(): R|kotlin/String| { ::R|/MyAndroidXViewModel1.returnSuspendValue1| R|kotlin/TODO|(String(KMP-NativeCoroutines generated declaration must be implemented in IR)) } } @R|com/rickclephas/kmp/nativecoroutines/NativeCoroutines|() public final suspend fun R|MyAndroidXViewModel1|.returnSuspendValue2(): R|kotlin/String| { ^returnSuspendValue2 String(OK2) } ``` -------------------------------- ### Deprecated Property with Error Level Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/annotations.fir.kt.txt Example of a Flow property marked as deprecated with an ERROR level, indicating it should not be used. ```kotlin @NativeCoroutines @Deprecated(message = "This is deprecated 6", replaceWith = ReplaceWith(expression = "deprecatedProperty2", imports = []), level = DeprecationLevel.ERROR) val deprecatedProperty3: Flow field = flowOf(value = "OK6") get ``` -------------------------------- ### Basic Suspend Function Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/swift5/coroutinescope.fir.ir.txt Demonstrates a simple suspend function that returns a String. This serves as a fundamental example of coroutine bridging. ```kotlin FUN name:returnSuspendValue visibility:public modality:FINAL returnType:kotlin.String [suspend] annotations: NativeCoroutines BLOCK_BODY RETURN type=kotlin.Nothing from='public final fun returnSuspendValue (): kotlin.String declared in ' CONST String type=kotlin.String value="OK1" ``` -------------------------------- ### Swift 5: Basic Function Call Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/swift5/functions.box.txt Demonstrates a simple function call in Swift, likely generated from a basic Kotlin function. ```swift func testBasic(){ let result = NativeCoroutinesSwift.basic() XCTAssertEqual(result, "OK") } ``` -------------------------------- ### ObservableViewModel with Native Coroutines Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/swift3/viewmodelscope.fir.txt Shows how to use KMP-NativeCoroutines with a custom `ObservableViewModel`. This example includes a suspend function and its native counterpart. ```kotlin public final class MyObservableViewModel1 : R|com/rickclephas/kmp/observableviewmodel/ViewModel| { public constructor(): R|MyObservableViewModel1| { super() } @R|com/rickclephas/kmp/nativecoroutines/NativeCoroutines|() public final suspend fun returnSuspendValue1(): R|kotlin/String| { ^returnSuspendValue1 String(OK3) } @R|kotlin/native/ObjCName|(name = String(returnSuspendValue1)) public final suspend fun returnSuspendValue1Native(): R|kotlin/String| { ::R|/MyObservableViewModel1.returnSuspendValue1| R|kotlin/TODO|(String(KMP-NativeCoroutines generated declaration must be implemented in IR)) } } @R|com/rickclephas/kmp/nativecoroutines/NativeCoroutines|() public final suspend fun R|MyObservableViewModel1|.returnSuspendValue2(): R|kotlin/String| { ^returnSuspendValue2 String(OK4) } ``` -------------------------------- ### Exposing a Top-Level SharedFlow Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/properties.fir.ir.txt Shows how to expose a top-level `SharedFlow` with a replay cache of 1 using `NativeCoroutines`. The `tryEmit` function is used to send a value. ```kotlin import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.SharedFlow import ru.kontur.kmp.kmpnativecoroutines.NativeCoroutines @NativeCoroutines val topLevelSharedFlow: SharedFlow = MutableSharedFlow(replay = 1).apply { tryEmit("OK2") } ``` -------------------------------- ### Custom Flow Assertions Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/swift1/properties.fir.kt.txt Use this to test custom flow implementations. This example demonstrates asserting a custom flow value. ```kotlin fun box(): String { return runBoxTest(action = local suspend fun BoxTest.() { $this$runBoxTest.collect(flow = ()) }) } ``` -------------------------------- ### Exposing Top-Level Flow to Swift Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/swift1/properties.fir.kt.txt Demonstrates exposing a top-level Flow to Swift, with a specific Objective-C name. This allows Swift to subscribe to the flow. ```swift @ObjCName(name = "topLevelFlow") val topLevelFlowNative: Flow get(): Flow { val tmp_36: CoroutineScope? = null return () } ``` -------------------------------- ### Get Refined State Value Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/properties.fir.kt.txt Retrieves the value from a refined state. Use when accessing the current value of a refined state. ```kotlin $this$runBoxTest.value(value = ()) ``` -------------------------------- ### Get State Property Value Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/properties.fir.kt.txt Retrieves the value from a state property. Use when accessing the current value of a state property. ```kotlin $this$runBoxTest.value(value = ()) ``` -------------------------------- ### Collect Nullable StateFlow and Get Value Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/properties.fir.kt.txt Collects a nullable StateFlow and retrieves its value. Use when dealing with nullable StateFlows. ```kotlin $this$runBoxTest.value(value = ()) ``` -------------------------------- ### Top-Level SharedFlow Property Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/swift13/properties.fir.txt Exposes a top-level Kotlin SharedFlow of strings to Swift. This example demonstrates initializing a MutableSharedFlow and emitting a value. ```kotlin public final val topLevelSharedFlow: SharedFlow = MutableSharedFlow(1).apply { this.tryEmit("OK2") } public get(): SharedFlow ``` -------------------------------- ### Swift Properties: Top-Level Flows Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/swift13/properties.fir.txt Demonstrates collecting from top-level flows, shared flows with replay, and state flows in Swift. ```swift this@R|special/anonymous|.R|com/rickclephas/kmp/nativecoroutines/BoxTest.collect|(R|/topLevelFlowNative|) this@R|special/anonymous|.R|com/rickclephas/kmp/nativecoroutines/BoxTest.collect|(R|/topLevelSharedFlowNative|, Int(1)) this@R|special/anonymous|.R|com/rickclephas/kmp/nativecoroutines/BoxTest.values|(R|/topLevelSharedFlowReplayCache|) this@R|special/anonymous|.R|com/rickclephas/kmp/nativecoroutines/BoxTest.collect|(R|/topLevelStateFlowNative|, Int(1)) this@R|special/anonymous|.R|com/rickclephas/kmp/nativecoroutines/BoxTest.value|(R|/topLevelStateFlowValue|) ``` -------------------------------- ### Interface Flow Assertions Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/swift1/properties.fir.kt.txt Use this to test flows exposed through interfaces. This example specifically tests a flow obtained from an interface property. ```kotlin fun box(): String { return runBoxTest(action = local suspend fun BoxTest.() { $this$runBoxTest.collect(flow = MyClass28().()) }) } ``` -------------------------------- ### Mutable State Property Flow Conversion Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/properties.fir.kt.txt Converts a MutableStateFlow to a native flow representation. Requires a CoroutineScope, which is null in this example. ```kotlin val mutableStatePropertyFlow: Function3<@ParameterName(name = "onItem") Function3, Unit, Unit>, @ParameterName(name = "onComplete") Function2, @ParameterName(name = "onCancelled") Function2, Function0> get(): Function3<@ParameterName(name = "onItem") Function3, Unit, Unit>, @ParameterName(name = "onComplete") Function2, @ParameterName(name = "onCancelled") Function2, Function0> { val tmp_23: CoroutineScope? = null val tmp_24: MutableStateFlow = () return asNativeFlow(/* = tmp_24, */ scope = tmp_23) } ``` -------------------------------- ### MyGenericClass2 with Generic Flows Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/swift3/properties.fir.txt Demonstrates the implementation of generic Flow, SharedFlow, and StateFlow properties within a generic class MyGenericClass2. These properties are directly initialized. ```kotlin public final class MyGenericClass2 : R|kotlin/Any| { public constructor(value: R|T|): R|MyGenericClass2| { super() } private final val value: R|T| = R|/value| private get(): R|T| @R|com/rickclephas/kmp/nativecoroutines/NativeCoroutines|() public final val genericFlow: R|kotlinx/coroutines/flow/Flow| = R|kotlinx/coroutines/flow/flowOf|(this@R|/MyGenericClass2|.R|/MyGenericClass2.value|) public get(): R|kotlinx/coroutines/flow/Flow| @R|com/rickclephas/kmp/nativecoroutines/NativeCoroutines|() public final val genericSharedFlow: R|kotlinx/coroutines/flow/SharedFlow| = R|kotlinx/coroutines/flow/MutableSharedFlow|(Int(1)).R|kotlin/apply||>( = apply@fun R|kotlinx/coroutines/flow/MutableSharedFlow|.(): R|kotlin/Unit| { this@R|special/anonymous|.R|SubstitutionOverride|(this@R|/MyGenericClass2|.R|/MyGenericClass2.value|) } ) public get(): R|kotlinx/coroutines/flow/SharedFlow| @R|com/rickclephas/kmp/nativecoroutines/NativeCoroutines|() public final val genericStateFlow: R|kotlinx/coroutines/flow/StateFlow| = R|kotlinx/coroutines/flow/MutableStateFlow|(this@R|/MyGenericClass2|.R|/MyGenericClass2.value|) public get(): R|kotlinx/coroutines/flow/StateFlow| @R|kotlin/native/ObjCName|(name = String(genericFlow)) public final val genericFlowNative: R|kotlinx/coroutines/flow/Flow| public get(): R|kotlinx/coroutines/flow/Flow| { ::R|/MyGenericClass2.genericFlow| R|kotlin/TODO|(String(KMP-NativeCoroutines generated declaration must be implemented in IR)) } @R|kotlin/native/ObjCName|(name = String(genericSharedFlow)) public final val genericSharedFlowNative: R|kotlinx/coroutines/flow/SharedFlow| public get(): R|kotlinx/coroutines/flow/SharedFlow| { ::R|/MyGenericClass2.genericSharedFlow| R|kotlin/TODO|(String(KMP-NativeCoroutines generated declaration must be implemented in IR)) } public final val genericSharedFlowReplayCache: R|kotlin/collections/List| public get(): R|kotlin/collections/List| { ::R|/MyGenericClass2.genericSharedFlow| R|kotlin/TODO|(String(KMP-NativeCoroutines generated declaration must be implemented in IR)) } @R|kotlin/native/ObjCName|(name = String(genericStateFlow)) public final val genericStateFlowNative: R|kotlinx/coroutines/flow/StateFlow| public get(): R|kotlinx/coroutines/flow/StateFlow| { ::R|/MyGenericClass2.genericStateFlow| R|kotlin/TODO|(String(KMP-NativeCoroutines generated declaration must be implemented in IR)) } public final val genericStateFlowValue: R|T| public get(): R|T| { ::R|/MyGenericClass2.genericStateFlow| R|kotlin/TODO|(String(KMP-NativeCoroutines generated declaration must be implemented in IR)) } } ``` -------------------------------- ### Get Generic StateFlow Value Source: https://github.com/rickclephas/kmp-nativecoroutines/blob/master/kmp-nativecoroutines-compiler/src/testData/codegen/properties.fir.kt.txt Retrieves the value of a generic StateFlow from a generic class. Use when you need the current value of a StateFlow. ```kotlin $this$runBoxTest.value(value = (/* = MyGenericClass1(value = "OK16") */)) ```