From 2e8c25aeff7ade48106076934b807e0bd7595700 Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Thu, 28 Jul 2022 17:55:34 +0200 Subject: [PATCH] Native: rewrite Kotlin_Interop_getObjCClass in Objective-C This allows calling it in "Native" thread state. --- .../kotlin/kotlinx/cinterop/ObjectiveCImpl.kt | 16 ---------------- .../runtime/src/main/cpp/ObjCInterop.mm | 15 +++++---------- .../test_support/cpp/CompilerGeneratedObjC.mm | 9 --------- 3 files changed, 5 insertions(+), 35 deletions(-) diff --git a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCImpl.kt b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCImpl.kt index b717e6364ca..41d85fe34ce 100644 --- a/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCImpl.kt +++ b/kotlin-native/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCImpl.kt @@ -133,19 +133,6 @@ internal annotation class ObjCMethodImp(val selector: String, val encoding: Stri @TypedIntrinsic(IntrinsicType.OBJC_GET_SELECTOR) internal external fun objCGetSelector(selector: String): COpaquePointer -@kotlin.native.internal.ExportForCppRuntime("Kotlin_Interop_getObjCClass") -private fun getObjCClassByName(name: NativePtr): NativePtr { - val result = objc_lookUpClass(name) - if (result == nativeNullPtr) { - val className = interpretCPointer(name)!!.toKString() - val message = """Objective-C class '$className' not found. - |Ensure that the containing framework or library was linked.""".trimMargin() - - throw RuntimeException(message) - } - return result -} - @kotlin.native.internal.ExportForCompiler private fun allocObjCObject(clazz: NativePtr): NativePtr { val rawResult = objc_allocWithZone(clazz) @@ -219,6 +206,3 @@ external fun objc_retain(ptr: NativePtr): NativePtr @GCUnsafeCall("Kotlin_objc_release") external fun objc_release(ptr: NativePtr) - -@GCUnsafeCall("Kotlin_objc_lookUpClass") -external fun objc_lookUpClass(name: NativePtr): NativePtr diff --git a/kotlin-native/runtime/src/main/cpp/ObjCInterop.mm b/kotlin-native/runtime/src/main/cpp/ObjCInterop.mm index 6dfec20b6da..5539c166200 100644 --- a/kotlin-native/runtime/src/main/cpp/ObjCInterop.mm +++ b/kotlin-native/runtime/src/main/cpp/ObjCInterop.mm @@ -173,7 +173,11 @@ void releaseAsAssociatedObjectImp(id self, SEL _cmd, ReleaseMode mode) { extern "C" { -Class Kotlin_Interop_getObjCClass(const char* name); +Class Kotlin_Interop_getObjCClass(const char* name) { + Class result = objc_lookUpClass(name); + RuntimeCheck(result != nil, "Objective-C class '%s' not found. Ensure that the containing framework or library was linked.", name); + return result; +} const TypeInfo* GetObjCKotlinTypeInfo(ObjHeader* obj) RUNTIME_NOTHROW; @@ -391,10 +395,6 @@ void Kotlin_objc_release(id ptr) { objc_release(ptr); } -Class Kotlin_objc_lookUpClass(const char* name) { - return objc_lookUpClass(name); -} - } // extern "C" #else // KONAN_OBJC_INTEROP @@ -426,11 +426,6 @@ void Kotlin_objc_release(void* ptr) { RuntimeAssert(false, "Objective-C interop is disabled"); } -void* Kotlin_objc_lookUpClass(const char* name) { - RuntimeAssert(false, "Objective-C interop is disabled"); - return nullptr; -} - } // extern "C" #endif // KONAN_OBJC_INTEROP diff --git a/kotlin-native/runtime/src/test_support/cpp/CompilerGeneratedObjC.mm b/kotlin-native/runtime/src/test_support/cpp/CompilerGeneratedObjC.mm index f49694f4568..b138ff0b36c 100644 --- a/kotlin-native/runtime/src/test_support/cpp/CompilerGeneratedObjC.mm +++ b/kotlin-native/runtime/src/test_support/cpp/CompilerGeneratedObjC.mm @@ -14,15 +14,6 @@ extern "C" { -Class Kotlin_Interop_getObjCClass(const char* name) { - Class result = objc_lookUpClass(name); - if (result == nil) { - // GTest can display error messages of C++ exceptions so we use them instead of ObjC ones. - throw std::invalid_argument("Incorrect class name"); - } - return result; -} - RUNTIME_NORETURN OBJ_GETTER0(Kotlin_NSEnumeratorAsKIterator_create) { throw std::runtime_error("Not implemented for tests"); }