Squash commits for public release

This commit is contained in:
2025-02-12 09:54:05 -05:00
commit 7118adc514
1108 changed files with 80873 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
#ifndef _LIBOBJC_NSOBJECT_H
#define _LIBOBJC_NSOBJECT_H
#include <libobjc/runtime.h>
@interface NSObject {
Class isa;
}
+ (id)init;
+ (id)new;
+ (id)alloc;
- (id)init;
- (Class)class;
@end
#endif // _LIBOBJC_NSOBJECT_H

View File

@@ -0,0 +1,17 @@
#ifndef _LIBOBJC_CLASS_H
#define _LIBOBJC_CLASS_H
#include <libobjc/module.h>
#define DISPATCH_TABLE_NOT_INITIALIZED (void*)0x0
bool class_resolve_links(Class cls);
void class_resolve_all_unresolved();
void class_table_init();
void class_add_from_module(struct objc_symtab* symtab);
IMP class_get_implementation(Class cls, SEL sel);
Class objc_getClass(const char* name);
#endif // _LIBOBJC_CLASS_H

View File

@@ -0,0 +1,18 @@
#ifndef _LIBOBJC_HELPERS_H
#define _LIBOBJC_HELPERS_H
#include <stdio.h>
#define OBJC_EXPORT extern "C"
#define OBJC_DEBUG
#ifdef OBJC_DEBUG
#define OBJC_DEBUGPRINT(...) \
printf(__VA_ARGS__); \
fflush(stdout)
#else
#define OBJC_DEBUGPRINT(...) (sizeof(int))
#endif
#endif // _LIBOBJC_HELPERS_H

View File

@@ -0,0 +1,6 @@
#ifndef _LIBOBJC_ISA_H
#define _LIBOBJC_ISA_H
#define ISA_MASK 0xfffffffc
#endif // _LIBOBJC_ISA_H

View File

@@ -0,0 +1,14 @@
#ifndef _LIBOBJC_MEMORY_H
#define _LIBOBJC_MEMORY_H
#include <libobjc/runtime.h>
#include <stdlib.h>
#define objc_malloc(...) malloc(__VA_ARGS__)
#define objc_realloc(...) realloc(__VA_ARGS__)
#define objc_calloc(...) calloc(__VA_ARGS__)
#define objc_free(...) free(__VA_ARGS__)
id alloc_instance(Class cls);
#endif // _LIBOBJC_MEMORY_H

View File

@@ -0,0 +1,6 @@
#ifndef _LIBOBJC_MODULE_H
#define _LIBOBJC_MODULE_H
#include <libobjc/v1/decls.h>
#endif // _LIBOBJC_MODULE_H

View File

@@ -0,0 +1,10 @@
#ifndef _LIBOBJC_OBJC_H
#define _LIBOBJC_OBJC_H
#include <libobjc/helpers.h>
#include <libobjc/runtime.h>
OBJC_EXPORT id _Nullable objc_alloc(Class _Nullable cls);
OBJC_EXPORT id _Nullable objc_alloc_init(Class _Nullable cls);
#endif // _LIBOBJC_OBJC_H

View File

@@ -0,0 +1,21 @@
#ifndef _LIBOBJC_RUNTIME_H
#define _LIBOBJC_RUNTIME_H
#include <libobjc/helpers.h>
#include <libobjc/isa.h>
#include <libobjc/selector.h>
#include <libobjc/v1/decls.h>
#define ROOT_CLASS "NSObject"
OBJC_EXPORT void objc_msgSend(id reciever, SEL sel, ...);
static inline Class object_getClass(id object)
{
if (!object) {
return Nil;
}
return object->get_isa();
}
#endif // _LIBOBJC_RUNTIME_H

View File

@@ -0,0 +1,23 @@
#ifndef _LIBOBJC_SELECTOR_H
#define _LIBOBJC_SELECTOR_H
#include <libobjc/v1/decls.h>
#include <stddef.h>
static inline bool sel_equal(SEL s1, SEL s2)
{
if (s1 == NULL || s2 == NULL) {
return s1 == s2;
}
return s1->id == s2->id;
}
void selector_table_init();
void selector_add_from_module(struct objc_selector*);
void selector_add_from_method_list(struct objc_method_list*);
void selector_add_from_class(Class);
bool selector_is_valid(SEL sel);
SEL sel_registerName(const char* name);
SEL sel_registerTypedName(const char* name, const char* types);
#endif // _LIBOBJC_SELECTOR_H

