2009-09-08 16:18:59 +00:00
|
|
|
#include "objc/runtime.h"
|
2010-06-06 21:11:25 +00:00
|
|
|
#include "selector.h"
|
|
|
|
#include "class.h"
|
|
|
|
#include "protocol.h"
|
|
|
|
#include "ivar.h"
|
|
|
|
#include "method_list.h"
|
|
|
|
#include "lock.h"
|
|
|
|
#include "dtable.h"
|
2011-05-22 23:02:04 +00:00
|
|
|
#include "gc_ops.h"
|
2009-09-08 16:18:59 +00:00
|
|
|
|
|
|
|
/* Make glibc export strdup() */
|
|
|
|
|
|
|
|
#if defined __GLIBC__
|
|
|
|
#define __USE_BSD 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <string.h>
|
2010-06-06 21:11:25 +00:00
|
|
|
#include <stdio.h>
|
2009-09-08 16:18:59 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2010-09-02 14:52:35 +00:00
|
|
|
struct objc_slot *objc_get_slot(Class cls, SEL selector);
|
2011-02-13 11:06:25 +00:00
|
|
|
#define CHECK_ARG(arg) if (0 == arg) { return 0; }
|
2009-09-08 16:18:59 +00:00
|
|
|
|
2011-07-03 11:14:29 +00:00
|
|
|
/**
|
|
|
|
* Calls C++ destructors in the correct order.
|
|
|
|
*/
|
|
|
|
PRIVATE void call_cxx_destruct(id obj)
|
|
|
|
{
|
|
|
|
static SEL cxx_destruct;
|
|
|
|
if (NULL == cxx_destruct)
|
|
|
|
{
|
|
|
|
cxx_destruct = sel_registerName(".cxx_destruct");
|
|
|
|
}
|
|
|
|
// Don't call object_getClass(), because we want to get hidden classes too
|
2011-07-26 17:39:13 +00:00
|
|
|
Class cls = classForObject(obj);
|
2011-07-03 11:14:29 +00:00
|
|
|
|
|
|
|
while (cls)
|
|
|
|
{
|
|
|
|
struct objc_slot *slot = objc_get_slot(cls, cxx_destruct);
|
|
|
|
cls = Nil;
|
|
|
|
if (NULL != slot)
|
|
|
|
{
|
|
|
|
cls = slot->owner->super_class;
|
2011-08-07 17:04:44 +00:00
|
|
|
slot->method(obj, cxx_destruct);
|
2011-07-03 11:14:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void call_cxx_construct_for_class(Class cls, id obj)
|
|
|
|
{
|
|
|
|
static SEL cxx_construct;
|
|
|
|
if (NULL == cxx_construct)
|
|
|
|
{
|
2012-10-09 21:39:33 +00:00
|
|
|
cxx_construct = sel_registerName(".cxx_construct");
|
2011-07-03 11:14:29 +00:00
|
|
|
}
|
|
|
|
struct objc_slot *slot = objc_get_slot(cls, cxx_construct);
|
2011-07-04 11:28:33 +00:00
|
|
|
if (NULL != slot)
|
2011-07-03 11:14:29 +00:00
|
|
|
{
|
2011-07-04 11:28:33 +00:00
|
|
|
cls = slot->owner->super_class;
|
|
|
|
if (Nil != cls)
|
|
|
|
{
|
|
|
|
call_cxx_construct_for_class(cls, obj);
|
|
|
|
}
|
|
|
|
slot->method(obj, cxx_construct);
|
2011-07-03 11:14:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PRIVATE void call_cxx_construct(id obj)
|
|
|
|
{
|
2011-07-26 17:39:13 +00:00
|
|
|
call_cxx_construct_for_class(classForObject(obj), obj);
|
2011-07-03 11:14:29 +00:00
|
|
|
}
|
|
|
|
|
2012-02-06 17:15:48 +00:00
|
|
|
/**
|
2009-09-08 16:18:59 +00:00
|
|
|
* Looks up the instance method in a specific class, without recursing into
|
|
|
|
* superclasses.
|
|
|
|
*/
|
|
|
|
static Method class_getInstanceMethodNonrecursive(Class aClass, SEL aSelector)
|
|
|
|
{
|
|
|
|
for (struct objc_method_list *methods = aClass->methods;
|
2010-06-06 21:11:25 +00:00
|
|
|
methods != NULL ; methods = methods->next)
|
2009-09-08 16:18:59 +00:00
|
|
|
{
|
2010-06-06 21:11:25 +00:00
|
|
|
for (int i=0 ; i<methods->count ; i++)
|
2009-09-08 16:18:59 +00:00
|
|
|
{
|
2010-06-06 21:11:25 +00:00
|
|
|
Method method = &methods->methods[i];
|
2011-07-14 17:23:48 +00:00
|
|
|
if (sel_isEqual(method->selector, aSelector))
|
2009-09-08 16:18:59 +00:00
|
|
|
{
|
2010-05-16 20:39:54 +00:00
|
|
|
return method;
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void objc_updateDtableForClassContainingMethod(Method m)
|
|
|
|
{
|
|
|
|
Class nextClass = Nil;
|
2009-11-25 14:42:52 +00:00
|
|
|
void *state = NULL;
|
2009-09-08 16:18:59 +00:00
|
|
|
SEL sel = method_getName(m);
|
|
|
|
while (Nil != (nextClass = objc_next_class(&state)))
|
|
|
|
{
|
|
|
|
if (class_getInstanceMethodNonrecursive(nextClass, sel) == m)
|
|
|
|
{
|
2010-06-06 21:11:25 +00:00
|
|
|
objc_update_dtable_for_class(nextClass);
|
2009-09-08 16:18:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-06 21:11:25 +00:00
|
|
|
BOOL class_addIvar(Class cls, const char *name, size_t size, uint8_t alignment,
|
|
|
|
const char *types)
|
2009-09-08 16:18:59 +00:00
|
|
|
{
|
2011-02-13 11:06:25 +00:00
|
|
|
CHECK_ARG(cls);
|
|
|
|
CHECK_ARG(name);
|
|
|
|
CHECK_ARG(types);
|
2010-09-18 18:23:49 +00:00
|
|
|
// You can't add ivars to initialized classes. Note: We can't use the
|
|
|
|
// resolved flag here because class_getInstanceVariable() sets it.
|
|
|
|
if (objc_test_class_flag(cls, objc_class_flag_initialized))
|
2010-06-06 21:11:25 +00:00
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
2010-03-03 09:34:45 +00:00
|
|
|
|
2010-06-06 21:11:25 +00:00
|
|
|
if (class_getInstanceVariable(cls, name) != NULL)
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
2010-03-03 09:34:45 +00:00
|
|
|
|
2010-06-06 21:11:25 +00:00
|
|
|
struct objc_ivar_list *ivarlist = cls->ivars;
|
2010-03-03 09:34:45 +00:00
|
|
|
|
2010-06-06 21:11:25 +00:00
|
|
|
if (NULL == ivarlist)
|
|
|
|
{
|
2011-07-14 17:23:48 +00:00
|
|
|
cls->ivars = malloc(sizeof(struct objc_ivar_list) + sizeof(struct objc_ivar));
|
2010-06-06 21:11:25 +00:00
|
|
|
cls->ivars->count = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ivarlist->count++;
|
|
|
|
// objc_ivar_list contains one ivar. Others follow it.
|
|
|
|
cls->ivars = realloc(ivarlist, sizeof(struct objc_ivar_list) +
|
2011-07-14 17:23:48 +00:00
|
|
|
(ivarlist->count) * sizeof(struct objc_ivar));
|
2010-06-06 21:11:25 +00:00
|
|
|
}
|
|
|
|
Ivar ivar = &cls->ivars->ivar_list[cls->ivars->count - 1];
|
|
|
|
ivar->name = strdup(name);
|
|
|
|
ivar->type = strdup(types);
|
|
|
|
// Round up the offset of the ivar so it is correctly aligned.
|
2013-06-21 11:47:55 +00:00
|
|
|
long offset = cls->instance_size;
|
|
|
|
if (alignment != 0)
|
2010-06-06 21:11:25 +00:00
|
|
|
{
|
2013-06-21 11:47:55 +00:00
|
|
|
offset >>= alignment;
|
|
|
|
|
|
|
|
if (offset << alignment != cls->instance_size)
|
|
|
|
{
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
offset <<= alignment;
|
2010-06-06 21:11:25 +00:00
|
|
|
}
|
2010-03-03 09:34:45 +00:00
|
|
|
|
2010-06-06 21:11:25 +00:00
|
|
|
ivar->offset = offset;
|
|
|
|
// Increase the instance size to make space for this.
|
|
|
|
cls->instance_size = ivar->offset + size;
|
|
|
|
return YES;
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types)
|
|
|
|
{
|
2011-02-13 11:06:25 +00:00
|
|
|
CHECK_ARG(cls);
|
|
|
|
CHECK_ARG(name);
|
|
|
|
CHECK_ARG(imp);
|
|
|
|
CHECK_ARG(types);
|
2010-06-06 21:11:25 +00:00
|
|
|
const char *methodName = sel_getName(name);
|
2009-09-08 16:18:59 +00:00
|
|
|
struct objc_method_list *methods;
|
2010-06-06 21:11:25 +00:00
|
|
|
for (methods=cls->methods; methods!=NULL ; methods=methods->next)
|
2009-09-08 16:18:59 +00:00
|
|
|
{
|
2010-06-06 21:11:25 +00:00
|
|
|
for (int i=0 ; i<methods->count ; i++)
|
2009-09-08 16:18:59 +00:00
|
|
|
{
|
2010-06-06 21:11:25 +00:00
|
|
|
Method method = &methods->methods[i];
|
|
|
|
if (strcmp(sel_getName(method->selector), methodName) == 0)
|
2009-09-08 16:18:59 +00:00
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-14 17:23:48 +00:00
|
|
|
methods = malloc(sizeof(struct objc_method_list) + sizeof(struct objc_method));
|
2010-06-06 21:11:25 +00:00
|
|
|
methods->next = cls->methods;
|
2009-09-08 16:18:59 +00:00
|
|
|
cls->methods = methods;
|
|
|
|
|
2010-06-06 21:11:25 +00:00
|
|
|
methods->count = 1;
|
|
|
|
methods->methods[0].selector = sel_registerTypedName_np(methodName, types);
|
|
|
|
methods->methods[0].types = strdup(types);
|
|
|
|
methods->methods[0].imp = imp;
|
2009-09-08 16:18:59 +00:00
|
|
|
|
2010-06-06 21:11:25 +00:00
|
|
|
if (objc_test_class_flag(cls, objc_class_flag_resolved))
|
2009-09-08 16:18:59 +00:00
|
|
|
{
|
2011-04-19 10:55:10 +00:00
|
|
|
add_method_list_to_class(cls, methods);
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL class_addProtocol(Class cls, Protocol *protocol)
|
|
|
|
{
|
2011-02-13 11:06:25 +00:00
|
|
|
CHECK_ARG(cls);
|
|
|
|
CHECK_ARG(protocol);
|
2009-09-08 16:18:59 +00:00
|
|
|
if (class_conformsToProtocol(cls, protocol)) { return NO; }
|
2012-02-06 17:15:48 +00:00
|
|
|
struct objc_protocol_list *protocols =
|
2011-07-14 17:23:48 +00:00
|
|
|
malloc(sizeof(struct objc_protocol_list) + sizeof(Protocol2*));
|
2009-09-08 16:18:59 +00:00
|
|
|
if (protocols == NULL) { return NO; }
|
|
|
|
protocols->next = cls->protocols;
|
|
|
|
protocols->count = 1;
|
2010-06-06 21:11:25 +00:00
|
|
|
protocols->list[0] = (Protocol2*)protocol;
|
2009-09-08 16:18:59 +00:00
|
|
|
cls->protocols = protocols;
|
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
2011-02-13 11:06:25 +00:00
|
|
|
|
|
|
|
Ivar * class_copyIvarList(Class cls, unsigned int *outCount)
|
|
|
|
{
|
|
|
|
CHECK_ARG(cls);
|
|
|
|
struct objc_ivar_list *ivarlist = NULL;
|
|
|
|
unsigned int count = 0;
|
|
|
|
unsigned int index;
|
|
|
|
Ivar *list;
|
|
|
|
|
|
|
|
if (Nil != cls)
|
|
|
|
{
|
|
|
|
ivarlist = cls->ivars;
|
|
|
|
}
|
|
|
|
if (ivarlist != NULL)
|
|
|
|
{
|
|
|
|
count = ivarlist->count;
|
|
|
|
}
|
|
|
|
if (outCount != NULL)
|
|
|
|
{
|
|
|
|
*outCount = count;
|
|
|
|
}
|
|
|
|
if (count == 0)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
list = malloc((count + 1) * sizeof(struct objc_ivar *));
|
|
|
|
list[count] = NULL;
|
|
|
|
count = 0;
|
|
|
|
for (index = 0; index < ivarlist->count; index++)
|
|
|
|
{
|
|
|
|
list[count++] = &ivarlist->ivar_list[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
Method * class_copyMethodList(Class cls, unsigned int *outCount)
|
|
|
|
{
|
|
|
|
CHECK_ARG(cls);
|
|
|
|
unsigned int count = 0;
|
|
|
|
Method *list;
|
|
|
|
struct objc_method_list *methods;
|
|
|
|
|
|
|
|
if (cls != NULL)
|
|
|
|
{
|
|
|
|
for (methods = cls->methods; methods != NULL; methods = methods->next)
|
|
|
|
{
|
|
|
|
count += methods->count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (outCount != NULL)
|
|
|
|
{
|
|
|
|
*outCount = count;
|
|
|
|
}
|
|
|
|
if (count == 0)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
list = malloc((count + 1) * sizeof(struct objc_method *));
|
|
|
|
list[count] = NULL;
|
|
|
|
count = 0;
|
|
|
|
for (methods = cls->methods; methods != NULL; methods = methods->next)
|
|
|
|
{
|
|
|
|
unsigned int index;
|
|
|
|
for (index = 0; index < methods->count; index++)
|
|
|
|
{
|
|
|
|
list[count++] = &methods->methods[index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2011-06-29 10:12:29 +00:00
|
|
|
Protocol*__unsafe_unretained* class_copyProtocolList(Class cls, unsigned int *outCount)
|
2012-02-06 17:15:48 +00:00
|
|
|
{
|
2011-02-13 11:06:25 +00:00
|
|
|
CHECK_ARG(cls);
|
|
|
|
struct objc_protocol_list *protocolList = NULL;
|
|
|
|
struct objc_protocol_list *list;
|
|
|
|
unsigned int count = 0;
|
|
|
|
Protocol **protocols;
|
|
|
|
|
|
|
|
if (Nil != cls)
|
|
|
|
{
|
|
|
|
protocolList = cls->protocols;
|
|
|
|
}
|
|
|
|
for (list = protocolList; list != NULL; list = list->next)
|
|
|
|
{
|
|
|
|
count += list->count;
|
|
|
|
}
|
|
|
|
if (outCount != NULL)
|
|
|
|
{
|
|
|
|
*outCount = count;
|
|
|
|
}
|
|
|
|
if (count == 0)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
protocols = malloc((count + 1) * sizeof(Protocol *));
|
|
|
|
protocols[count] = NULL;
|
|
|
|
count = 0;
|
|
|
|
for (list = protocolList; list != NULL; list = list->next)
|
|
|
|
{
|
|
|
|
memcpy(&protocols[count], list->list, list->count * sizeof(Protocol *));
|
|
|
|
count += list->count;
|
|
|
|
}
|
|
|
|
return protocols;
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
id class_createInstance(Class cls, size_t extraBytes)
|
|
|
|
{
|
2011-02-13 11:06:25 +00:00
|
|
|
CHECK_ARG(cls);
|
2011-07-26 17:39:13 +00:00
|
|
|
if (sizeof(id) == 4)
|
|
|
|
{
|
|
|
|
if (cls == SmallObjectClasses[0])
|
|
|
|
{
|
|
|
|
return (id)1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (int i=0 ; i<4 ; i++)
|
|
|
|
{
|
|
|
|
if (cls == SmallObjectClasses[i])
|
|
|
|
{
|
2011-10-30 13:51:42 +00:00
|
|
|
return (id)(uintptr_t)((i<<1)+1);
|
2011-07-26 17:39:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-25 20:56:19 +00:00
|
|
|
if (Nil == cls) { return nil; }
|
2011-05-22 23:02:04 +00:00
|
|
|
id obj = gc->allocate_class(cls, extraBytes);
|
2009-09-08 16:18:59 +00:00
|
|
|
obj->isa = cls;
|
2011-07-03 11:14:29 +00:00
|
|
|
call_cxx_construct(obj);
|
2009-09-08 16:18:59 +00:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2011-03-21 23:54:42 +00:00
|
|
|
id object_copy(id obj, size_t size)
|
|
|
|
{
|
2011-03-22 12:36:47 +00:00
|
|
|
Class cls = object_getClass(obj);
|
|
|
|
id cpy = class_createInstance(cls, size - class_getInstanceSize(cls));
|
2011-03-21 23:54:42 +00:00
|
|
|
memcpy(((char*)cpy + sizeof(id)), ((char*)obj + sizeof(id)), size - sizeof(id));
|
|
|
|
return cpy;
|
|
|
|
}
|
|
|
|
|
|
|
|
id object_dispose(id obj)
|
|
|
|
{
|
2011-08-07 19:50:32 +00:00
|
|
|
call_cxx_destruct(obj);
|
|
|
|
gc->free_object(obj);
|
2011-03-22 12:36:47 +00:00
|
|
|
return nil;
|
2011-03-21 23:54:42 +00:00
|
|
|
}
|
|
|
|
|
2009-09-08 16:18:59 +00:00
|
|
|
Method class_getInstanceMethod(Class aClass, SEL aSelector)
|
|
|
|
{
|
2011-02-13 11:06:25 +00:00
|
|
|
CHECK_ARG(aClass);
|
|
|
|
CHECK_ARG(aSelector);
|
2011-07-11 22:51:18 +00:00
|
|
|
// If the class has a dtable installed, then we can use the fast path
|
|
|
|
if (classHasInstalledDtable(aClass))
|
|
|
|
{
|
|
|
|
// Do a dtable lookup to find out which class the method comes from.
|
2011-07-14 17:41:36 +00:00
|
|
|
struct objc_slot *slot = objc_get_slot(aClass, aSelector);
|
|
|
|
if (NULL == slot)
|
|
|
|
{
|
|
|
|
slot = objc_get_slot(aClass, sel_registerName(sel_getName(aSelector)));
|
|
|
|
if (NULL == slot)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2011-07-11 22:51:18 +00:00
|
|
|
|
|
|
|
// Now find the typed variant of the selector, with the correct types.
|
|
|
|
aSelector = slot->selector;
|
2012-02-06 17:15:48 +00:00
|
|
|
|
2011-07-11 22:51:18 +00:00
|
|
|
// Then do the slow lookup to find the method.
|
|
|
|
return class_getInstanceMethodNonrecursive(slot->owner, aSelector);
|
|
|
|
}
|
|
|
|
Method m = class_getInstanceMethodNonrecursive(aClass, aSelector);
|
|
|
|
if (NULL != m)
|
|
|
|
{
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
return class_getInstanceMethod(class_getSuperclass(aClass), aSelector);
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Method class_getClassMethod(Class aClass, SEL aSelector)
|
|
|
|
{
|
2010-09-25 20:56:19 +00:00
|
|
|
return class_getInstanceMethod(object_getClass((id)aClass), aSelector);
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ivar class_getClassVariable(Class cls, const char* name)
|
|
|
|
{
|
2011-02-13 11:06:25 +00:00
|
|
|
// Note: We don't have compiler support for cvars in ObjC
|
|
|
|
return class_getInstanceVariable(object_getClass((id)cls), name);
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t class_getInstanceSize(Class cls)
|
|
|
|
{
|
2010-09-25 20:56:19 +00:00
|
|
|
if (Nil == cls) { return 0; }
|
2010-01-07 14:41:38 +00:00
|
|
|
return cls->instance_size;
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
|
|
|
|
2011-02-13 11:06:25 +00:00
|
|
|
Ivar class_getInstanceVariable(Class cls, const char *name)
|
2009-09-08 16:18:59 +00:00
|
|
|
{
|
2010-09-18 16:33:34 +00:00
|
|
|
if (name != NULL)
|
2009-09-08 16:18:59 +00:00
|
|
|
{
|
2010-09-18 16:33:34 +00:00
|
|
|
while (cls != Nil)
|
2009-09-08 16:18:59 +00:00
|
|
|
{
|
2010-09-18 16:33:34 +00:00
|
|
|
struct objc_ivar_list *ivarlist = cls->ivars;
|
2010-02-28 17:26:41 +00:00
|
|
|
|
2010-09-18 16:33:34 +00:00
|
|
|
if (ivarlist != NULL)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < ivarlist->count; i++)
|
|
|
|
{
|
|
|
|
Ivar ivar = &ivarlist->ivar_list[i];
|
|
|
|
if (strcmp(ivar->name, name) == 0)
|
|
|
|
{
|
|
|
|
return ivar;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cls = class_getSuperclass(cls);
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
|
|
|
}
|
2010-09-18 16:33:34 +00:00
|
|
|
return NULL;
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The format of the char* is undocumented. This function is only ever used in
|
|
|
|
// conjunction with class_setIvarLayout().
|
|
|
|
const char *class_getIvarLayout(Class cls)
|
|
|
|
{
|
2011-02-13 11:06:25 +00:00
|
|
|
CHECK_ARG(cls);
|
2009-09-08 16:18:59 +00:00
|
|
|
return (char*)cls->ivars;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char * class_getName(Class cls)
|
|
|
|
{
|
2011-02-21 13:47:12 +00:00
|
|
|
if (Nil == cls) { return "nil"; }
|
2010-01-21 17:35:25 +00:00
|
|
|
return cls->name;
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int class_getVersion(Class theClass)
|
|
|
|
{
|
2011-02-13 11:06:25 +00:00
|
|
|
CHECK_ARG(theClass);
|
2010-06-06 21:11:25 +00:00
|
|
|
return theClass->version;
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *class_getWeakIvarLayout(Class cls)
|
|
|
|
{
|
|
|
|
assert(0 && "Weak ivars not supported");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL class_isMetaClass(Class cls)
|
|
|
|
{
|
2011-02-13 11:06:25 +00:00
|
|
|
CHECK_ARG(cls);
|
2010-06-06 21:11:25 +00:00
|
|
|
return objc_test_class_flag(cls, objc_class_flag_meta);
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IMP class_replaceMethod(Class cls, SEL name, IMP imp, const char *types)
|
|
|
|
{
|
2010-09-25 20:56:19 +00:00
|
|
|
if (Nil == cls) { return (IMP)0; }
|
2010-09-02 14:52:35 +00:00
|
|
|
SEL sel = sel_registerTypedName_np(sel_getName(name), types);
|
|
|
|
Method method = class_getInstanceMethodNonrecursive(cls, sel);
|
2009-09-08 16:18:59 +00:00
|
|
|
if (method == NULL)
|
|
|
|
{
|
2010-09-02 14:52:35 +00:00
|
|
|
class_addMethod(cls, sel, imp, types);
|
2009-09-08 16:18:59 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-06-06 21:11:25 +00:00
|
|
|
IMP old = (IMP)method->imp;
|
|
|
|
method->imp = imp;
|
2010-01-08 23:59:24 +00:00
|
|
|
|
2010-06-06 21:11:25 +00:00
|
|
|
if (objc_test_class_flag(cls, objc_class_flag_resolved))
|
2010-01-08 23:59:24 +00:00
|
|
|
{
|
2010-06-06 21:11:25 +00:00
|
|
|
objc_update_dtable_for_class(cls);
|
2010-01-08 23:59:24 +00:00
|
|
|
}
|
|
|
|
|
2009-09-08 16:18:59 +00:00
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void class_setIvarLayout(Class cls, const char *layout)
|
|
|
|
{
|
2011-02-13 11:06:25 +00:00
|
|
|
if ((Nil == cls) || (NULL == layout)) { return; }
|
2009-09-08 16:18:59 +00:00
|
|
|
struct objc_ivar_list *list = (struct objc_ivar_list*)layout;
|
2012-02-06 17:15:48 +00:00
|
|
|
size_t listsize = sizeof(struct objc_ivar_list) +
|
2011-07-14 17:23:48 +00:00
|
|
|
sizeof(struct objc_ivar) * (list->count);
|
2009-09-08 16:18:59 +00:00
|
|
|
cls->ivars = malloc(listsize);
|
|
|
|
memcpy(cls->ivars, list, listsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__((deprecated))
|
|
|
|
Class class_setSuperclass(Class cls, Class newSuper)
|
|
|
|
{
|
2011-02-13 11:06:25 +00:00
|
|
|
CHECK_ARG(cls);
|
|
|
|
CHECK_ARG(newSuper);
|
2010-09-25 20:56:19 +00:00
|
|
|
if (Nil == cls) { return Nil; }
|
2009-09-08 16:18:59 +00:00
|
|
|
Class oldSuper = cls->super_class;
|
|
|
|
cls->super_class = newSuper;
|
|
|
|
return oldSuper;
|
|
|
|
}
|
|
|
|
|
|
|
|
void class_setVersion(Class theClass, int version)
|
|
|
|
{
|
2010-09-25 20:56:19 +00:00
|
|
|
if (Nil == theClass) { return; }
|
2010-06-06 21:11:25 +00:00
|
|
|
theClass->version = version;
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void class_setWeakIvarLayout(Class cls, const char *layout)
|
|
|
|
{
|
|
|
|
assert(0 && "Not implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * ivar_getName(Ivar ivar)
|
|
|
|
{
|
2011-02-13 11:06:25 +00:00
|
|
|
CHECK_ARG(ivar);
|
2010-06-06 21:11:25 +00:00
|
|
|
return ivar->name;
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ptrdiff_t ivar_getOffset(Ivar ivar)
|
|
|
|
{
|
2011-02-13 11:06:25 +00:00
|
|
|
CHECK_ARG(ivar);
|
2010-06-06 21:11:25 +00:00
|
|
|
return ivar->offset;
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char * ivar_getTypeEncoding(Ivar ivar)
|
|
|
|
{
|
2011-02-13 11:06:25 +00:00
|
|
|
CHECK_ARG(ivar);
|
2010-06-06 21:11:25 +00:00
|
|
|
return ivar->type;
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void method_exchangeImplementations(Method m1, Method m2)
|
|
|
|
{
|
2010-09-25 20:56:19 +00:00
|
|
|
if (NULL == m1 || NULL == m2) { return; }
|
2010-06-06 21:11:25 +00:00
|
|
|
IMP tmp = (IMP)m1->imp;
|
|
|
|
m1->imp = m2->imp;
|
|
|
|
m2->imp = tmp;
|
2009-09-08 16:18:59 +00:00
|
|
|
objc_updateDtableForClassContainingMethod(m1);
|
|
|
|
objc_updateDtableForClassContainingMethod(m2);
|
|
|
|
}
|
|
|
|
|
|
|
|
IMP method_getImplementation(Method method)
|
|
|
|
{
|
2010-09-25 20:56:19 +00:00
|
|
|
if (NULL == method) { return (IMP)NULL; }
|
2010-06-06 21:11:25 +00:00
|
|
|
return (IMP)method->imp;
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SEL method_getName(Method method)
|
|
|
|
{
|
2010-09-25 20:56:19 +00:00
|
|
|
if (NULL == method) { return (SEL)NULL; }
|
2010-06-06 21:11:25 +00:00
|
|
|
return (SEL)method->selector;
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
IMP method_setImplementation(Method method, IMP imp)
|
|
|
|
{
|
2010-09-25 20:56:19 +00:00
|
|
|
if (NULL == method) { return (IMP)NULL; }
|
2010-06-06 21:11:25 +00:00
|
|
|
IMP old = (IMP)method->imp;
|
2013-02-28 14:31:53 +00:00
|
|
|
method->imp = imp;
|
2009-09-08 16:18:59 +00:00
|
|
|
objc_updateDtableForClassContainingMethod(method);
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
|
|
|
id objc_getRequiredClass(const char *name)
|
|
|
|
{
|
2011-02-13 11:06:25 +00:00
|
|
|
CHECK_ARG(name);
|
2009-09-08 16:18:59 +00:00
|
|
|
id cls = objc_getClass(name);
|
|
|
|
if (nil == cls)
|
|
|
|
{
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return cls;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void freeMethodLists(Class aClass)
|
|
|
|
{
|
|
|
|
struct objc_method_list *methods = aClass->methods;
|
|
|
|
while(methods != NULL)
|
|
|
|
{
|
2010-06-06 21:11:25 +00:00
|
|
|
for (int i=0 ; i<methods->count ; i++)
|
2009-09-08 16:18:59 +00:00
|
|
|
{
|
2010-06-06 21:11:25 +00:00
|
|
|
free((void*)methods->methods[i].types);
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
|
|
|
struct objc_method_list *current = methods;
|
2010-06-06 21:11:25 +00:00
|
|
|
methods = methods->next;
|
2009-09-08 16:18:59 +00:00
|
|
|
free(current);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void freeIvarLists(Class aClass)
|
|
|
|
{
|
|
|
|
struct objc_ivar_list *ivarlist = aClass->ivars;
|
|
|
|
if (NULL == ivarlist) { return; }
|
|
|
|
|
2010-06-06 21:11:25 +00:00
|
|
|
for (int i=0 ; i<ivarlist->count ; i++)
|
2009-09-08 16:18:59 +00:00
|
|
|
{
|
|
|
|
Ivar ivar = &ivarlist->ivar_list[i];
|
2010-06-06 21:11:25 +00:00
|
|
|
free((void*)ivar->type);
|
|
|
|
free((void*)ivar->name);
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
|
|
|
free(ivarlist);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Removes a class from the subclass list found on its super class.
|
|
|
|
* Must be called with the objc runtime mutex locked.
|
|
|
|
*/
|
|
|
|
static inline void safe_remove_from_subclass_list(Class cls)
|
|
|
|
{
|
2011-05-04 12:53:04 +00:00
|
|
|
// If this class hasn't been added to the class hierarchy, then this is easy
|
|
|
|
if (!objc_test_class_flag(cls, objc_class_flag_resolved)) { return; }
|
2009-09-08 16:18:59 +00:00
|
|
|
Class sub = cls->super_class->subclass_list;
|
|
|
|
if (sub == cls)
|
|
|
|
{
|
|
|
|
cls->super_class->subclass_list = cls->sibling_class;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (sub != NULL)
|
|
|
|
{
|
|
|
|
if (sub->sibling_class == cls)
|
|
|
|
{
|
|
|
|
sub->sibling_class = cls->sibling_class;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sub = sub->sibling_class;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void objc_disposeClassPair(Class cls)
|
|
|
|
{
|
2011-02-13 11:06:25 +00:00
|
|
|
if (0 == cls) { return; }
|
2009-09-08 16:18:59 +00:00
|
|
|
Class meta = ((id)cls)->isa;
|
|
|
|
// Remove from the runtime system so nothing tries updating the dtable
|
|
|
|
// while we are freeing the class.
|
2011-04-14 16:44:34 +00:00
|
|
|
{
|
|
|
|
LOCK_RUNTIME_FOR_SCOPE();
|
|
|
|
safe_remove_from_subclass_list(meta);
|
|
|
|
safe_remove_from_subclass_list(cls);
|
|
|
|
}
|
2009-09-08 16:18:59 +00:00
|
|
|
|
|
|
|
// Free the method and ivar lists.
|
|
|
|
freeMethodLists(cls);
|
|
|
|
freeMethodLists(meta);
|
|
|
|
freeIvarLists(cls);
|
2011-05-04 12:53:04 +00:00
|
|
|
if (cls->dtable != uninstalled_dtable)
|
|
|
|
{
|
|
|
|
free_dtable(cls->dtable);
|
|
|
|
}
|
|
|
|
if (meta->dtable != uninstalled_dtable)
|
|
|
|
{
|
|
|
|
free_dtable(meta->dtable);
|
|
|
|
}
|
2009-09-08 16:18:59 +00:00
|
|
|
|
|
|
|
// Free the class and metaclass
|
2011-08-07 19:50:32 +00:00
|
|
|
gc->free(meta);
|
|
|
|
gc->free(cls);
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Class objc_allocateClassPair(Class superclass, const char *name, size_t extraBytes)
|
|
|
|
{
|
|
|
|
// Check the class doesn't already exist.
|
|
|
|
if (nil != objc_lookUpClass(name)) { return Nil; }
|
|
|
|
|
2011-08-07 19:50:32 +00:00
|
|
|
Class newClass = gc->malloc(sizeof(struct objc_class) + extraBytes);
|
2009-09-08 16:18:59 +00:00
|
|
|
|
|
|
|
if (Nil == newClass) { return Nil; }
|
|
|
|
|
|
|
|
// Create the metaclass
|
2011-08-07 19:50:32 +00:00
|
|
|
Class metaClass = gc->malloc(sizeof(struct objc_class));
|
2009-09-08 16:18:59 +00:00
|
|
|
|
2012-02-06 17:15:48 +00:00
|
|
|
if (Nil == superclass)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Metaclasses of root classes are precious little flowers and work a
|
|
|
|
* little differently:
|
|
|
|
*/
|
|
|
|
metaClass->isa = metaClass;
|
|
|
|
metaClass->super_class = newClass;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Initialize the metaclass
|
|
|
|
// Set the meta-metaclass pointer to the name. The runtime will fix this
|
|
|
|
// in objc_resolve_class().
|
2013-01-14 10:34:17 +00:00
|
|
|
// If the superclass is not yet resolved, then we need to look it up
|
|
|
|
// via the class table.
|
2013-01-14 10:41:41 +00:00
|
|
|
metaClass->isa = (Class)superclass->isa->isa->name;
|
|
|
|
metaClass->super_class = superclass->isa;
|
2012-02-06 17:15:48 +00:00
|
|
|
}
|
2010-01-08 20:59:46 +00:00
|
|
|
metaClass->name = strdup(name);
|
2010-06-06 21:11:25 +00:00
|
|
|
metaClass->info = objc_class_flag_meta | objc_class_flag_user_created |
|
|
|
|
objc_class_flag_new_abi;
|
2011-04-14 16:44:34 +00:00
|
|
|
metaClass->dtable = uninstalled_dtable;
|
2010-01-06 22:28:59 +00:00
|
|
|
metaClass->instance_size = sizeof(struct objc_class);
|
2009-09-08 16:18:59 +00:00
|
|
|
|
|
|
|
// Set up the new class
|
2010-06-06 21:11:25 +00:00
|
|
|
newClass->isa = metaClass;
|
2010-01-07 13:31:18 +00:00
|
|
|
// Set the superclass pointer to the name. The runtime will fix this when
|
|
|
|
// the class links are resolved.
|
2012-02-06 17:15:48 +00:00
|
|
|
newClass->super_class = (Nil == superclass) ? Nil : (Class)(superclass->name);
|
|
|
|
|
2009-09-08 16:18:59 +00:00
|
|
|
newClass->name = strdup(name);
|
2010-06-06 21:11:25 +00:00
|
|
|
newClass->info = objc_class_flag_class | objc_class_flag_user_created |
|
|
|
|
objc_class_flag_new_abi;
|
2011-04-14 16:44:34 +00:00
|
|
|
newClass->dtable = uninstalled_dtable;
|
2012-02-06 17:15:48 +00:00
|
|
|
|
|
|
|
if (Nil == superclass)
|
|
|
|
{
|
|
|
|
newClass->instance_size = sizeof(struct objc_class);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newClass->instance_size = superclass->instance_size;
|
|
|
|
}
|
2009-09-08 16:18:59 +00:00
|
|
|
|
|
|
|
return newClass;
|
|
|
|
}
|
|
|
|
|
2010-02-27 18:24:03 +00:00
|
|
|
|
2009-09-08 16:18:59 +00:00
|
|
|
void *object_getIndexedIvars(id obj)
|
|
|
|
{
|
2011-02-13 11:06:25 +00:00
|
|
|
CHECK_ARG(obj);
|
2011-07-26 17:39:13 +00:00
|
|
|
size_t size = classForObject(obj)->instance_size;
|
|
|
|
if ((0 == size) && class_isMetaClass(classForObject(obj)))
|
2011-02-22 00:31:43 +00:00
|
|
|
{
|
|
|
|
Class cls = (Class)obj;
|
|
|
|
if (objc_test_class_flag(cls, objc_class_flag_new_abi))
|
|
|
|
{
|
|
|
|
size = sizeof(struct objc_class);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size = sizeof(struct legacy_abi_objc_class);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ((char*)obj) + size;
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Class object_getClass(id obj)
|
|
|
|
{
|
2011-02-13 11:06:25 +00:00
|
|
|
CHECK_ARG(obj);
|
2011-07-26 17:39:13 +00:00
|
|
|
Class isa = classForObject(obj);
|
2011-02-13 11:06:25 +00:00
|
|
|
while ((Nil != isa) && objc_test_class_flag(isa, objc_class_flag_hidden_class))
|
2009-09-08 16:18:59 +00:00
|
|
|
{
|
2011-02-13 11:06:25 +00:00
|
|
|
isa = isa->super_class;
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
2011-02-13 11:06:25 +00:00
|
|
|
return isa;
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
|
|
|
|
2009-12-27 22:00:44 +00:00
|
|
|
Class object_setClass(id obj, Class cls)
|
|
|
|
{
|
2011-02-13 11:06:25 +00:00
|
|
|
CHECK_ARG(obj);
|
2011-07-26 17:39:13 +00:00
|
|
|
// If this is a small object, then don't set its class.
|
2014-07-04 11:07:30 +00:00
|
|
|
if (isSmallObject(obj)) { return classForObject(obj); }
|
2011-02-13 11:06:25 +00:00
|
|
|
Class oldClass = obj->isa;
|
|
|
|
obj->isa = cls;
|
|
|
|
return oldClass;
|
2009-12-27 22:00:44 +00:00
|
|
|
}
|
|
|
|
|
2009-09-08 16:18:59 +00:00
|
|
|
const char *object_getClassName(id obj)
|
|
|
|
{
|
2011-02-13 11:06:25 +00:00
|
|
|
CHECK_ARG(obj);
|
2009-09-08 16:18:59 +00:00
|
|
|
return class_getName(object_getClass(obj));
|
|
|
|
}
|
|
|
|
|
2013-01-14 10:41:41 +00:00
|
|
|
PRIVATE void objc_resolve_class(Class);
|
|
|
|
|
2009-09-08 16:18:59 +00:00
|
|
|
void objc_registerClassPair(Class cls)
|
|
|
|
{
|
2011-04-14 16:44:34 +00:00
|
|
|
LOCK_RUNTIME_FOR_SCOPE();
|
2010-06-06 21:11:25 +00:00
|
|
|
class_table_insert(cls);
|
2013-01-14 10:41:41 +00:00
|
|
|
objc_resolve_class(cls);
|
2009-09-08 16:18:59 +00:00
|
|
|
}
|
2011-07-03 11:14:29 +00:00
|
|
|
|