<commit_before>#include <stdio.h>
#include "objc/runtime.h"
#include "loader.h"

#define BUFFER_TYPE struct objc_category
#include "buffer.h"

static void register_methods(struct objc_class *cls, struct objc_method_list *l)
{
	if (NULL == l) { return; }

	// Replace the method names with selectors.
	objc_register_selectors_from_list(l);
	// Add the method list at the head of the list of lists.
	l->next = cls->methods;
	cls->methods = l;
	// Update the dtable to catch the new methods.
	// FIXME: We can make this more efficient by simply passing the new method
	// list to the dtable and telling it only to update those methods.
	objc_update_dtable_for_class(cls);
}

static void load_category(struct objc_category *cat, struct objc_class *class)
{
	register_methods(class, cat->instance_methods);
	register_methods(class->isa, cat->class_methods);
	//fprintf(stderr, "Loading %s (%s)\n", cat->class_name, cat->name);

	if (cat->protocols)
	{
		objc_init_protocols(cat->protocols);
		cat->protocols->next = class->protocols;
		class->protocols = cat->protocols;
	}
}

static BOOL try_load_category(struct objc_category *cat)
{
	Class class = (Class)objc_getClass(cat->class_name);
	//fprintf(stderr, "Trying to load %s (%s)\n", cat->class_name, cat->name);
	if (Nil != class)
	{
		load_category(cat, class);
		return YES;
	}
	//fprintf(stderr, "waiting to load %s (%s)\n", cat->class_name, cat->name);
	return NO;
}

/**
 * Attaches a category to its class, if the class is already loaded.  Buffers
 * it for future resolution if not.
 */
void objc_try_load_category(struct objc_category *cat)
{
	if (!try_load_category(cat))
	{
		set_buffered_object_at_index(cat, buffered_objects++);
	}
}

void objc_load_buffered_categories(void)
{
	BOOL shouldReshuffle = NO;

	for (unsigned i=0 ; i<buffered_objects ; i++)
	{
		struct objc_category *c = buffered_object_at_index(i);
		if (NULL != c)
		{
			if (try_load_category(c))
			{
				set_buffered_object_at_index(NULL, i);
				shouldReshuffle = YES;
			}
		}
	}

	if (shouldReshuffle)
	{
		compact_buffer();
	}
}

<commit_msg>Remember to run +load methods in categories, as well as classes.<commit_after>#include <stdio.h>
#include "objc/runtime.h"
#include "loader.h"

#define BUFFER_TYPE struct objc_category
#include "buffer.h"

void objc_send_load_message(Class class);

static void register_methods(struct objc_class *cls, struct objc_method_list *l)
{
	if (NULL == l) { return; }

	// Replace the method names with selectors.
	objc_register_selectors_from_list(l);
	// Add the method list at the head of the list of lists.
	l->next = cls->methods;
	cls->methods = l;
	// Update the dtable to catch the new methods.
	// FIXME: We can make this more efficient by simply passing the new method
	// list to the dtable and telling it only to update those methods.
	objc_update_dtable_for_class(cls);
}

static void load_category(struct objc_category *cat, struct objc_class *class)
{
	register_methods(class, cat->instance_methods);
	register_methods(class->isa, cat->class_methods);
	//fprintf(stderr, "Loading %s (%s)\n", cat->class_name, cat->name);

	if (cat->protocols)
	{
		objc_init_protocols(cat->protocols);
		cat->protocols->next = class->protocols;
		class->protocols = cat->protocols;
	}
	objc_send_load_message(class);
}

static BOOL try_load_category(struct objc_category *cat)
{
	Class class = (Class)objc_getClass(cat->class_name);
	//fprintf(stderr, "Trying to load %s (%s)\n", cat->class_name, cat->name);
	if (Nil != class)
	{
		load_category(cat, class);
		return YES;
	}
	//fprintf(stderr, "waiting to load %s (%s)\n", cat->class_name, cat->name);
	return NO;
}

/**
 * Attaches a category to its class, if the class is already loaded.  Buffers
 * it for future resolution if not.
 */
void objc_try_load_category(struct objc_category *cat)
{
	if (!try_load_category(cat))
	{
		set_buffered_object_at_index(cat, buffered_objects++);
	}
}

void objc_load_buffered_categories(void)
{
	BOOL shouldReshuffle = NO;

	for (unsigned i=0 ; i<buffered_objects ; i++)
	{
		struct objc_category *c = buffered_object_at_index(i);
		if (NULL != c)
		{
			if (try_load_category(c))
			{
				set_buffered_object_at_index(NULL, i);
				shouldReshuffle = YES;
			}
		}
	}

	if (shouldReshuffle)
	{
		compact_buffer();
	}
}


{'fraction_non_alphanumeric': 0.12739597942964, 'fraction_numerical': 0.0004675081813931744, 'mean_word_length': 4.514175257731959, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1629750', 'n_tokens_mistral': 1598, 'n_tokens_neox': 1529, 'n_words': 398}