| 1 |
/* Test the Modern GNU Objective-C Runtime API. |
| 2 |
|
| 3 |
This is test 'class', covering all functions starting with 'class'. |
| 4 |
Tests calling the functions with a meta class as argument are covered |
| 5 |
in the separate file, gnu-api-2-class-meta.m. */ |
| 6 |
|
| 7 |
/* { dg-do run } */ |
| 8 |
/* { dg-skip-if "No API#2 pre-Darwin9" { *-*-darwin[5-8]* } { "-fnext-runtime" } { "" } } */ |
| 9 |
/* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */ |
| 10 |
/* { dg-additional-options "-Wno-objc-root-class" } */ |
| 11 |
/* { dg-additional-options "-DOBJC_OLD_DISPATCH_PROTOTYPES" { target { *-*-darwin* } } } */ |
| 12 |
|
| 13 |
/* To get the modern GNU Objective-C Runtime API, you include |
| 14 |
objc/runtime.h. */ |
| 15 |
#include <objc/runtime.h> |
| 16 |
#include <stdlib.h> |
| 17 |
#include <stdio.h> |
| 18 |
#include <string.h> |
| 19 |
|
| 20 |
@interface MyRootClass |
| 21 |
{ Class isa; } |
| 22 |
+ alloc; |
| 23 |
- init; |
| 24 |
+ initialize; |
| 25 |
@end |
| 26 |
|
| 27 |
@implementation MyRootClass |
| 28 |
+ alloc { return class_createInstance (self, 0); } |
| 29 |
- init { return self; } |
| 30 |
+ initialize { return self; } |
| 31 |
@end |
| 32 |
|
| 33 |
@protocol MyProtocol |
| 34 |
- (id) variable; |
| 35 |
@end |
| 36 |
|
| 37 |
@protocol MySecondProtocol |
| 38 |
- (id) setVariable: (id)value; |
| 39 |
@end |
| 40 |
|
| 41 |
@interface MySubClass : MyRootClass <MyProtocol> |
| 42 |
{ id variable_ivar; } |
| 43 |
- (void) setVariable: (id)value; |
| 44 |
- (id) variable; |
| 45 |
@end |
| 46 |
|
| 47 |
@implementation MySubClass |
| 48 |
- (void) setVariable: (id)value { variable_ivar = value; } |
| 49 |
- (id) variable { return variable_ivar; } |
| 50 |
@end |
| 51 |
|
| 52 |
@interface MyOtherSubClass : MySubClass |
| 53 |
@end |
| 54 |
|
| 55 |
@implementation MyOtherSubClass |
| 56 |
@end |
| 57 |
|
| 58 |
@interface DifferentClass : MyRootClass |
| 59 |
- (id) myClass; |
| 60 |
- (id) self; |
| 61 |
@end |
| 62 |
|
| 63 |
@implementation DifferentClass |
| 64 |
- (id) myClass { return object_getClass (self); } |
| 65 |
- (id) self { return self; } |
| 66 |
@end |
| 67 |
|
| 68 |
@interface MySubClass (MySelf) |
| 69 |
- (id) mySelf; |
| 70 |
@end |
| 71 |
|
| 72 |
/* Hack to calculate the log2 of a byte alignment. */ |
| 73 |
unsigned char |
| 74 |
log_2_of (unsigned int x) |
| 75 |
{ |
| 76 |
unsigned char result = 0; |
| 77 |
|
| 78 |
/* We count how many times we need to divide by 2 before we reach 1. |
| 79 |
This algorithm is good enough for the small numbers (such as 8, |
| 80 |
16 or 64) that we have to deal with. */ |
| 81 |
while (x > 1) |
| 82 |
{ |
| 83 |
x = x / 2; |
| 84 |
result++; |
| 85 |
} |
| 86 |
|
| 87 |
return result; |
| 88 |
} |
| 89 |
|
| 90 |
int main(int argc, void **args) |
| 91 |
{ |
| 92 |
/* Functions are tested in alphabetical order. */ |
| 93 |
|
| 94 |
printf ("Testing class_addIvar ()...\n"); |
| 95 |
{ |
| 96 |
Class new_class = objc_allocateClassPair (objc_getClass ("MySubClass"), "MySubSubClass", 0); |
| 97 |
|
| 98 |
if (new_class == Nil) |
| 99 |
abort (); |
| 100 |
|
| 101 |
if (! class_addIvar (new_class, "variable2_ivar", sizeof (id), |
| 102 |
log_2_of (__alignof__ (id)), @encode (id))) |
| 103 |
abort (); |
| 104 |
|
| 105 |
if (! class_addIvar (new_class, "variable3_ivar", sizeof (unsigned char), |
| 106 |
log_2_of (__alignof__ (unsigned char)), @encode (unsigned char))) |
| 107 |
abort (); |
| 108 |
|
| 109 |
if (! class_addIvar (new_class, "variable4_ivar", sizeof (unsigned long), |
| 110 |
log_2_of (__alignof__ (unsigned long)), @encode (unsigned long))) |
| 111 |
abort (); |
| 112 |
|
| 113 |
objc_registerClassPair (new_class); |
| 114 |
|
| 115 |
{ |
| 116 |
MySubClass *o = [[(Class)objc_getClass ("MySubSubClass") alloc] init]; |
| 117 |
Ivar variable2 = class_getInstanceVariable (objc_getClass ("MySubSubClass"), "variable2_ivar"); |
| 118 |
Ivar variable3 = class_getInstanceVariable (objc_getClass ("MySubSubClass"), "variable3_ivar"); |
| 119 |
Ivar variable4 = class_getInstanceVariable (objc_getClass ("MySubSubClass"), "variable4_ivar"); |
| 120 |
|
| 121 |
if (variable2 == NULL || variable3 == NULL || variable4 == NULL) |
| 122 |
abort (); |
| 123 |
|
| 124 |
if (strcmp (ivar_getName (variable2), "variable2_ivar") != 0) |
| 125 |
abort (); |
| 126 |
|
| 127 |
if (strcmp (ivar_getName (variable3), "variable3_ivar") != 0) |
| 128 |
abort (); |
| 129 |
|
| 130 |
if (strcmp (ivar_getName (variable4), "variable4_ivar") != 0) |
| 131 |
abort (); |
| 132 |
|
| 133 |
{ |
| 134 |
unsigned char *var3 = (unsigned char *)((char *)o + ivar_getOffset (variable3)); |
| 135 |
unsigned long *var4 = (unsigned long *)((char *)o + ivar_getOffset (variable4)); |
| 136 |
|
| 137 |
object_setIvar (o, variable2, new_class); |
| 138 |
*var3 = 230; |
| 139 |
*var4 = 89000L; |
| 140 |
|
| 141 |
if (object_getIvar (o, variable2) != new_class) |
| 142 |
abort (); |
| 143 |
|
| 144 |
if (*var3 != 230) |
| 145 |
abort (); |
| 146 |
|
| 147 |
if (*var4 != 89000L) |
| 148 |
abort (); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
printf ("Testing class_addMethod ()...\n"); |
| 154 |
{ |
| 155 |
Class new_class = objc_allocateClassPair (objc_getClass ("MyRootClass"), "MySubClass2", 0); |
| 156 |
Method method1 = class_getInstanceMethod (objc_getClass ("MySubClass"), @selector (setVariable:)); |
| 157 |
Method method2 = class_getInstanceMethod (objc_getClass ("MySubClass"), @selector (variable)); |
| 158 |
|
| 159 |
if (new_class == Nil) |
| 160 |
abort (); |
| 161 |
|
| 162 |
if (! class_addIvar (new_class, "variable_ivar", sizeof (id), |
| 163 |
log_2_of (__alignof__ (id)), @encode (id))) |
| 164 |
abort (); |
| 165 |
|
| 166 |
if (! class_addMethod (new_class, @selector (setVariable:), method_getImplementation (method1), |
| 167 |
method_getTypeEncoding (method1))) |
| 168 |
abort (); |
| 169 |
|
| 170 |
if (! class_addMethod (new_class, @selector (variable), method_getImplementation (method2), |
| 171 |
method_getTypeEncoding (method2))) |
| 172 |
abort (); |
| 173 |
|
| 174 |
/* Test that if the method already exists in the class, |
| 175 |
class_addMethod() returns NO. */ |
| 176 |
if (class_addMethod (new_class, @selector (variable), method_getImplementation (method2), |
| 177 |
method_getTypeEncoding (method2))) |
| 178 |
abort (); |
| 179 |
|
| 180 |
objc_registerClassPair (new_class); |
| 181 |
|
| 182 |
/* Now, MySubClass2 is basically the same as MySubClass! We'll |
| 183 |
use the variable and setVariable: methods on it. */ |
| 184 |
{ |
| 185 |
MySubClass *o = (MySubClass *)[[(Class)objc_getClass ("MySubClass2") alloc] init]; |
| 186 |
|
| 187 |
[o setVariable: o]; |
| 188 |
|
| 189 |
if ([o variable] != o) |
| 190 |
abort (); |
| 191 |
} |
| 192 |
|
| 193 |
/* Now, try that if you take an existing class and try to add an |
| 194 |
already existing method, class_addMethod returns NO. This is |
| 195 |
subtly different from before, when 'new_class' was still in |
| 196 |
construction. Now it's a real class and the libobjc internals |
| 197 |
differ between the two cases. */ |
| 198 |
if (class_addMethod (new_class, @selector (variable), method_getImplementation (method2), |
| 199 |
method_getTypeEncoding (method2))) |
| 200 |
abort (); |
| 201 |
} |
| 202 |
|
| 203 |
printf ("Testing class_addProtocol ()...\n"); |
| 204 |
{ |
| 205 |
if (!class_addProtocol (objc_getClass ("MySubClass"), @protocol (MySecondProtocol))) |
| 206 |
abort (); |
| 207 |
|
| 208 |
if (!class_conformsToProtocol (objc_getClass ("MySubClass"), @protocol (MyProtocol))) |
| 209 |
abort (); |
| 210 |
|
| 211 |
if (!class_conformsToProtocol (objc_getClass ("MySubClass"), @protocol (MySecondProtocol))) |
| 212 |
abort (); |
| 213 |
} |
| 214 |
|
| 215 |
printf ("Testing class_conformsToProtocol ()...\n"); |
| 216 |
{ |
| 217 |
if (class_conformsToProtocol (objc_getClass ("MyRootClass"), @protocol (MyProtocol))) |
| 218 |
abort (); |
| 219 |
|
| 220 |
if (!class_conformsToProtocol (objc_getClass ("MySubClass"), @protocol (MyProtocol))) |
| 221 |
abort (); |
| 222 |
|
| 223 |
/* Test that class_conformsToProtocol checks the class, but not |
| 224 |
superclasses. */ |
| 225 |
if (class_conformsToProtocol (objc_getClass ("MyOtherSubClass"), @protocol (MyProtocol))) |
| 226 |
abort (); |
| 227 |
} |
| 228 |
|
| 229 |
printf ("Testing class_copyIvarList ()...\n"); |
| 230 |
{ |
| 231 |
unsigned int count; |
| 232 |
Ivar * list = class_copyIvarList (objc_getClass ("MySubClass"), &count); |
| 233 |
|
| 234 |
if (count != 1) |
| 235 |
abort (); |
| 236 |
|
| 237 |
if (strcmp (ivar_getName (list[0]), "variable_ivar") != 0) |
| 238 |
abort (); |
| 239 |
|
| 240 |
if (list[1] != NULL) |
| 241 |
abort (); |
| 242 |
} |
| 243 |
|
| 244 |
printf ("Testing class_copyIvarList () on class with no instance variables...\n"); |
| 245 |
{ |
| 246 |
unsigned int count; |
| 247 |
Ivar * list = class_copyIvarList (objc_getClass ("MyOtherSubClass"), |
| 248 |
&count); |
| 249 |
|
| 250 |
if (count != 0) |
| 251 |
abort (); |
| 252 |
|
| 253 |
if (list != NULL) |
| 254 |
abort (); |
| 255 |
} |
| 256 |
|
| 257 |
printf ("Testing class_copyMethodList ()...\n"); |
| 258 |
{ |
| 259 |
unsigned int count; |
| 260 |
Method * list = class_copyMethodList (objc_getClass ("MySubClass"), &count); |
| 261 |
|
| 262 |
if (count != 2) |
| 263 |
abort (); |
| 264 |
|
| 265 |
if (! ((strcmp (sel_getName (method_getName (list[0])), "variable") == 0 |
| 266 |
&& strcmp (sel_getName (method_getName (list[1])), "setVariable:") == 0) |
| 267 |
|| (strcmp (sel_getName (method_getName (list[0])), "setVariable:") == 0 |
| 268 |
&& strcmp (sel_getName (method_getName (list[1])), "variable") == 0))) |
| 269 |
abort (); |
| 270 |
|
| 271 |
if (list[2] != NULL) |
| 272 |
abort (); |
| 273 |
} |
| 274 |
|
| 275 |
/* TODO: Test new ABI (when available). */ |
| 276 |
printf ("Testing class_copyPropertyList ()...\n"); |
| 277 |
{ |
| 278 |
unsigned int count; |
| 279 |
objc_property_t * list = class_copyPropertyList (objc_getClass ("MySubClass"), &count); |
| 280 |
|
| 281 |
if (count != 0 || list != NULL) |
| 282 |
abort (); |
| 283 |
} |
| 284 |
|
| 285 |
printf ("Testing class_copyProtocolList ()...\n"); |
| 286 |
{ |
| 287 |
unsigned int count; |
| 288 |
Protocol ** list = class_copyProtocolList (objc_getClass ("MySubClass"), &count); |
| 289 |
|
| 290 |
/* Remember that we added MySecondProtocol in the test above. */ |
| 291 |
if (count != 2) |
| 292 |
abort (); |
| 293 |
|
| 294 |
if (! ((strcmp (protocol_getName (list[0]), "MyProtocol") == 0 |
| 295 |
&& strcmp (protocol_getName (list[1]), "MySecondProtocol") == 0) |
| 296 |
|| (strcmp (protocol_getName (list[0]), "MySecondProtocol") == 0 |
| 297 |
&& strcmp (protocol_getName (list[1]), "MyProtocol") == 0))) |
| 298 |
abort (); |
| 299 |
|
| 300 |
if (list[2] != NULL) |
| 301 |
abort (); |
| 302 |
} |
| 303 |
|
| 304 |
printf ("Testing class_createInstance ()...\n"); |
| 305 |
{ |
| 306 |
MySubClass *object = [[MySubClass alloc] init]; |
| 307 |
|
| 308 |
[object setVariable: object]; |
| 309 |
if ([object variable] != object) |
| 310 |
abort (); |
| 311 |
} |
| 312 |
|
| 313 |
printf ("Testing class_getClassMethod ()...\n"); |
| 314 |
{ |
| 315 |
Method method = class_getClassMethod (objc_getClass ("MySubClass"), |
| 316 |
@selector(alloc)); |
| 317 |
|
| 318 |
if (method == NULL) |
| 319 |
abort (); |
| 320 |
|
| 321 |
if (strcmp (sel_getName (method_getName (method)), "alloc") != 0) |
| 322 |
abort (); |
| 323 |
|
| 324 |
if (class_getClassMethod (objc_getClass ("MySubClass"), |
| 325 |
@selector(variable))) |
| 326 |
abort (); |
| 327 |
} |
| 328 |
|
| 329 |
printf ("Testing class_getClassVariable ()...\n"); |
| 330 |
{ |
| 331 |
if (class_getClassVariable (objc_getClass ("MySubClass"), "variable_ivar")) |
| 332 |
abort (); |
| 333 |
} |
| 334 |
|
| 335 |
printf ("Testing class_getInstanceMethod ()...\n"); |
| 336 |
{ |
| 337 |
Method method = class_getInstanceMethod (objc_getClass ("MySubClass"), |
| 338 |
@selector(variable)); |
| 339 |
|
| 340 |
if (method == NULL) |
| 341 |
abort (); |
| 342 |
|
| 343 |
if (strcmp (sel_getName (method_getName (method)), "variable") != 0) |
| 344 |
abort (); |
| 345 |
|
| 346 |
if (class_getInstanceMethod (objc_getClass ("MySubClass"), |
| 347 |
@selector(alloc))) |
| 348 |
abort (); |
| 349 |
} |
| 350 |
|
| 351 |
printf ("Testing class_getInstanceSize ()...\n"); |
| 352 |
{ |
| 353 |
if (class_getInstanceSize (objc_getClass ("MyRootClass")) != sizeof (struct objc_object)) |
| 354 |
abort (); |
| 355 |
} |
| 356 |
|
| 357 |
printf ("Testing class_getInstanceVariable ()...\n"); |
| 358 |
{ |
| 359 |
Ivar variable = class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar"); |
| 360 |
|
| 361 |
if (variable == NULL) |
| 362 |
abort (); |
| 363 |
|
| 364 |
if (strcmp (ivar_getName (variable), "variable_ivar") != 0) |
| 365 |
abort (); |
| 366 |
|
| 367 |
if (class_getInstanceVariable (objc_getClass ("MySubClass"), "variable_ivar_no")) |
| 368 |
abort (); |
| 369 |
} |
| 370 |
|
| 371 |
printf ("Testing class_getIvarLayout ()...\n"); |
| 372 |
{ |
| 373 |
if (class_getIvarLayout (objc_getClass ("MyRootClass")) != NULL) |
| 374 |
abort (); |
| 375 |
} |
| 376 |
|
| 377 |
printf ("Testing class_getMethodImplementation ()...\n"); |
| 378 |
{ |
| 379 |
MySubClass *object = [[MySubClass alloc] init]; |
| 380 |
IMP imp = class_getMethodImplementation (objc_getClass ("MySubClass"), |
| 381 |
@selector(variable)); |
| 382 |
|
| 383 |
if (imp == NULL) |
| 384 |
abort (); |
| 385 |
|
| 386 |
[object setVariable: object]; |
| 387 |
|
| 388 |
if ((*imp)(object, @selector(variable)) != object) |
| 389 |
abort (); |
| 390 |
} |
| 391 |
|
| 392 |
/* This function does not exist with the GNU runtime. */ |
| 393 |
/* printf ("Testing class_getMethodImplementation_stret ()...\n"); */ |
| 394 |
|
| 395 |
printf ("Testing class_getName ()...\n"); |
| 396 |
{ |
| 397 |
if (strcmp (class_getName (objc_getClass ("MyRootClass")), |
| 398 |
"MyRootClass") != 0) |
| 399 |
abort (); |
| 400 |
} |
| 401 |
|
| 402 |
/* TODO: Test new ABI (when available). */ |
| 403 |
printf ("Testing class_getProperty ()...\n"); |
| 404 |
{ |
| 405 |
if (class_getProperty (objc_getClass ("MyRootClass"), "property") != NULL) |
| 406 |
abort (); |
| 407 |
} |
| 408 |
|
| 409 |
printf ("Testing class_getSuperclass ()...\n"); |
| 410 |
{ |
| 411 |
MySubClass *object = [[MySubClass alloc] init]; |
| 412 |
if (class_getSuperclass (object_getClass (object)) != objc_getClass ("MyRootClass")) |
| 413 |
abort (); |
| 414 |
|
| 415 |
/* Test that it works on a newly created, but not registered, class. */ |
| 416 |
{ |
| 417 |
Class new_class = objc_allocateClassPair (objc_getClass ("MyRootClass"), "MySubClass3", 0); |
| 418 |
|
| 419 |
if (class_getSuperclass (new_class) != objc_getClass ("MyRootClass")) |
| 420 |
abort (); |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
printf ("Testing class_getVersion ()...\n"); |
| 425 |
{ |
| 426 |
if (class_getVersion (objc_getClass ("MySubClass")) != 0) |
| 427 |
abort (); |
| 428 |
} |
| 429 |
|
| 430 |
printf ("Testing class_getWeakIvarLayout ()...\n"); |
| 431 |
{ |
| 432 |
if (class_getWeakIvarLayout (objc_getClass ("MyRootClass")) != NULL) |
| 433 |
abort (); |
| 434 |
} |
| 435 |
|
| 436 |
printf ("Testing class_isMetaClass ()...\n"); |
| 437 |
{ |
| 438 |
MySubClass *object = [[MySubClass alloc] init]; |
| 439 |
if (class_isMetaClass (object_getClass (object)) |
| 440 |
|| ! class_isMetaClass (object_getClass (object_getClass (object)))) |
| 441 |
abort (); |
| 442 |
} |
| 443 |
|
| 444 |
printf ("Testing class_replaceMethod ()...\n"); |
| 445 |
{ |
| 446 |
Method new_method = class_getInstanceMethod (objc_getClass ("DifferentClass"), |
| 447 |
@selector (myClass)); |
| 448 |
Method old_method = class_getInstanceMethod (objc_getClass ("MySubClass"), |
| 449 |
@selector (variable)); |
| 450 |
const char *new_types = method_getTypeEncoding (new_method); |
| 451 |
IMP new_imp = method_getImplementation (new_method); |
| 452 |
const char *old_types = method_getTypeEncoding (old_method); |
| 453 |
IMP old_imp = class_replaceMethod (objc_getClass ("MySubClass"), @selector (variable), |
| 454 |
method_getImplementation (new_method), |
| 455 |
method_getTypeEncoding (new_method)); |
| 456 |
MySubClass *o = [[MySubClass alloc] init]; |
| 457 |
|
| 458 |
[o setVariable: o]; |
| 459 |
|
| 460 |
/* Try the new method implementation. */ |
| 461 |
if ([o variable] != objc_getClass ("MySubClass")) |
| 462 |
abort (); |
| 463 |
|
| 464 |
/* Put the original method back. */ |
| 465 |
class_replaceMethod (objc_getClass ("MySubClass"), @selector (variable), |
| 466 |
old_imp, old_types); |
| 467 |
|
| 468 |
/* Test it's back to what it was. */ |
| 469 |
if ([o variable] != o) |
| 470 |
abort (); |
| 471 |
|
| 472 |
{ |
| 473 |
DifferentClass *o = [[DifferentClass alloc] init]; |
| 474 |
|
| 475 |
/* Finally, try adding a new method. */ |
| 476 |
class_replaceMethod (objc_getClass ("DifferentClass"), @selector (mySelf), |
| 477 |
new_imp, new_types); |
| 478 |
|
| 479 |
if ([(MySubClass*)o mySelf] != objc_getClass ("DifferentClass")) |
| 480 |
abort (); |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
printf ("Testing class_respondsToSelector ()...\n"); |
| 485 |
{ |
| 486 |
if (! class_respondsToSelector (objc_getClass ("MySubClass"), @selector(setVariable:))) |
| 487 |
abort (); |
| 488 |
|
| 489 |
if (class_respondsToSelector (objc_getClass ("MyRootClass"), @selector(setVariable:))) |
| 490 |
abort (); |
| 491 |
} |
| 492 |
|
| 493 |
/* This is not really implemented with the GNU runtime. */ |
| 494 |
/* printf ("Testing class_setIvarLayout ()...\n"); */ |
| 495 |
|
| 496 |
printf ("Testing class_setVersion ()...\n"); |
| 497 |
{ |
| 498 |
class_setVersion (objc_getClass ("MySubClass"), 45); |
| 499 |
|
| 500 |
if (class_getVersion (objc_getClass ("MySubClass")) != 45) |
| 501 |
abort (); |
| 502 |
|
| 503 |
class_setVersion (objc_getClass ("MySubClass"), 46); |
| 504 |
|
| 505 |
if (class_getVersion (objc_getClass ("MySubClass")) != 46) |
| 506 |
abort (); |
| 507 |
} |
| 508 |
|
| 509 |
/* This is not really implemented with the GNU runtime. */ |
| 510 |
/* printf ("Testing class_setWeakIvarLayout ()...\n"); */ |
| 511 |
|
| 512 |
return 0; |
| 513 |
} |