Skip to content

Commit

Permalink
Initialize ObjCExport during runtime init too (JetBrains#3471)
Browse files Browse the repository at this point in the history
as it may be required during execution of Kotlin code calling Obj-C.
  • Loading branch information
SvyatoslavScherbina authored Oct 18, 2019
1 parent 0b80666 commit 6a2812a
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 3 deletions.
3 changes: 2 additions & 1 deletion backend.native/tests/interop/objc/smoke.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ fun main(args: Array<String>) {
}

fun run() {
testTypeOps()
testConversions()
testTypeOps()
testWeakRefs()
testExceptions()
testBlocks()
Expand Down Expand Up @@ -191,6 +191,7 @@ fun testConversions() {
testMethodsOfAny(emptyList<Nothing>(), NSArray())
testMethodsOfAny(listOf(1, "foo"), nsArrayOf(1, "foo"))
testMethodsOfAny(42, NSNumber.numberWithInt(42), 17)
testMethodsOfAny(true, NSNumber.numberWithBool(true), false)
}

fun testMethodsOfAny(kotlinObject: Any, equalNsObject: NSObject, otherObject: Any = Any()) {
Expand Down
14 changes: 13 additions & 1 deletion runtime/src/main/cpp/ObjCExport.mm
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#import <dispatch/dispatch.h>

#import "ObjCExport.h"
#import "ObjCExportInit.h"
#import "ObjCExportPrivate.h"
#import "MemoryPrivate.hpp"
#import "Runtime.h"
Expand Down Expand Up @@ -321,7 +322,7 @@ static void initTypeAdapters() {
initTypeAdaptersFrom(Kotlin_ObjCExport_sortedProtocolAdapters, Kotlin_ObjCExport_sortedProtocolAdaptersNum);
}

extern "C" void Kotlin_ObjCExport_initialize() {
static void Kotlin_ObjCExport_initializeImpl() {
initTypeAdapters();

SEL toKotlinSelector = Kotlin_ObjCExport_toKotlinSelector;
Expand Down Expand Up @@ -363,6 +364,17 @@ static void initTypeAdapters() {
}
}

// 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) {
RETURN_RESULT_OF(Kotlin_ObjCExport_convertUnmappedObjCObject, self);
}
Expand Down
15 changes: 15 additions & 0 deletions runtime/src/main/cpp/ObjCExportInit.h
Original file line number Diff line number Diff line change
@@ -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 change: 0 additions & 1 deletion runtime/src/main/cpp/ObjCExportPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
@end;

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" OBJ_GETTER(Kotlin_ObjCExport_convertUnmappedObjCObject, id obj);
extern "C" SEL Kotlin_ObjCExport_toKotlinSelector;
Expand Down
6 changes: 6 additions & 0 deletions runtime/src/main/cpp/Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "Exceptions.h"
#include "KAssert.h"
#include "Memory.h"
#include "ObjCExportInit.h"
#include "Porting.h"
#include "Runtime.h"
#include "Worker.h"
Expand Down Expand Up @@ -97,6 +98,11 @@ RuntimeState* initRuntime() {
if (firstRuntime) {
isMainThread = 1;
konan::consoleInit();

#if KONAN_OBJC_INTEROP
Kotlin_ObjCExport_initialize();
#endif

InitOrDeinitGlobalVariables(INIT_GLOBALS);
}
InitOrDeinitGlobalVariables(INIT_THREAD_LOCAL_GLOBALS);
Expand Down
1 change: 1 addition & 0 deletions runtime/src/objc/cpp/ObjCExportClasses.mm
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#import <dispatch/dispatch.h>

#import "ObjCExport.h"
#import "ObjCExportInit.h"
#import "ObjCExportPrivate.h"
#import "MemoryPrivate.hpp"
#import "Runtime.h"
Expand Down

0 comments on commit 6a2812a

Please sign in to comment.