#include "runtime.h"
#include "sarray.h"
#include "encoding.h"
#define SELECTOR_HASH_SIZE 128
static struct sarray *__objc_selector_array = 0;
static struct sarray *__objc_selector_names = 0;
static cache_ptr __objc_selector_hash = 0;
static void register_selectors_from_list (MethodList_t);
unsigned int __objc_selector_max_index = 0;
void __objc_init_selector_tables ()
{
__objc_selector_array = sarray_new (SELECTOR_HASH_SIZE, 0);
__objc_selector_names = sarray_new (SELECTOR_HASH_SIZE, 0);
__objc_selector_hash
= hash_new (SELECTOR_HASH_SIZE,
(hash_func_type) hash_string,
(compare_func_type) compare_strings);
}
void
__objc_register_selectors_from_class (Class class)
{
MethodList_t method_list;
method_list = class->methods;
while (method_list)
{
register_selectors_from_list (method_list);
method_list = method_list->method_next;
}
}
static void
register_selectors_from_list (MethodList_t method_list)
{
int i = 0;
while (i < method_list->method_count)
{
Method_t method = &method_list->method_list[i];
method->method_name
= sel_register_typed_name ((const char *) method->method_name,
method->method_types);
i += 1;
}
}
void __objc_register_instance_methods_to_class (Class class)
{
MethodList_t method_list;
MethodList_t class_method_list;
int max_methods_no = 16;
MethodList_t new_list;
Method_t curr_method;
if (class->super_class)
return;
new_list = objc_calloc (sizeof (struct objc_method_list)
+ sizeof (struct objc_method[max_methods_no]), 1);
method_list = class->methods;
class_method_list = class->class_pointer->methods;
curr_method = &new_list->method_list[0];
while (method_list)
{
int i;
for (i = 0; i < method_list->method_count; i++)
{
Method_t mth = &method_list->method_list[i];
if (mth->method_name
&& ! search_for_method_in_list (class_method_list,
mth->method_name))
{
*curr_method = *mth;
if (++new_list->method_count == max_methods_no)
new_list =
objc_realloc (new_list, sizeof (struct objc_method_list)
+ sizeof (struct
objc_method[max_methods_no += 16]));
curr_method = &new_list->method_list[new_list->method_count];
}
}
method_list = method_list->method_next;
}
if (new_list->method_count)
{
new_list =
objc_realloc (new_list, sizeof (struct objc_method_list)
+ sizeof (struct objc_method[new_list->method_count]));
new_list->method_next = class->class_pointer->methods;
class->class_pointer->methods = new_list;
}
__objc_update_dispatch_table_for_class (class->class_pointer);
}
BOOL
sel_types_match (const char *t1, const char *t2)
{
if (! t1 || ! t2)
return NO;
while (*t1 && *t2)
{
if (*t1 == '+') t1++;
if (*t2 == '+') t2++;
while (isdigit ((unsigned char) *t1)) t1++;
while (isdigit ((unsigned char) *t2)) t2++;
t1 = objc_skip_type_qualifiers (t1);
t2 = objc_skip_type_qualifiers (t2);
if (! *t1 && ! *t2)
return YES;
if (*t1 != *t2)
return NO;
t1++;
t2++;
}
return NO;
}
SEL
sel_get_typed_uid (const char *name, const char *types)
{
struct objc_list *l;
sidx i;
objc_mutex_lock (__objc_runtime_mutex);
i = (sidx) hash_value_for_key (__objc_selector_hash, name);
if (i == 0)
{
objc_mutex_unlock (__objc_runtime_mutex);
return 0;
}
for (l = (struct objc_list *) sarray_get_safe (__objc_selector_array, i);
l; l = l->tail)
{
SEL s = (SEL) l->head;
if (types == 0 || s->sel_types == 0)
{
if (s->sel_types == types)
{
objc_mutex_unlock (__objc_runtime_mutex);
return s;
}
}
else if (sel_types_match (s->sel_types, types))
{
objc_mutex_unlock (__objc_runtime_mutex);
return s;
}
}
objc_mutex_unlock (__objc_runtime_mutex);
return 0;
}
SEL
sel_get_any_typed_uid (const char *name)
{
struct objc_list *l;
sidx i;
SEL s = NULL;
objc_mutex_lock (__objc_runtime_mutex);
i = (sidx) hash_value_for_key (__objc_selector_hash, name);
if (i == 0)
{
objc_mutex_unlock (__objc_runtime_mutex);
return 0;
}
for (l = (struct objc_list *) sarray_get_safe (__objc_selector_array, i);
l; l = l->tail)
{
s = (SEL) l->head;
if (s->sel_types)
{
objc_mutex_unlock (__objc_runtime_mutex);
return s;
}
}
objc_mutex_unlock (__objc_runtime_mutex);
return s;
}
SEL
sel_get_any_uid (const char *name)
{
struct objc_list *l;
sidx i;
objc_mutex_lock (__objc_runtime_mutex);
i = (sidx) hash_value_for_key (__objc_selector_hash, name);
if (soffset_decode (i) == 0)
{
objc_mutex_unlock (__objc_runtime_mutex);
return 0;
}
l = (struct objc_list *) sarray_get_safe (__objc_selector_array, i);
objc_mutex_unlock (__objc_runtime_mutex);
if (l == 0)
return 0;
return (SEL) l->head;
}
SEL
sel_get_uid (const char *name)
{
return sel_register_typed_name (name, 0);
}
const char *sel_get_name (SEL selector)
{
const char *ret;
objc_mutex_lock (__objc_runtime_mutex);
if ((soffset_decode ((sidx)selector->sel_id) > 0)
&& (soffset_decode ((sidx)selector->sel_id) <= __objc_selector_max_index))
ret = sarray_get_safe (__objc_selector_names, (sidx) selector->sel_id);
else
ret = 0;
objc_mutex_unlock (__objc_runtime_mutex);
return ret;
}
BOOL
sel_is_mapped (SEL selector)
{
unsigned int idx = soffset_decode ((sidx)selector->sel_id);
return ((idx > 0) && (idx <= __objc_selector_max_index));
}
const char *sel_get_type (SEL selector)
{
if (selector)
return selector->sel_types;
else
return 0;
}
extern struct sarray *__objc_uninstalled_dtable;
SEL
__sel_register_typed_name (const char *name, const char *types,
struct objc_selector *orig, BOOL is_const)
{
struct objc_selector *j;
sidx i;
struct objc_list *l;
i = (sidx) hash_value_for_key (__objc_selector_hash, name);
if (soffset_decode (i) != 0)
{
for (l = (struct objc_list *) sarray_get_safe (__objc_selector_array, i);
l; l = l->tail)
{
SEL s = (SEL) l->head;
if (types == 0 || s->sel_types == 0)
{
if (s->sel_types == types)
{
if (orig)
{
orig->sel_id = (void *) i;
return orig;
}
else
return s;
}
}
else if (! strcmp (s->sel_types, types))
{
if (orig)
{
orig->sel_id = (void *) i;
return orig;
}
else
return s;
}
}
if (orig)
j = orig;
else
j = objc_malloc (sizeof (struct objc_selector));
j->sel_id = (void *) i;
if ((is_const) || (types == 0))
j->sel_types = (const char *) types;
else {
j->sel_types = (char *) objc_malloc (strlen (types) + 1);
strcpy ((char *) j->sel_types, types);
}
l = (struct objc_list *) sarray_get_safe (__objc_selector_array, i);
}
else
{
__objc_selector_max_index += 1;
i = soffset_encode (__objc_selector_max_index);
if (orig)
j = orig;
else
j = objc_malloc (sizeof (struct objc_selector));
j->sel_id = (void *) i;
if ((is_const) || (types == 0))
j->sel_types = (const char *) types;
else {
j->sel_types = (char *) objc_malloc (strlen (types) + 1);
strcpy ((char *) j->sel_types, types);
}
l = 0;
}
DEBUG_PRINTF ("Record selector %s[%s] as: %ld\n", name, types,
soffset_decode (i));
{
int is_new = (l == 0);
const char *new_name;
if ((is_const) || (name == 0))
new_name = name;
else {
new_name = (char *) objc_malloc (strlen (name) + 1);
strcpy ((char *) new_name, name);
}
l = list_cons ((void *) j, l);
sarray_at_put_safe (__objc_selector_names, i, (void *) new_name);
sarray_at_put_safe (__objc_selector_array, i, (void *) l);
if (is_new)
hash_add (&__objc_selector_hash, (void *) new_name, (void *) i);
}
sarray_realloc (__objc_uninstalled_dtable, __objc_selector_max_index + 1);
return (SEL) j;
}
SEL
sel_register_name (const char *name)
{
SEL ret;
objc_mutex_lock (__objc_runtime_mutex);
ret = __sel_register_typed_name (name, 0, 0, NO);
objc_mutex_unlock (__objc_runtime_mutex);
return ret;
}
SEL
sel_register_typed_name (const char *name, const char *type)
{
SEL ret;
objc_mutex_lock (__objc_runtime_mutex);
ret = __sel_register_typed_name (name, type, 0, NO);
objc_mutex_unlock (__objc_runtime_mutex);
return ret;
}