Skip to content

Commit

Permalink
Add a check that linking worked when Crashlytics is enabled.
Browse files Browse the repository at this point in the history
  • Loading branch information
TadeasKriz committed Jun 27, 2024
1 parent f35584a commit a883376
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import kotlinx.cinterop.convert
@OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
actual class CrashlyticsCallsActual : CrashlyticsCalls {

init {
FIRCheckLinkDependencies()
}

override fun logMessage(message: String) {
FIRCrashlyticsLog(message)
Expand Down
49 changes: 40 additions & 9 deletions crashlytics/src/nativeInterop/cinterop/crashlytics.def
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ language = Objective-C

extern void FIRCLSExceptionRecordNSException(NSException *exception) __attribute__((weak));

void tryFIRCLSExceptionRecordNSException(NSException *exception) {
if (FIRCLSExceptionRecordNSException) {
FIRCLSExceptionRecordNSException(exception);
} else {
void __verifyFIRCLSExceptionRecordNSExceptionExists(void) {
if (!FIRCLSExceptionRecordNSException) {
@throw [NSException
exceptionWithName:@"FIRFunctionNotFound"
reason:@"Function 'FIRCLSExceptionRecordNSException' not available, make sure you're adding the Firebase Crashlytics dependency."
Expand All @@ -19,6 +17,11 @@ void tryFIRCLSExceptionRecordNSException(NSException *exception) {
}
}

void tryFIRCLSExceptionRecordNSException(NSException *exception) {
__verifyFIRCLSExceptionRecordNSExceptionExists();
FIRCLSExceptionRecordNSException(exception);
}

#define LoadClass(name)\
static Class objClass;\
if (!objClass) {\
Expand Down Expand Up @@ -52,8 +55,23 @@ void* FIRMethodForSelector(id _Nonnull target, SEL _Nonnull selector) {
}
}

id FIRExceptionModelWithNameAndReason(NSString* _Nonnull name, NSString* _Nonnull reason) {
Class FIRExceptionModelClass(void) {
LoadClass(FIRExceptionModel);
return objClass;
}

Class FIRStackFrameClass(void) {
LoadClass(FIRStackFrame);
return objClass;
}

Class FIRCrashlyticsClass(void) {
LoadClass(FIRCrashlytics);
return objClass;
}

id FIRExceptionModelWithNameAndReason(NSString* _Nonnull name, NSString* _Nonnull reason) {
Class objClass = FIRExceptionModelClass();
SEL selector = NSSelectorFromString(@"exceptionModelWithName:reason:");
id (*exceptionModelWithNameAndReason)(id, SEL, NSString*, NSString*) = FIRMethodForSelector(objClass, selector);
return exceptionModelWithNameAndReason(objClass, selector, name, reason);
Expand All @@ -66,17 +84,21 @@ void FIRExceptionModelSetStackTrace(id exceptionModel, NSArray<id>* _Nonnull sta
}

id FIRStackFrameWithAddress(NSUInteger address) {
LoadClass(FIRStackFrame);
Class objClass = FIRStackFrameClass();
SEL selector = NSSelectorFromString(@"stackFrameWithAddress:");
id (*stackFrameWithAddress)(id, SEL, NSUInteger) = FIRMethodForSelector(objClass, selector);
return stackFrameWithAddress(objClass, selector, address);
}

id FIRCrashlyticsInstance(void) {
LoadClass(FIRCrashlytics);
id _Nullable FIRCrashlyticsInstanceOrNull(void) {
Class objClass = FIRCrashlyticsClass();
SEL selector = NSSelectorFromString(@"crashlytics");
id (*crashlytics)(id, SEL) = FIRMethodForSelector(objClass, selector);
id instance = crashlytics(objClass, selector);
return crashlytics(objClass, selector);
}

id _Nonnull FIRCrashlyticsInstance(void) {
id instance = FIRCrashlyticsInstanceOrNull();
if (instance) {
return instance;
} else {
Expand Down Expand Up @@ -120,3 +142,12 @@ void FIRCrashlyticsSetCustomValue(NSString* _Nonnull key, id __nullable value) {
void (*setCustomValueForKey)(id, SEL, id __nullable, NSString* _Nonnull) = FIRMethodForSelector(crashlytics, selector);
setCustomValueForKey(crashlytics, selector, value, key);
}

void FIRCheckLinkDependencies(void) {
__verifyFIRCLSExceptionRecordNSExceptionExists();

// Load classes to verify we have them all
FIRExceptionModelClass();
FIRStackFrameClass();
FIRCrashlyticsClass();
}

0 comments on commit a883376

Please sign in to comment.