opuntiaOS - an operating system targeting x86 and ARMv7
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

[libobjc] Parse module and selectors

+184 -16
+2 -2
build/libs/BUILD.gn
··· 16 16 lib_c_flags += [ "-Os" ] 17 17 } 18 18 19 - lib_objcc_flags = [ "-Wno-nullability-completeness" ] 20 - 21 19 lib_cc_flags = [ 22 20 "-std=c++2a", 23 21 "-fno-sized-deallocation", ··· 25 23 "-fno-exceptions", 26 24 "-D_LIBCXX_BUILD_ONEOS_EXTENSIONS", 27 25 ] 26 + 27 + lib_objcc_flags = lib_cc_flags + [ "-Wno-nullability-completeness" ] 28 28 29 29 lib_asm_flags = [] 30 30
+9 -1
build/userland/BUILD.gn
··· 23 23 "-D_LIBCXX_BUILD_ONEOS_EXTENSIONS", 24 24 ] 25 25 26 + uland_objcc_flags = uland_cc_flags + [ 27 + "-Wno-objc-method-access", 28 + "-Wno-objc-root-class", 29 + "-fno-objc-exceptions", 30 + "-fno-objc-arc", 31 + ] 32 + 26 33 uland_asm_flags = [] 27 34 uland_ld_flags = [] 28 35 ··· 82 89 config("userland_flags") { 83 90 cflags = uland_c_flags 84 91 cflags_cc = uland_cc_flags 92 + cflags_objcc = uland_objcc_flags 85 93 asmflags = uland_asm_flags 86 94 ldflags = uland_ld_flags 87 95 defines = [ "opuntiaOS" ] ··· 112 120 ] 113 121 114 122 if (objc_support) { 115 - # deps += [ "//userland/utilities/testobjc:testobjc" ] 123 + deps += [ "//userland/utilities/testobjc:testobjc" ] 116 124 } 117 125 }
+1
libs/libc/include/string.h
··· 40 40 Note that this is dangerous because it writes memory no matter the size 41 41 the 'dest' buffer is. */ 42 42 char* strcpy(char* dest, const char* src); 43 + int strcmp(const char* a, const char* b); 43 44 44 45 /* Copy 'src' into 'dest' until it finds a null byte or reaches the 'nbytes' 45 46 limit provided by the user. This is the recommended way of copying strings,
+7
libs/libc/init/_lib.c
··· 8 8 { 9 9 _malloc_init(); 10 10 _stdio_init(); 11 + extern void (*__init_array_start[])(int, char**, char**) __attribute__((visibility("hidden"))); 12 + extern void (*__init_array_end[])(int, char**, char**) __attribute__((visibility("hidden"))); 13 + 14 + const unsigned int size = __init_array_end - __init_array_start; 15 + for (unsigned int i = 0; i < size; i++) { 16 + (*__init_array_start[i])(0, 0, 0); 17 + } 11 18 } 12 19 13 20 void _libc_deinit()
+16
libs/libc/string/string.c
··· 93 93 return 0; 94 94 } 95 95 96 + int strcmp(const char* a, const char* b) 97 + { 98 + while (*a == *b && *a != '\0' && *b != '\0') { 99 + a++; 100 + b++; 101 + } 102 + 103 + if (a < b) { 104 + return -1; 105 + } 106 + if (a > b) { 107 + return 1; 108 + } 109 + return 0; 110 + } 111 + 96 112 size_t strlen(const char* str) 97 113 { 98 114 size_t i = 0;
-8
libs/libcxx/init/_init.cpp
··· 12 12 { 13 13 _libc_init(); 14 14 _Z12_libcpp_initv(); 15 - 16 - extern void (*__init_array_start[])(int, char**, char**) __attribute__((visibility("hidden"))); 17 - extern void (*__init_array_end[])(int, char**, char**) __attribute__((visibility("hidden"))); 18 - 19 - const size_t size = __init_array_end - __init_array_start; 20 - for (size_t i = 0; i < size; i++) { 21 - (*__init_array_start[i])(0, 0, 0); 22 - } 23 15 } 24 16 25 17 void _deinit()
+5
libs/libcxxabi/src/aeabi_runtime.cpp
··· 5 5 { 6 6 return __cxa_atexit(func, arg, dso_handle); 7 7 } 8 + 9 + int __aeabi_unwind_cpp_pr0() 10 + { 11 + return 0; 12 + } 8 13 }
+5 -1
libs/libobjc/BUILD.gn
··· 1 1 import("//build/libs/TEMPLATE.gni") 2 2 3 3 opuntiaOS_static_library("libobjc") { 4 - sources = [ "src/NSObject.mm" ] 4 + sources = [ 5 + "src/NSObject.mm", 6 + "src/init.mm", 7 + "src/selector.mm", 8 + ] 5 9 6 10 include_dirs = [ 7 11 "include/",
+19
libs/libobjc/include/libobjc/module.h
··· 1 + #ifndef _LIBOBJC_MODULE_H 2 + #define _LIBOBJC_MODULE_H 3 + 4 + struct objc_symtab { 5 + unsigned long sel_ref_cnt; 6 + struct objc_selector* refs; 7 + unsigned short cls_def_cnt; 8 + unsigned short cat_def_cnt; 9 + void* defs[1]; 10 + }; 11 + 12 + struct objc_module { 13 + unsigned long version; 14 + unsigned long size; 15 + const char* name; 16 + struct objc_symtab* symtab; 17 + }; 18 + 19 + #endif // _LIBOBJC_MODULE_H
+4
libs/libobjc/include/libobjc/selector.h
··· 5 5 6 6 struct objc_selector { 7 7 void* id; 8 + const char* types; 8 9 }; 9 10 typedef struct objc_selector* SEL; 10 11 ··· 15 16 } 16 17 return s1->id == s2->id; 17 18 } 19 + 20 + void selector_init_table(); 21 + void selector_add_from_module(struct objc_selector*); 18 22 19 23 #endif // _LIBOBJC_SELECTOR_H
+1 -1
libs/libobjc/src/NSObject.mm
··· 6 6 return nil_method; 7 7 } 8 8 9 - id objc_msgSend(id reciever, SEL selector) 9 + OBJC_EXPORT id objc_msgSend(id reciever, SEL selector) 10 10 { 11 11 // FIXME 12 12 return reciever;
+36
libs/libobjc/src/init.mm
··· 1 + #include <libobjc/objc.h> 2 + #include <libobjc/runtime.h> 3 + #include <libobjc/module.h> 4 + #include <stdio.h> 5 + 6 + // The function is called by constructor of each module. 7 + OBJC_EXPORT void __objc_exec_class(struct objc_module* module) 8 + { 9 + static bool prepared_data_structures = false; 10 + 11 + printf("Called __objc_exec_class, starting to init module\n"); 12 + fflush(stdout); 13 + 14 + if (!prepared_data_structures) { 15 + printf(" Prepearing ENV"); 16 + fflush(stdout); 17 + selector_init_table(); 18 + prepared_data_structures = true; 19 + } 20 + 21 + 22 + struct objc_symtab* symtab = module->symtab; 23 + struct objc_selector* selectors = symtab->refs; 24 + 25 + if (selectors) { 26 + selector_add_from_module(selectors); 27 + } 28 + 29 + while (1) { } 30 + 31 + } 32 + 33 + OBJC_EXPORT Class objc_lookup_class(const char *name) 34 + { 35 + return (Class)NULL; 36 + }
+68
libs/libobjc/src/selector.mm
··· 1 + #include <libobjc/objc.h> 2 + #include <libobjc/runtime.h> 3 + #include <libobjc/selector.h> 4 + #include <stdio.h> 5 + #include <stdlib.h> 6 + #include <string.h> 7 + 8 + static struct objc_selector* selector_pool_start; 9 + static struct objc_selector* selector_pool_next; 10 + 11 + #define CONST_DATA true 12 + #define VOLATILE_DATA false 13 + 14 + static SEL selector_add(char* name, const char* types, bool const_data) 15 + { 16 + // Checking if we have this selector 17 + for (struct objc_selector* cur_sel = selector_pool_start; cur_sel != selector_pool_next; cur_sel++) { 18 + if (strcmp(name, (char*)cur_sel->id) == 0) { 19 + if (cur_sel->types == 0 || types == 0) { 20 + if (cur_sel->types == types) { 21 + return (SEL)cur_sel; 22 + } 23 + } else { 24 + if (strcmp(types, cur_sel->types)) { 25 + return (SEL)cur_sel; 26 + } 27 + } 28 + } 29 + } 30 + 31 + SEL sel = (SEL)selector_pool_next++; 32 + if (const_data) { 33 + sel->id = name; 34 + sel->types = types; 35 + } else { 36 + int name_len = strlen(name); 37 + char* name_data = (char*)malloc(name_len + 1); 38 + memcpy(name_data, name, name_len); 39 + name_data[name_len] = '\0'; 40 + sel->id = name_data; 41 + 42 + if (types) { 43 + int types_len = strlen(types); 44 + char* types_data = (char*)malloc(types_len + 1); 45 + memcpy(types_data, types, types_len); 46 + types_data[types_len] = '\0'; 47 + sel->types = types_data; 48 + } 49 + } 50 + 51 + return sel; 52 + } 53 + 54 + // TODO: We currently do it really stupid 55 + void selector_init_table() 56 + { 57 + selector_pool_start = selector_pool_next = (struct objc_selector*)malloc(1024); 58 + } 59 + 60 + 61 + void selector_add_from_module(struct objc_selector* selectors) 62 + { 63 + for (int i = 0; selectors[i].id; i++) { 64 + char* name = (char*)selectors[i].id; 65 + const char* types = selectors[i].types; 66 + selector_add(name, types, CONST_DATA); 67 + } 68 + }
+1 -1
toolchains/BUILD.gn
··· 100 100 } 101 101 tool("objcxx") { 102 102 depfile = "{{output}}.d" 103 - command = "$llvm_cc -MMD -MF $depfile $llvm_ccpp_target_flag {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} {{cflags_objcc}} -c {{source}} -o {{output}}" 103 + command = "$llvm_cxx -MMD -MF $depfile $llvm_ccpp_target_flag {{defines}} {{include_dirs}} {{cflags}} {{cflags_cc}} {{cflags_objcc}} -c {{source}} -o {{output}}" 104 104 depsformat = "gcc" 105 105 description = "OBJCXX {{output}}" 106 106 outputs =
+4 -2
userland/utilities/testobjc/BUILD.gn
··· 4 4 install_path = "bin/" 5 5 sources = [ "main.mm" ] 6 6 configs = [ "//build/userland:userland_flags" ] 7 - deplibs = [ "libobjc" ] 8 - cflags_objcc = [ "-Wno-objc-method-access", "-Wno-objc-root-class" ] 7 + deplibs = [ 8 + "libobjc", 9 + "libcxx", 10 + ] 9 11 }
+6
userland/utilities/testobjc/main.mm
··· 2 2 3 3 @interface SampleClass 4 4 - (void)sampleMethod; 5 + - (void)sampleMethod:(int)val; 5 6 @end 6 7 7 8 @implementation SampleClass ··· 10 11 printf("Hello, Obj-C!"); 11 12 } 12 13 14 + - (void)sampleMethod:(int)val { 15 + printf("Hello, Obj-C!"); 16 + } 17 + 13 18 @end 14 19 15 20 int main() { 16 21 SampleClass *sampleClass = [[SampleClass alloc]init]; 17 22 [sampleClass sampleMethod]; 23 + [sampleClass sampleMethod:5]; 18 24 return 0; 19 25 }