-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathTNSTestNativeCallbacks.m
338 lines (280 loc) · 14.7 KB
/
TNSTestNativeCallbacks.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
#import "TNSTestNativeCallbacks.h"
#import <objc/message.h>
#if !defined(UNUSED)
#define UNUSED(variable) (void)variable
#endif
@implementation TNSTestNativeCallbacks
+ (void)inheritanceMethodCalls:(TNSDerivedInterface*)object {
[object baseMethod];
[object baseProtocolMethod2];
[object baseProtocolMethod2Optional];
[object baseProtocolMethod1];
[object baseProtocolMethod1Optional];
[object baseCategoryMethod];
[object baseCategoryProtocolMethod2];
[object baseCategoryProtocolMethod2Optional];
[object baseCategoryProtocolMethod1];
[object baseCategoryProtocolMethod1Optional];
[object derivedMethod];
[object derivedProtocolMethod2];
[object derivedProtocolMethod2Optional];
[object derivedProtocolMethod1];
[object derivedProtocolMethod1Optional];
[object derivedCategoryMethod];
[object derivedCategoryProtocolMethod2];
[object derivedCategoryProtocolMethod2Optional];
[object derivedCategoryProtocolMethod1];
[object derivedCategoryProtocolMethod1Optional];
}
+ (void)inheritanceConstructorCalls:(Class)JSDerivedInterface {
NSAssert([[[JSDerivedInterface alloc] initBaseProtocolMethod1] isKindOfClass:JSDerivedInterface], @"initBaseProtocolMethod1 failed");
NSAssert([[[JSDerivedInterface alloc] initBaseProtocolMethod1Optional] isKindOfClass:JSDerivedInterface], @"initBaseProtocolMethod1Optional failed");
NSAssert([[[JSDerivedInterface alloc] initBaseProtocolMethod2] isKindOfClass:JSDerivedInterface], @"initBaseProtocolMethod2 failed");
NSAssert([[[JSDerivedInterface alloc] initBaseProtocolMethod2Optional] isKindOfClass:JSDerivedInterface], @"initBaseProtocolMethod2Optional failed");
NSAssert([[[JSDerivedInterface alloc] initBaseMethod] isKindOfClass:JSDerivedInterface], @"initBaseMethod failed");
NSAssert([[[JSDerivedInterface alloc] initBaseCategoryProtocolMethod1] isKindOfClass:JSDerivedInterface], @"initBaseCategoryProtocolMethod1 failed");
NSAssert([[[JSDerivedInterface alloc] initBaseCategoryProtocolMethod1Optional] isKindOfClass:JSDerivedInterface], @"initBaseCategoryProtocolMethod1Optional failed");
NSAssert([[[JSDerivedInterface alloc] initBaseCategoryProtocolMethod2] isKindOfClass:JSDerivedInterface], @"initBaseCategoryProtocolMethod2 failed");
NSAssert([[[JSDerivedInterface alloc] initBaseCategoryProtocolMethod2Optional] isKindOfClass:JSDerivedInterface], @"initBaseCategoryProtocolMethod2Optional failed");
NSAssert([[[JSDerivedInterface alloc] initBaseCategoryMethod] isKindOfClass:JSDerivedInterface], @"initBaseCategoryMethod failed");
NSAssert([[[JSDerivedInterface alloc] initDerivedProtocolMethod1] isKindOfClass:JSDerivedInterface], @"initDerivedProtocolMethod1 failed");
NSAssert([[[JSDerivedInterface alloc] initDerivedProtocolMethod1Optional] isKindOfClass:JSDerivedInterface], @"initDerivedProtocolMethod1Optional failed");
NSAssert([[[JSDerivedInterface alloc] initDerivedProtocolMethod2] isKindOfClass:JSDerivedInterface], @"initDerivedProtocolMethod2 failed");
NSAssert([[[JSDerivedInterface alloc] initDerivedProtocolMethod2Optional] isKindOfClass:JSDerivedInterface], @"initDerivedProtocolMethod2Optional failed");
NSAssert([[[JSDerivedInterface alloc] initDerivedMethod] isKindOfClass:JSDerivedInterface], @"initDerivedMethod failed");
NSAssert([[[JSDerivedInterface alloc] initDerivedCategoryProtocolMethod1] isKindOfClass:JSDerivedInterface], @"initDerivedCategoryProtocolMethod1 failed");
NSAssert([[[JSDerivedInterface alloc] initDerivedCategoryProtocolMethod1Optional] isKindOfClass:JSDerivedInterface], @"initDerivedCategoryProtocolMethod1Optional failed");
NSAssert([[[JSDerivedInterface alloc] initDerivedCategoryProtocolMethod2] isKindOfClass:JSDerivedInterface], @"initDerivedCategoryProtocolMethod2 failed");
NSAssert([[[JSDerivedInterface alloc] initDerivedCategoryProtocolMethod2Optional] isKindOfClass:JSDerivedInterface], @"initDerivedCategoryProtocolMethod2Optional failed");
NSAssert([[[JSDerivedInterface alloc] initDerivedCategoryMethod] isKindOfClass:JSDerivedInterface], @"initDerivedCategoryMethod failed");
}
+ (void)inheritancePropertyCalls:(TNSDerivedInterface*)object {
object.baseProtocolProperty1 = 0;
UNUSED(object.baseProtocolProperty1);
object.baseProtocolProperty1Optional = 0;
UNUSED(object.baseProtocolProperty1Optional);
object.baseProtocolProperty2 = 0;
UNUSED(object.baseProtocolProperty2);
object.baseProtocolProperty2Optional = 0;
UNUSED(object.baseProtocolProperty2Optional);
object.baseProperty = 0;
UNUSED(object.baseProperty);
object.baseCategoryProtocolProperty1 = 0;
UNUSED(object.baseCategoryProtocolProperty1);
object.baseCategoryProtocolProperty1Optional = 0;
UNUSED(object.baseCategoryProtocolProperty1Optional);
object.baseCategoryProtocolProperty2 = 0;
UNUSED(object.baseCategoryProtocolProperty2);
object.baseCategoryProtocolProperty2Optional = 0;
UNUSED(object.baseCategoryProtocolProperty2Optional);
object.baseCategoryProperty = 0;
UNUSED(object.baseCategoryProperty);
object.derivedProtocolProperty1 = 0;
UNUSED(object.derivedProtocolProperty1);
object.derivedProtocolProperty1Optional = 0;
UNUSED(object.derivedProtocolProperty1Optional);
object.derivedProtocolProperty2 = 0;
UNUSED(object.derivedProtocolProperty2);
object.derivedProtocolProperty2Optional = 0;
UNUSED(object.derivedProtocolProperty2Optional);
object.derivedProperty = 0;
UNUSED(object.derivedProperty);
object.derivedCategoryProtocolProperty1 = 0;
UNUSED(object.derivedCategoryProtocolProperty1);
object.derivedCategoryProtocolProperty1Optional = 0;
UNUSED(object.derivedCategoryProtocolProperty1Optional);
object.derivedCategoryProtocolProperty2 = 0;
UNUSED(object.derivedCategoryProtocolProperty2);
object.derivedCategoryProtocolProperty2Optional = 0;
UNUSED(object.derivedCategoryProtocolProperty2Optional);
object.derivedCategoryProperty = 0;
UNUSED(object.derivedCategoryProperty);
}
+ (void)inheritanceVoidSelector:(id)object {
SEL sel = NSSelectorFromString(@"voidSelector");
NSAssert([object respondsToSelector:sel], @"Object does not respond to selector");
((void (*)(id, SEL))objc_msgSend)(object, sel);
}
+ (id)inheritanceVariadicSelector:(id)object {
SEL sel = NSSelectorFromString(@"variadicSelector:x:");
NSAssert([object respondsToSelector:sel], @"Object does not respond to selector");
id result = ((id(*)(id, SEL, NSString*, int))objc_msgSend)(object, sel, @"native", 9);
return result;
}
+ (void)inheritanceOptionalProtocolMethodsAndCategories:(TNSIDerivedInterface*)object {
BOOL responds;
// Base
[object baseImplementedOptionalMethod];
responds = [object respondsToSelector:@selector(baseNotImplementedOptionalMethod)];
NSAssert(!responds, NSStringFromSelector(_cmd));
[object baseNotImplementedOptionalMethodImplementedInJavaScript];
[object baseImplementedCategoryMethod];
responds = [object respondsToSelector:@selector(baseNotImplementedCategoryMethod)];
NSAssert(!responds, NSStringFromSelector(_cmd));
[object baseNotImplementedNativeCategoryMethodOverridenInJavaScript];
// Derived
[object derivedImplementedOptionalMethod];
responds = [object respondsToSelector:@selector(derivedNotImplementedOptionalMethod)];
NSAssert(!responds, NSStringFromSelector(_cmd));
[object derivedNotImplementedOptionalMethodImplementedInJavaScript];
[object derivedImplementedCategoryMethod];
responds = [object respondsToSelector:@selector(derivedNotImplementedCategoryMethod)];
NSAssert(!responds, NSStringFromSelector(_cmd));
[object derivedNotImplementedNativeCategoryMethodOverridenInJavaScript];
}
+ (void)apiCustomGetterAndSetter:(TNSApi*)object {
NSAssert(object.property == 3, NSStringFromSelector(_cmd));
}
+ (void)apiOverrideWithCustomGetterAndSetter:(TNSApi*)object {
NSAssert(object.property == -6, NSStringFromSelector(_cmd));
}
+ (void)apiReadonlyPropertyInProtocolAndOverrideWithSetterInInterface:(UIView*)object {
NSAssert(CGRectEqualToRect([object bounds], CGRectMake(10, 20, 30, 40)), NSStringFromSelector(_cmd));
}
+ (void)apiDescriptionOverride:(id)object {
NSAssert([[object description] isEqualToString:@"js description"], NSStringFromSelector(_cmd));
}
+ (void)apiNSErrorOverride:(TNSApi*)object {
{
NSError* error = nil;
NSAssert([object method:0 error:&error] == false, NSStringFromSelector(_cmd));
NSAssert(error == nil, NSStringFromSelector(_cmd));
}
NSAssert([object method:1 error:nil] == false, NSStringFromSelector(_cmd));
{
NSError* error = nil;
NSAssert([object method:1 error:&error] == false, NSStringFromSelector(_cmd));
TNSLog(error.domain);
}
}
+ (void)apiNSErrorExpose:(TNSApi*)object {
{
NSError* error = nil;
NSAssert(((BOOL(*)(id, SEL, int, NSError**))objc_msgSend)(object, NSSelectorFromString(@"method:error2:"), 0, &error) == true, NSStringFromSelector(_cmd));
NSAssert(error == nil, NSStringFromSelector(_cmd));
}
NSAssert(((BOOL(*)(id, SEL, int, NSError**))objc_msgSend)(object, NSSelectorFromString(@"method:error2:"), 1, nil) == false, NSStringFromSelector(_cmd));
{
NSError* error = nil;
NSAssert(((BOOL(*)(id, SEL, int, NSError**))objc_msgSend)(object, NSSelectorFromString(@"method:error2:"), 1, &error) == false, NSStringFromSelector(_cmd));
TNSLog(error.domain);
}
}
+ (void)protocolImplementationMethods:(id<TNSBaseProtocol1, NSObject>)object {
NSAssert([object conformsToProtocol:@protocol(TNSBaseProtocol1)], NSStringFromSelector(_cmd));
NSAssert([[object class] conformsToProtocol:@protocol(TNSBaseProtocol1)], NSStringFromSelector(_cmd));
[object baseProtocolMethod1];
}
+ (void)categoryProtocolImplementationMethods:(id<TNSBaseCategoryProtocol1, NSObject>)object {
NSAssert([object conformsToProtocol:@protocol(TNSBaseCategoryProtocol1)], NSStringFromSelector(_cmd));
NSAssert([[object class] conformsToProtocol:@protocol(TNSBaseCategoryProtocol1)], NSStringFromSelector(_cmd));
[object baseCategoryProtocolMethod1];
}
+ (void)protocolImplementationProtocolInheritance:(id<TNSBaseProtocol2, NSObject>)object {
NSAssert([object conformsToProtocol:@protocol(TNSBaseProtocol1)], NSStringFromSelector(_cmd));
NSAssert([[object class] conformsToProtocol:@protocol(TNSBaseProtocol1)], NSStringFromSelector(_cmd));
[object baseProtocolMethod1];
NSAssert([object conformsToProtocol:@protocol(TNSBaseProtocol2)], NSStringFromSelector(_cmd));
NSAssert([[object class] conformsToProtocol:@protocol(TNSBaseProtocol2)], NSStringFromSelector(_cmd));
[object baseProtocolMethod2];
}
+ (void)protocolImplementationOptionalMethods:(id<TNSBaseProtocol2, NSObject>)object {
BOOL responds;
responds = [object respondsToSelector:@selector(baseProtocolMethod1Optional)];
NSAssert(responds, NSStringFromSelector(_cmd));
[object baseProtocolMethod1Optional];
responds = [object respondsToSelector:@selector(baseProtocolMethod2Optional)];
NSAssert(!responds, NSStringFromSelector(_cmd));
}
+ (void)protocolImplementationProperties:(id<TNSBaseProtocol1, NSObject>)object {
object.baseProtocolProperty1 = 0;
UNUSED(object.baseProtocolProperty1);
object.baseProtocolProperty1Optional = 0;
UNUSED(object.baseProtocolProperty1Optional);
}
+ (BOOL)protocolWithNameConflict:(id<TNSPropertyMethodConflictProtocol, NSObject>)object {
return object.conflict;
}
+ (TNSSimpleStruct)recordsSimpleStruct:(TNSSimpleStruct)object {
TNSLog([NSString stringWithFormat:@"%d %d", object.x, object.y]);
return object;
}
+ (TNSStruct16)recordsStruct16:(TNSStruct16)object {
TNSLog([NSString stringWithFormat:@"%lld %d %d", object.x, object.y, object.z]);
return object;
}
+ (TNSStruct24)recordsStruct24:(TNSStruct24)object {
TNSLog([NSString stringWithFormat:@"%lld %d %lld", object.x, object.y, object.z]);
return object;
}
+ (TNSStruct32)recordsStruct32:(TNSStruct32)object {
TNSLog([NSString stringWithFormat:@"%lld %lld %lld", object.x, object.y, object.z]);
return object;
}
+ (TNSNestedStruct)recordsNestedStruct:(TNSNestedStruct)object {
TNSLog([NSString stringWithFormat:@"%d %d %d %d", object.a.x, object.a.y, object.b.x, object.b.y]);
return object;
}
+ (TNSStructWithArray)recordsStructWithArray:(TNSStructWithArray)object {
TNSLog([NSString stringWithFormat:@"%d %d %d %d %d", object.x, object.arr[0], object.arr[1], object.arr[2], object.arr[3]]);
return object;
}
+ (TNSNestedAnonymousStruct)recordsNestedAnonymousStruct:(TNSNestedAnonymousStruct)object {
TNSLog([NSString stringWithFormat:@"%d %d %d", object.x1, object.y1.x2, object.y1.y2.x3]);
return object;
}
+ (TNSComplexStruct)recordsComplexStruct:(TNSComplexStruct)object {
TNSLog([NSString stringWithFormat:@"%d %d %d %d %d %d %d",
object.x1,
object.y1[0].x2,
object.y1[0].y2.x3[0],
object.y1[0].y2.x3[1],
object.y1[1].x2,
object.y1[1].y2.x3[0],
object.y1[1].y2.x3[1]]);
return object;
}
+ (void)recordsPointer:(TNSSimpleStruct*)object {
TNSLog([NSString stringWithFormat:@"%d %d", object->x, object->y]);
}
+ (void)apiNSMutableArrayMethods:(NSMutableArray*)object {
[object addObject:@"b"];
[object addObject:@"x"];
[object addObject:@"c"];
[object addObject:@"y"];
[object addObject:@"z"];
[object insertObject:@"a" atIndex:0];
[object removeObjectAtIndex:2];
[object removeLastObject];
object[3] = @"d";
TNSLog([NSString stringWithFormat:@"%tu%tu", [object count], [object hash]]);
for (id x in object) {
TNSLog([NSString stringWithFormat:@"%@", x]);
}
}
+ (void)apiSwizzle:(TNSSwizzleKlass*)object {
object.aProperty = 3;
TNSLog([NSString stringWithFormat:@"%d", object.aProperty]);
TNSLog([NSString stringWithFormat:@"%d", [[object class] staticMethod:3]]);
TNSLog([NSString stringWithFormat:@"%d", [object instanceMethod:3]]);
}
+ (NSString*)callRecursively:(NSString* (^)())block {
return block();
}
+ (NSString*)callOnThread:(NSString* (^)())block {
NSString* __block result = nil;
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
result = block();
dispatch_semaphore_signal(semaphore);
});
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
return result;
}
- (void (^)())getBlock {
return nil;
}
- (void (^)())getBlockFromNative {
return [self getBlock];
}
@end