public
Description: A lean-and-mean Ruby/ObjC bridge
Homepage: http://rubyobjc.com
Clone URL: git://github.com/timburks/rubyobjc.git
rubyobjc / objc / objc_class.m
100644 380 lines (353 sloc) 12.398 kb
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
* objc_class.m
*
* Defines a Ruby wrapper that allows Objective-C classes to be manipulated from Ruby.
*
* Copyright (c) 2007 Tim Burks, Neon Design Technology, Inc.
* For more information about this file, visit http://www.rubyobjc.com.
*/
 
/*
* Document-class: ObjC::Class
*
* ObjC::Class wraps Objective-C classes for manipulation from Ruby.
*
* Using methods of this class, Objective-C class methods and instance methods can be accessed from Ruby.
* New Objective-C classes can be added that subclass existing Objective-C classes;
* this allows Ruby classes derived from Objective-C classes to be made visible to Objective-C callers.
* New Objective-C method handlers can be added to allow Objective-C code to call methods written in Ruby.
*
* Information about Objective-C classes obtained from ObjC::Class instances is used to
* automatically build a tree of Ruby classes descending from ObjC::Object.
* Instances of these classes wrap individual Objective-C objects.
*/
#import "rubyobjc.h"
 
static VALUE __class_class;
 
//////////////////////////////////////////////////////////////////
 
void class_free(void *p)
{
    free(p);
}
 
VALUE class_alloc(VALUE klass)
{
    ObjC_Class *class = (ObjC_Class *) malloc (sizeof (ObjC_Class));
    class->c = 0;
    return Data_Wrap_Struct(klass, 0, class_free, class);
}
 
VALUE class_create(Class c)
{
    ObjC_Class *class = (ObjC_Class *) malloc (sizeof (ObjC_Class));
    class->c = c;
    return Data_Wrap_Struct(__class_class, 0, class_free, class);
}
 
/*
* Lookup an Objective-C class for the given name <i>p1</i> and return an ObjC::Class wrapper.
*/
VALUE class_find(VALUE self, VALUE name)
{
    char *class_name = StringValuePtr(name);
    Class c = objc_getClass(class_name);
    if (!c) { return Qnil; }
    return class_create(c);
}
 
/*
* Get the name of the corresponding Objective-C class.
*/
VALUE class_name(VALUE self)
{
    ObjC_Class *class;
    Data_Get_Struct(self, ObjC_Class, class);
    Class c = class->c;
    return c ? rb_str_new2(c->name) : rb_str_new2("NULL");
}
 
/*
* Get the superclass of the corresponding Objective-C class.
*/
VALUE class_super(VALUE self)
{
    ObjC_Class *class;
    Data_Get_Struct(self, ObjC_Class, class);
    Class c = class->c;
    Class s = class_getSuperclass(c);
    if (!s) { return Qnil; }
    return class_create(s);
}
 
/*
* Compare the class with another class <i>p1</i>.
*/
VALUE class_compare(VALUE self, VALUE other)
{
    ObjC_Class *class;
    Data_Get_Struct(self, ObjC_Class, class);
    ObjC_Class *other_class;
    Data_Get_Struct(other, ObjC_Class, other_class);
    int r = strcmp(class->c->name, other_class->c->name);
    return INT2NUM(r);
}
 
/*
* Test class for equality with another class <i>p1</i>.
*/
VALUE class_equal(VALUE self, VALUE other)
{
    if (TYPE(other) != T_DATA) return Qfalse;
    ObjC_Class *class, *other_class;
    Data_Get_Struct(self, ObjC_Class, class);
    Data_Get_Struct(other, ObjC_Class, other_class);
    return (class->c == other_class->c) ? Qtrue : Qfalse;
}
 
/*
* Get a hash value for a class.
*/
VALUE class_hash(VALUE self)
{
    ObjC_Class *class;
    Data_Get_Struct(self, ObjC_Class, class);
    Class c = class->c;
    return INT2FIX((int) c);
}
 
/*
* Get an array containing the instance methods of the corresponding Objective-C class.
* Returns an array of objects of type ObjC::Method.
*/
VALUE class_instance_methods(VALUE self)
{
    ObjC_Class *class;
    Data_Get_Struct(self, ObjC_Class, class);
    Class c = class->c;
    VALUE arr = rb_ary_new();
    unsigned int method_count;
    Method *method_list = class_copyMethodList(c, &method_count);
    int i;
    for (i = 0; i < method_count; i++) {
        rb_ary_push(arr, method_create(method_list[i]));
    }
    free(method_list);
    return arr;
}
 
