-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNSObject+SteinInternalSupport.m
398 lines (317 loc) · 10.6 KB
/
NSObject+SteinInternalSupport.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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
//
// NSObject+SteinInternalSupport.m
// stein
//
// Created by Kevin MacWhinnie on 7/11/10.
// Copyright 2010 Stein Language. All rights reserved.
//
#import "NSObject+SteinInternalSupport.h"
#import "STInterpreter.h"
#import "STList.h"
#import <objc/runtime.h>
#import "STTypeBridge.h"
static NSString *const kNSObjectAdditionalIvarsTableKey = @"NSObject_additionalIvarsTable";
@implementation NSObject (SteinInternalSupport)
#pragma mark Overrides
+ (void)load
{
//Overrides for <STMethodMissing>
method_exchangeImplementations(class_getInstanceMethod(self, @selector(respondsToSelector:)),
class_getInstanceMethod(self, @selector(stein_respondsToSelector:)));
method_exchangeImplementations(class_getClassMethod(self, @selector(respondsToSelector:)),
class_getClassMethod(self, @selector(stein_respondsToSelector:)));
//Overrides for Ivar
method_exchangeImplementations(class_getInstanceMethod(self, @selector(valueForUndefinedKey:)),
class_getInstanceMethod(self, @selector(stein_valueForUndefinedKey:)));
method_exchangeImplementations(class_getClassMethod(self, @selector(setValue:forUndefinedKey:)),
class_getClassMethod(self, @selector(stein_setValue:forUndefinedKey:)));
//Overrides for kSTUseUniqueRuntimeClassNames
method_exchangeImplementations(class_getClassMethod(self, @selector(className)),
class_getClassMethod(self, @selector(stein_className)));
method_exchangeImplementations(class_getClassMethod(self, @selector(description)),
class_getClassMethod(self, @selector(stein_description)));
method_exchangeImplementations(class_getInstanceMethod(self, @selector(description)),
class_getInstanceMethod(self, @selector(stein_description)));
}
#pragma mark - • Overrides for <STMethodMissing>
+ (BOOL)stein_respondsToSelector:(SEL)selector
{
return [self stein_respondsToSelector:selector] || [self canHandleMissingMethodWithSelector:selector];
}
- (BOOL)stein_respondsToSelector:(SEL)selector
{
return [self stein_respondsToSelector:selector] || [self canHandleMissingMethodWithSelector:selector];
}
#pragma mark - • Overrides for kSTUseUniqueRuntimeClassNames
+ (NSString *)stein_className
{
return [self valueForIvarNamed:@"$steinClassName"] ?: [self stein_className];
}
- (NSString *)stein_className
{
return [[self class] stein_className];
}
+ (NSString *)stein_description
{
return [self className];
}
- (NSString *)stein_description
{
return [NSString stringWithFormat:@"<%@:%p>", [[self class] className], self];
}
#pragma mark - • Overrides for Ivar
- (id)stein_valueForUndefinedKey:(NSString *)key
{
return [self valueForIvarNamed:key] ?: [self stein_valueForUndefinedKey:key];
}
- (void)stein_setValue:(id)value forUndefinedKey:(NSString *)key
{
if([self valueForIvarNamed:key] != nil)
[self stein_setValue:value forUndefinedKey:key];
else
[self setValue:value forIvarNamed:key];
}
#pragma mark - Ivars
- (void)setValue:(id)value forIvarNamed:(NSString *)name
{
Ivar ivar = class_getInstanceVariable([self class], [name UTF8String]);
if(!ivar)
{
NSMutableDictionary *ivarTable = objc_getAssociatedObject(self, (__bridge const void *)(kNSObjectAdditionalIvarsTableKey));
if(!ivarTable)
{
ivarTable = [NSMutableDictionary dictionary];
objc_setAssociatedObject(self, (__bridge const void *)(kNSObjectAdditionalIvarsTableKey), ivarTable, OBJC_ASSOCIATION_RETAIN);
}
if(value)
[ivarTable setObject:value forKey:name];
else
[ivarTable removeObjectForKey:value];
return;
}
const char *ivarTypeEncoding = ivar_getTypeEncoding(ivar);
Byte buffer[STTypeBridgeGetSizeOfObjCType(ivarTypeEncoding)];
STTypeBridgeConvertObjectIntoType(value, ivarTypeEncoding, (void **)&buffer);
object_setIvar(self, ivar, (__bridge id)((void *)buffer));
}
- (id)valueForIvarNamed:(NSString *)name
{
Ivar ivar = class_getInstanceVariable([self class], [name UTF8String]);
if(!ivar)
{
NSMutableDictionary *ivarTable = objc_getAssociatedObject(self, (__bridge const void *)(kNSObjectAdditionalIvarsTableKey));
return [ivarTable objectForKey:name];
}
void *location = (__bridge void *)(object_getIvar(self, ivar));
return STTypeBridgeConvertValueOfTypeIntoObject(&location, ivar_getTypeEncoding(ivar));
}
#pragma mark -
+ (void)setValue:(id)value forIvarNamed:(NSString *)name
{
NSMutableDictionary *ivarTable = objc_getAssociatedObject(self, (__bridge const void *)(kNSObjectAdditionalIvarsTableKey));
if(!ivarTable)
{
ivarTable = [NSMutableDictionary dictionary];
objc_setAssociatedObject(self, (__bridge const void *)(kNSObjectAdditionalIvarsTableKey), ivarTable, OBJC_ASSOCIATION_RETAIN);
}
if(value)
[ivarTable setObject:value forKey:name];
else
[ivarTable removeObjectForKey:value];
}
+ (id)valueForIvarNamed:(NSString *)name
{
NSMutableDictionary *ivarTable = objc_getAssociatedObject(self, (__bridge const void *)(kNSObjectAdditionalIvarsTableKey));
return [ivarTable objectForKey:name];
}
#pragma mark - Implementing <STMethodMissing>
+ (BOOL)canHandleMissingMethodWithSelector:(SEL)selector
{
return NO;
}
+ (id)handleMissingMethodWithSelector:(SEL)selector arguments:(NSArray *)arguments inScope:(STScope *)scope
{
NSLog(@"[%s %s] called without concrete implementation. Did you forget to override it in your subclass?", class_getName([self class]), sel_getName(selector));
return STNull;
}
#pragma mark - -
#pragma mark Infix Notation Support
static BOOL IsCharacterSequenceOperator(unichar left, unichar right)
{
return (left == '+' || left == '-' || left == '*' || left == '/' || left == '^');
}
static BOOL IsSelectorComposedOfOperators(SEL selector)
{
NSString *selectorString = NSStringFromSelector(selector);
for (NSUInteger index = 0, length = [selectorString length]; index < length; index++)
{
unichar leftCharacter = [selectorString characterAtIndex:index];
unichar rightCharacter = (index + 1 < length)? [selectorString characterAtIndex:index + 1] : 0;
if(!IsCharacterSequenceOperator(leftCharacter, rightCharacter))
return NO;
if(rightCharacter != 0)
index++;
}
return YES;
}
- (BOOL)canHandleMissingMethodWithSelector:(SEL)selector
{
return IsSelectorComposedOfOperators(selector);
}
#pragma mark -
typedef struct Operation {
int originalPosition;
char operatorName[2];
} Operation;
static BOOL streq(const char *left, const char *right)
{
if(strlen(left) != strlen(right))
return NO;
for (int index = 0, length = strlen(left); index < length; index++)
{
if(left[index] != right[index])
return NO;
}
return YES;
}
ST_INLINE int PrecedenceOfOperatorNamed(char operatorName[3])
{
if(streq(operatorName, "|"))
{
return 1;
}
if(streq(operatorName, "&"))
{
return 2;
}
if(streq(operatorName, "+") || streq(operatorName, "-"))
{
return 3;
}
else if(streq(operatorName, "*") || streq(operatorName, "/"))
{
return 4;
}
else if(streq(operatorName, "^"))
{
return 5;
}
return 0;
}
static int NumberOfOperators(const char *operatorString)
{
return strlen(operatorString);
}
static int OperationPrecedenceComparator(Operation *left, Operation *right)
{
int leftPrecedence = PrecedenceOfOperatorNamed(left->operatorName);
int rightPrecedence = PrecedenceOfOperatorNamed(right->operatorName);
if(leftPrecedence > rightPrecedence)
return -1;
else if(leftPrecedence < rightPrecedence)
return 1;
return 0;
}
- (id)handleMissingMethodWithSelector:(SEL)selector arguments:(NSArray *)arguments inScope:(STScope *)scope
{
const char *operators = sel_getName(selector);
int numberOfOperations = NumberOfOperators(operators);
Operation operations[numberOfOperations];
for (int operationOffset = 0, operatorsLength = strlen(operators); operationOffset < operatorsLength; operationOffset++)
{
operations[operationOffset].originalPosition = operationOffset;
operations[operationOffset].operatorName[0] = operators[operationOffset];
operations[operationOffset].operatorName[1] = '\0';
}
qsort(operations, numberOfOperations, sizeof(Operation), (void *)&OperationPrecedenceComparator);
NSMutableArray *pool = [NSMutableArray arrayWithArray:arguments];
[pool insertObject:self atIndex:0];
for (int index = 0; index < numberOfOperations; index++)
{
Operation operation = operations[index];
id leftOperand = [pool objectAtIndex:operation.originalPosition];
id rightOperand = [pool objectAtIndex:operation.originalPosition + 1];
id result = 0;
if(streq(operation.operatorName, "+"))
{
result = [leftOperand operatorAdd:rightOperand];
}
else if(streq(operation.operatorName, "-"))
{
result = [leftOperand operatorSubtract:rightOperand];
}
else if(streq(operation.operatorName, "*"))
{
result = [leftOperand operatorMultiply:rightOperand];
}
else if(streq(operation.operatorName, "/"))
{
result = [leftOperand operatorDivide:rightOperand];
}
else if(streq(operation.operatorName, "^"))
{
result = [leftOperand operatorPower:rightOperand];
}
[pool replaceObjectAtIndex:operation.originalPosition withObject:result];
[pool replaceObjectAtIndex:operation.originalPosition + 1 withObject:result];
}
return [pool objectAtIndex:operations[numberOfOperations - 1].originalPosition];
}
#pragma mark - Implementing <STFunction>
- (BOOL)evaluatesOwnArguments
{
return YES;
}
- (STScope *)superscope
{
return nil;
}
#pragma mark -
- (id)applyWithArguments:(STList *)message inScope:(STScope *)scope
{
if(message.count == 0)
STRaiseIssue(message.creationLocation, @"malformed message to %@", self);
NSMutableString *selectorString = [NSMutableString string];
NSMutableArray *parameters = [NSMutableArray array];
BOOL isLookingForLabel = YES;
for (id component in message)
{
if(isLookingForLabel)
{
[selectorString appendString:[component string]];
}
else
{
[parameters addObject:STEvaluate(component, scope)];
}
isLookingForLabel = !isLookingForLabel;
}
return STObjectBridgeSend(self, NSSelectorFromString(selectorString), parameters, scope);
}
#pragma mark - Operators
- (id)operatorAdd:(id)rightOperand
{
[NSException raise:NSInternalInconsistencyException format:@"abstract operator"];
return STNull;
}
- (id)operatorSubtract:(id)rightOperand
{
[NSException raise:NSInternalInconsistencyException format:@"abstract operator"];
return STNull;
}
- (id)operatorMultiply:(id)rightOperand
{
[NSException raise:NSInternalInconsistencyException format:@"abstract operator"];
return STNull;
}
- (id)operatorDivide:(id)rightOperand
{
[NSException raise:NSInternalInconsistencyException format:@"abstract operator"];
return STNull;
}
- (id)operatorPower:(id)rightOperand
{
[NSException raise:NSInternalInconsistencyException format:@"abstract operator"];
return STNull;
}
@end