Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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