/*
* Get an array containing the class methods of the corresponding Objective-C class.
* Returns an array of objects of type ObjC::Method.
*/
VALUE class_class_methods(VALUE self)
{
    ObjC_Class *class;
    Data_Get_Struct(self, ObjC_Class, class);
    Class c = class->c;
    VALUE arr = rb_ary_new();
    unsigned int method_count;
    Method *method_list = class_copyMethodList(object_getClass(c), &method_count);
    int i;
    for (i = 0; i < method_count; i++) {
        rb_ary_push(arr, method_create(method_list[i]));
    }
    free(method_list);
    return arr;
}
 
/*
* Lookup an instance method of the corresponding Objective-C class by name <i>p1</i>.
*/
VALUE class_get_instance_method(VALUE self, VALUE name)
{
    ObjC_Class *class;
    Data_Get_Struct(self, ObjC_Class, class);
    Class c = class->c;
    char *method_name = StringValuePtr(name);
    SEL sel = sel_getUid(method_name);
    if (!sel) return Qnil;
    Method method = class_getInstanceMethod(c, sel);
    return method ? method_create(method) : Qnil;
}
 
/*
* Lookup a class method of the corresponding Objective-C class by name <i>p1</i>.
*/
VALUE class_get_class_method(VALUE self, VALUE name)
{
    ObjC_Class *class;
    Data_Get_Struct(self, ObjC_Class, class);
    Class c = class->c;
    char *method_name = StringValuePtr(name);
    SEL sel = sel_getUid(method_name);
    if (!sel) return Qnil;
    Method method = class_getClassMethod(c, sel);
    return method ? method_create(method) : Qnil;
}
 
/*
* Get an array containing the instance variables associated with the class.
* Returns an array of objects of type ObjC::Variable.
*/
VALUE class_instance_variables(VALUE self)
{
    ObjC_Class *class;
    Data_Get_Struct(self, ObjC_Class, class);
    Class c = class->c;
    unsigned int ivar_count;
    Ivar *ivars = class_copyIvarList(c, &ivar_count);
    VALUE ivar_list = rb_ary_new();
    if (ivar_count == 0)
        return ivar_list;
    int i;
    for (i = 0; i < ivar_count; i++) {
        rb_ary_push(ivar_list, variable_create(ivars[i]));
    }
    free(ivars);
    return ivar_list;
}
 
/*
* Get an array containing all classes currently known to the Objective-C runtime. Classes are returned as ObjC::Class objects.
*/
VALUE class_all(VALUE self)
{
    VALUE arr = rb_ary_new();
    int numClasses = objc_getClassList(NULL, 0);
    if(numClasses > 0) {
        Class *classes = (Class *) malloc( sizeof(Class) * numClasses );
        objc_getClassList(classes, numClasses);
        int i = 0;
        while (i < numClasses) {
            rb_ary_push(arr, class_create(classes[i]));
            i++;
        }
        free(classes);
    }
    return arr;
}
 
/*
* Iterate over all classes currently known to the Objective-C runtime. Classes are returned as ObjC::Class objects.
*/
VALUE class_each(VALUE self)
{
    if (rb_block_given_p()) {
        int numClasses = objc_getClassList(NULL, 0);
        if(numClasses > 0) {
            Class *classes = (Class *) malloc( sizeof(Class) * numClasses );
            objc_getClassList(classes, numClasses);
            int i = 0;
            while (i < numClasses) {
                rb_yield(class_create(classes[i]));
                i++;
            }
            free(classes);
        }
    }
    return self;
}
 
/*
* Get an array containing the names of all protocols supported by the corresponding Objective-C class.
*/
VALUE class_protocols(VALUE self)
{
    ObjC_Class *class;
    Data_Get_Struct(self, ObjC_Class, class);
    Class c = class->c;
    VALUE arr = rb_ary_new();
    struct objc_protocol_list *protocolList;
    protocolList = c->protocols;
    while( protocolList != NULL ) {
        int protocol_count = protocolList->count;
        int i = 0;
        while (i < protocol_count) {
            Protocol *p = protocolList->list[i];
            VALUE protocolName = rb_str_new2([p name]);
            rb_ary_push(arr, protocolName);
            i++;
        }
        // do something with the method list here
        protocolList = protocolList->next;
    }
    return arr;
}
 
