diff --git a/YSArray.m b/YSArray.m index b253955..117577c 100644 --- a/YSArray.m +++ b/YSArray.m @@ -5,35 +5,29 @@ @implementation YSArray -(id) init { self = [super init]; - array = NULL; + array = malloc(sizeof(id)); //allocate for one id return self; } -(void) addObject: (id) theObject { - //printf("%s\n", "YSArray: addObject"); if (array == NULL) { - //printf("%s\n", "before malloc"); + printf("%s\n", "addObject Init"); array = malloc(sizeof(id)); if (array == NULL) { - //printf("%s\n", "addObject: malloc failed."); return; } - //printf("%s\n", "after malloc"); } else { - //printf("%s\n", "before realloc"); - array = realloc(array, count * sizeof(id)); + array = realloc(array, (count + 1) * sizeof(id)); if (array == NULL) { - //printf("%s\n", "addObject: realloc failed."); return; } } array[count++] = theObject; - //printf("%s\n", "YSArray: object added."); } -(void) removeObject: (unsigned int) index { diff --git a/YSObject.m b/YSObject.m index e083cb3..5e0563d 100644 --- a/YSObject.m +++ b/YSObject.m @@ -1,9 +1,175 @@ +//yes its ugly i know, but it works + #include #include #include +#if !defined(__MINGW32__) && !defined(__MINGW64__) + #include + #include +#endif #import "YSObject.h" #import "YSZone.h" +#if !defined(__MINGW32__) && !defined(__MINGW64__) + +typedef struct obj_layout_unpadded { + int32_t retained; +} unp; +#define UNP sizeof(unp) + +#ifndef __BIGGEST_ALIGNMENT__ +#define __BIGGEST_ALIGNMENT__ (sizeof(void *) + sizeof(void *)) +#endif + +struct obj_layout { + char padding[__BIGGEST_ALIGNMENT__ - ((UNP % __BIGGEST_ALIGNMENT__) + ? (UNP % __BIGGEST_ALIGNMENT__) : __BIGGEST_ALIGNMENT__)]; + int32_t retained; +}; +typedef struct obj_layout *obj; + + +inline id YSAllocateObject(Class theClass, YSUInteger extraBytes, YSZone *zone) { + id new; + int size; + + size = class_getInstanceSize(theClass) + extraBytes + sizeof(struct obj_layout); + if (zone == 0) { + zone = YSDefaultMallocZone(); + } + + new = YSZoneMalloc(zone, size); + if (new != nil) { + memset(new, 0, size); + new = (id) &( (obj)new )[1]; + object_setClass(new, theClass); + //AADD + } + + return new; +} + +inline id YSDeallocateObject(id theObject) { + Class theClass = object_getClass(theObject); + + if ( (theObject != nil) && !class_isMetaClass(theClass) ) { + object_dispose(theObject); + } +} + +#endif + @implementation YSObject +#if !defined(__MINGW32__) && !defined(__MINGW64__) +/* ++(void) load { + //impl + return; +}*/ + +-(id) init { + return self; +} + +-(id) self { + return self; +} + ++(void) initialize { + //implement later + return; +} + ++(id) allocWithZone: (YSZone *) zone { + return YSAllocateObject(self, 0, zone); +} + ++(id) copyWithZone: (YSZone *) zone { + return self; +} + ++(id) alloc { + return [self allocWithZone: YSDefaultMallocZone()]; +} + ++(id) new { + return [[self alloc] init]; +} + +-(Class) class { + return object_getClass(self); +} + +-(void) dealloc { + YSDeallocateObject(self); +} + +-(id) copy { + return [(id)self copyWithZone: YSDefaultMallocZone()]; +} + ++(Class) superclass { + return class_getSuperclass(self); +} +-(Class) superclass { + return class_getSuperclass(object_getClass(self)); +} + ++(Class) class { + return self; +} + +-(BOOL) isEqual: (id) anObject { + return (self == anObject); +} + ++(BOOL) isKindOfClass: (Class) theClass { + if (theClass == [YSObject class]) { + return YES; + } + + return NO; +} + +-(BOOL) isProxy { + return NO; +} + +-(id) performSelector: (SEL) theSelector { + IMP msg; + + if (theSelector == 0) { + //raise exception + perror("null selector given"); + return; + } + + msg = objc_msg_lookup(self, theSelector); + if (!msg) { + //raise exception + perror("invalid selector"); + return nil; + } + + return (*msg)(self, theSelector); +} + +-(YSZone *) zone { + return YSZoneFromPointer(self); +} + ++(YSZone *) zone { + return YSDefaultMallocZone(); +} + +//dummy methods ++(oneway void) release { + return; +} + ++(id) retain { + return self; +} +#endif @end diff --git a/makefile b/makefile index 50ac38b..6489b84 100644 --- a/makefile +++ b/makefile @@ -18,15 +18,13 @@ static: $(CC) YSObject.m -c $(CC) YSZone.m -c $(CC) YSArray.m -c - $(CC) -I ../include YSTest.m -c - ar rcs libyeslib.a YSObject.o YSZone.o YSArray.o YSTest.o + ar rcs libyeslib.a YSObject.o YSZone.o YSArray.o shared: $(CC) YSObject.m -c -Wl,--enable-auto-import $(CC) YSZone.m -c $(CC) YSArray.m -c - $(CC) -I ../include YSTest.m -c - $(CC) -shared -o yeslib.dll YSObject.o YSZone.o YSArray.o YSTest.o $(LIB) + $(CC) -shared -o yeslib.dll YSObject.o YSZone.o YSArray.o $(LIB) clean: rm *.o \ No newline at end of file diff --git a/mingw32.c b/mingw32.c new file mode 100644 index 0000000..e1a57e8 --- /dev/null +++ b/mingw32.c @@ -0,0 +1,30 @@ +#include +#include +#include + +//a wrapper to make linking easier +#define YLAPI + +#if defined(__MINGW32__) || defined(__MINGW64__) + #define YLAPI __declspec(dllexport) +#endif + +YLAPI size_t __imp_class_getInstanceSize(Class class_) { + extern size_t class_getInstanceSize(class_); +} + +YLAPI Class __imp_object_setClass(id object, Class class_) { + extern Class object_setClass(object, class_); +} + +YLAPI BOOL __imp_class_isMetaClass(Class class_) { + extern BOOL class_isMetaClass(class_); +} + +YLAPI id __imp_object_dispose(id object) { + extern id object_dispose(object); +} + +YLAPI Class __imp_class_getSuperclass(Class class_) { + extern Class class_getSuperclass(class_); +} \ No newline at end of file