Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 277 lines (235 sloc) 6.134 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
0daff74 honor bridgesupport metadata when calculating the default objc signat…
Laurent Sansonetti authored
18 static inline const char *
19 rb_get_bs_method_type(bs_element_method_t *bs_method, int arg)
20 {
21 if (bs_method != NULL) {
22 if (arg == -1) {
23 if (bs_method->retval != NULL) {
24 return bs_method->retval->type;
25 }
26 }
27 else {
e031699 don't use c99 features in headers
Laurent Sansonetti authored
28 unsigned int i;
29 for (i = 0; i < bs_method->args_count; i++) {
0daff74 honor bridgesupport metadata when calculating the default objc signat…
Laurent Sansonetti authored
30 if (bs_method->args[i].index == arg) {
31 return bs_method->args[i].type;
32 }
33 }
34 }
35 }
36 return NULL;
37 }
38
65c5fb4 implemented informal protocol method definition + some refactoring
Laurent Sansonetti authored
39 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
40 bs_element_method_t *bs_method, char *buf, size_t buflen);
9c1d230 committing experimental branch content
Laurent Sansonetti authored
41
c306298 fixed the NSNumber conversion (which broke NSProxy), moved the suppor…
Laurent Sansonetti authored
42 bool rb_objc_supports_forwarding(VALUE recv, SEL sel);
43
9c1d230 committing experimental branch content
Laurent Sansonetti authored
44 void rb_objc_define_kvo_setter(VALUE klass, ID mid);
45
ab1a543 fixing more stupid IO bugs
Laurent Sansonetti authored
46 static inline IMP
9c1d230 committing experimental branch content
Laurent Sansonetti authored
47 rb_objc_install_method(Class klass, SEL sel, IMP imp)
48 {
ab1a543 fixing more stupid IO bugs
Laurent Sansonetti authored
49 Method method = class_getInstanceMethod(klass, sel);
9bf5ea3 disable the _cfindexOfObject:range: fix if > 10.5
Laurent Sansonetti authored
50 if (method == NULL) {
65c5fb4 implemented informal protocol method definition + some refactoring
Laurent Sansonetti authored
51 printf("method %s not found on class %p - aborting\n",
52 sel_getName(sel), klass);
9bf5ea3 disable the _cfindexOfObject:range: fix if > 10.5
Laurent Sansonetti authored
53 abort();
54 }
8ec0b76 fix a bug in rb_objc_install_method() where the previous IMP would no…
Laurent Sansonetti authored
55 IMP old = method_getImplementation(method);
56 class_replaceMethod(klass, sel, imp, method_getTypeEncoding(method));
57 return old;
9c1d230 committing experimental branch content
Laurent Sansonetti authored
58 }
59
ab1a543 fixing more stupid IO bugs
Laurent Sansonetti authored
60 static inline IMP
9c1d230 committing experimental branch content
Laurent Sansonetti authored
61 rb_objc_install_method2(Class klass, const char *selname, IMP imp)
62 {
ab1a543 fixing more stupid IO bugs
Laurent Sansonetti authored
63 return rb_objc_install_method(klass, sel_registerName(selname), imp);
9c1d230 committing experimental branch content
Laurent Sansonetti authored
64 }
65
66 static inline bool
67 rb_objc_is_kind_of(id object, Class klass)
68 {
69 Class cls;
70 for (cls = *(Class *)object; cls != NULL; cls = class_getSuperclass(cls)) {
71 if (cls == klass) {
72 return true;
73 }
74 }
75 return false;
76 }
77
bd7eba3 objc exceptions should now be catchable in ruby
Laurent Sansonetti authored
78 id rb_rb2oc_exception(VALUE exc);
6a0b646 finally re-commit r4185, apparently the problem is somewhere else
Laurent Sansonetti authored
79 VALUE rb_oc2rb_exception(id exc, bool *created);
1d49107 now throwing pure objc exceptions from ruby, this way they can be cat…
Laurent Sansonetti authored
80
9213dad expose a new API (to be used by a future patch)
Laurent Sansonetti authored
81 size_t rb_objc_type_size(const char *type);
82
ad6ef44 more work on the stub system (still a work in progress, though)
Laurent Sansonetti authored
83 static inline int
84 SubtypeUntil(const char *type, char end)
85 {
86 int level = 0;
87 const char *head = type;
88
89 while (*type)
90 {
91 if (!*type || (!level && (*type == end)))
92 return (int)(type - head);
93
94 switch (*type)
95 {
a8bf586 Add support for Proc as function pointers (BridgeSupport only)
Thibault Martin-Lagardette authored
96 case ']': case '}': case ')': case '>': level--; break;
97 case '[': case '{': case '(': case '<': level += 1; break;
ad6ef44 more work on the stub system (still a work in progress, though)
Laurent Sansonetti authored
98 }
99
100 type += 1;
101 }
102
103 rb_bug ("Object: SubtypeUntil: end of type encountered prematurely\n");
104 return 0;
105 }
106
107 static inline const char *
108 SkipStackSize(const char *type)
109 {
110 while ((*type >= '0') && (*type <= '9')) {
111 type += 1;
112 }
113 return type;
114 }
115
116 static inline const char *
025be3d added support for incoming/outcoming 'char *' types
Laurent Sansonetti authored
117 SkipTypeModifiers(const char *type)
118 {
119 while (true) {
120 switch (*type) {
121 case _C_CONST:
122 case 'O': /* bycopy */
123 case 'n': /* in */
124 case 'o': /* out */
125 case 'N': /* inout */
126 case 'V': /* oneway */
127 type++;
128 break;
129
130 default:
131 return type;
132 }
133 }
134 }
135
136 static inline const char *
ad6ef44 more work on the stub system (still a work in progress, though)
Laurent Sansonetti authored
137 SkipFirstType(const char *type)
138 {
139 while (1) {
140 switch (*type++) {
4a99756 don't recognize ^v as a CF type + misc cleaning
Laurent Sansonetti authored
141 case _C_CONST:
142 case _C_PTR:
ad6ef44 more work on the stub system (still a work in progress, though)
Laurent Sansonetti authored
143 case 'O': /* bycopy */
144 case 'n': /* in */
145 case 'o': /* out */
146 case 'N': /* inout */
147 case 'V': /* oneway */
148 break;
149
6418ac7 define our own version of method_getNumberOfArguments() since the sys…
Laurent Sansonetti authored
150 case _C_ID:
151 if (*type == _C_UNDEF) {
152 type++; /* Blocks */
153 }
154 return type;
155
ad6ef44 more work on the stub system (still a work in progress, though)
Laurent Sansonetti authored
156 /* arrays */
4a99756 don't recognize ^v as a CF type + misc cleaning
Laurent Sansonetti authored
157 case _C_ARY_B:
158 return type + SubtypeUntil (type, _C_ARY_E) + 1;
ad6ef44 more work on the stub system (still a work in progress, though)
Laurent Sansonetti authored
159
160 /* structures */
4a99756 don't recognize ^v as a CF type + misc cleaning
Laurent Sansonetti authored
161 case _C_STRUCT_B:
162 return type + SubtypeUntil (type, _C_STRUCT_E) + 1;
ad6ef44 more work on the stub system (still a work in progress, though)
Laurent Sansonetti authored
163
164 /* unions */
4a99756 don't recognize ^v as a CF type + misc cleaning
Laurent Sansonetti authored
165 case _C_UNION_B:
166 return type + SubtypeUntil (type, _C_UNION_E) + 1;
ad6ef44 more work on the stub system (still a work in progress, though)
Laurent Sansonetti authored
167
112362a support for C-level blocks (note: this requires a not-yet-released Br…
Laurent Sansonetti authored
168 /* lambdas */
169 case _MR_C_LAMBDA_B:
170 return type + SubtypeUntil (type, _MR_C_LAMBDA_E) + 1;
a8bf586 Add support for Proc as function pointers (BridgeSupport only)
Thibault Martin-Lagardette authored
171
ad6ef44 more work on the stub system (still a work in progress, though)
Laurent Sansonetti authored
172 /* basic types */
173 default:
174 return type;
175 }
176 }
177 }
178
8feda73 added support for anonymous structs
Laurent Sansonetti authored
179 static inline const char *
180 GetFirstType(const char *p, char *buf, size_t buflen)
181 {
182 const char *p2 = SkipFirstType(p);
183 const size_t len = p2 - p;
184 assert(len < buflen);
185 strncpy(buf, p, len);
186 buf[len] = '\0';
187 return SkipStackSize(p2);
188 }
189
a17a08a when registering a method as a BS informal protocol, pad the signatur…
Laurent Sansonetti authored
190 static inline unsigned int
191 TypeArity(const char *type)
192 {
193 unsigned int arity = 0;
194 while (*type != '\0') {
195 type = SkipFirstType(type);
59a8629 fix a bug in TypeArity() where stack size annotations were not ignored
Laurent Sansonetti authored
196 type = SkipStackSize(type);
a17a08a when registering a method as a BS informal protocol, pad the signatur…
Laurent Sansonetti authored
197 arity++;
198 }
199 return arity;
200 }
201
6418ac7 define our own version of method_getNumberOfArguments() since the sys…
Laurent Sansonetti authored
202 // We do not use method_getNumberOfArguments since it's broken on
203 // SnowLeopard for signatures containing Block objects.
204 static inline unsigned int
205 rb_method_getNumberOfArguments(Method m)
206 {
207 const unsigned int arity = TypeArity(method_getTypeEncoding(m));
208 assert(arity >= 2);
209 return arity - 1; // Skip return type.
210 }
211
002ed5d now compile some of the VM primitives into bitcode that will be added…
Laurent Sansonetti authored
212 id rb_objc_numeric2nsnumber(VALUE obj);
3760073 Handle NSDate immediate
Thibault Martin-Lagardette authored
213 VALUE rb_objc_convert_immediate(id obj);
002ed5d now compile some of the VM primitives into bitcode that will be added…
Laurent Sansonetti authored
214
4b7c757 cleaning a bit public headers, added support for methods defined for …
Laurent Sansonetti authored
215 static inline id
216 rb_rval_to_ocid(VALUE obj)
217 {
218 if (SPECIAL_CONST_P(obj)) {
219 if (obj == Qtrue) {
220 return (id)kCFBooleanTrue;
221 }
222 if (obj == Qfalse) {
223 return (id)kCFBooleanFalse;
224 }
225 if (obj == Qnil) {
226 return (id)kCFNull;
227 }
002ed5d now compile some of the VM primitives into bitcode that will be added…
Laurent Sansonetti authored
228 if (IMMEDIATE_P(obj)) {
229 return rb_objc_numeric2nsnumber(obj);
4b7c757 cleaning a bit public headers, added support for methods defined for …
Laurent Sansonetti authored
230 }
231 }
232 return (id)obj;
233 }
234
235 static inline VALUE
236 rb_ocid_to_rval(id obj)
237 {
238 if (obj == (id)kCFBooleanTrue) {
239 return Qtrue;
240 }
241 if (obj == (id)kCFBooleanFalse) {
242 return Qfalse;
243 }
244 if (obj == (id)kCFNull || obj == nil) {
245 return Qnil;
246 }
3760073 Handle NSDate immediate
Thibault Martin-Lagardette authored
247 return rb_objc_convert_immediate(obj);
4b7c757 cleaning a bit public headers, added support for methods defined for …
Laurent Sansonetti authored
248 }
249
250 #define RB2OC(obj) (rb_rval_to_ocid((VALUE)obj))
251 #define OC2RB(obj) (rb_ocid_to_rval((id)obj))
252
768bfbb added #%, added remaining NSString primitives
Laurent Sansonetti authored
253 void rb_objc_exception_raise(const char *name, const char *message);
254
d40a42f a better fix for ignored selectors, which handle the case where they …
Laurent Sansonetti authored
255 bool rb_objc_ignored_sel(SEL sel);
2edcd79 make NSObject#== and NSObject#eql? use -[NSObject isEqual:]
Laurent Sansonetti authored
256 bool rb_objc_isEqual(VALUE x, VALUE y);
13c85fa force class initialization upon lazy lookup
Laurent Sansonetti authored
257 void rb_objc_force_class_initialize(Class klass);
fa47b46 in case we are in a .app with MacRuby.framework embedded inside, relo…
Laurent Sansonetti authored
258 void rb_objc_fix_relocatable_load_path(void);
a3a0414 Xcode MacRuby apps will load the BridgeSupport files of frameworks li…
Laurent Sansonetti authored
259 void rb_objc_load_loaded_frameworks_bridgesupport(void);
fdb8951 better ignored sel detection
Laurent Sansonetti authored
260
002ed5d now compile some of the VM primitives into bitcode that will be added…
Laurent Sansonetti authored
261 extern bool rb_objc_enable_ivar_set_kvo_notifications;
262
9d1b612 aot: precompiling BridgeSupport metadata (work in progress)
Laurent Sansonetti authored
263 #if !defined(MACRUBY_STATIC)
264 void rb_vm_parse_bs_full_file(const char *path,
265 void (*add_stub_types_cb)(SEL, const char *, bool, void *),
266 void *ctx);
267 #endif
268
3760073 Handle NSDate immediate
Thibault Martin-Lagardette authored
269 #define SINCE_EPOCH 978307200.0
270 #define CF_REFERENCE_DATE SINCE_EPOCH
271
9c1d230 committing experimental branch content
Laurent Sansonetti authored
272 #if defined(__cplusplus)
273 }
274 #endif
275
276 #endif /* __OBJC_H_ */
Something went wrong with that request. Please try again.