1 /* Test the Modern GNU Objective-C Runtime API.
3 This is test 'class-meta', covering calling functions starting with
4 'class' but using a meta class as argument.
6 Functions that manipulate methods (adding, replacing methods)
7 usually take a meta class argument to manipulate the class methods
8 instead of the instance ones. This is an important part of the API
11 Functions that manipulate instances, instance variables, properties
12 and protocols at the moment must take a normal class as argument;
13 calling them with a meta class as argument is of no particular use
14 and generally produces a behavior that is undocumented and/or
15 undefined (and this is true with all runtimes). As in the future
16 this behavior may be defined or documented (for example, if class
17 variables are implemented as instance variables of meta classes) we
18 avoid testing it for compatibility with future runtimes. */
21 /* { dg-skip-if "No API#2 pre-Darwin9" { *-*-darwin[5-8]* } { "-fnext-runtime" } { "" } } */
22 /* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */
24 /* To get the modern GNU Objective-C Runtime API, you include
26 #include <objc/runtime.h>
31 @interface MyRootClass
38 @implementation MyRootClass
39 + alloc { return class_createInstance (self, 0); }
40 - init { return self; }
41 + initialize { return self; }
44 static id static_variable = nil;
46 @interface MySubClass : MyRootClass
47 + (void) setVariable: (id)value;
51 @implementation MySubClass
52 + (void) setVariable: (id)value { static_variable = value; }
53 + (id) variable { return static_variable; }
56 @interface DifferentClass : MyRootClass
60 @implementation DifferentClass
61 + (id) myClass { return self; }
64 @interface MySubClass (MySelf)
68 int main(int argc, void **args)
70 /* Functions are tested in alphabetical order. */
72 /* Calling class_addIvar() with a meta class is not documented and
73 (currently) of no use. */
74 /* printf ("Testing class_addIvar ()...\n"); */
76 printf ("Testing class_addMethod () on a meta class...\n");
78 /* Here we test adding methods to meta classes, ie, adding class methods. */
79 Class new_class = objc_allocateClassPair (objc_getClass ("MyRootClass"), "MySubClass2", 0);
80 Method method1 = class_getInstanceMethod (objc_getMetaClass ("MySubClass"), @selector (setVariable:));
81 Method method2 = class_getInstanceMethod (objc_getMetaClass ("MySubClass"), @selector (variable));
86 if (! class_addMethod (object_getClass (new_class), @selector (setVariable:), method_getImplementation (method1),
87 method_getTypeEncoding (method1)))
90 if (! class_addMethod (object_getClass (new_class), @selector (variable), method_getImplementation (method2),
91 method_getTypeEncoding (method2)))
94 /* Test that if the method already exists in the class,
95 class_addMethod() returns NO. */
96 if (class_addMethod (object_getClass (new_class), @selector (variable), method_getImplementation (method2),
97 method_getTypeEncoding (method2)))
100 objc_registerClassPair (new_class);
102 /* Now, MySubClass2 is basically the same as MySubClass! We'll
103 use the +variable and +setVariable: methods on it. */
105 Class c = objc_getClass ("MySubClass2");
106 id o = [[MyRootClass alloc] init];
110 if ([c variable] != o)
114 /* Now, try that if you take an existing class and try to add an
115 already existing method, class_addMethod returns NO. This is
116 subtly different from before, when 'new_class' was still in
117 construction. Now it's a real class and the libobjc internals
118 differ between the two cases. */
119 if (class_addMethod (object_getClass (new_class), @selector (variable), method_getImplementation (method2),
120 method_getTypeEncoding (method2)))
124 /* Calling class_addProtocol() on a meta class is not documented and
125 (currently) of no use. */
126 /* printf ("Testing class_addProtocol () on a meta class...\n"); */
128 /* Calling class_conformsToProtocol() on a meta class is not
129 documented and (currently) of no use. */
130 /* printf ("Testing class_conformsToProtocol () on a meta class...\n"); */
132 /* Calling class_copyIvarList() on a meta class is not documented
133 and (currently) of no use. */
134 /* printf ("Testing class_copyIvarList () on a meta class...\n"); */
136 printf ("Testing class_copyMethodList () on a meta class...\n");
138 /* Test that you can copy the method list of a meta class. They
139 are the class methods of the class. */
141 Method * list = class_copyMethodList (objc_getMetaClass ("MySubClass"), &count);
146 if (! ((strcmp (sel_getName (method_getName (list[0])), "variable") == 0
147 && strcmp (sel_getName (method_getName (list[1])), "setVariable:") == 0)
148 || (strcmp (sel_getName (method_getName (list[0])), "setVariable:") == 0
149 && strcmp (sel_getName (method_getName (list[1])), "variable") == 0)))
156 /* Calling class_copyPropertyList() on a meta class is not
157 documented and (currently) of no use. */
158 /* printf ("Testing class_copyPropertyList () on a meta class...\n"); */
160 /* Calling class_copyProtocolList() on a meta class is not
161 documented and (currently) of no use. */
162 /* printf ("Testing class_copyProtocolList () on a meta class...\n"); */
164 /* Calling class_createInstance() on a meta class is not documented
165 and (currently) of no use. */
166 /* printf ("Testing class_createInstance () on a meta class...\n"); */
168 /* Calling class_getClassMethod () on a meta class is not documented
169 and (currently) of no use. */
170 /* printf ("Testing class_getClassMethod () on a meta class...\n"); */
172 /* Calling class_getClassVariable () on a meta class is not
173 documented and (currently) of no use. */
174 /* printf ("Testing class_getClassVariable () on a meta class ...\n"); */
176 printf ("Testing class_getInstanceMethod () on a meta class...\n");
178 /* The instance method of a meta class is the class method with
179 the same name of the class. */
180 Method method_1 = class_getInstanceMethod (objc_getMetaClass ("MySubClass"),
181 @selector(variable));
182 Method method_2 = class_getClassMethod (objc_getClass ("MySubClass"),
183 @selector(variable));
185 if (method_1 == NULL || method_2 == NULL)
188 if (method_1 != method_2)
191 if (strcmp (sel_getName (method_getName (method_1)), "variable") != 0)
195 /* Calling class_getInstanceSize() with a meta class is not
196 documented and (currently) of no use. */
197 /* printf ("Testing class_getInstanceSize () on a meta class...\n"); */
199 /* Calling class_getInstanceVariable() with a meta class is not
200 documented and (currently) of no use. */
201 /* printf ("Testing class_getInstanceVariable () on a meta class...\n"); */
203 /* Calling class_getIvarLayout() with a meta class is not documented
204 and (currently) of no use. */
205 /* printf ("Testing class_getIvarLayout () on a meta class...\n"); */
207 printf ("Testing class_getMethodImplementation () on a meta class...\n");
209 /* Getting the method implementation with a meta class returns a
211 MySubClass *object = [[MySubClass alloc] init];
212 IMP imp = class_getMethodImplementation (objc_getMetaClass ("MySubClass"),
213 @selector(variable));
218 [MySubClass setVariable: object];
220 if ((*imp)(objc_getClass ("MySubClass"), @selector(variable)) != object)
224 /* This function does not exist with the GNU runtime. */
225 /* printf ("Testing class_getMethodImplementation_stret () on a meta class...\n"); */
227 printf ("Testing class_getName () on a meta class...\n");
229 /* Traditionally, a meta class has the same name as the class. */
230 if (strcmp (class_getName (objc_getMetaClass ("MyRootClass")),
235 /* Calling class_getProperty() with a meta class is not documented
236 and (currently) of no use. */
237 /* printf ("Testing class_getProperty ()...\n"); */
239 printf ("Testing class_getSuperclass () on a meta class...\n");
241 /* The superclass of a meta class is the meta class of the superclass. */
242 if (class_getSuperclass (objc_getMetaClass ("MySubClass")) != objc_getMetaClass ("MyRootClass"))
245 /* Test that it works on a newly created, but not registered, class. */
247 Class new_class = objc_allocateClassPair (objc_getClass ("MyRootClass"), "MySubClass3", 0);
249 if (class_getSuperclass (object_getClass (new_class)) != object_getClass (objc_getClass ("MyRootClass")))
254 /* Calling class_getVersion() with a meta class is not documented
255 and (currently) of no use. */
256 /* printf ("Testing class_getVersion ()...\n"); */
258 /* Calling class_getWeakIvarLayout() with a meta class is not
259 documented and (currently) of no use. */
260 /* printf ("Testing class_getWeakIvarLayout () on a meta class...\n"); */
262 /* class_isMetaClass() is already tested in gnu-api-2-class.m */
263 /* printf ("Testing class_isMetaClass ()...\n"); */
265 printf ("Testing class_replaceMethod () on a meta class...\n");
267 /* We are going to replace the [MySubclass +variable] method with
268 the [DifferentClass +myClass] one. */
269 Method new_method = class_getClassMethod (objc_getClass ("DifferentClass"),
270 @selector (myClass));
271 Method old_method = class_getClassMethod (objc_getClass ("MySubClass"),
272 @selector (variable));
273 const char *new_types = method_getTypeEncoding (new_method);
274 IMP new_imp = method_getImplementation (new_method);
275 const char *old_types = method_getTypeEncoding (old_method);
276 IMP old_imp = class_replaceMethod (objc_getMetaClass ("MySubClass"), @selector (variable),
277 method_getImplementation (new_method),
278 method_getTypeEncoding (new_method));
279 id o = [[MyRootClass alloc] init];
281 [MySubClass setVariable: o];
283 /* Try the new method implementation. */
284 if ([MySubClass variable] != objc_getClass ("MySubClass"))
287 /* Put the original method back. */
288 class_replaceMethod (objc_getMetaClass ("MySubClass"), @selector (variable),
291 /* Test it's back to what it was. */
292 if ([MySubClass variable] != o)
296 /* Finally, try adding a new method. */
297 class_replaceMethod (objc_getMetaClass ("DifferentClass"), @selector (mySelf),
300 if ([(Class)objc_getClass ("DifferentClass") mySelf] != objc_getClass ("DifferentClass"))
305 printf ("Testing class_respondsToSelector () on a meta class...\n");
307 /* A meta class responds to a selector if and only if the class
308 responds to the corresponding class method. */
309 if (! class_respondsToSelector (objc_getMetaClass ("MySubClass"), @selector(setVariable:)))
312 if (class_respondsToSelector (objc_getMetaClass ("MyRootClass"), @selector(setVariable:)))
316 /* This is not really implemented with the GNU runtime. */
317 /* printf ("Testing class_setIvarLayout () on a meta class...\n"); */
319 /* Calling class_setVersion() with a meta class is not documented
320 and (currently) of no use. */
321 /* printf ("Testing class_setVersion () on a meta class...\n"); */
323 /* This is not really implemented with the GNU runtime. */
324 /* printf ("Testing class_setWeakIvarLayout () on a meta class...\n"); */