diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/InteropLowering.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/InteropLowering.kt index 7e228c6fb83..66720bdb0ad 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/InteropLowering.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/InteropLowering.kt @@ -127,7 +127,7 @@ private class InteropLoweringPart1(val context: Context) : BaseInteropIrTransfor lateinit var currentFile: IrFile - private val topLevelInitializers = mutableListOf() + private val eagerTopLevelInitializers = mutableListOf() private val newTopLevelDeclarations = mutableListOf() override val irFile: IrFile @@ -142,8 +142,8 @@ private class InteropLoweringPart1(val context: Context) : BaseInteropIrTransfor currentFile = irFile irFile.transformChildrenVoid(this) - topLevelInitializers.forEach { irFile.addTopLevelInitializer(it, context, false) } - topLevelInitializers.clear() + eagerTopLevelInitializers.forEach { irFile.addTopLevelInitializer(it, context, threadLocal = false, eager = true) } + eagerTopLevelInitializers.clear() irFile.addChildren(newTopLevelDeclarations) newTopLevelDeclarations.clear() @@ -191,7 +191,7 @@ private class InteropLoweringPart1(val context: Context) : BaseInteropIrTransfor if (irClass.annotations.hasAnnotation(interop.exportObjCClass.fqNameSafe)) { val irBuilder = context.createIrBuilder(currentFile.symbol).at(irClass) - topLevelInitializers.add(irBuilder.getObjCClass(symbols, irClass.symbol)) + eagerTopLevelInitializers.add(irBuilder.getObjCClass(symbols, irClass.symbol)) } } diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/ir/util/IrUtils2.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/ir/util/IrUtils2.kt index 446b486e22e..a0f5b4f1319 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/ir/util/IrUtils2.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/ir/util/IrUtils2.kt @@ -62,7 +62,7 @@ internal fun irBuilder( private var topLevelInitializersCounter = 0 -internal fun IrFile.addTopLevelInitializer(expression: IrExpression, context: KonanBackendContext, threadLocal: Boolean) { +internal fun IrFile.addTopLevelInitializer(expression: IrExpression, context: KonanBackendContext, threadLocal: Boolean, eager: Boolean) { val irField = IrFieldImpl( expression.startOffset, expression.endOffset, IrDeclarationOrigin.DEFINED, @@ -79,6 +79,9 @@ internal fun IrFile.addTopLevelInitializer(expression: IrExpression, context: Ko if (threadLocal) annotations += buildSimpleAnnotation(context.irBuiltIns, startOffset, endOffset, context.ir.symbols.threadLocal.owner) + if (eager) + annotations += buildSimpleAnnotation(context.irBuiltIns, startOffset, endOffset, context.ir.symbols.eagerInitialization.owner) + initializer = IrExpressionBodyImpl(startOffset, endOffset, expression) } addChild(irField) diff --git a/kotlin-native/backend.native/tests/interop/objc/objcTests.def b/kotlin-native/backend.native/tests/interop/objc/objcTests.def index 66090941d9a..e3583653cb0 100644 --- a/kotlin-native/backend.native/tests/interop/objc/objcTests.def +++ b/kotlin-native/backend.native/tests/interop/objc/objcTests.def @@ -1,6 +1,8 @@ language = Objective-C -headers = Foundation/NSArray.h Foundation/NSValue.h Foundation/NSString.h Foundation/NSStream.h Foundation/NSBundle.h +headers = Foundation/NSArray.h Foundation/NSValue.h Foundation/NSString.h Foundation/NSStream.h Foundation/NSBundle.h \ + objc/runtime.h headerFilter = **/interop/objc/tests/**.h \ Foundation/NSArray.h Foundation/NSValue.h Foundation/NSString.h Foundation/NSStream.h \ - objc/objc.h Foundation/NSBundle.h + objc/objc.h Foundation/NSBundle.h \ + objc/runtime.h linkerOpts = -lobjctests diff --git a/kotlin-native/backend.native/tests/interop/objc/tests/kt53373_1.kt b/kotlin-native/backend.native/tests/interop/objc/tests/kt53373_1.kt new file mode 100644 index 00000000000..9072ccd431c --- /dev/null +++ b/kotlin-native/backend.native/tests/interop/objc/tests/kt53373_1.kt @@ -0,0 +1,5 @@ +import objcTests.* +import kotlinx.cinterop.* + +@ExportObjCClass +class KT53373 : NSObject() \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/interop/objc/tests/kt53373_2.kt b/kotlin-native/backend.native/tests/interop/objc/tests/kt53373_2.kt new file mode 100644 index 00000000000..45957434c2e --- /dev/null +++ b/kotlin-native/backend.native/tests/interop/objc/tests/kt53373_2.kt @@ -0,0 +1,10 @@ +import kotlinx.cinterop.* +import kotlin.test.* +import objcTests.* + +@Test +fun testKT53373() { + val kt53373Class = objc_lookUpClass("KT53373") + assertNotNull(kt53373Class) + assertEquals("KT53373", class_getName(kt53373Class)?.toKString()) +}