View File

@@ -0,0 +1,139 @@
#ifndef _LIBOBJC_V1_DECLS_H
#define _LIBOBJC_V1_DECLS_H
#include <stdint.h>
struct objc_class;
struct objc_object;
struct objc_selector;
struct objc_method;
typedef struct objc_class* Class;
typedef struct objc_object* id;
typedef struct objc_selector* SEL;
typedef struct objc_method* Method;
typedef void (*IMP)(id, SEL, ...);
#define nil_method (IMP) NULL
#define nil (id) NULL
#define Nil (Class) NULL
struct objc_symtab {
unsigned long sel_ref_cnt;
struct objc_selector* refs;
unsigned short cls_def_cnt;
unsigned short cat_def_cnt;
void* defs[1]; // Variable len
};
struct objc_module {
unsigned long version;
unsigned long size;
const char* name;
struct objc_symtab* symtab;
};
struct objc_object final {
Class isa;
inline void set_isa(Class nisa) { isa = nisa; }
inline Class get_isa() { return isa; }
};
struct objc_ivar {
const char* ivar_name;
const char* ivar_type;
int ivar_offset;
};
struct objc_ivar_list {
int ivar_count;
struct objc_ivar ivar_list[1]; // Variable len
};
struct objc_method {
SEL method_name;
const char* method_types;
IMP method_imp;
};
struct objc_method_description {
SEL name;
char* types;
};
struct objc_method_list {
struct objc_method_list* method_next;
int method_count;
struct objc_method method_list[1]; // Variable len
};
struct objc_method_description_list {
int count;
struct objc_method_description list[1]; // Variable len
};
struct objc_protocol {
struct objc_class* class_pointer;
char* protocol_name;
struct objc_protocol_list* protocol_list;
struct objc_method_description_list* instance_methods;
struct objc_method_description_list* class_methods;
};
struct objc_protocol_list {
struct objc_protocol_list* next;
uint32_t count;
struct objc_protocol* list[1]; // Variable len
};
#define CLS_CLASS 0x1
#define CLS_META 0x2
#define CLS_INITIALIZED 0x4
#define CLS_RESOLVED 0x8 // This means that it has had correct super and sublinks assigned
#define CLS_INCONSTRUCTION 0x10
#define CLS_NUMBER_OFFSET 16 // Long is 32bit long on our target, we use 16bit for number
struct objc_class final {
Class isa;
Class superclass;
const char* name;
long version;
unsigned long info;
long instance_size;
struct objc_ivar_list* ivars;
struct objc_method_list* methods;
void* disp_table;
struct objc_class* subclass_list; // TODO: Currently unresolved
struct objc_class* sibling_class; // TODO: Currently unresolved
struct objc_protocol_list* protocols;
void* gc_object_type;
inline void set_isa(Class nisa) { isa = nisa; }
inline Class get_isa() { return isa; }
inline void set_info(unsigned long mask) { info = mask; }
inline void add_info(unsigned long mask) { info |= mask; }
inline void rem_info(unsigned long mask) { info &= (~mask); }
inline bool has_info(unsigned long mask) { return ((info & mask) == mask); }
inline unsigned long get_info() { return info; }
inline bool is_class() { return has_info(CLS_CLASS); }
inline bool is_meta() { return has_info(CLS_META); }
inline bool is_resolved() { return has_info(CLS_RESOLVED); }
inline bool is_initialized() { return has_info(CLS_INITIALIZED); }
inline bool is_root() { return (bool)superclass; }
inline void set_number(int num) { info = (info & ((1 << CLS_NUMBER_OFFSET) - 1)) | (num << CLS_NUMBER_OFFSET); }
inline int number() { return get_info() >> CLS_NUMBER_OFFSET; }
inline long size() { return instance_size; }
inline long aligned_size() { return instance_size; } // FIXME
};
struct objc_selector {
void* id;
const char* types;
};
#endif // _LIBOBJC_V1_DECLS_H