Initial commit
This commit is contained in:
commit
f222bb92af
9 changed files with 359 additions and 0 deletions
37
mini-obj-c.c
Normal file
37
mini-obj-c.c
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#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;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue