-
Notifications
You must be signed in to change notification settings - Fork 0
/
objc.h
186 lines (154 loc) · 3.93 KB
/
objc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* MacRuby ObjC helpers.
*
* This file is covered by the Ruby license. See COPYING for more details.
*
* Copyright (C) 2007-2009, Apple Inc. All rights reserved.
*/
#ifndef __OBJC_H_
#define __OBJC_H_
#if defined(__cplusplus)
extern "C" {
#endif
#include "bs.h"
struct rb_objc_method_sig {
const char *types;
unsigned int argc;
};
bool rb_objc_get_types(VALUE recv, Class klass, SEL sel, Method m,
bs_element_method_t *bs_method, char *buf, size_t buflen);
VALUE rb_objc_call(VALUE recv, SEL sel, int argc, VALUE *argv);
VALUE rb_objc_call2(VALUE recv, VALUE klass, SEL sel, IMP imp,
struct rb_objc_method_sig *sig, bs_element_method_t *bs_method,
int argc, VALUE *argv);
void rb_objc_define_kvo_setter(VALUE klass, ID mid);
void rb_objc_change_ruby_method_signature(VALUE mod, ID mid, VALUE sig);
static inline IMP
rb_objc_install_method(Class klass, SEL sel, IMP imp)
{
Method method = class_getInstanceMethod(klass, sel);
if (method == NULL) {
printf("method %s not found on class %p - aborting\n",
sel_getName(sel), klass);
abort();
}
return class_replaceMethod(klass, sel, imp, method_getTypeEncoding(method));
}
static inline IMP
rb_objc_install_method2(Class klass, const char *selname, IMP imp)
{
return rb_objc_install_method(klass, sel_registerName(selname), imp);
}
static inline bool
rb_objc_is_kind_of(id object, Class klass)
{
Class cls;
for (cls = *(Class *)object; cls != NULL; cls = class_getSuperclass(cls)) {
if (cls == klass) {
return true;
}
}
return false;
}
extern void *placeholder_String;
extern void *placeholder_Dictionary;
extern void *placeholder_Array;
static inline bool
rb_objc_is_placeholder(id obj)
{
void *klass = *(void **)obj;
return klass == placeholder_String || klass == placeholder_Dictionary
|| klass == placeholder_Array;
}
bool rb_objc_symbolize_address(void *addr, void **start, char *name,
size_t name_len);
id rb_rb2oc_exception(VALUE exc);
VALUE rb_oc2rb_exception(id exc);
size_t rb_objc_type_size(const char *type);
static inline int
SubtypeUntil(const char *type, char end)
{
int level = 0;
const char *head = type;
while (*type)
{
if (!*type || (!level && (*type == end)))
return (int)(type - head);
switch (*type)
{
case ']': case '}': case ')': level--; break;
case '[': case '{': case '(': level += 1; break;
}
type += 1;
}
rb_bug ("Object: SubtypeUntil: end of type encountered prematurely\n");
return 0;
}
static inline const char *
SkipStackSize(const char *type)
{
while ((*type >= '0') && (*type <= '9')) {
type += 1;
}
return type;
}
static inline const char *
SkipTypeModifiers(const char *type)
{
while (true) {
switch (*type) {
case _C_CONST:
case 'O': /* bycopy */
case 'n': /* in */
case 'o': /* out */
case 'N': /* inout */
case 'V': /* oneway */
type++;
break;
default:
return type;
}
}
}
static inline const char *
SkipFirstType(const char *type)
{
while (1) {
switch (*type++) {
case 'O': /* bycopy */
case 'n': /* in */
case 'o': /* out */
case 'N': /* inout */
case 'r': /* const */
case 'V': /* oneway */
case '^': /* pointers */
break;
/* arrays */
case '[':
return type + SubtypeUntil (type, ']') + 1;
/* structures */
case '{':
return type + SubtypeUntil (type, '}') + 1;
/* unions */
case '(':
return type + SubtypeUntil (type, ')') + 1;
/* basic types */
default:
return type;
}
}
}
static inline unsigned int
TypeArity(const char *type)
{
unsigned int arity = 0;
while (*type != '\0') {
type = SkipFirstType(type);
arity++;
}
return arity;
}
#if defined(__cplusplus)
}
#endif
#endif /* __OBJC_H_ */