diff --git a/kotlin-native/runtime/build.gradle.kts b/kotlin-native/runtime/build.gradle.kts index 27852619030..ca770cc49b8 100644 --- a/kotlin-native/runtime/build.gradle.kts +++ b/kotlin-native/runtime/build.gradle.kts @@ -113,6 +113,10 @@ bitcode { onlyIf { targetSupportsLibBacktrace(target.name) } } + module("compiler_interface") { + headersDirs.from(files("src/main/cpp")) + } + module("launcher") { headersDirs.from(files("src/main/cpp")) } @@ -344,7 +348,7 @@ targetList.forEach { targetName -> from(project.buildDir.resolve("stdlib/${hostName}/stdlib")) from(project.buildDir.resolve("bitcode/main/$targetName")) { - include("runtime.bc") + include("runtime.bc", "compiler_interface.bc") into("default/targets/$targetName/native") } diff --git a/kotlin-native/runtime/src/compiler_interface/cpp/CompilerCInterface.cpp b/kotlin-native/runtime/src/compiler_interface/cpp/CompilerCInterface.cpp new file mode 100644 index 00000000000..bd0cce35a3e --- /dev/null +++ b/kotlin-native/runtime/src/compiler_interface/cpp/CompilerCInterface.cpp @@ -0,0 +1,81 @@ +/* + * Copyright 2010-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ + +#include "TypeInfo.h" +#include "Memory.h" +#include "Types.h" +#include "Runtime.h" +#include "Exceptions.h" +#include "MemorySharedRefs.hpp" + +#define touchType(type) void touch##type(type*) {} +#define touchFunction(function) void* touch##function() { return reinterpret_cast(&::function); } + +// Types and functions used by the compiler (at Runtime.kt and ContextUtils.kt) +#ifdef __cplusplus +extern "C" { +#endif + +touchType(TypeInfo) +touchType(ExtendedTypeInfo) +touchType(InterfaceTableRecord) +touchType(AssociatedObjectTableRecord) + +touchType(ObjHeader) +touchType(ArrayHeader) +touchType(FrameOverlay) + +touchType(KRefSharedHolder) + +touchFunction(AllocInstance) +touchFunction(AllocArrayInstance) +touchFunction(InitThreadLocalSingleton) +touchFunction(InitSingleton) +touchFunction(InitAndRegisterGlobal) +touchFunction(UpdateHeapRef) +touchFunction(UpdateStackRef) +touchFunction(UpdateReturnRef) +touchFunction(ZeroHeapRef) +touchFunction(ZeroArrayRefs) + +touchFunction(EnterFrame) +touchFunction(LeaveFrame) +touchFunction(SetCurrentFrame) +touchFunction(CheckCurrentFrame) + +touchFunction(MutationCheck) +touchFunction(CheckLifetimesConstraint) +touchFunction(FreezeSubgraph) +touchFunction(CheckGlobalsAccessible) + +touchFunction(LookupInterfaceTableRecord) +touchFunction(IsInstance) +touchFunction(IsInstanceOfClassFast) + +touchFunction(ThrowException) +touchFunction(Kotlin_getExceptionObject) + +touchFunction(AppendToInitializersTail) +touchFunction(CallInitGlobalPossiblyLock) +touchFunction(CallInitThreadLocal) + +touchFunction(AddTLSRecord) +touchFunction(LookupTLS) + +touchFunction(Kotlin_initRuntimeIfNeeded) + +touchFunction(KRefSharedHolder_initLocal) +touchFunction(KRefSharedHolder_init) +touchFunction(KRefSharedHolder_dispose) +touchFunction(KRefSharedHolder_ref) + +touchFunction(Kotlin_mm_switchThreadStateNative) +touchFunction(Kotlin_mm_switchThreadStateRunnable) +touchFunction(Kotlin_mm_safePointFunctionPrologue) +touchFunction(Kotlin_mm_safePointWhileLoopBody) + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/kotlin-native/runtime/src/compiler_interface/cpp/CompilerObjCInterface.mm b/kotlin-native/runtime/src/compiler_interface/cpp/CompilerObjCInterface.mm new file mode 100644 index 00000000000..6082eb1818d --- /dev/null +++ b/kotlin-native/runtime/src/compiler_interface/cpp/CompilerObjCInterface.mm @@ -0,0 +1,61 @@ +/* + * Copyright 2010-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ + +#if KONAN_OBJC_INTEROP + +#include "ObjCInterop.h" +#include "ObjCInteropUtils.h" +#include "ObjCExport.h" +#include "ObjCExportErrors.h" +#include "ObjCExportCoroutines.h" + +#define touchType(type) void touch##type(type*) {} +#define touchFunction(function) void* touch##function() { return reinterpret_cast(&::function); } + +// Types and functions used by the compiler (at Runtime.kt and ContextUtils.kt) +extern "C" { + +touchType(WritableTypeInfo) +touchType(KotlinObjCClassData) +touchType(KotlinObjCClassInfo) +touchType(ObjCMethodDescription) +touchType(ObjCTypeAdapter) +touchType(ObjCToKotlinMethodAdapter) +touchType(KotlinToObjCMethodAdapter) +touchType(TypeInfoObjCExportAddition) + +touchType(Block_literal_1) +touchType(Block_descriptor_1) + +NSString* touchNSString() { return [[NSString alloc] init]; } + +touchFunction(CreateKotlinObjCClass) +touchFunction(GetObjCKotlinTypeInfo) +touchFunction(MissingInitImp) + +touchFunction(Kotlin_Interop_DoesObjectConformToProtocol) +touchFunction(Kotlin_Interop_IsObjectKindOfClass) + +touchFunction(Kotlin_ObjCExport_refToLocalObjC) +touchFunction(Kotlin_ObjCExport_refToRetainedObjC) +touchFunction(Kotlin_ObjCExport_refFromObjC) +touchFunction(Kotlin_ObjCExport_CreateRetainedNSStringFromKString) +touchFunction(Kotlin_ObjCExport_convertUnitToRetained) +touchFunction(Kotlin_ObjCExport_GetAssociatedObject) +touchFunction(Kotlin_ObjCExport_AbstractMethodCalled) +touchFunction(Kotlin_ObjCExport_AbstractClassConstructorCalled) +touchFunction(Kotlin_ObjCExport_RethrowExceptionAsNSError) +touchFunction(Kotlin_ObjCExport_WrapExceptionToNSError) +touchFunction(Kotlin_ObjCExport_NSErrorAsException) +touchFunction(Kotlin_ObjCExport_AllocInstanceWithAssociatedObject) +touchFunction(Kotlin_ObjCExport_createContinuationArgument) +touchFunction(Kotlin_ObjCExport_createUnitContinuationArgument) +touchFunction(Kotlin_ObjCExport_resumeContinuation) +touchFunction(Kotlin_ObjCExport_NSIntegerTypeProvider) +touchFunction(Kotlin_longTypeProvider) + +} + +#endif // KONAN_OBJC_INTEROP diff --git a/kotlin-native/runtime/src/main/cpp/Exceptions.h b/kotlin-native/runtime/src/main/cpp/Exceptions.h index 0046a14c6c3..3d2f84ef39c 100644 --- a/kotlin-native/runtime/src/main/cpp/Exceptions.h +++ b/kotlin-native/runtime/src/main/cpp/Exceptions.h @@ -1,17 +1,6 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. */ #ifndef RUNTIME_EXCEPTIONS_H @@ -24,7 +13,7 @@ extern "C" { #endif // Throws arbitrary exception. -void ThrowException(KRef exception); +void RUNTIME_NORETURN ThrowException(KRef exception); void SetKonanTerminateHandler(); diff --git a/kotlin-native/runtime/src/main/cpp/MemorySharedRefs.hpp b/kotlin-native/runtime/src/main/cpp/MemorySharedRefs.hpp index ee14d5c8d8f..af0a86bc9de 100644 --- a/kotlin-native/runtime/src/main/cpp/MemorySharedRefs.hpp +++ b/kotlin-native/runtime/src/main/cpp/MemorySharedRefs.hpp @@ -1,5 +1,5 @@ /* - * Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ @@ -75,4 +75,11 @@ static_assert( std::is_trivially_destructible_v, "BackRefFromAssociatedObject destructor is not guaranteed to be called."); +extern "C" { +RUNTIME_NOTHROW void KRefSharedHolder_initLocal(KRefSharedHolder* holder, ObjHeader* obj); +RUNTIME_NOTHROW void KRefSharedHolder_init(KRefSharedHolder* holder, ObjHeader* obj); +RUNTIME_NOTHROW void KRefSharedHolder_dispose(const KRefSharedHolder* holder); +RUNTIME_NOTHROW ObjHeader* KRefSharedHolder_ref(const KRefSharedHolder* holder); +} // extern "C" + #endif // RUNTIME_MEMORYSHAREDREFS_HPP diff --git a/kotlin-native/runtime/src/main/cpp/ObjCExport.h b/kotlin-native/runtime/src/main/cpp/ObjCExport.h index eb921a7a7b8..14faa81fb2e 100644 --- a/kotlin-native/runtime/src/main/cpp/ObjCExport.h +++ b/kotlin-native/runtime/src/main/cpp/ObjCExport.h @@ -1,3 +1,8 @@ +/* + * Copyright 2010-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ + #ifndef RUNTIME_OBJCEXPORT_H #define RUNTIME_OBJCEXPORT_H @@ -9,9 +14,116 @@ #import "Types.h" #import "Memory.h" -extern "C" id objc_retain(id self); -extern "C" id objc_retainBlock(id self); -extern "C" void objc_release(id self); +extern "C" { + +struct ObjCToKotlinMethodAdapter { + const char* selector; + const char* encoding; + IMP imp; +}; + +struct KotlinToObjCMethodAdapter { + const char* selector; + ClassId interfaceId; + int itableSize; + int itableIndex; + int vtableIndex; + const void* kotlinImpl; +}; + +struct ObjCTypeAdapter { + const TypeInfo* kotlinTypeInfo; + + const void * const * kotlinVtable; + int kotlinVtableSize; + + const InterfaceTableRecord* kotlinItable; + int kotlinItableSize; + + const char* objCName; + + const ObjCToKotlinMethodAdapter* directAdapters; + int directAdapterNum; + + const ObjCToKotlinMethodAdapter* classAdapters; + int classAdapterNum; + + const ObjCToKotlinMethodAdapter* virtualAdapters; + int virtualAdapterNum; + + const KotlinToObjCMethodAdapter* reverseAdapters; + int reverseAdapterNum; +}; + +struct TypeInfoObjCExportAddition { + /*convertReferenceToRetainedObjC*/ void* convertToRetained; + Class objCClass; + const ObjCTypeAdapter* typeAdapter; +}; + +struct WritableTypeInfo { + TypeInfoObjCExportAddition objCExport; +}; + +struct Block_descriptor_1; + +// Based on https://clang.llvm.org/docs/Block-ABI-Apple.html and libclosure source. +struct Block_literal_1 { + void *isa; // initialized to &_NSConcreteStackBlock or &_NSConcreteGlobalBlock + int flags; + int reserved; + void (*invoke)(void *, ...); + struct Block_descriptor_1 *descriptor; // IFF (1<<25) + // Or: + // struct Block_descriptor_1_without_helpers* descriptor // if hasn't (1<<25). + + // imported variables +}; + +struct Block_descriptor_1 { + unsigned long int reserved; // NULL + unsigned long int size; // sizeof(struct Block_literal_1) + + // optional helper functions + void (*copy_helper)(void *dst, void *src); + void (*dispose_helper)(void *src); + // required ABI.2010.3.16 + const char *signature; // IFF (1<<30) + const void* layout; // IFF (1<<31) +}; + +struct Block_descriptor_1_without_helpers { + unsigned long int reserved; // NULL + unsigned long int size; // sizeof(struct Block_literal_1) + + // required ABI.2010.3.16 + const char *signature; // IFF (1<<30) + const void* layout; // IFF (1<<31) +}; + +id objc_retain(id self); +id objc_retainBlock(id self); +void objc_release(id self); + +id Kotlin_ObjCExport_refToObjC(ObjHeader* obj); +id Kotlin_ObjCExport_refToLocalObjC(ObjHeader* obj); +id Kotlin_ObjCExport_refToRetainedObjC(ObjHeader* obj); +id Kotlin_ObjCExport_CreateRetainedNSStringFromKString(ObjHeader* str); +id Kotlin_ObjCExport_convertUnitToRetained(ObjHeader* unitInstance); +id Kotlin_ObjCExport_GetAssociatedObject(ObjHeader* obj); +void Kotlin_ObjCExport_AbstractMethodCalled(id self, SEL selector); +void Kotlin_ObjCExport_AbstractClassConstructorCalled(id self, const TypeInfo *clazz); +OBJ_GETTER(Kotlin_ObjCExport_refFromObjC, id obj); +RUNTIME_NOTHROW OBJ_GETTER(Kotlin_ObjCExport_AllocInstanceWithAssociatedObject, + const TypeInfo* typeInfo, id associatedObject); + +id Kotlin_Interop_CreateNSStringFromKString(KRef str); +OBJ_GETTER(Kotlin_Interop_CreateKStringFromNSString, NSString* str); + +/// Utility function that is used to determine NSInteger size in compile time. +NSInteger Kotlin_ObjCExport_NSIntegerTypeProvider(); + +} // extern "C" inline static id GetAssociatedObject(ObjHeader* obj) { return (id)obj->GetAssociatedObject(); @@ -32,16 +144,6 @@ inline static OBJ_GETTER(AllocInstanceWithAssociatedObject, const TypeInfo* type return result; } -extern "C" id Kotlin_ObjCExport_refToObjC(ObjHeader* obj); -extern "C" id Kotlin_ObjCExport_refToLocalObjC(ObjHeader* obj); -extern "C" OBJ_GETTER(Kotlin_ObjCExport_refFromObjC, id obj); - -extern "C" id Kotlin_Interop_CreateNSStringFromKString(KRef str); -extern "C" OBJ_GETTER(Kotlin_Interop_CreateKStringFromNSString, NSString* str); - -/// Utility function that is used to determine NSInteger size in compile time. -extern "C" NSInteger Kotlin_ObjCExport_NSIntegerTypeProvider(); - #endif // KONAN_OBJC_INTEROP #endif // RUNTIME_OBJCEXPORT_H diff --git a/kotlin-native/runtime/src/main/cpp/ObjCExport.mm b/kotlin-native/runtime/src/main/cpp/ObjCExport.mm index 8d65ac90027..8b1dcdce8c4 100644 --- a/kotlin-native/runtime/src/main/cpp/ObjCExport.mm +++ b/kotlin-native/runtime/src/main/cpp/ObjCExport.mm @@ -1,17 +1,6 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. */ #import "Types.h" @@ -61,58 +50,9 @@ inline T* konanAllocArray(size_t length) { } -struct ObjCToKotlinMethodAdapter { - const char* selector; - const char* encoding; - IMP imp; -}; - -struct KotlinToObjCMethodAdapter { - const char* selector; - ClassId interfaceId; - int itableSize; - int itableIndex; - int vtableIndex; - const void* kotlinImpl; -}; - -struct ObjCTypeAdapter { - const TypeInfo* kotlinTypeInfo; - - const void * const * kotlinVtable; - int kotlinVtableSize; - - const InterfaceTableRecord* kotlinItable; - int kotlinItableSize; - - const char* objCName; - - const ObjCToKotlinMethodAdapter* directAdapters; - int directAdapterNum; - - const ObjCToKotlinMethodAdapter* classAdapters; - int classAdapterNum; - - const ObjCToKotlinMethodAdapter* virtualAdapters; - int virtualAdapterNum; - - const KotlinToObjCMethodAdapter* reverseAdapters; - int reverseAdapterNum; -}; - typedef id (*convertReferenceToRetainedObjC)(ObjHeader* obj); typedef OBJ_GETTER((*convertReferenceFromObjC), id obj); -struct TypeInfoObjCExportAddition { - /*convertReferenceToRetainedObjC*/ void* convertToRetained; - Class objCClass; - const ObjCTypeAdapter* typeAdapter; -}; - -struct WritableTypeInfo { - TypeInfoObjCExportAddition objCExport; -}; - static char associatedTypeInfoKey; @@ -454,44 +394,8 @@ static OBJ_GETTER(boxedBooleanToKotlinImp, NSNumber* self, SEL cmd) { RETURN_RESULT_OF(Kotlin_boxBoolean, self.boolValue); } -struct Block_descriptor_1; - -// Based on https://clang.llvm.org/docs/Block-ABI-Apple.html and libclosure source. -struct Block_literal_1 { - void *isa; // initialized to &_NSConcreteStackBlock or &_NSConcreteGlobalBlock - int flags; - int reserved; - void (*invoke)(void *, ...); - struct Block_descriptor_1 *descriptor; // IFF (1<<25) - // Or: - // struct Block_descriptor_1_without_helpers* descriptor // if hasn't (1<<25). - - // imported variables -}; - struct Block_literal_1 exportBlockLiteral; -struct Block_descriptor_1 { - unsigned long int reserved; // NULL - unsigned long int size; // sizeof(struct Block_literal_1) - - // optional helper functions - void (*copy_helper)(void *dst, void *src); - void (*dispose_helper)(void *src); - // required ABI.2010.3.16 - const char *signature; // IFF (1<<30) - const void* layout; // IFF (1<<31) -}; - -struct Block_descriptor_1_without_helpers { - unsigned long int reserved; // NULL - unsigned long int size; // sizeof(struct Block_literal_1) - - // required ABI.2010.3.16 - const char *signature; // IFF (1<<30) - const void* layout; // IFF (1<<31) -}; - static const char* getBlockEncoding(id block) { Block_literal_1* literal = reinterpret_cast(block); diff --git a/kotlin-native/runtime/src/main/cpp/ObjCExportCoroutines.h b/kotlin-native/runtime/src/main/cpp/ObjCExportCoroutines.h new file mode 100644 index 00000000000..bf64a5b73b9 --- /dev/null +++ b/kotlin-native/runtime/src/main/cpp/ObjCExportCoroutines.h @@ -0,0 +1,10 @@ +/* + * Copyright 2010-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ + +#include "Types.h" + +extern "C" OBJ_GETTER(Kotlin_ObjCExport_createContinuationArgument, id completion, const TypeInfo** exceptionTypes); +extern "C" OBJ_GETTER(Kotlin_ObjCExport_createUnitContinuationArgument, id completion, const TypeInfo** exceptionTypes); +extern "C" void Kotlin_ObjCExport_resumeContinuation(KRef continuation, KRef result, id error); \ No newline at end of file diff --git a/kotlin-native/runtime/src/main/cpp/ObjCExportErrors.h b/kotlin-native/runtime/src/main/cpp/ObjCExportErrors.h index 85762623439..527a9f316be 100644 --- a/kotlin-native/runtime/src/main/cpp/ObjCExportErrors.h +++ b/kotlin-native/runtime/src/main/cpp/ObjCExportErrors.h @@ -1,10 +1,11 @@ /* - * Copyright 2010-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ #import "Types.h" extern "C" id Kotlin_ObjCExport_ExceptionAsNSError(KRef exception, const TypeInfo** types); +extern "C" void Kotlin_ObjCExport_RethrowExceptionAsNSError(KRef exception, id* outError, const TypeInfo** types); extern "C" id Kotlin_ObjCExport_WrapExceptionToNSError(KRef exception); extern "C" OBJ_GETTER(Kotlin_ObjCExport_NSErrorAsException, id error); diff --git a/kotlin-native/runtime/src/main/cpp/ObjCInterop.h b/kotlin-native/runtime/src/main/cpp/ObjCInterop.h index 30df929206a..c3a29d33886 100644 --- a/kotlin-native/runtime/src/main/cpp/ObjCInterop.h +++ b/kotlin-native/runtime/src/main/cpp/ObjCInterop.h @@ -1,5 +1,5 @@ /* - * Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ @@ -8,6 +8,58 @@ #if KONAN_OBJC_INTEROP +#include +#include +#include + +#include +#include +#include + +#include "TypeInfo.h" + +extern "C" { + +struct KotlinObjCClassData { + const TypeInfo* typeInfo; + Class objcClass; + int32_t bodyOffset; +}; + +struct ObjCMethodDescription { + void* (*imp)(void*, void*, ...); + const char* selector; + const char* encoding; +}; + +struct KotlinObjCClassInfo { + const char* name; + int exported; + + const char* superclassName; + const char** protocolNames; + + const struct ObjCMethodDescription* instanceMethods; + int32_t instanceMethodsNum; + + const struct ObjCMethodDescription* classMethods; + int32_t classMethodsNum; + + int32_t* bodyOffset; + + const TypeInfo* typeInfo; + const TypeInfo* metaTypeInfo; + + void** createdClass; + + KotlinObjCClassData* (*classDataImp)(void*, void*); +}; + +void* CreateKotlinObjCClass(const KotlinObjCClassInfo* info); +RUNTIME_NOTHROW const TypeInfo* GetObjCKotlinTypeInfo(ObjHeader* obj); + +} // extern "C" + const char* Kotlin_ObjCInterop_getUniquePrefix(); #endif // KONAN_OBJC_INTEROP diff --git a/kotlin-native/runtime/src/main/cpp/ObjCInterop.mm b/kotlin-native/runtime/src/main/cpp/ObjCInterop.mm index 49961417995..e557e09b8b3 100644 --- a/kotlin-native/runtime/src/main/cpp/ObjCInterop.mm +++ b/kotlin-native/runtime/src/main/cpp/ObjCInterop.mm @@ -1,17 +1,6 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. */ #if KONAN_OBJC_INTEROP @@ -51,12 +40,6 @@ const char* Kotlin_ObjCInterop_getUniquePrefix() { extern "C" id objc_msgSendSuper2(struct objc_super *super, SEL op, ...); -struct KotlinObjCClassData { - const TypeInfo* typeInfo; - Class objcClass; - int32_t bodyOffset; -}; - // Acts only as container for the method, not actually applied to any class. @protocol HasKotlinObjCClassData @required @@ -179,8 +162,6 @@ Class Kotlin_Interop_getObjCClass(const char* name) { return result; } -const TypeInfo* GetObjCKotlinTypeInfo(ObjHeader* obj) RUNTIME_NOTHROW; - RUNTIME_NOTHROW const TypeInfo* GetObjCKotlinTypeInfo(ObjHeader* obj) { void* objcPtr = obj->GetAssociatedObject(); RuntimeAssert(objcPtr != nullptr, ""); @@ -223,35 +204,6 @@ static void AddKotlinClassData(bool isClassMethod, Class clazz, void* imp) { RuntimeCheck(added, "Unable to add method to Objective-C class"); } -struct ObjCMethodDescription { - void* (*imp)(void*, void*, ...); - const char* selector; - const char* encoding; -}; - -struct KotlinObjCClassInfo { - const char* name; - int exported; - - const char* superclassName; - const char** protocolNames; - - const struct ObjCMethodDescription* instanceMethods; - int32_t instanceMethodsNum; - - const struct ObjCMethodDescription* classMethods; - int32_t classMethodsNum; - - int32_t* bodyOffset; - - const TypeInfo* typeInfo; - const TypeInfo* metaTypeInfo; - - void** createdClass; - - KotlinObjCClassData* (*classDataImp)(void*, void*); -}; - static void AddMethods(Class clazz, const struct ObjCMethodDescription* methods, int32_t methodsNum) { for (int32_t i = 0; i < methodsNum; ++i) { const struct ObjCMethodDescription* method = &methods[i]; diff --git a/kotlin-native/runtime/src/main/cpp/ObjCInteropUtils.h b/kotlin-native/runtime/src/main/cpp/ObjCInteropUtils.h new file mode 100644 index 00000000000..69f3ff6afd2 --- /dev/null +++ b/kotlin-native/runtime/src/main/cpp/ObjCInteropUtils.h @@ -0,0 +1,22 @@ +/* + * Copyright 2010-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ + +#ifndef RUNTIME_OBJCINTEROPUTILS_H +#define RUNTIME_OBJCINTEROPUTILS_H + +#if KONAN_OBJC_INTEROP + +#import +#import "Types.h" + +extern "C" { +id MissingInitImp(id self, SEL _cmd); +KBoolean Kotlin_Interop_DoesObjectConformToProtocol(id obj, void* prot, KBoolean isMeta); +KBoolean Kotlin_Interop_IsObjectKindOfClass(id obj, void* cls); +} + +#endif // KONAN_OBJC_INTEROP + +#endif // RUNTIME_OBJCINTEROPUTILS_H \ No newline at end of file diff --git a/kotlin-native/runtime/src/main/cpp/ObjCInteropUtils.mm b/kotlin-native/runtime/src/main/cpp/ObjCInteropUtils.mm index fa5416641e4..7941af55c41 100644 --- a/kotlin-native/runtime/src/main/cpp/ObjCInteropUtils.mm +++ b/kotlin-native/runtime/src/main/cpp/ObjCInteropUtils.mm @@ -1,17 +1,6 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. */ #include "Natives.h" @@ -23,6 +12,7 @@ #import #import #import "Memory.h" +#import "ObjCInteropUtils.h" #import "ObjCInteropUtilsPrivate.h" namespace { diff --git a/kotlin-native/runtime/src/main/cpp/ObjCInteropUtilsPrivate.h b/kotlin-native/runtime/src/main/cpp/ObjCInteropUtilsPrivate.h index e5081fa4e15..01673ff7b5f 100644 --- a/kotlin-native/runtime/src/main/cpp/ObjCInteropUtilsPrivate.h +++ b/kotlin-native/runtime/src/main/cpp/ObjCInteropUtilsPrivate.h @@ -1,5 +1,5 @@ /* - * Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ @@ -13,6 +13,7 @@ #import "Types.h" #import "Memory.h" +extern "C" id MissingInitImp(id self, SEL _cmd); extern "C" id (*Kotlin_Interop_createKotlinObjectHolder_ptr)(KRef any); extern "C" KRef (*Kotlin_Interop_unwrapKotlinObjectHolder_ptr)(id holder); extern "C" OBJ_GETTER((*Konan_ObjCInterop_getWeakReference_ptr), KRef ref); diff --git a/kotlin-native/runtime/src/main/cpp/Runtime.cpp b/kotlin-native/runtime/src/main/cpp/Runtime.cpp index 42f8258702c..4c23a1d084b 100644 --- a/kotlin-native/runtime/src/main/cpp/Runtime.cpp +++ b/kotlin-native/runtime/src/main/cpp/Runtime.cpp @@ -1,17 +1,6 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. */ #include "Atomic.h" @@ -41,12 +30,6 @@ using kotlin::internal::FILE_BEING_INITIALIZED; using kotlin::internal::FILE_INITIALIZED; using kotlin::internal::FILE_FAILED_TO_INITIALIZE; -typedef void (*Initializer)(int initialize, MemoryState* memory); -struct InitNode { - Initializer init; - InitNode* next; -}; - namespace { InitNode* initHeadNode = nullptr; @@ -209,7 +192,7 @@ void Kotlin_deinitRuntimeCallback(void* argument) { extern "C" { -void AppendToInitializersTail(InitNode *next) { +RUNTIME_NOTHROW void AppendToInitializersTail(InitNode *next) { // TODO: use RuntimeState. if (initHeadNode == nullptr) { initHeadNode = next; diff --git a/kotlin-native/runtime/src/main/cpp/Runtime.h b/kotlin-native/runtime/src/main/cpp/Runtime.h index 218e8a13115..b7b073fc55b 100644 --- a/kotlin-native/runtime/src/main/cpp/Runtime.h +++ b/kotlin-native/runtime/src/main/cpp/Runtime.h @@ -1,17 +1,6 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. */ #ifndef RUNTIME_RUNTIME_H @@ -19,12 +8,16 @@ #include "Porting.h" -struct InitNode; - #ifdef __cplusplus extern "C" { #endif +typedef void (*Initializer)(int initialize, MemoryState* memory); +struct InitNode { + Initializer init; + InitNode* next; +}; + // For experimental MM, if runtime gets initialized, it will be in the native state after this. RUNTIME_NOTHROW void Kotlin_initRuntimeIfNeeded(); void Kotlin_deinitRuntimeIfNeeded(); @@ -36,7 +29,7 @@ void Kotlin_deinitRuntimeIfNeeded(); void Kotlin_shutdownRuntime(); // Appends given node to an initializer list. -void AppendToInitializersTail(struct InitNode*); +RUNTIME_NOTHROW void AppendToInitializersTail(struct InitNode*); void CallInitGlobalPossiblyLock(int* state, void (*init)()); void CallInitThreadLocal(int volatile* globalState, int* localState, void (*init)()); diff --git a/kotlin-native/runtime/src/main/cpp/TypeInfo.h b/kotlin-native/runtime/src/main/cpp/TypeInfo.h index c776f3bec0c..df973debe7a 100644 --- a/kotlin-native/runtime/src/main/cpp/TypeInfo.h +++ b/kotlin-native/runtime/src/main/cpp/TypeInfo.h @@ -1,17 +1,6 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. */ #ifndef RUNTIME_TYPEINFO_H @@ -27,7 +16,12 @@ struct WritableTypeInfo; #endif struct ObjHeader; -struct AssociatedObjectTableRecord; +struct TypeInfo; + +struct AssociatedObjectTableRecord { + const TypeInfo* key; + ObjHeader* (*getAssociatedObjectInstance)(ObjHeader**); +}; // Type for runtime representation of Konan object. // Keep in sync with runtimeTypeMap in RTTIGenerator. diff --git a/kotlin-native/runtime/src/main/cpp/Types.cpp b/kotlin-native/runtime/src/main/cpp/Types.cpp index d5f2f04c44d..e2c58146589 100644 --- a/kotlin-native/runtime/src/main/cpp/Types.cpp +++ b/kotlin-native/runtime/src/main/cpp/Types.cpp @@ -1,17 +1,6 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. */ #include "Types.h" @@ -73,11 +62,6 @@ OBJ_GETTER(Kotlin_TypeInfo_getRelativeName, KNativePtr typeInfo, KBoolean checkF } } -struct AssociatedObjectTableRecord { - const TypeInfo* key; - OBJ_GETTER0((*getAssociatedObjectInstance)); -}; - OBJ_GETTER(Kotlin_TypeInfo_findAssociatedObject, KNativePtr typeInfo, KNativePtr key) { const AssociatedObjectTableRecord* associatedObjects = reinterpret_cast(typeInfo)->associatedObjects; if (associatedObjects == nullptr) {