Improve kotlin.Unit support in produced framework

Make the Objective-C wrapper unique, and provide the method to get it
This commit is contained in:
Svyatoslav Scherbina
2017-12-07 14:00:45 +03:00
committed by SvyatoslavScherbina
parent 7696f41f6c
commit 0d082d6730
6 changed files with 64 additions and 6 deletions
@@ -398,6 +398,7 @@ internal class Llvm(val context: Context, val llvmModule: LLVMModuleRef) {
val Kotlin_ObjCExport_refFromObjC by lazyRtFunction
val Kotlin_Interop_CreateNSStringFromKString by lazyRtFunction
val Kotlin_Interop_CreateNSArrayFromKList by lazyRtFunction
val Kotlin_ObjCExport_convertUnit by lazyRtFunction
val Kotlin_ObjCExport_GetAssociatedObject by lazyRtFunction
val Kotlin_ObjCExport_AbstractMethodCalled by lazyRtFunction
@@ -254,6 +254,7 @@ internal class ObjCExportCodeGenerator(
methodTable: List<RTTIGenerator.MethodTableRecord>,
val objCName: String,
directAdapters: List<ObjCToKotlinMethodAdapter>,
classAdapters: List<ObjCToKotlinMethodAdapter>,
virtualAdapters: List<ObjCToKotlinMethodAdapter>,
reverseAdapters: List<KotlinToObjCMethodAdapter>
) : Struct(
@@ -275,6 +276,13 @@ internal class ObjCExportCodeGenerator(
),
Int32(directAdapters.size),
staticData.placeGlobalConstArray(
"",
runtime.objCToKotlinMethodAdapter,
classAdapters
),
Int32(classAdapters.size),
staticData.placeGlobalConstArray(
"",
runtime.objCToKotlinMethodAdapter,
@@ -611,7 +619,8 @@ private fun ObjCExportCodeGenerator.createTypeAdapterForPackage(
vtableSize = -1,
methodTable = emptyList(),
objCName = name,
directAdapters = adapters,
directAdapters = emptyList(),
classAdapters = adapters,
virtualAdapters = emptyList(),
reverseAdapters = emptyList()
)
@@ -719,6 +728,12 @@ private fun ObjCExportCodeGenerator.createTypeAdapter(
emptyList()
}
val classAdapters = mutableListOf<ObjCExportCodeGenerator.ObjCToKotlinMethodAdapter>()
if (descriptor.isUnit()) {
classAdapters += createUnitInstanceAdapter()
}
return ObjCTypeAdapter(
descriptor,
typeInfo,
@@ -727,11 +742,26 @@ private fun ObjCExportCodeGenerator.createTypeAdapter(
methodTable,
objCName,
adapters,
classAdapters,
virtualAdapters,
reverseAdapters
)
}
private fun ObjCExportCodeGenerator.createUnitInstanceAdapter(): ObjCExportCodeGenerator.ObjCToKotlinMethodAdapter {
val selector = "unit"
val methodBridge = MethodBridge(ReferenceBridge, listOf(ReferenceBridge, ReferenceBridge))
val encoding = getEncoding(methodBridge)
val imp = generateFunction(codegen, objCFunctionType(methodBridge), "") {
ret(callFromBridge(context.llvm.Kotlin_ObjCExport_convertUnit, listOf(codegen.theUnitInstanceRef.llvm)))
}
LLVMSetLinkage(imp, LLVMLinkage.LLVMPrivateLinkage)
return ObjCToKotlinMethodAdapter(selector, encoding, constPointer(imp))
}
private fun List<CallableMemberDescriptor>.toMethods(): List<FunctionDescriptor> = this.flatMap {
when (it) {
is PropertyDescriptor -> listOfNotNull(it.getter, it.setter)
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.backend.konan.Context
import org.jetbrains.kotlin.backend.konan.KonanCompilationException
import org.jetbrains.kotlin.backend.konan.descriptors.getPackageFragments
import org.jetbrains.kotlin.backend.konan.descriptors.isInterface
import org.jetbrains.kotlin.backend.konan.descriptors.isUnit
import org.jetbrains.kotlin.backend.konan.reportCompilationError
import org.jetbrains.kotlin.builtins.getReceiverTypeFromFunctionType
import org.jetbrains.kotlin.builtins.getReturnTypeFromFunctionType
@@ -201,6 +202,11 @@ internal class ObjCExportHeaderGenerator(val context: Context) {
+"@interface $name : $superName${descriptor.superProtocolsClause}"
if (descriptor.isUnit()) {
+"+ (instancetype)unit NS_SWIFT_NAME(init());"
+""
}
val presentConstructors = mutableSetOf<String>()
descriptor.constructors.filter { mapper.shouldBeExposed(it) }.forEach {