@@ -1,7 +1,7 @@ /* - * Copyright (c) 2008-2022 Jonathan Schleifer + * Copyright (c) 2008-2024 Jonathan Schleifer * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in @@ -18,12 +18,10 @@ #include #include #include #include "unistd_wrapper.h" -#include - #ifdef OF_APPLE_RUNTIME # include #endif #ifdef HAVE_GETRANDOM @@ -39,10 +37,11 @@ #import "OFMethodSignature.h" #import "OFRunLoop.h" #if !defined(OF_HAVE_ATOMIC_OPS) && defined(OF_HAVE_THREADS) # import "OFPlainMutex.h" /* For OFSpinlock */ #endif +#import "OFStdIOStream.h" #import "OFString.h" #import "OFThread.h" #import "OFTimer.h" #import "OFValue.h" @@ -63,11 +62,13 @@ #ifdef OF_WINDOWS # include #endif #ifdef OF_AMIGAOS +# define Class IntuitionClass # include +# undef Class #endif #ifdef OF_APPLE_RUNTIME extern id _Nullable _objc_rootAutorelease(id _Nullable object); #endif @@ -250,17 +251,16 @@ #if !defined(OF_APPLE_RUNTIME) || defined(__OBJC2__) static void uncaughtExceptionHandler(id exception) { - OFString *description = [exception description]; OFArray OF_GENERIC(OFValue *) *stackTraceAddresses = nil; OFArray OF_GENERIC(OFString *) *stackTraceSymbols = nil; OFStringEncoding encoding = [OFLocale encoding]; - fprintf(stderr, "\nRuntime error: Unhandled exception:\n%s\n", - [description cStringWithEncoding: encoding]); + OFLog(@"Runtime error: Unhandled exception:"); + OFLog(@"%@", exception); if ([exception respondsToSelector: @selector(stackTraceAddresses)]) stackTraceAddresses = [exception stackTraceAddresses]; if (stackTraceAddresses != nil) { @@ -271,32 +271,31 @@ stackTraceSymbols = [exception stackTraceSymbols]; if (stackTraceSymbols.count != count) stackTraceSymbols = nil; - fputs("\nStack trace:\n", stderr); + OFLog(@""); + OFLog(@"Stack trace:"); if (stackTraceSymbols != nil) { for (size_t i = 0; i < count; i++) { void *address = [[stackTraceAddresses objectAtIndex: i] pointerValue]; const char *symbol = [[stackTraceSymbols objectAtIndex: i] cStringWithEncoding: encoding]; - fprintf(stderr, " %p %s\n", address, symbol); + OFLog(@" %p %s", address, symbol); } } else { for (size_t i = 0; i < count; i++) { void *address = [[stackTraceAddresses objectAtIndex: i] pointerValue]; - fprintf(stderr, " %p\n", address); + OFLog(@" %p", address); } } - - fputs("\n", stderr); } abort(); } #endif @@ -335,18 +334,20 @@ size_t instanceSize; instanceSize = class_getInstanceSize(class); if OF_UNLIKELY (extraAlignment > 1) - extraAlignment = ((instanceSize + extraAlignment - 1) & - ~(extraAlignment - 1)) - extraAlignment; + extraAlignment = OFRoundUpToPowerOf2(extraAlignment, + PRE_IVARS_ALIGN + instanceSize) - + PRE_IVARS_ALIGN - instanceSize; instance = calloc(1, PRE_IVARS_ALIGN + instanceSize + extraAlignment + extraSize); if OF_UNLIKELY (instance == nil) { - allocFailedException.isa = [OFAllocFailedException class]; + object_setClass((id)&allocFailedException, + [OFAllocFailedException class]); @throw (id)&allocFailedException; } ((struct PreIvars *)instance)->retainCount = 1; @@ -386,11 +387,10 @@ /* References for static linking */ void _references_to_categories_of_OFObject(void) { _OFObject_KeyValueCoding_reference = 1; - _OFObject_Serialization_reference = 1; } @implementation OFObject + (void)load { @@ -438,15 +438,10 @@ + (instancetype)alloc { return OFAllocObject(self, 0, 0, NULL); } -+ (instancetype)new -{ - return [[self alloc] init]; -} - + (Class)class { return self; } @@ -475,14 +470,12 @@ return class_respondsToSelector(self, selector); } + (bool)conformsToProtocol: (Protocol *)protocol { - Class c; - - for (c = self; c != Nil; c = class_getSuperclass(c)) - if (class_conformsToProtocol(c, protocol)) + for (Class iter = self; iter != Nil; iter = class_getSuperclass(iter)) + if (class_conformsToProtocol(iter, protocol)) return true; return false; } @@ -580,16 +573,16 @@ [self inheritMethodsFromClass: superclass]; } + (bool)resolveClassMethod: (SEL)selector { - return NO; + return false; } + (bool)resolveInstanceMethod: (SEL)selector { - return NO; + return false; } - (instancetype)init { return self; @@ -1169,11 +1162,11 @@ return self; } - (unsigned int)retainCount { - assert(PRE_IVARS->retainCount >= 0); + OFAssert(PRE_IVARS->retainCount >= 0); return PRE_IVARS->retainCount; } - (void)release {