mini-obj-c/mini-obj-c.c
2026-10-20 10:12:45 +00:00

37 lines
878 B
C

#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include "mini-obj-c.h"
#define WARN(f, ...) fprintf(stderr, f "\n", ##__VA_ARGS__)
MethodImplementation method_lookup(struct Class* class, const char* sel)
{
for (; class; class = class->super) {
for (size_t i = 0; i < class->methods_count; ++i) {
auto entry = class->methods[i];
if (strcmp(entry.sel, sel) == 0) {
return entry.impl;
}
}
}
return nullptr;
}
struct Object* msg_send(struct Object* self, const char* sel)
{
if (!self || !self->class) {
WARN("Invalid object");
return nullptr;
}
auto impl = method_lookup(self->class, sel);
if (impl) {
return impl(self);
} else {
WARN("Method '%s' not found for class '%s'", sel, self->class->name);
return nullptr;
}
}