Index: configure.ac ================================================================== --- configure.ac +++ configure.ac @@ -628,14 +628,10 @@ ;; esac AC_CHECK_FUNCS(_Unwind_Backtrace) -AC_CHECK_FUNC(objc_constructInstance, [], [ - AC_SUBST(INSTANCE_M, "instance.m") -]) - AC_CHECK_FUNC(objc_autoreleasePoolPush, [], [ AC_SUBST(AUTORELEASE_M, "autorelease.m") ]) case "$host_os" in Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -178,11 +178,10 @@ OFSerialization.h \ OFTLSSocket.h \ ObjFW.h \ autorelease.h \ block.h \ - instance.h \ macros.h \ objfw-defs.h \ platform.h \ ${USE_INCLUDES_ATOMIC} @@ -207,11 +206,10 @@ OFRangeValue.m \ OFRectangleValue.m \ OFSubarray.m \ OFUTF8String.m \ ${AUTORELEASE_M} \ - ${INSTANCE_M} \ ${LIBBASES_M} SRCS_FILES += OFFileURLHandler.m \ OFINIFileSettings.m SRCS_SOCKETS += OFDNSResolverSettings.m \ OFHTTPURLHandler.m \ Index: src/OFObject.m ================================================================== --- src/OFObject.m +++ src/OFObject.m @@ -58,11 +58,10 @@ # include #endif #import "OFString.h" -#import "instance.h" #if defined(OF_HAVE_ATOMIC_OPS) # import "atomic.h" #elif defined(OF_HAVE_THREADS) # import "mutex.h" #endif Index: src/ObjFW.h ================================================================== --- src/ObjFW.h +++ src/ObjFW.h @@ -265,13 +265,12 @@ #import "base64.h" #import "crc16.h" #import "crc32.h" #import "huffman_tree.h" -#import "instance.h" #import "of_asprintf.h" #import "of_strptime.h" #import "pbkdf2.h" #import "scrypt.h" #ifdef OF_HAVE_UNICODE_TABLES # import "unicode.h" #endif DELETED src/instance.h Index: src/instance.h ================================================================== --- src/instance.h +++ src/instance.h @@ -1,31 +0,0 @@ -/* - * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, - * 2018, 2019 - * 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 - * the packaging of this file. - * - * Alternatively, it may be distributed under the terms of the GNU General - * Public License, either version 2 or 3, which can be found in the file - * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this - * file. - */ - -#import "macros.h" - -OF_ASSUME_NONNULL_BEGIN - -#ifdef __cplusplus -extern "C" { -#endif -extern id objc_constructInstance(Class _Nullable, void *_Nullable); -extern void *objc_destructInstance(id _Nullable); -#ifdef __cplusplus -} -#endif - -OF_ASSUME_NONNULL_END DELETED src/instance.m Index: src/instance.m ================================================================== --- src/instance.m +++ src/instance.m @@ -1,101 +0,0 @@ -/* - * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, - * 2018, 2019 - * 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 - * the packaging of this file. - * - * Alternatively, it may be distributed under the terms of the GNU General - * Public License, either version 2 or 3, which can be found in the file - * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this - * file. - */ - -#include "config.h" - -#import "OFObject.h" - -static SEL constructSelector = NULL; -static SEL destructSelector = NULL; - -static bool -callConstructors(Class class, id object) -{ - Class super = class_getSuperclass(class); - id (*construct)(id, SEL); - id (*last)(id, SEL); - - if (super != nil) - if (!callConstructors(super, object)) - return false; - - if (constructSelector == NULL) - constructSelector = sel_registerName(".cxx_construct"); - - if (!class_respondsToSelector(class, constructSelector)) - return true; - - construct = (id (*)(id, SEL)) - class_getMethodImplementation(class, constructSelector); - last = (id (*)(id, SEL)) - class_getMethodImplementation(super, constructSelector); - - if (construct == last) - return true; - - return (construct(object, constructSelector) != nil); -} - -id -objc_constructInstance(Class class, void *bytes) -{ - id object = (id)bytes; - - if (class == Nil || bytes == NULL) - return nil; - - object_setClass(object, class); - - if (!callConstructors(class, object)) - return nil; - - return object; -} - -void * -objc_destructInstance(id object) -{ - Class class; - void (*last)(id, SEL) = NULL; - - if (object == nil) - return NULL; - -#ifdef OF_OBJFW_RUNTIME - objc_zero_weak_references(object); -#endif - - if (destructSelector == NULL) - destructSelector = sel_registerName(".cxx_destruct"); - - for (class = object_getClass(object); class != Nil; - class = class_getSuperclass(class)) { - void (*destruct)(id, SEL); - - if (class_respondsToSelector(class, destructSelector)) { - if ((destruct = (void (*)(id, SEL)) - class_getMethodImplementation(class, - destructSelector)) != last) - destruct(object, destructSelector); - - last = destruct; - } else - break; - } - - return object; -} Index: src/runtime/Makefile ================================================================== --- src/runtime/Makefile +++ src/runtime/Makefile @@ -16,10 +16,11 @@ class.m \ dtable.m \ exception.m \ hashtable.m \ init.m \ + instance.m \ ivar.m \ lookup.m \ method.m \ misc.m \ property.m \ Index: src/runtime/ObjFWRT.h ================================================================== --- src/runtime/ObjFWRT.h +++ src/runtime/ObjFWRT.h @@ -146,11 +146,13 @@ objc_uncaught_exception_handler_t _Nullable handler); extern void objc_setForwardHandler(IMP _Nullable forward, IMP _Nullable stretForward); extern void objc_setEnumerationMutationHandler( objc_enumeration_mutation_handler_t _Nullable handler); -extern void objc_zero_weak_references(id _Nonnull value); +extern id _Nullable objc_constructInstance(Class _Nullable class_, + void *_Nullable bytes); +extern void *_Nullable objc_destructInstance(id _Nullable object); /* * Used by the compiler, but can also be called manually. * * These declarations are also required to prevent Clang's implicit Index: src/runtime/amiga-glue.m ================================================================== --- src/runtime/amiga-glue.m +++ src/runtime/amiga-glue.m @@ -618,16 +618,17 @@ M68K_ARG(objc_enumeration_mutation_handler_t, handler, a0) objc_setEnumerationMutationHandler(handler); } -void __saveds -glue_objc_zero_weak_references PPC_PARAMS(id value) +id __saveds +glue_objc_constructInstance PPC_PARAMS(Class class, void *bytes) { - M68K_ARG(id, value, a0) + M68K_ARG(Class, class, a0) + M68K_ARG(void *, bytes, a1) - objc_zero_weak_references(value); + return objc_constructInstance(class, bytes); } void __saveds glue_objc_exit(void) { @@ -716,5 +717,13 @@ M68K_ARG(objc_property_t, property, a0) M68K_ARG(const char *, name, a1) return property_copyAttributeValue(property, name); } + +void *__saveds +glue_objc_destructInstance PPC_PARAMS(id object) +{ + M68K_ARG(id, object, a0) + + return objc_destructInstance(object); +} Index: src/runtime/amiga-library.m ================================================================== --- src/runtime/amiga-library.m +++ src/runtime/amiga-library.m @@ -124,11 +124,11 @@ extern bool glue_protocol_conformsToProtocol(void); extern objc_uncaught_exception_handler_t glue_objc_setUncaughtExceptionHandler(void); extern void glue_objc_setForwardHandler(void); extern void glue_objc_setEnumerationMutationHandler(void); -extern void glue_objc_zero_weak_references(void); +extern id glue_objc_constructInstance(void); extern void glue_objc_exit(void); extern Ivar *glue_class_copyIvarList(void); extern const char *glue_ivar_getName(void); extern const char *glue_ivar_getTypeEncoding(void); extern ptrdiff_t glue_ivar_getOffset(void); @@ -136,10 +136,11 @@ extern SEL glue_method_getName(void); extern const char *glue_method_getTypeEncoding(void); extern objc_property_t *glue_class_copyPropertyList(void); extern const char *glue_property_getName(void); extern char *glue_property_copyAttributeValue(void); +extern void *glue_objc_destructInstance(void); #ifdef OF_MORPHOS const ULONG __abox__ = 1; #endif struct ExecBase *SysBase; @@ -641,11 +642,11 @@ (CONST_APTR)glue_protocol_isEqual, (CONST_APTR)glue_protocol_conformsToProtocol, (CONST_APTR)glue_objc_setUncaughtExceptionHandler, (CONST_APTR)glue_objc_setForwardHandler, (CONST_APTR)glue_objc_setEnumerationMutationHandler, - (CONST_APTR)glue_objc_zero_weak_references, + (CONST_APTR)glue_objc_constructInstance, (CONST_APTR)glue_objc_exit, (CONST_APTR)glue_class_copyIvarList, (CONST_APTR)glue_ivar_getName, (CONST_APTR)glue_ivar_getTypeEncoding, (CONST_APTR)glue_ivar_getOffset, @@ -653,10 +654,11 @@ (CONST_APTR)glue_method_getName, (CONST_APTR)glue_method_getTypeEncoding, (CONST_APTR)glue_class_copyPropertyList, (CONST_APTR)glue_property_getName, (CONST_APTR)glue_property_copyAttributeValue, + (CONST_APTR)glue_objc_destructInstance, (CONST_APTR)-1, #ifdef OF_MORPHOS (CONST_APTR)FUNCARRAY_END #endif }; Index: src/runtime/amigaos3.sfd ================================================================== --- src/runtime/amigaos3.sfd +++ src/runtime/amigaos3.sfd @@ -67,11 +67,11 @@ bool glue_protocol_isEqual(Protocol *_Nonnull protocol1, Protocol *_Nonnull protocol2)(a0,a1) bool glue_protocol_conformsToProtocol(Protocol *_Nonnull protocol1, Protocol *_Nonnull protocol2)(a0,a1) _Nullable objc_uncaught_exception_handler_t glue_objc_setUncaughtExceptionHandler(objc_uncaught_exception_handler_t _Nullable handler)(a0) void glue_objc_setForwardHandler(IMP _Nullable forward, IMP _Nullable stretForward)(a0,a1) void glue_objc_setEnumerationMutationHandler(objc_enumeration_mutation_handler_t _Nullable handler)(a0) -void glue_objc_zero_weak_references(id _Nonnull value)(a0) +id _Nullable glue_objc_constructInstance(Class _Nullable class_, void *_Nullable bytes)(a0,a1) void glue_objc_exit(void)() Ivar _Nullable *_Nullable glue_class_copyIvarList(Class _Nullable class_, unsigned int *_Nullable outCount)(a0,a1) const char *_Nonnull glue_ivar_getName(Ivar _Nonnull ivar)(a0) const char *_Nonnull glue_ivar_getTypeEncoding(Ivar _Nonnull ivar)(a0) ptrdiff_t glue_ivar_getOffset(Ivar _Nonnull ivar)(a0) @@ -79,6 +79,7 @@ SEL _Nonnull glue_method_getName(Method _Nonnull method)(a0) const char *_Nullable glue_method_getTypeEncoding(Method _Nonnull method)(a0) objc_property_t _Nullable *_Nullable glue_class_copyPropertyList(Class _Nullable class_, unsigned int *_Nullable outCount)(a0,a1) const char *_Nonnull glue_property_getName(objc_property_t _Nonnull property)(a0) char *_Nullable glue_property_copyAttributeValue(objc_property_t _Nonnull property, const char *_Nonnull name)(a0,a1) +void *_Nullable glue_objc_destructInstance(id _Nullable object)(a0) ==end ADDED src/runtime/instance.m Index: src/runtime/instance.m ================================================================== --- src/runtime/instance.m +++ src/runtime/instance.m @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, + * 2018, 2019 + * 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 + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#include "config.h" + +#import "ObjFWRT.h" +#import "private.h" + +static SEL constructSelector = NULL; +static SEL destructSelector = NULL; + +static bool +callConstructors(Class class, id object) +{ + Class super = class_getSuperclass(class); + id (*construct)(id, SEL); + id (*last)(id, SEL); + + if (super != nil) + if (!callConstructors(super, object)) + return false; + + if (constructSelector == NULL) + constructSelector = sel_registerName(".cxx_construct"); + + if (!class_respondsToSelector(class, constructSelector)) + return true; + + construct = (id (*)(id, SEL)) + class_getMethodImplementation(class, constructSelector); + last = (id (*)(id, SEL)) + class_getMethodImplementation(super, constructSelector); + + if (construct == last) + return true; + + return (construct(object, constructSelector) != nil); +} + +id +objc_constructInstance(Class class, void *bytes) +{ + id object = (id)bytes; + + if (class == Nil || bytes == NULL) + return nil; + + object_setClass(object, class); + + if (!callConstructors(class, object)) + return nil; + + return object; +} + +void * +objc_destructInstance(id object) +{ + Class class; + void (*last)(id, SEL) = NULL; + + if (object == nil) + return NULL; + +#ifdef OF_OBJFW_RUNTIME + objc_zero_weak_references(object); +#endif + + if (destructSelector == NULL) + destructSelector = sel_registerName(".cxx_destruct"); + + for (class = object_getClass(object); class != Nil; + class = class_getSuperclass(class)) { + void (*destruct)(id, SEL); + + if (class_respondsToSelector(class, destructSelector)) { + if ((destruct = (void (*)(id, SEL)) + class_getMethodImplementation(class, + destructSelector)) != last) + destruct(object, destructSelector); + + last = destruct; + } else + break; + } + + return object; +} Index: src/runtime/linklib/linklib.m ================================================================== --- src/runtime/linklib/linklib.m +++ src/runtime/linklib/linklib.m @@ -578,14 +578,14 @@ objc_setEnumerationMutationHandler(objc_enumeration_mutation_handler_t handler) { glue_objc_setEnumerationMutationHandler(handler); } -void -objc_zero_weak_references(id value) +id +objc_constructInstance(Class class, void *_Nullable bytes) { - glue_objc_zero_weak_references(value); + return glue_objc_constructInstance(class, bytes); } void objc_exit(void) { @@ -649,5 +649,11 @@ char * property_copyAttributeValue(objc_property_t property, const char *name) { return glue_property_copyAttributeValue(property, name); } + +void * +objc_destructInstance(id object) +{ + return glue_objc_destructInstance(object); +} Index: src/runtime/morphos-clib.h ================================================================== --- src/runtime/morphos-clib.h +++ src/runtime/morphos-clib.h @@ -61,11 +61,11 @@ bool glue_protocol_isEqual(Protocol *, Protocol *); bool glue_protocol_conformsToProtocol(Protocol *, Protocol *); objc_uncaught_exception_handler_t glue_objc_setUncaughtExceptionHandler(objc_uncaught_exception_handler_t); void glue_objc_setForwardHandler(IMP, IMP); void glue_objc_setEnumerationMutationHandler(objc_enumeration_mutation_handler_t); -void glue_objc_zero_weak_references(id); +id _Nullable glue_objc_constructInstance(Class class_, void *bytes); void glue_objc_exit(void); Ivar *glue_class_copyIvarList(Class class_, unsigned int *outCount); const char *glue_ivar_getName(Ivar ivar); const char *glue_ivar_getTypeEncoding(Ivar ivar); ptrdiff_t glue_ivar_getOffset(Ivar ivar); @@ -73,5 +73,6 @@ SEL glue_method_getName(Method method); const char *glue_method_getTypeEncoding(Method method); objc_property_t *glue_class_copyPropertyList(Class class_, unsigned int *outCount); const char *glue_property_getName(objc_property_t property); char *glue_property_copyAttributeValue(objc_property_t property, const char *name); +void *glue_objc_destructInstance(id object); Index: src/runtime/morphos.fd ================================================================== --- src/runtime/morphos.fd +++ src/runtime/morphos.fd @@ -63,11 +63,11 @@ glue_protocol_isEqual(protocol1,protocol2)(sysv,r12base) glue_protocol_conformsToProtocol(protocol1,protocol2)(sysv,r12base) glue_objc_setUncaughtExceptionHandler(handler)(sysv,r12base) glue_objc_setForwardHandler(forward,stretForward)(sysv,r12base) glue_objc_setEnumerationMutationHandler(handler)(sysv,r12base) -glue_objc_zero_weak_references(value)(sysv,r12base) +glue_objc_constructInstance(class_,bytes)(sysv,r12base) glue_objc_exit()(sysv,r12base) glue_class_copyIvarList(class_,outCount)(sysv,r12base) glue_ivar_getName(ivar)(sysv,r12base) glue_ivar_getTypeEncoding(ivar)(sysv,r12base) glue_ivar_getOffset(ivar)(sysv,r12base) @@ -75,6 +75,7 @@ glue_method_getName(method)(sysv,r12base) glue_method_getTypeEncoding(method)(sysv,r12base) glue_class_copyPropertyList(class_,outCount)(sysv,r12base) glue_property_getName(property)(sysv,r12base) glue_property_copyAttributeValue(property,name)(sysv,r12base) +glue_objc_destructInstance(object)(sysv,r12base) ##end Index: src/runtime/private.h ================================================================== --- src/runtime/private.h +++ src/runtime/private.h @@ -304,10 +304,11 @@ IMP _Nullable); extern void objc_dtable_free(struct objc_dtable *_Nonnull); extern void objc_dtable_cleanup(void); extern void objc_init_static_instances(struct objc_symtab *_Nonnull); extern void objc_forget_pending_static_instances(void); +extern void objc_zero_weak_references(id _Nonnull); #ifdef OF_HAVE_THREADS extern void objc_global_mutex_lock(void); extern void objc_global_mutex_unlock(void); extern void objc_global_mutex_free(void); #else