VALUE add_objc_subclass(VALUE self, VALUE subclass_name)
/*
* INTERNAL: Create a subclass of the underlying Objective-C class with the given name <i>p1</i>.
*/
{
    ObjC_Class *class;
    Data_Get_Struct(self, ObjC_Class, class);
    Class c = class->c;
    const char *name = StringValuePtr(subclass_name);
    Class s = objc_allocateClassPair(c, name, 0);
    objc_registerClassPair(s);
    return class_create(s);
}
 
VALUE class_add_ruby_instance_method_handler(VALUE self, VALUE method_name, VALUE signature)
/*
* INTERNAL: Add an instance method to the underlying Objective-C class to call Ruby instance methods with the given name <i>p1</i> and signature <i>p2</i>.
* RubyObjC uses this internally to make Ruby instance methods available from Objective-C.
*/
{
    ObjC_Class *class;
    Data_Get_Struct(self, ObjC_Class, class);
    Class c = class->c;
    const char *method_name_str = StringValuePtr(method_name);
    const char *signature_str = StringValuePtr(signature);
    SEL sel = sel_registerName(method_name_str);
 
    IMP imp = construct_ruby_method_handler(sel, signature_str);
    if (imp == NULL) {
        NSLog(@"failed to construct handler for %s(%s)", method_name_str, signature_str);
        return Qfalse;
    }
    else {
        BOOL status = class_addMethod(c, sel, imp, signature_str);
        if (!status) {
            // don't complain, the class probably already had the method
            //NSLog(@"failed to add handler for %s(%s) to class %s", method_name_str, signature_str, c->name);
            return Qfalse;
        }
    }
    return Qtrue;
}
 
VALUE class_add_ruby_class_method_handler(VALUE self, VALUE method_name, VALUE signature)
/*
* INTERNAL: Add a class method to the underlying Objective-C class to call Ruby class methods with the given name <i>p1</i> and signature <i>p2</i>.
* RubyObjC uses this internally to make Ruby class methods available from Objective-C.
*/
{
    ObjC_Class *class;
    Data_Get_Struct(self, ObjC_Class, class);
    Class c = class->c->isa;
    const char *method_name_str = StringValuePtr(method_name);
    const char *signature_str = StringValuePtr(signature);
    SEL sel = sel_registerName(method_name_str);
 
    IMP imp = construct_ruby_method_handler(sel, signature_str);
    if (imp == NULL) {
        NSLog(@"failed to construct handler for %s(%s)", method_name_str, signature_str);
        return Qfalse;
    }
    else {
        BOOL status = class_addMethod(c, sel, imp, signature_str);
        if (!status) {
            // don't complain, the class probably already had the method
            //NSLog(@"failed to add handler for %s(%s) to class %s", method_name_str, signature_str, c->name);
            return Qfalse;
        }
    }
    return Qtrue;
}
 
//
// ObjC classes are mapped to instances of OC::Class and classes of OC::Objects
//
void Init_ObjC_Class(VALUE module)
{
                                                  // needed by RDoc
    if (!module) module = rb_define_module("ObjC");
    __class_class = rb_define_class_under(module, "Class", rb_cObject);
    rb_define_alloc_func(__class_class, class_alloc);
    rb_define_singleton_method(__class_class, "to_a", class_all, 0);
    rb_define_singleton_method(__class_class, "each", class_each, 0);
    rb_define_singleton_method(__class_class, "find", class_find, 1);
    rb_define_method(__class_class, "name", class_name, 0);
    rb_define_method(__class_class, "super", class_super, 0);
    rb_define_method(__class_class, "to_s", class_name, 0);
    rb_define_method(__class_class, "cmethods", class_class_methods, 0);
    rb_define_method(__class_class, "imethods", class_instance_methods, 0);
    rb_define_method(__class_class, "get_cmethod", class_get_class_method, 1);
    rb_define_method(__class_class, "get_imethod", class_get_instance_method, 1);
    rb_define_method(__class_class, "ivars", class_instance_variables, 0);
    rb_define_method(__class_class, "add_objc_subclass", add_objc_subclass, 1);
    rb_define_method(__class_class, "protocols", class_protocols, 0);
    rb_define_method(__class_class, "add_ruby_imethod_handler", class_add_ruby_instance_method_handler, 2);
    rb_define_method(__class_class, "add_ruby_cmethod_handler", class_add_ruby_class_method_handler, 2);
    rb_define_method(__class_class, "<=>", class_compare, 1);
    rb_define_method(__class_class, "==", class_equal, 1);
    rb_define_method(__class_class, "eql?", class_equal, 1);
    rb_define_method(__class_class, "hash", class_hash, 0);
}