| 1 |
/* Test the Modern GNU Objective-C Runtime API. |
| 2 |
|
| 3 |
This is test 'method', covering all functions starting with 'method'. */ |
| 4 |
|
| 5 |
/* { dg-do run } */ |
| 6 |
/* { dg-skip-if "No API#2 pre-Darwin9" { *-*-darwin[5-8]* } { "-fnext-runtime" } { "" } } */ |
| 7 |
/* { dg-xfail-run-if "Needs OBJC2 ABI" { *-*-darwin* && { lp64 && { ! objc2 } } } { "-fnext-runtime" } { "" } } */ |
| 8 |
/* { dg-additional-options "-Wno-objc-root-class" } */ |
| 9 |
|
| 10 |
/* To get the modern GNU Objective-C Runtime API, you include |
| 11 |
objc/runtime.h. */ |
| 12 |
#include <objc/runtime.h> |
| 13 |
#include <stdlib.h> |
| 14 |
#include <stdio.h> |
| 15 |
#include <string.h> |
| 16 |
|
| 17 |
@interface MyRootClass |
| 18 |
{ Class isa; } |
| 19 |
+ alloc; |
| 20 |
- init; |
| 21 |
+ initialize; |
| 22 |
@end |
| 23 |
|
| 24 |
@implementation MyRootClass |
| 25 |
+ alloc { return class_createInstance (self, 0); } |
| 26 |
- init { return self; } |
| 27 |
+ initialize { return self; } |
| 28 |
@end |
| 29 |
|
| 30 |
@protocol MyProtocol |
| 31 |
- (id) variable; |
| 32 |
@end |
| 33 |
|
| 34 |
@protocol MySecondProtocol |
| 35 |
- (id) setVariable: (id)value; |
| 36 |
@end |
| 37 |
|
| 38 |
@interface MySubClass : MyRootClass <MyProtocol> |
| 39 |
{ id variable_ivar; } |
| 40 |
- (void) setVariable: (id)value; |
| 41 |
- (id) variable; |
| 42 |
- (id) constant; |
| 43 |
@end |
| 44 |
|
| 45 |
@implementation MySubClass |
| 46 |
- (void) setVariable: (id)value { variable_ivar = value; } |
| 47 |
- (id) variable { return variable_ivar; } |
| 48 |
- (id) constant { return nil; } |
| 49 |
@end |
| 50 |
|
| 51 |
|
| 52 |
int main(int argc, void **args) |
| 53 |
{ |
| 54 |
/* Functions are tested in alphabetical order. */ |
| 55 |
|
| 56 |
printf ("Testing method_copyArgumentType () ...\n"); |
| 57 |
{ |
| 58 |
Method method = class_getInstanceMethod (objc_getClass ("MySubClass"), |
| 59 |
@selector (setVariable:)); |
| 60 |
char *type = method_copyArgumentType (method, 2); |
| 61 |
|
| 62 |
if (type == NULL || type[0] != '@') |
| 63 |
abort (); |
| 64 |
} |
| 65 |
|
| 66 |
printf ("Testing method_copyReturnType () ...\n"); |
| 67 |
{ |
| 68 |
Method method = class_getClassMethod (objc_getClass ("MyRootClass"), |
| 69 |
@selector (alloc)); |
| 70 |
char *type = method_copyReturnType (method); |
| 71 |
|
| 72 |
/* Check that it returns an object. */ |
| 73 |
if (type == NULL || type[0] != '@') |
| 74 |
abort (); |
| 75 |
} |
| 76 |
|
| 77 |
printf ("Testing method_exchangeImplementations () ...\n"); |
| 78 |
{ |
| 79 |
Method method_a = class_getInstanceMethod (objc_getClass ("MySubClass"), |
| 80 |
@selector (variable)); |
| 81 |
Method method_b = class_getInstanceMethod (objc_getClass ("MySubClass"), |
| 82 |
@selector (constant)); |
| 83 |
MySubClass *object = [[MySubClass alloc] init]; |
| 84 |
|
| 85 |
/* Check that things work as expected before the swap. */ |
| 86 |
[object setVariable: object]; |
| 87 |
|
| 88 |
if ([object variable] != object || [object constant] != nil) |
| 89 |
abort (); |
| 90 |
|
| 91 |
/* Swap the methods. */ |
| 92 |
method_exchangeImplementations (method_a, method_b); |
| 93 |
|
| 94 |
/* Check that behavior has changed. */ |
| 95 |
if ([object variable] != nil || [object constant] != object) |
| 96 |
abort (); |
| 97 |
|
| 98 |
/* Swap the methods again. */ |
| 99 |
method_exchangeImplementations (method_a, method_b); |
| 100 |
|
| 101 |
/* Check that behavior is back to normal. */ |
| 102 |
if ([object variable] != object || [object constant] != nil) |
| 103 |
abort (); |
| 104 |
} |
| 105 |
|
| 106 |
printf ("Testing method_getArgumentType () ...\n"); |
| 107 |
{ |
| 108 |
Method method = class_getInstanceMethod (objc_getClass ("MyRootClass"), |
| 109 |
@selector (init)); |
| 110 |
char type[16]; |
| 111 |
|
| 112 |
method_getArgumentType (method, 1, type, 16); |
| 113 |
|
| 114 |
/* Check the second argument (_cmd), which should be a SEL. */ |
| 115 |
if (type[0] != ':') |
| 116 |
abort (); |
| 117 |
} |
| 118 |
|
| 119 |
printf ("Testing method_getDescription () ...\n"); |
| 120 |
{ |
| 121 |
Method method = class_getInstanceMethod (objc_getClass ("MySubClass"), |
| 122 |
@selector (variable)); |
| 123 |
struct objc_method_description *description = method_getDescription (method); |
| 124 |
|
| 125 |
if (strcmp (sel_getName (description->name), "variable") != 0) |
| 126 |
abort (); |
| 127 |
|
| 128 |
if (method_getDescription (NULL) != NULL) |
| 129 |
abort (); |
| 130 |
} |
| 131 |
|
| 132 |
printf ("Testing method_getImplementation () ...\n"); |
| 133 |
{ |
| 134 |
typedef void (*set_variable_function) (id receiver, SEL _cmd, id variable); |
| 135 |
Method method = class_getInstanceMethod (objc_getClass ("MySubClass"), |
| 136 |
@selector (setVariable:)); |
| 137 |
set_variable_function imp; |
| 138 |
MySubClass *object = [[MySubClass alloc] init]; |
| 139 |
|
| 140 |
imp = (set_variable_function)(method_getImplementation (method)); |
| 141 |
|
| 142 |
(*imp)(object, @selector (setVariable:), object); |
| 143 |
|
| 144 |
if ([object variable] != object) |
| 145 |
abort (); |
| 146 |
} |
| 147 |
|
| 148 |
printf ("Testing method_getName () ...\n"); |
| 149 |
{ |
| 150 |
Method method = class_getInstanceMethod (objc_getClass ("MySubClass"), |
| 151 |
@selector (setVariable:)); |
| 152 |
if (strcmp (sel_getName (method_getName (method)), "setVariable:") != 0) |
| 153 |
abort (); |
| 154 |
} |
| 155 |
|
| 156 |
printf ("Testing method_getNumberOfArguments () ...\n"); |
| 157 |
{ |
| 158 |
Method method = class_getInstanceMethod (objc_getClass ("MySubClass"), |
| 159 |
@selector (setVariable:)); |
| 160 |
if (method_getNumberOfArguments (method) != 3) |
| 161 |
abort (); |
| 162 |
|
| 163 |
method = class_getInstanceMethod (objc_getClass ("MySubClass"), |
| 164 |
@selector (variable)); |
| 165 |
if (method_getNumberOfArguments (method) != 2) |
| 166 |
abort (); |
| 167 |
} |
| 168 |
|
| 169 |
printf ("Testing method_getTypeEncoding () ...\n"); |
| 170 |
{ |
| 171 |
Method method = class_getInstanceMethod (objc_getClass ("MySubClass"), |
| 172 |
@selector (setVariable:)); |
| 173 |
const char *types = method_getTypeEncoding (method); |
| 174 |
|
| 175 |
/* Check that method type string starts with 'v' (void) */ |
| 176 |
if (types == NULL || types[0] != 'v') |
| 177 |
abort (); |
| 178 |
} |
| 179 |
|
| 180 |
printf ("Testing method_getReturnType () ...\n"); |
| 181 |
{ |
| 182 |
Method method = class_getInstanceMethod (objc_getClass ("MySubClass"), |
| 183 |
@selector (setVariable:)); |
| 184 |
char type[16]; |
| 185 |
|
| 186 |
method_getReturnType (method, type, 16); |
| 187 |
|
| 188 |
if (type[0] != 'v') |
| 189 |
abort (); |
| 190 |
|
| 191 |
method_getReturnType (NULL, type, 16); |
| 192 |
|
| 193 |
if (type[0] != 0) |
| 194 |
abort (); |
| 195 |
} |
| 196 |
|
| 197 |
printf ("Testing method_setImplementation () ...\n"); |
| 198 |
{ |
| 199 |
Method method_a = class_getInstanceMethod (objc_getClass ("MySubClass"), |
| 200 |
@selector (variable)); |
| 201 |
Method method_b = class_getInstanceMethod (objc_getClass ("MySubClass"), |
| 202 |
@selector (constant)); |
| 203 |
IMP original_imp_a = method_getImplementation (method_a); |
| 204 |
IMP original_imp_b = method_getImplementation (method_b); |
| 205 |
MySubClass *object = [[MySubClass alloc] init]; |
| 206 |
|
| 207 |
/* Check that things work as expected before the swap. */ |
| 208 |
[object setVariable: object]; |
| 209 |
|
| 210 |
if ([object variable] != object || [object constant] != nil) |
| 211 |
abort (); |
| 212 |
|
| 213 |
/* Have 'variable' use the same implementation as 'constant'. */ |
| 214 |
if (method_setImplementation (method_a, original_imp_b) != original_imp_a) |
| 215 |
abort (); |
| 216 |
|
| 217 |
/* Check that behavior has changed. */ |
| 218 |
if ([object variable] != nil || [object constant] != nil) |
| 219 |
abort (); |
| 220 |
|
| 221 |
/* Put the original method back. */ |
| 222 |
if (method_setImplementation (method_a, original_imp_a) != original_imp_b) |
| 223 |
abort (); |
| 224 |
|
| 225 |
/* Check that behavior is back to normal. */ |
| 226 |
if ([object variable] != object || [object constant] != nil) |
| 227 |
abort (); |
| 228 |
} |
| 229 |
|
| 230 |
return 0; |
| 231 |
} |