-
Notifications
You must be signed in to change notification settings - Fork 0
/
objc_types_conversion.m
333 lines (256 loc) · 7.68 KB
/
objc_types_conversion.m
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#import <ObjFW/ObjFW.h>
#import "objc_types_conversion.h"
#import "objc_signature.h"
static void OF_INLINE skipName(const char ** signature) {
while (**signature != '=')
(*signature)++;
(*signature)++;
}
static size_t OF_INLINE sint_size_of_type(char type) {
switch (type) {
case 'B':
return sizeof(_Bool);
case 'c':
return sizeof(signed char);
case 's':
return sizeof(short);
case 'i':
return sizeof(int);
case 'l':
return sizeof(long);
case 'q':
return sizeof(long long);
default:
break;
}
OF_UNREACHABLE;
}
static size_t OF_INLINE uint_size_of_type(char type) {
switch (type) {
case 'C':
return sizeof(unsigned char);
case 'S':
return sizeof(unsigned short);
case 'I':
return sizeof(unsigned int);
case 'L':
return sizeof(unsigned long);
case 'Q':
return sizeof(unsigned long long);
default:
break;
}
OF_UNREACHABLE;
}
static size_t OF_INLINE int_size_of_type(char type) {
switch (type) {
case 'B':
case 'c':
case 's':
case 'i':
case 'l':
case 'q':
return sint_size_of_type(type);
case 'C':
case 'S':
case 'I':
case 'L':
case 'Q':
return uint_size_of_type(type);
default:
break;
}
OF_UNREACHABLE;
} /* int_size_of_type */
static ffi_type * sint_ffi_type(size_t typeSize) {
switch (typeSize) {
case 1:
return &ffi_type_sint8;
case 2:
return &ffi_type_sint16;
case 4:
return &ffi_type_sint32;
case 8:
return &ffi_type_sint64;
default:
break;
}
OF_UNREACHABLE;
}
static ffi_type * uint_ffi_type(size_t typeSize) {
switch (typeSize) {
case 1:
return &ffi_type_uint8;
case 2:
return &ffi_type_uint16;
case 4:
return &ffi_type_uint32;
case 8:
return &ffi_type_uint64;
default:
break;
}
OF_UNREACHABLE;
}
static ffi_type * int_type_to_ffi_type(char type) {
size_t typeSize = int_size_of_type(type);
switch (type) {
case 'B':
case 'c':
case 's':
case 'i':
case 'l':
case 'q':
return sint_ffi_type(typeSize);
case 'C':
case 'S':
case 'I':
case 'L':
case 'Q':
return uint_ffi_type(typeSize);
default:
break;
}
OF_UNREACHABLE;
} /* int_type_to_ffi_type */
static ffi_type * complex_ffi_type(char type) {
switch (type) {
case 'f':
return &ffi_type_complex_float;
case 'd':
return &ffi_type_complex_double;
case 'D':
return &ffi_type_complex_longdouble;
default:
break;
}
OF_UNREACHABLE;
}
ffi_type * of_ctype_to_ffi_type(const char * type, void *(*allocator)(void *, size_t), void * ctx) {
switch (type[0]) {
case 'B':
case 'c':
case 's':
case 'i':
case 'l':
case 'q':
case 'C':
case 'S':
case 'I':
case 'L':
case 'Q':
return int_type_to_ffi_type(type[0]);
case '@':
case '#':
case ':':
case '*':
case '^':
case '[':
return &ffi_type_pointer;
case 'f':
return &ffi_type_float;
case 'd':
return &ffi_type_double;
case 'D':
return &ffi_type_longdouble;
case 'j':
return complex_ffi_type(type[1]);
case '{':
return of_cstruct_to_ffi_struct(type, allocator, ctx);
case 'v':
return &ffi_type_void;
default:
break;
} /* switch */
OF_UNREACHABLE;
} /* ctype_to_ffi_type */
static ffi_type * parseArray(const char * signature, size_t * count, void *(*allocator)(void *, size_t), void * ctx) {
const char * signPtr = signature;
ffi_type * result = NULL;
size_t arraySize = 0;
while (*signPtr >= '0' && *signPtr <= '9')
arraySize = (arraySize * 10) + (size_t)((*signPtr) - '0');
*count = (arraySize == 0) ? 1 : arraySize;
if (*signPtr == '[') {
arraySize = 0;
result = parseArray((signPtr + 1), &arraySize, allocator, ctx);
if ((SIZE_MAX - *count) < arraySize)
@throw [OFOutOfRangeException exception];
*count += arraySize;
} else {
result = of_ctype_to_ffi_type(signPtr, allocator, ctx);
}
return result;
} /* parseArray */
ffi_type * of_cstruct_to_ffi_struct(const char * type, void *(*allocator)(void *, size_t), void * ctx) {
if (NULL == allocator)
@throw [OFInvalidArgumentException exception];
ffi_type * ffiStruct = NULL;
const char * typePtr = type;
const char * end = of_skip_struct(type);
OFDataArray * elementsLocal = [[OFDataArray alloc] initWithItemSize:sizeof(ffi_type *)];
skipName(&typePtr);
do {
ffi_type * element = NULL;
@try {
if (*typePtr == '[') {
size_t arraySize = 0;
element = parseArray((typePtr + 1), &arraySize, allocator, ctx);
[elementsLocal addItems:element count:arraySize];
typePtr = of_skip_array(typePtr);
continue;
}
element = of_ctype_to_ffi_type(typePtr, allocator, ctx);
if (NULL == element)
OF_UNREACHABLE;
[elementsLocal addItem:&element];
} @catch (id e) {
[elementsLocal release];
@throw e;
}
typePtr = of_next_type(typePtr);
} while (typePtr != NULL && (typePtr != (end - 1)) && *typePtr);
size_t elementsCount = [elementsLocal count] + 1;
ffi_type ** elements = allocator(ctx, elementsCount * sizeof(ffi_type *));
if (elements == NULL) {
[elementsLocal release];
@throw [OFOutOfMemoryException exceptionWithRequestedSize:(elementsCount * sizeof(ffi_type *))];
}
memcpy(elements, [elementsLocal items], ([elementsLocal count] * [elementsLocal itemSize]));
elements[elementsCount - 1] = NULL;
ffiStruct = allocator(ctx, sizeof(*ffiStruct));
if (ffiStruct == NULL) {
[elementsLocal release];
@throw [OFOutOfMemoryException exceptionWithRequestedSize:sizeof(*ffiStruct)];
}
ffiStruct->type = FFI_TYPE_STRUCT;
ffiStruct->elements = elements;
[elementsLocal release];
return ffiStruct;
} /* cstruct_to_ffi_struct */
int of_cfunction_argc(const char * signature) {
int argc = -1;
const char * signPtr = signature;
while ((signPtr = of_next_type(signPtr)) != NULL)
argc++;
return argc;
}
ffi_type ** of_cfunction_args_list(const char * signature, int * argc, ffi_type ** rvalue, void *(*allocator)(void *, size_t), void * ctx) {
const char * signPtr = signature;
int _argc = of_cfunction_argc(signPtr);
ffi_type ** argv = allocator(ctx, ((size_t)_argc * sizeof(*argv)));
if (NULL == argv)
@throw [OFOutOfMemoryException exceptionWithRequestedSize:((size_t)_argc * sizeof(*argv))];
int i = -1;
do {
if (i >= 0)
argv[i] = of_ctype_to_ffi_type(signPtr, allocator, ctx);
else {
if (NULL != rvalue)
*rvalue = of_ctype_to_ffi_type(signPtr, allocator, ctx);
}
i++;
} while ((signPtr = of_next_type(signPtr)) != NULL);
*argc = _argc;
return argv;
} /* of_cfunction_args_list */