forked from programming-nu/nu
/
NuProtocol.m
353 lines (311 loc) · 11.4 KB
/
NuProtocol.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*!
@file protocol.m
@description Nu support for protocols.
@copyright Copyright (c) 2007 Neon Design Technology, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifdef GNUSTEP
#define true 1
#define false 0
#import <objc/objc.h>
#import <objc/objc-api.h>
#endif
#import <Foundation/Foundation.h>
#include "NuClass.h"
#include "NuExtensions.h"
#ifdef DARWIN
#include "objc/runtime.h"
#include "mach_override.h"
#endif
#ifdef GNUSTEP
struct objc_method_description_list
{
int count;
struct objc_method_description list[1];
};
#endif
#ifndef IPHONE
#ifndef __DARWIN_10_6_AND_LATER
#ifndef __x86_64__
#ifndef SNOWLEOPARD
@interface Protocol : NSObject
{
@public
char *protocol_name;
struct objc_protocol_list *protocol_list;
struct objc_method_description_list *instance_methods;
struct objc_method_description_list *class_methods;
}
- (const char *) name;
+ (Protocol *) protocolWithName:(NSString *) name;
+ (Protocol *) protocolNamed:(NSString *) name;
+ (NSArray *) all;
- (Protocol *) initWithName:(NSString *) name;
- (NSArray *) methodDescriptions;
- (NSComparisonResult) compare:(Protocol *) other;
- (NSArray *) protocols;
- (void) addInstanceMethod:(NSString *)name withSignature:(NSString *)signature;
- (void) addClassMethod:(NSString *)name withSignature:(NSString *)signature;
@end
// When we create protocols at runtime, we put them here.
static NSMutableDictionary *nuProtocols;
@implementation Protocol (Nu)
+ (Protocol *) protocolWithName:(NSString *) name
{
return [[[Protocol alloc] initWithName:name] autorelease];
}
+ (Protocol *) protocolNamed:(NSString *) name
{
Protocol *protocol = objc_getProtocol([name cStringUsingEncoding:NSUTF8StringEncoding]);
if (protocol) return protocol;
return nil;
}
+ (NSArray *) all
{
unsigned int count;
Protocol **protocolList = objc_copyProtocolList(&count);
NSMutableArray *array = [NSMutableArray array];
for (int i = 0; i < count; i++) {
[array addObject:protocolList[i]];
}
free(protocolList);
return array;
}
- (Protocol *) initWithName:(NSString *) name
{
[super init];
protocol_name = strdup([name cStringUsingEncoding:NSUTF8StringEncoding]);
protocol_list = NULL;
instance_methods = NULL;
class_methods = NULL;
if (!nuProtocols)
nuProtocols = [[NSMutableDictionary alloc] init];
[nuProtocols setPossiblyNullObject:self forKey:name];
return self;
}
static void addMethodDescriptionsToArray(Protocol *protocol, BOOL isRequiredMethod, BOOL isInstanceMethod, NSMutableArray *array)
{
unsigned int count;
struct objc_method_description *method_descriptions =
protocol_copyMethodDescriptionList(protocol, isRequiredMethod, isInstanceMethod, &count);
for (int i = 0; i < count; i++) {
#ifdef DARWIN
NSString *name = [NSString stringWithCString:sel_getName(method_descriptions[i].name) encoding:NSASCIIStringEncoding];
#else
NSString *name = [NSString stringWithCString:sel_get_name(method_descriptions[i].name) encoding:NSASCIIStringEncoding];
#endif
NSString *signature = [NSString stringWithCString:method_descriptions[i].types encoding:NSASCIIStringEncoding];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:name, @"name",
signature, @"signature",
[NSNumber numberWithInt:(isRequiredMethod ? 1 : 0)], @"required",
[NSNumber numberWithInt:(isInstanceMethod ? 1 : 0)], @"instance",
nil];
[array addObject:dictionary];
}
free(method_descriptions);
}
- (NSArray *) methodDescriptions
{
NSMutableArray *array = [NSMutableArray array];
addMethodDescriptionsToArray(self, true, true, array);
addMethodDescriptionsToArray(self, true, false, array);
addMethodDescriptionsToArray(self, false, true, array);
addMethodDescriptionsToArray(self, false, false, array);
return array;
}
- (NSComparisonResult) compare:(Protocol *) other
{
return strcmp([self name], [other name]);
}
- (NSArray *) protocols
{
NSMutableArray *array = [NSMutableArray array];
unsigned int count;
Protocol **protocolList = protocol_copyProtocolList(self, &count);
for (int i = 0; i < count; i++) {
[array addObject:protocolList[i]];
}
free(protocolList);
return array;
}
- (void) addInstanceMethod:(NSString *)name withSignature:(NSString *)signature
{
#ifdef DARWIN
SEL sel = sel_registerName([name cStringUsingEncoding:NSUTF8StringEncoding]);
#else
SEL sel = sel_register_name([name cStringUsingEncoding:NSUTF8StringEncoding]);
#endif
const char *types = [signature cStringUsingEncoding:NSUTF8StringEncoding];
struct objc_method_description_list *oldList = self->instance_methods;
//NSLog(@"old instance method list is %d", oldList);
if (oldList) {
struct objc_method_description_list *newList = (struct objc_method_description_list *) malloc
(sizeof(struct objc_method_description_list) + oldList->count * (sizeof (struct objc_method_description)));
memcpy(newList, oldList,
(sizeof(struct objc_method_description_list) + (oldList->count-1) * (sizeof (struct objc_method_description))));
newList->list[oldList->count].name = sel;
newList->list[oldList->count].types = strdup(types);
newList->count++;
//free(oldList); leak. It seems that we can't free the lists created when protocols are loaded from compiled objects.
self->instance_methods = newList;
}
else {
struct objc_method_description_list *newList = (struct objc_method_description_list *) malloc
(sizeof(struct objc_method_description_list));
newList->list[0].name = sel;
newList->list[0].types = strdup(types);
newList->count = 1;
self->instance_methods = newList;
}
}
- (void) addClassMethod:(NSString *)name withSignature:(NSString *)signature
{
#ifdef DARWIN
SEL sel = sel_registerName([name cStringUsingEncoding:NSUTF8StringEncoding]);
#else
SEL sel = sel_register_name([name cStringUsingEncoding:NSUTF8StringEncoding]);
#endif
const char *types = [signature cStringUsingEncoding:NSUTF8StringEncoding];
struct objc_method_description_list *oldList = self->class_methods;
//NSLog(@"old class method list is %d", oldList);
if (oldList) {
struct objc_method_description_list *newList = (struct objc_method_description_list *) malloc
(sizeof(struct objc_method_description_list) + oldList->count * (sizeof (struct objc_method_description)));
memcpy(newList, oldList,
(sizeof(struct objc_method_description_list) + (oldList->count-1) * (sizeof (struct objc_method_description))));
newList->list[oldList->count].name = sel;
newList->list[oldList->count].types = strdup(types);
newList->count++;
//free(oldList);
self->class_methods = newList;
}
else {
struct objc_method_description_list *newList = (struct objc_method_description_list *) malloc
(sizeof(struct objc_method_description_list));
newList->list[0].name = sel;
newList->list[0].types = strdup(types);
newList->count = 1;
self->class_methods = newList;
}
}
@end
@implementation NuClass (Protocols)
- (BOOL) addProtocol:(Protocol *)p
{
return class_addProtocol(self->c, p);
}
- (BOOL) conformsToProtocol:(Protocol *)p
{
return class_conformsToProtocol(self->c, p);
}
- (NSArray *) protocols
{
NSMutableArray *array = [NSMutableArray array];
unsigned int count;
Protocol **protocolList = class_copyProtocolList(self->c, &count);
for (int i = 0; i < count; i++) {
[array addObject:protocolList[i]];
}
free(protocolList);
return array;
}
@end
typedef Protocol *(*objc_getProtocol_ptr)(const char *name);
static objc_getProtocol_ptr original_objc_getProtocol;
Protocol *nu_objc_getProtocol(const char *name)
{
// NSLog(@"nu_objc_getProtocol %s", name);
Protocol *p = (*original_objc_getProtocol)(name);
if (p) return p;
else if (nuProtocols) {
// lookup protocol in our dictionary
id key = [NSString stringWithCString:name encoding:NSUTF8StringEncoding];
return [nuProtocols objectForKey:key];
} else
return nil;
}
typedef Protocol **(*objc_copyProtocolList_ptr)(unsigned int *outCount);
static objc_copyProtocolList_ptr original_objc_copyProtocolList;
Protocol **nu_objc_copyProtocolList(unsigned int *outCount)
{
//NSLog(@"nu_objc_copyProtocolList");
Protocol **originalProtocolList = (*original_objc_copyProtocolList)(outCount);
if (!nuProtocols || ([nuProtocols count] == 0)) {
return originalProtocolList;
}
Protocol **newProtocolList = (Protocol **) malloc ((*outCount + [nuProtocols count]) * sizeof (Protocol *));
for (int i = 0; i < *outCount; i++) {
newProtocolList[i] = originalProtocolList[i];
}
id myProtocolArray = [nuProtocols allValues];
for (int i = 0; i < [nuProtocols count]; i++) {
newProtocolList[i+*outCount] = [myProtocolArray objectAtIndex:i];
}
free(originalProtocolList);
*outCount += [nuProtocols count];
return newProtocolList;
}
#endif
void nu_initProtocols()
{
#ifndef __x86_64__
#ifndef SNOWLEOPARD
static int initialized = 0;
if (!initialized) {
initialized = 1;
nuProtocols = nil;
// We wish that Protocal inherited from NSObject instead of Object, so we make it so.
// This makes it easier to manipulate Protocols from Nu.
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[[NuClass classWithClass:[Protocol class]] setSuperclass:[NuClass classWithClass:[NSObject class]]];
[pool release];
#ifdef DARWIN
// Since Apple doesn't have an API to add new protocols, we make our own.
// We replace these functions with our own versions to include the protocols we create at runtime.
mach_override("_objc_getProtocol", NULL, (void*)&nu_objc_getProtocol, (void**)&original_objc_getProtocol);
mach_override("_objc_copyProtocolList", NULL, (void*)&nu_objc_copyProtocolList, (void**)&original_objc_copyProtocolList);
#endif
}
#endif
#endif
}
#ifdef DARWIN
// bonus: I found this in the ObjC2.0 runtime.
@interface NuImage : NSObject
{
}
@end
@implementation NuImage
+ (NSArray *) all
{
NSMutableArray *array = [NSMutableArray array];
unsigned int count;
const char **names = objc_copyImageNames(&count);
for (int i = 0; i < count; i++) {
[array addObject:[NSString stringWithCString:names[i] encoding:NSUTF8StringEncoding]];
}
return array;
}
+ (NSArray *) classNamesForImageName:(NSString *) imageName
{
NSMutableArray *array = [NSMutableArray array];
unsigned int count;
const char **names = objc_copyClassNamesForImage([imageName cStringUsingEncoding:NSUTF8StringEncoding], &count);
for (int i = 0; i < count; i++) {
[array addObject:[NSString stringWithCString:names[i] encoding:NSUTF8StringEncoding]];
}
return array;
}
@end
#endif
#endif
#endif
#endif