forked from newyankeecodeshop/GAJavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSObject+GAJavaScript.m
More file actions
301 lines (224 loc) · 7.31 KB
/
NSObject+GAJavaScript.m
File metadata and controls
301 lines (224 loc) · 7.31 KB
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
/*
Copyright (c) 2010 Andrew Goodale. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.
THIS SOFTWARE IS PROVIDED BY ANDREW GOODALE "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those of the
authors and should not be interpreted as representing official policies, either expressed
or implied, of Andrew Goodale.
*/
#import "NSObject+GAJavaScript.h"
#import <objc/runtime.h>
@implementation NSObject (GAJavaScript)
- (NSString *)stringForJavaScript
{
unsigned int numProps = 0;
objc_property_t* propList = class_copyPropertyList([self class], &numProps);
NSMutableString* target = [[NSMutableString alloc] initWithCapacity:64];
[target appendFormat:@"%c", '{'];
for (unsigned int i = 0; i < numProps; i++)
{
const char* propName = property_getName(propList[i]);
id propValue = [self valueForKey:[NSString stringWithCString:propName encoding:NSASCIIStringEncoding]];
if (propValue == self) // To prevent endless recursion, check for property values that are us!
continue;
else if (propValue == nil)
propValue = [NSNull null];
if (i > 0)
[target appendFormat:@"%c", ','];
[target appendFormat:@" %s:%@", propName, [propValue stringForJavaScript]];
}
free(propList);
[target appendFormat:@"%c", '}'];
return [target autorelease];
}
- (BOOL)isJavaScriptTrue
{
return YES;
}
@end
#pragma mark -
@implementation NSNull (GAJavaScript)
- (NSString *)stringForJavaScript
{
return @"null";
}
- (BOOL)isJavaScriptTrue
{
return NO;
}
@end
#pragma mark -
@implementation NSNumber (GAJavaScript)
- (NSString *)stringForJavaScript
{
const char* numberType = [self objCType];
// If the number contains BOOL, its objC type will be "c"
if (*numberType == 'c')
return [self boolValue] ? @"true" : @"false";
else
return [self stringValue];
}
- (BOOL)isJavaScriptTrue
{
// This handles booleans, integers and floats.
//
return [self intValue] != 0;
}
@end
#pragma mark -
@implementation NSString (GAJavaScript)
- (NSString *)stringForJavaScript
{
if ([self length] == 0)
return @"''";
NSCharacterSet* charsToEscape = [NSCharacterSet escapeForJavaScriptSet];
// Avoid creating an NSMutableString if we don't need to do any encoding.
//
if ([self rangeOfCharacterFromSet:charsToEscape].location == NSNotFound)
return [NSString stringWithFormat:@"'%@'", self];
NSMutableString* target = [[NSMutableString alloc] initWithCapacity:[self length] + 4];
[target appendFormat:@"%c", '\''];
for (NSInteger i = 0; i < [self length]; ++i)
{
unichar ch = [self characterAtIndex:i];
switch (ch)
{
case '\'':
case '\"':
case '\\':
[target appendFormat:@"\\%C", ch];
break;
default:
if ([charsToEscape characterIsMember:ch]) // It's a control character
[target appendFormat:@"\\u%04hX", ch];
else
[target appendFormat:@"%C", ch];
break;
}
}
[target appendFormat:@"%c", '\''];
return [target autorelease];
}
- (BOOL)isJavaScriptTrue
{
return [self length] != 0;
}
@end
#pragma mark -
@implementation NSDate (GAJavaScript)
- (NSString *)stringForJavaScript
{
return [NSString stringWithFormat:@"new Date(%.0f)", [self timeIntervalSince1970] * 1000];
}
@end
#pragma mark -
@implementation NSArray (GAJavaScript)
- (NSString *)stringForJavaScript
{
NSMutableString* target = [[NSMutableString alloc] initWithCapacity:64];
[target appendString:@"new Array("];
for (id elem in self)
{
if ([target length] > 10)
[target appendString:@", "];
[target appendString:[elem stringForJavaScript]];
}
[target appendString:@")"];
return [target autorelease];
}
@end
#pragma mark -
@implementation NSDictionary (GAJavaScript)
- (NSString *)stringForJavaScript
{
NSArray* allKeys = [self allKeys];
NSMutableString* target = [[NSMutableString alloc] initWithCapacity:64];
[target appendFormat:@"%c", '{'];
for (NSString* key in allKeys)
{
id propValue = [self objectForKey:key];
[target appendFormat:@"%@: %@, ", key, [propValue stringForJavaScript]];
}
// Remove the trailing comma
if ([target characterAtIndex:[target length] - 2] == ',')
[target deleteCharactersInRange:NSMakeRange([target length] - 2, 2)];
[target appendFormat:@"%c", '}'];
return [target autorelease];
}
@end
#pragma mark -
@implementation NSInvocation (GAJavaScript)
- (void)setArgumentsFromJavaScript:(NSArray *)arguments
{
NSMethodSignature* methodSig = [self methodSignature];
NSInteger argIndex = 2; // Because target and _cmd are the first two arguments
for (id arg in arguments)
{
// Need to get the address right based on types...
//
const char* argType = [methodSig getArgumentTypeAtIndex:argIndex];
switch (*argType)
{
case 'c':
{
BOOL boolArg = [arg boolValue];
[self setArgument:&boolArg atIndex:argIndex++];
break;
}
case 'i':
{
NSInteger intArg = [arg intValue];
[self setArgument:&intArg atIndex:argIndex++];
break;
}
case 'f':
{
float floatArg = [arg floatValue];
[self setArgument:&floatArg atIndex:argIndex++];
break;
}
default:
[self setArgument:&arg atIndex:argIndex++];
break;
}
}
}
@end
#pragma mark -
@implementation NSError (GAJavaScript)
- (BOOL)isJavaScriptTrue
{
return NO;
}
@end
#pragma mark -
@implementation NSCharacterSet (GAJavaScript)
+ (id)escapeForJavaScriptSet
{
static NSCharacterSet* kNeedsEscapingSet = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^(void)
{
NSMutableCharacterSet* newSet = [[NSMutableCharacterSet alloc] init];
[newSet addCharactersInString:@"\"\'\\"];
[newSet formUnionWithCharacterSet:[NSCharacterSet controlCharacterSet]];
kNeedsEscapingSet = [newSet copy]; // Make an immutable NSCharacterSet
[newSet release];
});
return kNeedsEscapingSet;
}
@end