Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 231 lines (193 sloc) 5.038 kb
65c5fb4 implemented informal protocol method definition + some refactoring
Laurent Sansonetti authored
1 /*
2 * MacRuby ObjC helpers.
3 *
4 * This file is covered by the Ruby license. See COPYING for more details.
5 *
4aca101 @drernie Added 2010 Copyrights
drernie authored
6 * Copyright (C) 2007-2010, Apple Inc. All rights reserved.
65c5fb4 implemented informal protocol method definition + some refactoring
Laurent Sansonetti authored
7 */
8
9c1d230 committing experimental branch content
Laurent Sansonetti authored
9 #ifndef __OBJC_H_
10 #define __OBJC_H_
11
12 #if defined(__cplusplus)
13 extern "C" {
14 #endif
15
16 #include "bs.h"
17
65c5fb4 implemented informal protocol method definition + some refactoring
Laurent Sansonetti authored
18 bool rb_objc_get_types(VALUE recv, Class klass, SEL sel, Method m,
c59aa3c working on a new objc dispatch, marked a few methods are always_inline
Laurent Sansonetti authored
19 bs_element_method_t *bs_method, char *buf, size_t buflen);
9c1d230 committing experimental branch content
Laurent Sansonetti authored
20
c306298 fixed the NSNumber conversion (which broke NSProxy), moved the support f...
Laurent Sansonetti authored
21 bool rb_objc_supports_forwarding(VALUE recv, SEL sel);
22
9c1d230 committing experimental branch content
Laurent Sansonetti authored
23 void rb_objc_define_kvo_setter(VALUE klass, ID mid);
24
ab1a543 fixing more stupid IO bugs
Laurent Sansonetti authored
25 static inline IMP
9c1d230 committing experimental branch content
Laurent Sansonetti authored
26 rb_objc_install_method(Class klass, SEL sel, IMP imp)
27 {
ab1a543 fixing more stupid IO bugs
Laurent Sansonetti authored
28 Method method = class_getInstanceMethod(klass, sel);
9bf5ea3 disable the _cfindexOfObject:range: fix if > 10.5
Laurent Sansonetti authored
29 if (method == NULL) {
65c5fb4 implemented informal protocol method definition + some refactoring
Laurent Sansonetti authored
30 printf("method %s not found on class %p - aborting\n",
31 sel_getName(sel), klass);
9bf5ea3 disable the _cfindexOfObject:range: fix if > 10.5
Laurent Sansonetti authored
32 abort();
33 }
ab1a543 fixing more stupid IO bugs
Laurent Sansonetti authored
34 return class_replaceMethod(klass, sel, imp, method_getTypeEncoding(method));
9c1d230 committing experimental branch content
Laurent Sansonetti authored
35 }
36
ab1a543 fixing more stupid IO bugs
Laurent Sansonetti authored
37 static inline IMP
9c1d230 committing experimental branch content
Laurent Sansonetti authored
38 rb_objc_install_method2(Class klass, const char *selname, IMP imp)
39 {
ab1a543 fixing more stupid IO bugs
Laurent Sansonetti authored
40 return rb_objc_install_method(klass, sel_registerName(selname), imp);
9c1d230 committing experimental branch content
Laurent Sansonetti authored
41 }
42
43 static inline bool
44 rb_objc_is_kind_of(id object, Class klass)
45 {
46 Class cls;
47 for (cls = *(Class *)object; cls != NULL; cls = class_getSuperclass(cls)) {
48 if (cls == klass) {
49 return true;
50 }
51 }
52 return false;
53 }
54
65c5fb4 implemented informal protocol method definition + some refactoring
Laurent Sansonetti authored
55 bool rb_objc_symbolize_address(void *addr, void **start, char *name,
56 size_t name_len);
9c1d230 committing experimental branch content
Laurent Sansonetti authored
57
bd7eba3 objc exceptions should now be catchable in ruby
Laurent Sansonetti authored
58 id rb_rb2oc_exception(VALUE exc);
6a0b646 finally re-commit r4185, apparently the problem is somewhere else
Laurent Sansonetti authored
59 VALUE rb_oc2rb_exception(id exc, bool *created);
1d49107 now throwing pure objc exceptions from ruby, this way they can be catche...
Laurent Sansonetti authored
60
9213dad expose a new API (to be used by a future patch)
Laurent Sansonetti authored
61 size_t rb_objc_type_size(const char *type);
62
ad6ef44 more work on the stub system (still a work in progress, though)
Laurent Sansonetti authored
63 static inline int
64 SubtypeUntil(const char *type, char end)
65 {
66 int level = 0;
67 const char *head = type;
68
69 while (*type)
70 {
71 if (!*type || (!level && (*type == end)))
72 return (int)(type - head);
73
74 switch (*type)
75 {
a8bf586 Add support for Proc as function pointers (BridgeSupport only)
Thibault Martin-Lagardette authored
76 case ']': case '}': case ')': case '>': level--; break;
77 case '[': case '{': case '(': case '<': level += 1; break;
ad6ef44 more work on the stub system (still a work in progress, though)
Laurent Sansonetti authored
78 }
79
80 type += 1;
81 }
82
83 rb_bug ("Object: SubtypeUntil: end of type encountered prematurely\n");
84 return 0;
85 }
86
87 static inline const char *
88 SkipStackSize(const char *type)
89 {
90 while ((*type >= '0') && (*type <= '9')) {
91 type += 1;
92 }
93 return type;
94 }
95
96 static inline const char *
025be3d added support for incoming/outcoming 'char *' types
Laurent Sansonetti authored
97 SkipTypeModifiers(const char *type)
98 {
99 while (true) {
100 switch (*type) {
101 case _C_CONST:
102 case 'O': /* bycopy */
103 case 'n': /* in */
104 case 'o': /* out */
105 case 'N': /* inout */
106 case 'V': /* oneway */
107 type++;
108 break;
109
110 default:
111 return type;
112 }
113 }
114 }
115
116 static inline const char *
ad6ef44 more work on the stub system (still a work in progress, though)
Laurent Sansonetti authored
117 SkipFirstType(const char *type)
118 {
119 while (1) {
120 switch (*type++) {
4a99756 don't recognize ^v as a CF type + misc cleaning
Laurent Sansonetti authored
121 case _C_CONST:
122 case _C_PTR:
ad6ef44 more work on the stub system (still a work in progress, though)
Laurent Sansonetti authored
123 case 'O': /* bycopy */
124 case 'n': /* in */
125 case 'o': /* out */
126 case 'N': /* inout */
127 case 'V': /* oneway */
128 break;
129
130 /* arrays */
4a99756 don't recognize ^v as a CF type + misc cleaning
Laurent Sansonetti authored
131 case _C_ARY_B:
132 return type + SubtypeUntil (type, _C_ARY_E) + 1;
ad6ef44 more work on the stub system (still a work in progress, though)
Laurent Sansonetti authored
133
134 /* structures */
4a99756 don't recognize ^v as a CF type + misc cleaning
Laurent Sansonetti authored
135 case _C_STRUCT_B:
136 return type + SubtypeUntil (type, _C_STRUCT_E) + 1;
ad6ef44 more work on the stub system (still a work in progress, though)
Laurent Sansonetti authored
137
138 /* unions */
4a99756 don't recognize ^v as a CF type + misc cleaning
Laurent Sansonetti authored
139 case _C_UNION_B:
140 return type + SubtypeUntil (type, _C_UNION_E) + 1;
ad6ef44 more work on the stub system (still a work in progress, though)
Laurent Sansonetti authored
141
a8bf586 Add support for Proc as function pointers (BridgeSupport only)
Thibault Martin-Lagardette authored
142 /* Function pointers */
4a99756 don't recognize ^v as a CF type + misc cleaning
Laurent Sansonetti authored
143 case _MR_C_FPTR_B:
144 return type + SubtypeUntil (type, _MR_C_FPTR_E) + 1;
a8bf586 Add support for Proc as function pointers (BridgeSupport only)
Thibault Martin-Lagardette authored
145
ad6ef44 more work on the stub system (still a work in progress, though)
Laurent Sansonetti authored
146 /* basic types */
147 default:
148 return type;
149 }
150 }
151 }
152
8feda73 added support for anonymous structs
Laurent Sansonetti authored
153 static inline const char *
154 GetFirstType(const char *p, char *buf, size_t buflen)
155 {
156 const char *p2 = SkipFirstType(p);
157 const size_t len = p2 - p;
158 assert(len < buflen);
159 strncpy(buf, p, len);
160 buf[len] = '\0';
161 return SkipStackSize(p2);
162 }
163
a17a08a when registering a method as a BS informal protocol, pad the signature w...
Laurent Sansonetti authored
164 static inline unsigned int
165 TypeArity(const char *type)
166 {
167 unsigned int arity = 0;
168 while (*type != '\0') {
169 type = SkipFirstType(type);
59a8629 fix a bug in TypeArity() where stack size annotations were not ignored
Laurent Sansonetti authored
170 type = SkipStackSize(type);
a17a08a when registering a method as a BS informal protocol, pad the signature w...
Laurent Sansonetti authored
171 arity++;
172 }
173 return arity;
174 }
175
002ed5d now compile some of the VM primitives into bitcode that will be added to...
Laurent Sansonetti authored
176 id rb_objc_numeric2nsnumber(VALUE obj);
177 VALUE rb_objc_nsnumber2numeric(id obj);
178
4b7c757 cleaning a bit public headers, added support for methods defined for the...
Laurent Sansonetti authored
179 static inline id
180 rb_rval_to_ocid(VALUE obj)
181 {
182 if (SPECIAL_CONST_P(obj)) {
183 if (obj == Qtrue) {
184 return (id)kCFBooleanTrue;
185 }
186 if (obj == Qfalse) {
187 return (id)kCFBooleanFalse;
188 }
189 if (obj == Qnil) {
190 return (id)kCFNull;
191 }
002ed5d now compile some of the VM primitives into bitcode that will be added to...
Laurent Sansonetti authored
192 if (IMMEDIATE_P(obj)) {
193 return rb_objc_numeric2nsnumber(obj);
4b7c757 cleaning a bit public headers, added support for methods defined for the...
Laurent Sansonetti authored
194 }
195 }
196 return (id)obj;
197 }
198
199 static inline VALUE
200 rb_ocid_to_rval(id obj)
201 {
202 if (obj == (id)kCFBooleanTrue) {
203 return Qtrue;
204 }
205 if (obj == (id)kCFBooleanFalse) {
206 return Qfalse;
207 }
208 if (obj == (id)kCFNull || obj == nil) {
209 return Qnil;
210 }
002ed5d now compile some of the VM primitives into bitcode that will be added to...
Laurent Sansonetti authored
211 return rb_objc_nsnumber2numeric(obj);
4b7c757 cleaning a bit public headers, added support for methods defined for the...
Laurent Sansonetti authored
212 }
213
214 #define RB2OC(obj) (rb_rval_to_ocid((VALUE)obj))
215 #define OC2RB(obj) (rb_ocid_to_rval((id)obj))
216
768bfbb added #%, added remaining NSString primitives
Laurent Sansonetti authored
217 void rb_objc_exception_raise(const char *name, const char *message);
218
fdb8951 better ignored sel detection
Laurent Sansonetti authored
219 bool rb_objc_ignore_sel(SEL sel);
2edcd79 make NSObject#== and NSObject#eql? use -[NSObject isEqual:]
Laurent Sansonetti authored
220 bool rb_objc_isEqual(VALUE x, VALUE y);
13c85fa force class initialization upon lazy lookup
Laurent Sansonetti authored
221 void rb_objc_force_class_initialize(Class klass);
fa47b46 in case we are in a .app with MacRuby.framework embedded inside, relocat...
Laurent Sansonetti authored
222 void rb_objc_fix_relocatable_load_path(void);
fdb8951 better ignored sel detection
Laurent Sansonetti authored
223
002ed5d now compile some of the VM primitives into bitcode that will be added to...
Laurent Sansonetti authored
224 extern bool rb_objc_enable_ivar_set_kvo_notifications;
225
9c1d230 committing experimental branch content
Laurent Sansonetti authored
226 #if defined(__cplusplus)
227 }
228 #endif
229
230 #endif /* __OBJC_H_ */
Something went wrong with that request. Please try again.