diff --git a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/SimpleBridgeGeneratorImpl.kt b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/SimpleBridgeGeneratorImpl.kt index 06164e6658d..41c4cf0da0a 100644 --- a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/SimpleBridgeGeneratorImpl.kt +++ b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/SimpleBridgeGeneratorImpl.kt @@ -17,6 +17,7 @@ package org.jetbrains.kotlin.native.interop.gen import org.jetbrains.kotlin.native.interop.gen.jvm.KotlinPlatform +import org.jetbrains.kotlin.native.interop.indexer.Language import org.jetbrains.kotlin.native.interop.indexer.NativeLibrary import org.jetbrains.kotlin.native.interop.indexer.mapFragmentIsCompilable @@ -120,6 +121,14 @@ class SimpleBridgeGeneratorImpl( nativeLines.add(" $it") } + if (libraryForCStubs.language == Language.OBJECTIVE_C) { + // Prevent Objective-C exceptions from passing to Kotlin: + nativeLines.add(1, "@try {") + nativeLines.add("} @catch (id e) { objc_terminate(); }") + // 'objc_terminate' will report the exception. + // TODO: consider implementing this in bitcode generator. + } + nativeLines.add("}") kotlinLines.add("private external fun $kotlinFunctionName($kotlinParameters): ${returnType.kotlinType}") diff --git a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/StubGenerator.kt b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/StubGenerator.kt index 3a5a30af9b1..9a2011d39fc 100644 --- a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/StubGenerator.kt +++ b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/jvm/StubGenerator.kt @@ -801,7 +801,13 @@ class StubGenerator( "-I$javaHome/../include/$it" } KotlinPlatform.NATIVE -> emptyList() - } + }, + + additionalPreambleLines = configuration.library.additionalPreambleLines + + when (configuration.library.language) { + Language.C -> emptyList() + Language.OBJECTIVE_C -> listOf("void objc_terminate();") + } ) /**