[K/N] Catch exceptions in CAdapters when caches enabled (KT-47828 fixed)
This commit is contained in:
+2
-8
@@ -466,7 +466,7 @@ private class ExportedElement(val kind: ElementKind,
|
|||||||
"result", cfunction[0], Direction.KOTLIN_TO_C, builder)
|
"result", cfunction[0], Direction.KOTLIN_TO_C, builder)
|
||||||
builder.append(" return $result;\n")
|
builder.append(" return $result;\n")
|
||||||
}
|
}
|
||||||
builder.append(" } catch (ExceptionObjHolder& e) { std::terminate(); } \n")
|
builder.append(" } catch (...) { HandleCurrentExceptionForCInterop(); } \n")
|
||||||
|
|
||||||
builder.append("}\n")
|
builder.append("}\n")
|
||||||
|
|
||||||
@@ -927,6 +927,7 @@ internal class CAdapterGenerator(val context: Context) : DeclarationDescriptorVi
|
|||||||
|void Kotlin_initRuntimeIfNeeded();
|
|void Kotlin_initRuntimeIfNeeded();
|
||||||
|void Kotlin_mm_switchThreadStateRunnable() RUNTIME_NOTHROW;
|
|void Kotlin_mm_switchThreadStateRunnable() RUNTIME_NOTHROW;
|
||||||
|void Kotlin_mm_switchThreadStateNative() RUNTIME_NOTHROW;
|
|void Kotlin_mm_switchThreadStateNative() RUNTIME_NOTHROW;
|
||||||
|
|void HandleCurrentExceptionForCInterop();
|
||||||
|
|
|
|
||||||
|KObjHeader* CreateStringFromCString(const char*, KObjHeader**);
|
|KObjHeader* CreateStringFromCString(const char*, KObjHeader**);
|
||||||
|char* CreateCStringFromString(const KObjHeader*);
|
|char* CreateCStringFromString(const KObjHeader*);
|
||||||
@@ -961,13 +962,6 @@ internal class CAdapterGenerator(val context: Context) : DeclarationDescriptorVi
|
|||||||
| KObjHeader** frame() { return reinterpret_cast<KObjHeader**>(&frame_); }
|
| KObjHeader** frame() { return reinterpret_cast<KObjHeader**>(&frame_); }
|
||||||
|};
|
|};
|
||||||
|
|
|
|
||||||
|class ExceptionObjHolder {
|
|
||||||
|public:
|
|
||||||
| virtual ~ExceptionObjHolder() = default;
|
|
||||||
|
|
|
||||||
| KObjHeader* GetExceptionObject() noexcept;
|
|
||||||
|};
|
|
||||||
|
|
|
||||||
|class ScopedRunnableState {
|
|class ScopedRunnableState {
|
||||||
|public:
|
|public:
|
||||||
| ScopedRunnableState() noexcept { Kotlin_mm_switchThreadStateRunnable(); }
|
| ScopedRunnableState() noexcept { Kotlin_mm_switchThreadStateRunnable(); }
|
||||||
|
|||||||
@@ -45,6 +45,16 @@ void ThrowException(KRef exception) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void HandleCurrentExceptionForCInterop() {
|
||||||
|
try {
|
||||||
|
std::rethrow_exception(std::current_exception());
|
||||||
|
} catch (ExceptionObjHolder& e) {
|
||||||
|
std::terminate(); // Terminate when it's a kotlin exception.
|
||||||
|
} catch (...) {
|
||||||
|
throw; // Just rethrow if it's unknown.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
class {
|
class {
|
||||||
|
|||||||
@@ -28,6 +28,8 @@ void ThrowException(KRef exception);
|
|||||||
|
|
||||||
void SetKonanTerminateHandler();
|
void SetKonanTerminateHandler();
|
||||||
|
|
||||||
|
void HandleCurrentExceptionForCInterop();
|
||||||
|
|
||||||
RUNTIME_NOTHROW OBJ_GETTER(Kotlin_getExceptionObject, void* holder);
|
RUNTIME_NOTHROW OBJ_GETTER(Kotlin_getExceptionObject, void* holder);
|
||||||
|
|
||||||
// The functions below are implemented in Kotlin (at package kotlin.native.internal).
|
// The functions below are implemented in Kotlin (at package kotlin.native.internal).
|
||||||
|
|||||||
Reference in New Issue
Block a user