]>
Commit | Line | Data |
---|---|---|
62ca2b82 | 1 | /* Cyrker - Remove Execution Server and Disassembler |
c1582939 JF |
2 | * Copyright (C) 2009 Jay Freeman (saurik) |
3 | */ | |
4 | ||
62ca2b82 | 5 | /* Modified BSD License {{{ */ |
c1582939 JF |
6 | /* |
7 | * Redistribution and use in source and binary | |
8 | * forms, with or without modification, are permitted | |
9 | * provided that the following conditions are met: | |
10 | * | |
11 | * 1. Redistributions of source code must retain the | |
12 | * above copyright notice, this list of conditions | |
13 | * and the following disclaimer. | |
14 | * 2. Redistributions in binary form must reproduce the | |
15 | * above copyright notice, this list of conditions | |
16 | * and the following disclaimer in the documentation | |
17 | * and/or other materials provided with the | |
18 | * distribution. | |
19 | * 3. The name of the author may not be used to endorse | |
20 | * or promote products derived from this software | |
21 | * without specific prior written permission. | |
22 | * | |
23 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' | |
24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, | |
25 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
26 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE | |
28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
29 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
30 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
33 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR | |
34 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
35 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
36 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
37 | */ | |
62ca2b82 | 38 | /* }}} */ |
c1582939 | 39 | |
8d9b5eed JF |
40 | #define _GNU_SOURCE |
41 | ||
c1582939 | 42 | #include <substrate.h> |
30ddc20c | 43 | #include "cycript.hpp" |
c1582939 | 44 | |
ea2d184c JF |
45 | #include "sig/parse.hpp" |
46 | #include "sig/ffi_type.hpp" | |
47 | ||
5999c315 | 48 | #include "Pooling.hpp" |
057f943f | 49 | #include "Struct.hpp" |
ea2d184c | 50 | |
c1582939 JF |
51 | #include <unistd.h> |
52 | ||
53 | #include <CoreFoundation/CoreFoundation.h> | |
54 | #include <CoreFoundation/CFLogUtilities.h> | |
55 | ||
56 | #include <CFNetwork/CFNetwork.h> | |
c1582939 JF |
57 | |
58 | #include <WebKit/WebScriptObject.h> | |
59 | ||
60 | #include <sys/types.h> | |
61 | #include <sys/socket.h> | |
62 | #include <netinet/in.h> | |
63 | ||
8d9b5eed JF |
64 | #include <iostream> |
65 | #include <ext/stdio_filebuf.h> | |
a2d9403c JF |
66 | #include <set> |
67 | #include <map> | |
8d9b5eed | 68 | |
e5332278 | 69 | #include "Parser.hpp" |
63b4c5a8 | 70 | #include "Cycript.tab.hh" |
e5332278 | 71 | |
ea2d184c JF |
72 | #undef _assert |
73 | #undef _trace | |
74 | ||
c1582939 | 75 | #define _assert(test) do { \ |
f5e9be24 JF |
76 | if (!(test)) \ |
77 | @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"_assert(%s):%s(%u):%s", #test, __FILE__, __LINE__, __FUNCTION__] userInfo:nil]; \ | |
c1582939 JF |
78 | } while (false) |
79 | ||
80 | #define _trace() do { \ | |
62ca2b82 | 81 | CFLog(kCFLogLevelNotice, CFSTR("_trace():%u"), __LINE__); \ |
c1582939 JF |
82 | } while (false) |
83 | ||
707bcb93 | 84 | |
ea2d184c JF |
85 | static JSContextRef Context_; |
86 | ||
88c977fa JF |
87 | static JSClassRef Functor_; |
88 | static JSClassRef Instance_; | |
89 | static JSClassRef Pointer_; | |
90 | static JSClassRef Selector_; | |
ea2d184c | 91 | |
c1582939 | 92 | static JSObjectRef Array_; |
ea2d184c | 93 | |
62ca2b82 JF |
94 | static JSStringRef name_; |
95 | static JSStringRef message_; | |
c1582939 | 96 | static JSStringRef length_; |
ea2d184c | 97 | |
c1582939 JF |
98 | static Class NSCFBoolean_; |
99 | ||
88c977fa JF |
100 | static NSMutableDictionary *Bridge_; |
101 | ||
c1582939 JF |
102 | struct Client { |
103 | CFHTTPMessageRef message_; | |
104 | CFSocketRef socket_; | |
105 | }; | |
106 | ||
0c862573 | 107 | JSObjectRef CYMakeObject(JSContextRef context, id object) { |
88c977fa | 108 | return JSObjectMake(context, Instance_, [object retain]); |
0c862573 JF |
109 | } |
110 | ||
107e3ed0 | 111 | @interface NSMethodSignature (Cycript) |
7ba62cfd JF |
112 | - (NSString *) _typeString; |
113 | @end | |
114 | ||
107e3ed0 | 115 | @interface NSObject (Cycript) |
6b8a9500 | 116 | - (bool) cy$isUndefined; |
c1582939 | 117 | - (NSString *) cy$toJSON; |
ea2d184c | 118 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context; |
c1582939 JF |
119 | @end |
120 | ||
107e3ed0 | 121 | @interface NSString (Cycript) |
88c977fa JF |
122 | - (void *) cy$symbol; |
123 | @end | |
124 | ||
107e3ed0 | 125 | @interface NSNumber (Cycript) |
88c977fa JF |
126 | - (void *) cy$symbol; |
127 | @end | |
128 | ||
107e3ed0 | 129 | @implementation NSObject (Cycript) |
62ca2b82 | 130 | |
6b8a9500 JF |
131 | - (bool) cy$isUndefined { |
132 | return false; | |
133 | } | |
134 | ||
c1582939 | 135 | - (NSString *) cy$toJSON { |
62ca2b82 JF |
136 | return [self description]; |
137 | } | |
138 | ||
ea2d184c | 139 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { |
0c862573 | 140 | return CYMakeObject(context, self); |
ea2d184c JF |
141 | } |
142 | ||
62ca2b82 | 143 | @end |
c1582939 | 144 | |
107e3ed0 | 145 | @implementation WebUndefined (Cycript) |
62ca2b82 | 146 | |
6b8a9500 JF |
147 | - (bool) cy$isUndefined { |
148 | return true; | |
149 | } | |
150 | ||
c1582939 JF |
151 | - (NSString *) cy$toJSON { |
152 | return @"undefined"; | |
62ca2b82 JF |
153 | } |
154 | ||
155 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { | |
156 | return JSValueMakeUndefined(context); | |
157 | } | |
158 | ||
159 | @end | |
c1582939 | 160 | |
107e3ed0 | 161 | @implementation NSArray (Cycript) |
62ca2b82 | 162 | |
c1582939 JF |
163 | - (NSString *) cy$toJSON { |
164 | NSMutableString *json([[[NSMutableString alloc] init] autorelease]); | |
165 | [json appendString:@"["]; | |
166 | ||
167 | bool comma(false); | |
62ca2b82 | 168 | for (id object in self) { |
c1582939 JF |
169 | if (comma) |
170 | [json appendString:@","]; | |
171 | else | |
172 | comma = true; | |
6b8a9500 JF |
173 | if (![object cy$isUndefined]) |
174 | [json appendString:[object cy$toJSON]]; | |
175 | else { | |
176 | [json appendString:@","]; | |
177 | comma = false; | |
178 | } | |
c1582939 JF |
179 | } |
180 | ||
181 | [json appendString:@"]"]; | |
182 | return json; | |
62ca2b82 JF |
183 | } |
184 | ||
185 | @end | |
186 | ||
107e3ed0 | 187 | @implementation NSDictionary (Cycript) |
62ca2b82 JF |
188 | |
189 | - (NSString *) cy$toJSON { | |
190 | NSMutableString *json([[[NSMutableString alloc] init] autorelease]); | |
d35a3b07 | 191 | [json appendString:@"({"]; |
62ca2b82 JF |
192 | |
193 | bool comma(false); | |
194 | for (id key in self) { | |
195 | if (comma) | |
196 | [json appendString:@","]; | |
197 | else | |
198 | comma = true; | |
199 | [json appendString:[key cy$toJSON]]; | |
200 | [json appendString:@":"]; | |
201 | NSObject *object([self objectForKey:key]); | |
202 | [json appendString:[object cy$toJSON]]; | |
203 | } | |
204 | ||
205 | [json appendString:@"})"]; | |
206 | return json; | |
207 | } | |
208 | ||
209 | @end | |
c1582939 | 210 | |
107e3ed0 | 211 | @implementation NSNumber (Cycript) |
62ca2b82 | 212 | |
c1582939 JF |
213 | - (NSString *) cy$toJSON { |
214 | return [self class] != NSCFBoolean_ ? [self stringValue] : [self boolValue] ? @"true" : @"false"; | |
62ca2b82 JF |
215 | } |
216 | ||
217 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { | |
218 | return [self class] != NSCFBoolean_ ? JSValueMakeNumber(context, [self doubleValue]) : JSValueMakeBoolean(context, [self boolValue]); | |
219 | } | |
220 | ||
88c977fa JF |
221 | - (void *) cy$symbol { |
222 | return [self pointerValue]; | |
223 | } | |
224 | ||
62ca2b82 | 225 | @end |
c1582939 | 226 | |
107e3ed0 | 227 | @implementation NSString (Cycript) |
62ca2b82 | 228 | |
c1582939 JF |
229 | - (NSString *) cy$toJSON { |
230 | CFMutableStringRef json(CFStringCreateMutableCopy(kCFAllocatorDefault, 0, (CFStringRef) self)); | |
231 | ||
c1582939 JF |
232 | CFStringFindAndReplace(json, CFSTR("\\"), CFSTR("\\\\"), CFRangeMake(0, CFStringGetLength(json)), 0); |
233 | CFStringFindAndReplace(json, CFSTR("\""), CFSTR("\\\""), CFRangeMake(0, CFStringGetLength(json)), 0); | |
234 | CFStringFindAndReplace(json, CFSTR("\t"), CFSTR("\\t"), CFRangeMake(0, CFStringGetLength(json)), 0); | |
235 | CFStringFindAndReplace(json, CFSTR("\r"), CFSTR("\\r"), CFRangeMake(0, CFStringGetLength(json)), 0); | |
236 | CFStringFindAndReplace(json, CFSTR("\n"), CFSTR("\\n"), CFRangeMake(0, CFStringGetLength(json)), 0); | |
237 | ||
238 | CFStringInsert(json, 0, CFSTR("\"")); | |
239 | CFStringAppend(json, CFSTR("\"")); | |
240 | ||
62ca2b82 JF |
241 | return [reinterpret_cast<const NSString *>(json) autorelease]; |
242 | } | |
243 | ||
88c977fa JF |
244 | - (void *) cy$symbol { |
245 | return dlsym(RTLD_DEFAULT, [self UTF8String]); | |
246 | } | |
247 | ||
62ca2b82 JF |
248 | @end |
249 | ||
b21525c7 | 250 | @interface CYJSObject : NSDictionary { |
62ca2b82 JF |
251 | JSObjectRef object_; |
252 | JSContextRef context_; | |
253 | } | |
254 | ||
255 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context; | |
256 | ||
257 | - (NSUInteger) count; | |
258 | - (id) objectForKey:(id)key; | |
259 | - (NSEnumerator *) keyEnumerator; | |
260 | - (void) setObject:(id)object forKey:(id)key; | |
261 | - (void) removeObjectForKey:(id)key; | |
262 | ||
263 | @end | |
c1582939 | 264 | |
b21525c7 | 265 | @interface CYJSArray : NSArray { |
c1582939 JF |
266 | JSObjectRef object_; |
267 | JSContextRef context_; | |
268 | } | |
269 | ||
270 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context; | |
271 | ||
272 | - (NSUInteger) count; | |
273 | - (id) objectAtIndex:(NSUInteger)index; | |
274 | ||
275 | @end | |
276 | ||
30ddc20c | 277 | JSContextRef CYGetJSContext() { |
ea2d184c | 278 | return Context_; |
62ca2b82 JF |
279 | } |
280 | ||
0c862573 JF |
281 | #define CYCatch \ |
282 | @catch (id error) { \ | |
283 | CYThrow(context, error, exception); \ | |
284 | return NULL; \ | |
285 | } | |
286 | ||
ea2d184c JF |
287 | void CYThrow(JSContextRef context, JSValueRef value); |
288 | ||
b21525c7 | 289 | id CYCastNSObject(JSContextRef context, JSObjectRef object) { |
88c977fa | 290 | if (JSValueIsObjectOfClass(context, object, Instance_)) |
c1582939 | 291 | return reinterpret_cast<id>(JSObjectGetPrivate(object)); |
ea2d184c JF |
292 | JSValueRef exception(NULL); |
293 | bool array(JSValueIsInstanceOfConstructor(context, object, Array_, &exception)); | |
294 | CYThrow(context, exception); | |
295 | if (array) | |
b21525c7 JF |
296 | return [[[CYJSArray alloc] initWithJSObject:object inContext:context] autorelease]; |
297 | return [[[CYJSObject alloc] initWithJSObject:object inContext:context] autorelease]; | |
62ca2b82 JF |
298 | } |
299 | ||
77e87a6c JF |
300 | JSStringRef CYCopyJSString(id value) { |
301 | return JSStringCreateWithCFString(reinterpret_cast<CFStringRef>([value description])); | |
62ca2b82 JF |
302 | } |
303 | ||
77e87a6c JF |
304 | JSStringRef CYCopyJSString(const char *value) { |
305 | return JSStringCreateWithUTF8CString(value); | |
306 | } | |
307 | ||
308 | JSStringRef CYCopyJSString(JSStringRef value) { | |
309 | return JSStringRetain(value); | |
310 | } | |
311 | ||
312 | JSStringRef CYCopyJSString(JSContextRef context, JSValueRef value) { | |
62ca2b82 | 313 | JSValueRef exception(NULL); |
ea2d184c JF |
314 | JSStringRef string(JSValueToStringCopy(context, value, &exception)); |
315 | CYThrow(context, exception); | |
77e87a6c JF |
316 | return string; |
317 | } | |
318 | ||
319 | // XXX: this is not a safe handle | |
cf7d4c69 | 320 | class CYJSString { |
77e87a6c JF |
321 | private: |
322 | JSStringRef string_; | |
323 | ||
324 | public: | |
325 | template <typename Arg0_> | |
cf7d4c69 | 326 | CYJSString(Arg0_ arg0) { |
77e87a6c JF |
327 | string_ = CYCopyJSString(arg0); |
328 | } | |
329 | ||
330 | template <typename Arg0_, typename Arg1_> | |
cf7d4c69 | 331 | CYJSString(Arg0_ arg0, Arg1_ arg1) { |
77e87a6c JF |
332 | string_ = CYCopyJSString(arg0, arg1); |
333 | } | |
334 | ||
cf7d4c69 | 335 | ~CYJSString() { |
77e87a6c JF |
336 | JSStringRelease(string_); |
337 | } | |
338 | ||
339 | operator JSStringRef() const { | |
340 | return string_; | |
341 | } | |
342 | }; | |
343 | ||
344 | CFStringRef CYCopyCFString(JSStringRef value) { | |
345 | return JSStringCopyCFString(kCFAllocatorDefault, value); | |
346 | } | |
347 | ||
348 | CFStringRef CYCopyCFString(JSContextRef context, JSValueRef value) { | |
cf7d4c69 | 349 | return CYCopyCFString(CYJSString(context, value)); |
c1582939 JF |
350 | } |
351 | ||
f610e1a0 | 352 | double CYCastDouble(JSContextRef context, JSValueRef value) { |
0c862573 JF |
353 | JSValueRef exception(NULL); |
354 | double number(JSValueToNumber(context, value, &exception)); | |
355 | CYThrow(context, exception); | |
f610e1a0 JF |
356 | return number; |
357 | } | |
358 | ||
359 | CFNumberRef CYCopyCFNumber(JSContextRef context, JSValueRef value) { | |
360 | double number(CYCastDouble(context, value)); | |
0c862573 JF |
361 | return CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &number); |
362 | } | |
363 | ||
62ca2b82 JF |
364 | NSString *CYCastNSString(JSStringRef value) { |
365 | return [reinterpret_cast<const NSString *>(CYCopyCFString(value)) autorelease]; | |
366 | } | |
367 | ||
ea2d184c | 368 | CFTypeRef CYCopyCFType(JSContextRef context, JSValueRef value) { |
f610e1a0 | 369 | switch (JSType type = JSValueGetType(context, value)) { |
c1582939 | 370 | case kJSTypeUndefined: |
62ca2b82 | 371 | return CFRetain([WebUndefined undefined]); |
c1582939 JF |
372 | case kJSTypeNull: |
373 | return nil; | |
c1582939 | 374 | case kJSTypeBoolean: |
ea2d184c | 375 | return CFRetain(JSValueToBoolean(context, value) ? kCFBooleanTrue : kCFBooleanFalse); |
0c862573 JF |
376 | case kJSTypeNumber: |
377 | return CYCopyCFNumber(context, value); | |
62ca2b82 | 378 | case kJSTypeString: |
ea2d184c | 379 | return CYCopyCFString(context, value); |
c1582939 | 380 | case kJSTypeObject: |
b21525c7 | 381 | return CFRetain((CFTypeRef) CYCastNSObject(context, (JSObjectRef) value)); |
c1582939 | 382 | default: |
f5e9be24 | 383 | @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:[NSString stringWithFormat:@"JSValueGetType() == 0x%x", type] userInfo:nil]; |
c1582939 JF |
384 | } |
385 | } | |
386 | ||
62ca2b82 JF |
387 | NSArray *CYCastNSArray(JSPropertyNameArrayRef names) { |
388 | size_t size(JSPropertyNameArrayGetCount(names)); | |
389 | NSMutableArray *array([NSMutableArray arrayWithCapacity:size]); | |
390 | for (size_t index(0); index != size; ++index) | |
391 | [array addObject:CYCastNSString(JSPropertyNameArrayGetNameAtIndex(names, index))]; | |
392 | return array; | |
393 | } | |
394 | ||
ea2d184c JF |
395 | id CYCastNSObject(JSContextRef context, JSValueRef value) { |
396 | const NSObject *object(reinterpret_cast<const NSObject *>(CYCopyCFType(context, value))); | |
c1582939 JF |
397 | return object == nil ? nil : [object autorelease]; |
398 | } | |
399 | ||
ea2d184c | 400 | void CYThrow(JSContextRef context, JSValueRef value) { |
62ca2b82 JF |
401 | if (value == NULL) |
402 | return; | |
ea2d184c | 403 | @throw CYCastNSObject(context, value); |
62ca2b82 JF |
404 | } |
405 | ||
ea2d184c JF |
406 | JSValueRef CYCastJSValue(JSContextRef context, id value) { |
407 | return value == nil ? JSValueMakeNull(context) : [value cy$JSValueInContext:context]; | |
62ca2b82 JF |
408 | } |
409 | ||
30ddc20c | 410 | void CYThrow(JSContextRef context, id error, JSValueRef *exception) { |
7ba62cfd JF |
411 | *exception = CYCastJSValue(context, error); |
412 | } | |
413 | ||
b21525c7 | 414 | @implementation CYJSObject |
62ca2b82 JF |
415 | |
416 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { | |
417 | if ((self = [super init]) != nil) { | |
418 | object_ = object; | |
419 | context_ = context; | |
420 | } return self; | |
421 | } | |
422 | ||
423 | - (NSUInteger) count { | |
424 | JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_)); | |
425 | size_t size(JSPropertyNameArrayGetCount(names)); | |
426 | JSPropertyNameArrayRelease(names); | |
427 | return size; | |
428 | } | |
429 | ||
430 | - (id) objectForKey:(id)key { | |
431 | JSValueRef exception(NULL); | |
cf7d4c69 | 432 | JSValueRef value(JSObjectGetProperty(context_, object_, CYJSString(key), &exception)); |
62ca2b82 JF |
433 | CYThrow(context_, exception); |
434 | return CYCastNSObject(context_, value); | |
435 | } | |
436 | ||
437 | - (NSEnumerator *) keyEnumerator { | |
438 | JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_)); | |
439 | NSEnumerator *enumerator([CYCastNSArray(names) objectEnumerator]); | |
440 | JSPropertyNameArrayRelease(names); | |
441 | return enumerator; | |
442 | } | |
443 | ||
444 | - (void) setObject:(id)object forKey:(id)key { | |
445 | JSValueRef exception(NULL); | |
cf7d4c69 | 446 | JSObjectSetProperty(context_, object_, CYJSString(key), CYCastJSValue(context_, object), kJSPropertyAttributeNone, &exception); |
62ca2b82 JF |
447 | CYThrow(context_, exception); |
448 | } | |
449 | ||
450 | - (void) removeObjectForKey:(id)key { | |
451 | JSValueRef exception(NULL); | |
f610e1a0 | 452 | // XXX: this returns a bool... throw exception, or ignore? |
cf7d4c69 | 453 | JSObjectDeleteProperty(context_, object_, CYJSString(key), &exception); |
62ca2b82 JF |
454 | CYThrow(context_, exception); |
455 | } | |
456 | ||
457 | @end | |
458 | ||
b21525c7 | 459 | @implementation CYJSArray |
c1582939 JF |
460 | |
461 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { | |
462 | if ((self = [super init]) != nil) { | |
463 | object_ = object; | |
464 | context_ = context; | |
465 | } return self; | |
466 | } | |
467 | ||
468 | - (NSUInteger) count { | |
62ca2b82 JF |
469 | JSValueRef exception(NULL); |
470 | JSValueRef value(JSObjectGetProperty(context_, object_, length_, &exception)); | |
471 | CYThrow(context_, exception); | |
f610e1a0 | 472 | return CYCastDouble(context_, value); |
c1582939 JF |
473 | } |
474 | ||
475 | - (id) objectAtIndex:(NSUInteger)index { | |
62ca2b82 JF |
476 | JSValueRef exception(NULL); |
477 | JSValueRef value(JSObjectGetPropertyAtIndex(context_, object_, index, &exception)); | |
478 | CYThrow(context_, exception); | |
479 | id object(CYCastNSObject(context_, value)); | |
c1582939 JF |
480 | return object == nil ? [NSNull null] : object; |
481 | } | |
482 | ||
483 | @end | |
484 | ||
30ddc20c | 485 | CFStringRef CYCopyJSONString(JSContextRef context, JSValueRef value) { |
ea2d184c | 486 | id object(CYCastNSObject(context, value)); |
62ca2b82 | 487 | return reinterpret_cast<CFStringRef>([(object == nil ? @"null" : [object cy$toJSON]) retain]); |
c1582939 JF |
488 | } |
489 | ||
490 | static void OnData(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *value, void *info) { | |
491 | switch (type) { | |
492 | case kCFSocketDataCallBack: | |
493 | CFDataRef data(reinterpret_cast<CFDataRef>(value)); | |
494 | Client *client(reinterpret_cast<Client *>(info)); | |
495 | ||
496 | if (client->message_ == NULL) | |
497 | client->message_ = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, TRUE); | |
498 | ||
499 | if (!CFHTTPMessageAppendBytes(client->message_, CFDataGetBytePtr(data), CFDataGetLength(data))) | |
500 | CFLog(kCFLogLevelError, CFSTR("CFHTTPMessageAppendBytes()")); | |
501 | else if (CFHTTPMessageIsHeaderComplete(client->message_)) { | |
502 | CFURLRef url(CFHTTPMessageCopyRequestURL(client->message_)); | |
503 | Boolean absolute; | |
504 | CFStringRef path(CFURLCopyStrictPath(url, &absolute)); | |
505 | CFRelease(client->message_); | |
506 | ||
507 | CFStringRef code(CFURLCreateStringByReplacingPercentEscapes(kCFAllocatorDefault, path, CFSTR(""))); | |
508 | CFRelease(path); | |
509 | ||
510 | JSStringRef script(JSStringCreateWithCFString(code)); | |
511 | CFRelease(code); | |
512 | ||
057f943f | 513 | JSValueRef result(JSEvaluateScript(CYGetJSContext(), script, NULL, NULL, 0, NULL)); |
c1582939 JF |
514 | JSStringRelease(script); |
515 | ||
516 | CFHTTPMessageRef response(CFHTTPMessageCreateResponse(kCFAllocatorDefault, 200, NULL, kCFHTTPVersion1_1)); | |
517 | CFHTTPMessageSetHeaderFieldValue(response, CFSTR("Content-Type"), CFSTR("application/json; charset=utf-8")); | |
518 | ||
057f943f | 519 | CFStringRef json(CYCopyJSONString(CYGetJSContext(), result)); |
c1582939 JF |
520 | CFDataRef body(CFStringCreateExternalRepresentation(kCFAllocatorDefault, json, kCFStringEncodingUTF8, NULL)); |
521 | CFRelease(json); | |
522 | ||
523 | CFStringRef length(CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%u"), CFDataGetLength(body))); | |
524 | CFHTTPMessageSetHeaderFieldValue(response, CFSTR("Content-Length"), length); | |
525 | CFRelease(length); | |
526 | ||
527 | CFHTTPMessageSetBody(response, body); | |
528 | CFRelease(body); | |
529 | ||
530 | CFDataRef serialized(CFHTTPMessageCopySerializedMessage(response)); | |
531 | CFRelease(response); | |
532 | ||
533 | CFSocketSendData(socket, NULL, serialized, 0); | |
534 | CFRelease(serialized); | |
535 | ||
536 | CFRelease(url); | |
537 | } | |
538 | break; | |
539 | } | |
540 | } | |
541 | ||
542 | static void OnAccept(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *value, void *info) { | |
543 | switch (type) { | |
544 | case kCFSocketAcceptCallBack: | |
545 | Client *client(new Client()); | |
546 | ||
547 | client->message_ = NULL; | |
548 | ||
549 | CFSocketContext context; | |
550 | context.version = 0; | |
551 | context.info = client; | |
552 | context.retain = NULL; | |
553 | context.release = NULL; | |
554 | context.copyDescription = NULL; | |
555 | ||
556 | client->socket_ = CFSocketCreateWithNative(kCFAllocatorDefault, *reinterpret_cast<const CFSocketNativeHandle *>(value), kCFSocketDataCallBack, &OnData, &context); | |
557 | ||
558 | CFRunLoopAddSource(CFRunLoopGetCurrent(), CFSocketCreateRunLoopSource(kCFAllocatorDefault, client->socket_, 0), kCFRunLoopDefaultMode); | |
559 | break; | |
560 | } | |
561 | } | |
562 | ||
f610e1a0 JF |
563 | static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { _pooled |
564 | @try { | |
565 | NSString *name(CYCastNSString(property)); | |
566 | NSLog(@"%@", name); | |
567 | return NULL; | |
568 | } CYCatch | |
c1582939 JF |
569 | } |
570 | ||
ea2d184c JF |
571 | typedef id jocData; |
572 | ||
88c977fa | 573 | static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { _pooled |
0c862573 JF |
574 | @try { |
575 | id data(reinterpret_cast<jocData>(JSObjectGetPrivate(object))); | |
576 | return CYMakeObject(context, [[data alloc] autorelease]); | |
577 | } CYCatch | |
578 | } | |
579 | ||
04450da0 | 580 | struct ptrData { |
b21525c7 | 581 | apr_pool_t *pool_; |
04450da0 | 582 | void *value_; |
b21525c7 | 583 | sig::Type type_; |
77e87a6c JF |
584 | |
585 | void *operator new(size_t size) { | |
586 | apr_pool_t *pool; | |
587 | apr_pool_create(&pool, NULL); | |
588 | void *data(apr_palloc(pool, size)); | |
589 | reinterpret_cast<ptrData *>(data)->pool_ = pool; | |
590 | return data;; | |
591 | } | |
592 | ||
593 | ptrData(void *value) : | |
594 | value_(value) | |
595 | { | |
596 | } | |
597 | }; | |
598 | ||
599 | struct ffiData : ptrData { | |
600 | sig::Signature signature_; | |
601 | ffi_cif cif_; | |
602 | ||
603 | ffiData(void (*value)(), const char *type) : | |
604 | ptrData(reinterpret_cast<void *>(value)) | |
605 | { | |
606 | sig::Parse(pool_, &signature_, type); | |
607 | sig::sig_ffi_cif(pool_, &sig::ObjectiveC, &signature_, &cif_); | |
608 | } | |
609 | }; | |
610 | ||
611 | struct selData : ptrData { | |
612 | selData(SEL value) : | |
613 | ptrData(value) | |
614 | { | |
615 | } | |
04450da0 JF |
616 | }; |
617 | ||
88c977fa | 618 | static void Pointer_finalize(JSObjectRef object) { |
04450da0 | 619 | ptrData *data(reinterpret_cast<ptrData *>(JSObjectGetPrivate(object))); |
b21525c7 | 620 | apr_pool_destroy(data->pool_); |
04450da0 JF |
621 | } |
622 | ||
88c977fa | 623 | static void Instance_finalize(JSObjectRef object) { |
ea2d184c JF |
624 | id data(reinterpret_cast<jocData>(JSObjectGetPrivate(object))); |
625 | [data release]; | |
626 | } | |
627 | ||
88c977fa JF |
628 | JSObjectRef CYMakeFunction(JSContextRef context, void (*function)(), const char *type) { |
629 | ffiData *data(new ffiData(function, type)); | |
630 | return JSObjectMake(context, Functor_, data); | |
631 | } | |
632 | ||
633 | ||
634 | JSObjectRef CYMakeFunction(JSContextRef context, void *function, const char *type) { | |
635 | return CYMakeFunction(context, reinterpret_cast<void (*)()>(function), type); | |
636 | } | |
637 | ||
ea2d184c JF |
638 | void CYSetProperty(JSContextRef context, JSObjectRef object, const char *name, JSValueRef value) { |
639 | JSValueRef exception(NULL); | |
cf7d4c69 | 640 | JSObjectSetProperty(context, object, CYJSString(name), value, kJSPropertyAttributeNone, &exception); |
ea2d184c JF |
641 | CYThrow(context, exception); |
642 | } | |
643 | ||
7ba62cfd JF |
644 | char *CYPoolCString(apr_pool_t *pool, JSStringRef value) { |
645 | size_t size(JSStringGetMaximumUTF8CStringSize(value)); | |
b21525c7 | 646 | char *string(new(pool) char[size]); |
7ba62cfd JF |
647 | JSStringGetUTF8CString(value, string, size); |
648 | JSStringRelease(value); | |
649 | return string; | |
650 | } | |
651 | ||
77e87a6c | 652 | char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSValueRef value) { |
cf7d4c69 | 653 | return CYPoolCString(pool, CYJSString(context, value)); |
77e87a6c JF |
654 | } |
655 | ||
f610e1a0 | 656 | // XXX: this macro is unhygenic |
b21525c7 JF |
657 | #define CYCastCString(context, value) ({ \ |
658 | JSValueRef exception(NULL); \ | |
659 | JSStringRef string(JSValueToStringCopy(context, value, &exception)); \ | |
660 | CYThrow(context, exception); \ | |
661 | size_t size(JSStringGetMaximumUTF8CStringSize(string)); \ | |
662 | char *utf8(reinterpret_cast<char *>(alloca(size))); \ | |
663 | JSStringGetUTF8CString(string, utf8, size); \ | |
664 | JSStringRelease(string); \ | |
665 | utf8; \ | |
666 | }) | |
667 | ||
7ba62cfd JF |
668 | SEL CYCastSEL(JSContextRef context, JSValueRef value) { |
669 | if (JSValueIsNull(context, value)) | |
670 | return NULL; | |
88c977fa | 671 | else if (JSValueIsObjectOfClass(context, value, Selector_)) { |
77e87a6c JF |
672 | selData *data(reinterpret_cast<selData *>(JSObjectGetPrivate((JSObjectRef) value))); |
673 | return reinterpret_cast<SEL>(data->value_); | |
674 | } else | |
b21525c7 JF |
675 | return sel_registerName(CYCastCString(context, value)); |
676 | } | |
677 | ||
678 | void *CYCastPointer(JSContextRef context, JSValueRef value) { | |
679 | switch (JSValueGetType(context, value)) { | |
680 | case kJSTypeNull: | |
681 | return NULL; | |
b21525c7 JF |
682 | case kJSTypeString: |
683 | return dlsym(RTLD_DEFAULT, CYCastCString(context, value)); | |
b21525c7 | 684 | case kJSTypeObject: |
88c977fa | 685 | if (JSValueIsObjectOfClass(context, value, Pointer_)) { |
b21525c7 JF |
686 | ptrData *data(reinterpret_cast<ptrData *>(JSObjectGetPrivate((JSObjectRef) value))); |
687 | return data->value_; | |
688 | } | |
689 | default: | |
f610e1a0 | 690 | return reinterpret_cast<void *>(static_cast<uintptr_t>(CYCastDouble(context, value))); |
7ba62cfd JF |
691 | } |
692 | } | |
693 | ||
43cb3d68 | 694 | void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, void *data, JSValueRef value) { |
ea2d184c JF |
695 | switch (type->primitive) { |
696 | case sig::boolean_P: | |
697 | *reinterpret_cast<bool *>(data) = JSValueToBoolean(context, value); | |
698 | break; | |
699 | ||
43cb3d68 | 700 | #define CYPoolFFI_(primitive, native) \ |
f610e1a0 JF |
701 | case sig::primitive ## _P: \ |
702 | *reinterpret_cast<native *>(data) = CYCastDouble(context, value); \ | |
703 | break; | |
ea2d184c | 704 | |
43cb3d68 JF |
705 | CYPoolFFI_(uchar, unsigned char) |
706 | CYPoolFFI_(char, char) | |
707 | CYPoolFFI_(ushort, unsigned short) | |
708 | CYPoolFFI_(short, short) | |
709 | CYPoolFFI_(ulong, unsigned long) | |
710 | CYPoolFFI_(long, long) | |
711 | CYPoolFFI_(uint, unsigned int) | |
712 | CYPoolFFI_(int, int) | |
713 | CYPoolFFI_(ulonglong, unsigned long long) | |
714 | CYPoolFFI_(longlong, long long) | |
715 | CYPoolFFI_(float, float) | |
716 | CYPoolFFI_(double, double) | |
ea2d184c JF |
717 | |
718 | case sig::object_P: | |
719 | case sig::typename_P: | |
7ba62cfd JF |
720 | *reinterpret_cast<id *>(data) = CYCastNSObject(context, value); |
721 | break; | |
722 | ||
ea2d184c | 723 | case sig::selector_P: |
7ba62cfd JF |
724 | *reinterpret_cast<SEL *>(data) = CYCastSEL(context, value); |
725 | break; | |
ea2d184c | 726 | |
b21525c7 JF |
727 | case sig::pointer_P: |
728 | *reinterpret_cast<void **>(data) = CYCastPointer(context, value); | |
729 | break; | |
ea2d184c | 730 | |
77e87a6c JF |
731 | case sig::string_P: |
732 | *reinterpret_cast<char **>(data) = CYPoolCString(pool, context, value); | |
733 | break; | |
7ba62cfd | 734 | |
ea2d184c JF |
735 | case sig::struct_P: |
736 | goto fail; | |
737 | ||
738 | case sig::void_P: | |
739 | break; | |
740 | ||
741 | default: fail: | |
43cb3d68 | 742 | NSLog(@"CYPoolFFI(%c)\n", type->primitive); |
ea2d184c JF |
743 | _assert(false); |
744 | } | |
745 | } | |
746 | ||
43cb3d68 | 747 | JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, void *data) { |
ea2d184c JF |
748 | JSValueRef value; |
749 | ||
750 | switch (type->primitive) { | |
751 | case sig::boolean_P: | |
752 | value = JSValueMakeBoolean(context, *reinterpret_cast<bool *>(data)); | |
753 | break; | |
754 | ||
755 | #define CYFromFFI_(primitive, native) \ | |
756 | case sig::primitive ## _P: \ | |
757 | value = JSValueMakeNumber(context, *reinterpret_cast<native *>(data)); \ | |
758 | break; | |
759 | ||
760 | CYFromFFI_(uchar, unsigned char) | |
761 | CYFromFFI_(char, char) | |
762 | CYFromFFI_(ushort, unsigned short) | |
763 | CYFromFFI_(short, short) | |
764 | CYFromFFI_(ulong, unsigned long) | |
765 | CYFromFFI_(long, long) | |
766 | CYFromFFI_(uint, unsigned int) | |
767 | CYFromFFI_(int, int) | |
768 | CYFromFFI_(ulonglong, unsigned long long) | |
769 | CYFromFFI_(longlong, long long) | |
770 | CYFromFFI_(float, float) | |
771 | CYFromFFI_(double, double) | |
772 | ||
773 | case sig::object_P: | |
774 | case sig::typename_P: { | |
775 | value = CYCastJSValue(context, *reinterpret_cast<id *>(data)); | |
776 | } break; | |
777 | ||
7ba62cfd | 778 | case sig::selector_P: { |
77e87a6c JF |
779 | if (SEL sel = *reinterpret_cast<SEL *>(data)) { |
780 | selData *data(new selData(sel)); | |
88c977fa | 781 | value = JSObjectMake(context, Selector_, data); |
f610e1a0 | 782 | } else goto null; |
7ba62cfd JF |
783 | } break; |
784 | ||
785 | case sig::pointer_P: { | |
04450da0 | 786 | if (void *pointer = *reinterpret_cast<void **>(data)) { |
77e87a6c | 787 | ptrData *data(new ptrData(pointer)); |
88c977fa | 788 | value = JSObjectMake(context, Pointer_, data); |
f610e1a0 | 789 | } else goto null; |
7ba62cfd | 790 | } break; |
ea2d184c JF |
791 | |
792 | case sig::string_P: { | |
f610e1a0 | 793 | if (char *utf8 = *reinterpret_cast<char **>(data)) |
cf7d4c69 | 794 | value = JSValueMakeString(context, CYJSString(utf8)); |
f610e1a0 | 795 | else goto null; |
ea2d184c JF |
796 | } break; |
797 | ||
798 | case sig::struct_P: | |
799 | goto fail; | |
800 | ||
801 | case sig::void_P: | |
f610e1a0 JF |
802 | value = JSValueMakeUndefined(context); |
803 | break; | |
804 | ||
805 | null: | |
806 | value = JSValueMakeNull(context); | |
ea2d184c JF |
807 | break; |
808 | ||
809 | default: fail: | |
810 | NSLog(@"CYFromFFI(%c)\n", type->primitive); | |
811 | _assert(false); | |
812 | } | |
813 | ||
814 | return value; | |
815 | } | |
816 | ||
0c862573 | 817 | static JSValueRef CYCallFunction(JSContextRef context, size_t count, const JSValueRef *arguments, JSValueRef *exception, sig::Signature *signature, ffi_cif *cif, void (*function)()) { _pooled |
7ba62cfd | 818 | @try { |
85a33bf5 | 819 | if (count != signature->count - 1) |
f5e9be24 | 820 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to ffi function" userInfo:nil]; |
85a33bf5 | 821 | |
7ba62cfd JF |
822 | CYPool pool; |
823 | void *values[count]; | |
ea2d184c | 824 | |
7ba62cfd JF |
825 | for (unsigned index(0); index != count; ++index) { |
826 | sig::Element *element(&signature->elements[index + 1]); | |
77e87a6c | 827 | // XXX: alignment? |
b21525c7 | 828 | values[index] = new(pool) uint8_t[cif->arg_types[index]->size]; |
43cb3d68 | 829 | CYPoolFFI(pool, context, element->type, values[index], arguments[index]); |
7ba62cfd | 830 | } |
ea2d184c | 831 | |
7ba62cfd JF |
832 | uint8_t value[cif->rtype->size]; |
833 | ffi_call(cif, function, value, values); | |
834 | ||
43cb3d68 | 835 | return CYFromFFI(context, signature->elements[0].type, value); |
0c862573 | 836 | } CYCatch |
7ba62cfd | 837 | } |
ea2d184c | 838 | |
f610e1a0 | 839 | static JSValueRef Global_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { _pooled |
707bcb93 | 840 | @try { |
f610e1a0 JF |
841 | NSString *name(CYCastNSString(property)); |
842 | if (Class _class = NSClassFromString(name)) | |
707bcb93 | 843 | return CYMakeObject(context, _class); |
f610e1a0 | 844 | if (NSMutableArray *entry = [Bridge_ objectForKey:name]) |
707bcb93 JF |
845 | switch ([[entry objectAtIndex:0] intValue]) { |
846 | case 0: | |
057f943f | 847 | return JSEvaluateScript(CYGetJSContext(), CYJSString([entry objectAtIndex:1]), NULL, NULL, 0, NULL); |
707bcb93 | 848 | case 1: |
f610e1a0 | 849 | return CYMakeFunction(context, [name cy$symbol], [[entry objectAtIndex:1] UTF8String]); |
707bcb93 JF |
850 | case 2: |
851 | CYPool pool; | |
852 | sig::Signature signature; | |
853 | sig::Parse(pool, &signature, [[entry objectAtIndex:1] UTF8String]); | |
f610e1a0 | 854 | return CYFromFFI(context, signature.elements[0].type, [name cy$symbol]); |
707bcb93 JF |
855 | } |
856 | return NULL; | |
857 | } CYCatch | |
858 | } | |
859 | ||
04450da0 JF |
860 | bool stret(ffi_type *ffi_type) { |
861 | return ffi_type->type == FFI_TYPE_STRUCT && ( | |
862 | ffi_type->size > OBJC_MAX_STRUCT_BY_VALUE || | |
863 | struct_forward_array[ffi_type->size] != 0 | |
864 | ); | |
865 | } | |
866 | ||
7ba62cfd | 867 | static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { _pooled |
7ba62cfd | 868 | const char *type; |
ea2d184c JF |
869 | |
870 | @try { | |
85a33bf5 | 871 | if (count < 2) |
f5e9be24 | 872 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"too few arguments to objc_msgSend" userInfo:nil]; |
85a33bf5 | 873 | |
7ba62cfd JF |
874 | id self(CYCastNSObject(context, arguments[0])); |
875 | if (self == nil) | |
876 | return JSValueMakeNull(context); | |
7ba62cfd | 877 | |
85a33bf5 | 878 | SEL _cmd(CYCastSEL(context, arguments[1])); |
7ba62cfd | 879 | NSMethodSignature *method([self methodSignatureForSelector:_cmd]); |
85a33bf5 | 880 | if (method == nil) |
f5e9be24 | 881 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"unrecognized selector %s sent to object %p", sel_getName(_cmd), self] userInfo:nil]; |
7ba62cfd JF |
882 | |
883 | type = [[method _typeString] UTF8String]; | |
0c862573 | 884 | } CYCatch |
ea2d184c | 885 | |
7ba62cfd JF |
886 | CYPool pool; |
887 | ||
888 | sig::Signature signature; | |
889 | sig::Parse(pool, &signature, type); | |
ea2d184c | 890 | |
7ba62cfd | 891 | ffi_cif cif; |
b21525c7 | 892 | sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); |
7ba62cfd | 893 | |
04450da0 | 894 | void (*function)() = stret(cif.rtype) ? reinterpret_cast<void (*)()>(&objc_msgSend_stret) : reinterpret_cast<void (*)()>(&objc_msgSend); |
7ba62cfd JF |
895 | return CYCallFunction(context, count, arguments, exception, &signature, &cif, function); |
896 | } | |
897 | ||
898 | static JSValueRef ffi_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
899 | ffiData *data(reinterpret_cast<ffiData *>(JSObjectGetPrivate(object))); | |
77e87a6c | 900 | return CYCallFunction(context, count, arguments, exception, &data->signature_, &data->cif_, reinterpret_cast<void (*)()>(data->value_)); |
ea2d184c JF |
901 | } |
902 | ||
b21525c7 JF |
903 | JSObjectRef ffi(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { |
904 | @try { | |
905 | if (count != 2) | |
f5e9be24 | 906 | @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to ffi constructor" userInfo:nil]; |
88c977fa | 907 | void *function(CYCastPointer(context, arguments[0])); |
b21525c7 JF |
908 | const char *type(CYCastCString(context, arguments[1])); |
909 | return CYMakeFunction(context, function, type); | |
0c862573 | 910 | } CYCatch |
ea2d184c JF |
911 | } |
912 | ||
f610e1a0 | 913 | JSValueRef Pointer_getProperty_value(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { |
04450da0 JF |
914 | ptrData *data(reinterpret_cast<ptrData *>(JSObjectGetPrivate(object))); |
915 | return JSValueMakeNumber(context, reinterpret_cast<uintptr_t>(data->value_)); | |
916 | } | |
917 | ||
88c977fa JF |
918 | static JSStaticValue Pointer_staticValues[2] = { |
919 | {"value", &Pointer_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete}, | |
04450da0 JF |
920 | {NULL, NULL, NULL, 0} |
921 | }; | |
922 | ||
5999c315 | 923 | CYDriver::CYDriver(const std::string &filename) : |
db5e2840 | 924 | state_(CYClear), |
e7ed5354 JF |
925 | data_(NULL), |
926 | size_(0), | |
5999c315 JF |
927 | filename_(filename), |
928 | source_(NULL) | |
929 | { | |
924f67b2 JF |
930 | ScannerInit(); |
931 | } | |
932 | ||
5999c315 | 933 | CYDriver::~CYDriver() { |
924f67b2 JF |
934 | ScannerDestroy(); |
935 | } | |
936 | ||
5befe15e JF |
937 | void cy::parser::error(const cy::parser::location_type &location, const std::string &message) { |
938 | CYDriver::Error error; | |
939 | error.location_ = location; | |
940 | error.message_ = message; | |
941 | driver.errors_.push_back(error); | |
63b4c5a8 JF |
942 | } |
943 | ||
7ba62cfd | 944 | MSInitialize { _pooled |
ea2d184c JF |
945 | apr_initialize(); |
946 | ||
c1582939 JF |
947 | NSCFBoolean_ = objc_getClass("NSCFBoolean"); |
948 | ||
949 | pid_t pid(getpid()); | |
950 | ||
951 | struct sockaddr_in address; | |
952 | address.sin_len = sizeof(address); | |
953 | address.sin_family = AF_INET; | |
954 | address.sin_addr.s_addr = INADDR_ANY; | |
955 | address.sin_port = htons(10000 + pid); | |
956 | ||
957 | CFDataRef data(CFDataCreate(kCFAllocatorDefault, reinterpret_cast<UInt8 *>(&address), sizeof(address))); | |
958 | ||
959 | CFSocketSignature signature; | |
960 | signature.protocolFamily = AF_INET; | |
961 | signature.socketType = SOCK_STREAM; | |
962 | signature.protocol = IPPROTO_TCP; | |
963 | signature.address = data; | |
964 | ||
965 | CFSocketRef socket(CFSocketCreateWithSocketSignature(kCFAllocatorDefault, &signature, kCFSocketAcceptCallBack, &OnAccept, NULL)); | |
966 | CFRunLoopAddSource(CFRunLoopGetCurrent(), CFSocketCreateRunLoopSource(kCFAllocatorDefault, socket, 0), kCFRunLoopDefaultMode); | |
967 | ||
968 | JSClassDefinition definition; | |
969 | ||
970 | definition = kJSClassDefinitionEmpty; | |
88c977fa JF |
971 | definition.className = "Pointer"; |
972 | definition.staticValues = Pointer_staticValues; | |
973 | definition.finalize = &Pointer_finalize; | |
974 | Pointer_ = JSClassCreate(&definition); | |
c1582939 JF |
975 | |
976 | definition = kJSClassDefinitionEmpty; | |
88c977fa JF |
977 | definition.className = "Functor"; |
978 | definition.parentClass = Pointer_; | |
979 | definition.callAsFunction = &ffi_callAsFunction; | |
980 | Functor_ = JSClassCreate(&definition); | |
7ba62cfd | 981 | |
77e87a6c | 982 | definition = kJSClassDefinitionEmpty; |
88c977fa JF |
983 | definition.className = "Selector"; |
984 | definition.parentClass = Pointer_; | |
985 | Selector_ = JSClassCreate(&definition); | |
77e87a6c | 986 | |
7ba62cfd | 987 | definition = kJSClassDefinitionEmpty; |
88c977fa JF |
988 | definition.className = "Instance_"; |
989 | definition.getProperty = &Instance_getProperty; | |
990 | definition.callAsConstructor = &Instance_callAsConstructor; | |
991 | definition.finalize = &Instance_finalize; | |
992 | Instance_ = JSClassCreate(&definition); | |
7ba62cfd JF |
993 | |
994 | definition = kJSClassDefinitionEmpty; | |
88c977fa JF |
995 | definition.getProperty = &Global_getProperty; |
996 | JSClassRef Global(JSClassCreate(&definition)); | |
c1582939 | 997 | |
88c977fa | 998 | JSContextRef context(JSGlobalContextCreate(Global)); |
ea2d184c JF |
999 | Context_ = context; |
1000 | ||
1001 | JSObjectRef global(JSContextGetGlobalObject(context)); | |
c1582939 | 1002 | |
88c977fa | 1003 | CYSetProperty(context, global, "ffi", JSObjectMakeConstructor(context, Functor_, &ffi)); |
7ba62cfd | 1004 | |
cf7d4c69 | 1005 | CYSetProperty(context, global, "objc_msgSend", JSObjectMakeFunctionWithCallback(context, CYJSString("objc_msgSend"), &$objc_msgSend)); |
7ba62cfd | 1006 | |
107e3ed0 | 1007 | Bridge_ = [[NSMutableDictionary dictionaryWithContentsOfFile:@"/usr/lib/libcycript.plist"] retain]; |
c1582939 | 1008 | |
62ca2b82 JF |
1009 | name_ = JSStringCreateWithUTF8CString("name"); |
1010 | message_ = JSStringCreateWithUTF8CString("message"); | |
c1582939 JF |
1011 | length_ = JSStringCreateWithUTF8CString("length"); |
1012 | ||
62ca2b82 | 1013 | JSValueRef exception(NULL); |
057f943f | 1014 | JSValueRef value(JSObjectGetProperty(CYGetJSContext(), global, CYJSString("Array"), &exception)); |
ea2d184c | 1015 | CYThrow(context, exception); |
057f943f | 1016 | Array_ = JSValueToObject(CYGetJSContext(), value, &exception); |
ea2d184c | 1017 | CYThrow(context, exception); |
c1582939 | 1018 | } |