Initialize ObjCExport during runtime init too (#3471)

as it may be required during execution of Kotlin code calling Obj-C.
This commit is contained in:
SvyatoslavScherbina
2019-10-18 18:54:52 +03:00
committed by GitHub
parent 0b8066602e
commit 6a2812a2d5
6 changed files with 37 additions and 3 deletions
+2 -1
View File
@@ -15,8 +15,8 @@ fun main(args: Array<String>) {
} }
fun run() { fun run() {
testTypeOps()
testConversions() testConversions()
testTypeOps()
testWeakRefs() testWeakRefs()
testExceptions() testExceptions()
testBlocks() testBlocks()
@@ -191,6 +191,7 @@ fun testConversions() {
testMethodsOfAny(emptyList<Nothing>(), NSArray()) testMethodsOfAny(emptyList<Nothing>(), NSArray())
testMethodsOfAny(listOf(1, "foo"), nsArrayOf(1, "foo")) testMethodsOfAny(listOf(1, "foo"), nsArrayOf(1, "foo"))
testMethodsOfAny(42, NSNumber.numberWithInt(42), 17) testMethodsOfAny(42, NSNumber.numberWithInt(42), 17)
testMethodsOfAny(true, NSNumber.numberWithBool(true), false)
} }
fun testMethodsOfAny(kotlinObject: Any, equalNsObject: NSObject, otherObject: Any = Any()) { fun testMethodsOfAny(kotlinObject: Any, equalNsObject: NSObject, otherObject: Any = Any()) {
+13 -1
View File
@@ -36,6 +36,7 @@
#import <dispatch/dispatch.h> #import <dispatch/dispatch.h>
#import "ObjCExport.h" #import "ObjCExport.h"
#import "ObjCExportInit.h"
#import "ObjCExportPrivate.h" #import "ObjCExportPrivate.h"
#import "MemoryPrivate.hpp" #import "MemoryPrivate.hpp"
#import "Runtime.h" #import "Runtime.h"
@@ -321,7 +322,7 @@ static void initTypeAdapters() {
initTypeAdaptersFrom(Kotlin_ObjCExport_sortedProtocolAdapters, Kotlin_ObjCExport_sortedProtocolAdaptersNum); initTypeAdaptersFrom(Kotlin_ObjCExport_sortedProtocolAdapters, Kotlin_ObjCExport_sortedProtocolAdaptersNum);
} }
extern "C" void Kotlin_ObjCExport_initialize() { static void Kotlin_ObjCExport_initializeImpl() {
initTypeAdapters(); initTypeAdapters();
SEL toKotlinSelector = Kotlin_ObjCExport_toKotlinSelector; SEL toKotlinSelector = Kotlin_ObjCExport_toKotlinSelector;
@@ -363,6 +364,17 @@ extern "C" void Kotlin_ObjCExport_initialize() {
} }
} }
// Initializes ObjCExport for current process (if not initialized yet).
// Generally this is equal to some "binary patching" (which is usually done at link time
// but postponed until runtime here due to various reasons):
// adds methods to Objective-C classes, initializes static memory with "constant" values etc.
extern "C" void Kotlin_ObjCExport_initialize() {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Kotlin_ObjCExport_initializeImpl();
});
}
static OBJ_GETTER(SwiftObject_toKotlinImp, id self, SEL cmd) { static OBJ_GETTER(SwiftObject_toKotlinImp, id self, SEL cmd) {
RETURN_RESULT_OF(Kotlin_ObjCExport_convertUnmappedObjCObject, self); RETURN_RESULT_OF(Kotlin_ObjCExport_convertUnmappedObjCObject, self);
} }
+15
View File
@@ -0,0 +1,15 @@
/*
* Copyright 2010-2019 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_OBJCEXPORTINIT_H
#define RUNTIME_OBJCEXPORTINIT_H
#if KONAN_OBJC_INTEROP
extern "C" void Kotlin_ObjCExport_initialize(void);
#endif // KONAN_OBJC_INTEROP
#endif // RUNTIME_OBJCEXPORTINIT_H
-1
View File
@@ -19,7 +19,6 @@
@end; @end;
extern "C" void Kotlin_ObjCExport_initializeClass(Class clazz); extern "C" void Kotlin_ObjCExport_initializeClass(Class clazz);
extern "C" void Kotlin_ObjCExport_initialize(void);
extern "C" const TypeInfo* Kotlin_ObjCExport_getAssociatedTypeInfo(Class clazz); extern "C" const TypeInfo* Kotlin_ObjCExport_getAssociatedTypeInfo(Class clazz);
extern "C" OBJ_GETTER(Kotlin_ObjCExport_convertUnmappedObjCObject, id obj); extern "C" OBJ_GETTER(Kotlin_ObjCExport_convertUnmappedObjCObject, id obj);
extern "C" SEL Kotlin_ObjCExport_toKotlinSelector; extern "C" SEL Kotlin_ObjCExport_toKotlinSelector;
+6
View File
@@ -19,6 +19,7 @@
#include "Exceptions.h" #include "Exceptions.h"
#include "KAssert.h" #include "KAssert.h"
#include "Memory.h" #include "Memory.h"
#include "ObjCExportInit.h"
#include "Porting.h" #include "Porting.h"
#include "Runtime.h" #include "Runtime.h"
#include "Worker.h" #include "Worker.h"
@@ -97,6 +98,11 @@ RuntimeState* initRuntime() {
if (firstRuntime) { if (firstRuntime) {
isMainThread = 1; isMainThread = 1;
konan::consoleInit(); konan::consoleInit();
#if KONAN_OBJC_INTEROP
Kotlin_ObjCExport_initialize();
#endif
InitOrDeinitGlobalVariables(INIT_GLOBALS); InitOrDeinitGlobalVariables(INIT_GLOBALS);
} }
InitOrDeinitGlobalVariables(INIT_THREAD_LOCAL_GLOBALS); InitOrDeinitGlobalVariables(INIT_THREAD_LOCAL_GLOBALS);
@@ -21,6 +21,7 @@
#import <dispatch/dispatch.h> #import <dispatch/dispatch.h>
#import "ObjCExport.h" #import "ObjCExport.h"
#import "ObjCExportInit.h"
#import "ObjCExportPrivate.h" #import "ObjCExportPrivate.h"
#import "MemoryPrivate.hpp" #import "MemoryPrivate.hpp"
#import "Runtime.h" #import "Runtime.h"