]>
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 JF |
39 | |
40 | #include <substrate.h> | |
04450da0 | 41 | #include "Struct.hpp" |
c1582939 | 42 | |
ea2d184c JF |
43 | #include "sig/parse.hpp" |
44 | #include "sig/ffi_type.hpp" | |
45 | ||
46 | #include <apr-1/apr_pools.h> | |
47 | #include <apr-1/apr_strings.h> | |
48 | ||
c1582939 JF |
49 | #include <unistd.h> |
50 | ||
51 | #include <CoreFoundation/CoreFoundation.h> | |
52 | #include <CoreFoundation/CFLogUtilities.h> | |
53 | ||
54 | #include <CFNetwork/CFNetwork.h> | |
55 | #include <Foundation/Foundation.h> | |
56 | ||
57 | #include <JavaScriptCore/JSBase.h> | |
58 | #include <JavaScriptCore/JSValueRef.h> | |
59 | #include <JavaScriptCore/JSObjectRef.h> | |
60 | #include <JavaScriptCore/JSContextRef.h> | |
61 | #include <JavaScriptCore/JSStringRef.h> | |
62 | #include <JavaScriptCore/JSStringRefCF.h> | |
63 | ||
64 | #include <WebKit/WebScriptObject.h> | |
65 | ||
66 | #include <sys/types.h> | |
67 | #include <sys/socket.h> | |
68 | #include <netinet/in.h> | |
69 | ||
ea2d184c JF |
70 | #undef _assert |
71 | #undef _trace | |
72 | ||
c1582939 JF |
73 | /* XXX: bad _assert */ |
74 | #define _assert(test) do { \ | |
75 | if ((test)) break; \ | |
62ca2b82 JF |
76 | CFLog(kCFLogLevelNotice, CFSTR("_assert(%s):%u"), #test, __LINE__); \ |
77 | throw; \ | |
c1582939 JF |
78 | } while (false) |
79 | ||
80 | #define _trace() do { \ | |
62ca2b82 | 81 | CFLog(kCFLogLevelNotice, CFSTR("_trace():%u"), __LINE__); \ |
c1582939 JF |
82 | } while (false) |
83 | ||
7ba62cfd JF |
84 | /* Objective-C Handle<> {{{ */ |
85 | template <typename Type_> | |
86 | class _H { | |
87 | typedef _H<Type_> This_; | |
88 | ||
89 | private: | |
90 | Type_ *value_; | |
91 | ||
92 | _finline void Retain_() { | |
93 | if (value_ != nil) | |
94 | [value_ retain]; | |
95 | } | |
96 | ||
97 | _finline void Clear_() { | |
98 | if (value_ != nil) | |
99 | [value_ release]; | |
100 | } | |
101 | ||
102 | public: | |
103 | _finline _H(const This_ &rhs) : | |
104 | value_(rhs.value_ == nil ? nil : [rhs.value_ retain]) | |
105 | { | |
106 | } | |
107 | ||
108 | _finline _H(Type_ *value = NULL, bool mended = false) : | |
109 | value_(value) | |
110 | { | |
111 | if (!mended) | |
112 | Retain_(); | |
113 | } | |
114 | ||
115 | _finline ~_H() { | |
116 | Clear_(); | |
117 | } | |
118 | ||
119 | _finline operator Type_ *() const { | |
120 | return value_; | |
121 | } | |
122 | ||
123 | _finline This_ &operator =(Type_ *value) { | |
124 | if (value_ != value) { | |
125 | Type_ *old(value_); | |
126 | value_ = value; | |
127 | Retain_(); | |
128 | if (old != nil) | |
129 | [old release]; | |
130 | } return *this; | |
131 | } | |
132 | }; | |
133 | /* }}} */ | |
134 | ||
135 | #define _pooled _H<NSAutoreleasePool> _pool([[NSAutoreleasePool alloc] init], true); | |
136 | ||
b21525c7 JF |
137 | void *operator new(size_t size, apr_pool_t *pool) { |
138 | return apr_palloc(pool, size); | |
139 | } | |
140 | ||
141 | void *operator new [](size_t size, apr_pool_t *pool) { | |
142 | return apr_palloc(pool, size); | |
143 | } | |
144 | ||
ea2d184c JF |
145 | static JSContextRef Context_; |
146 | ||
147 | static JSClassRef ffi_; | |
c1582939 | 148 | static JSClassRef joc_; |
7ba62cfd JF |
149 | static JSClassRef ptr_; |
150 | static JSClassRef sel_; | |
ea2d184c | 151 | |
c1582939 | 152 | static JSObjectRef Array_; |
ea2d184c | 153 | |
62ca2b82 JF |
154 | static JSStringRef name_; |
155 | static JSStringRef message_; | |
c1582939 | 156 | static JSStringRef length_; |
ea2d184c | 157 | |
c1582939 JF |
158 | static Class NSCFBoolean_; |
159 | ||
160 | struct Client { | |
161 | CFHTTPMessageRef message_; | |
162 | CFSocketRef socket_; | |
163 | }; | |
164 | ||
7ba62cfd JF |
165 | @interface NSMethodSignature (Cyrver) |
166 | - (NSString *) _typeString; | |
167 | @end | |
168 | ||
c1582939 JF |
169 | @interface NSObject (Cyrver) |
170 | - (NSString *) cy$toJSON; | |
ea2d184c | 171 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context; |
c1582939 JF |
172 | @end |
173 | ||
174 | @implementation NSObject (Cyrver) | |
62ca2b82 | 175 | |
c1582939 | 176 | - (NSString *) cy$toJSON { |
62ca2b82 JF |
177 | return [self description]; |
178 | } | |
179 | ||
ea2d184c JF |
180 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { |
181 | return JSObjectMake(context, joc_, [self retain]); | |
182 | } | |
183 | ||
62ca2b82 | 184 | @end |
c1582939 JF |
185 | |
186 | @implementation WebUndefined (Cyrver) | |
62ca2b82 | 187 | |
c1582939 JF |
188 | - (NSString *) cy$toJSON { |
189 | return @"undefined"; | |
62ca2b82 JF |
190 | } |
191 | ||
192 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { | |
193 | return JSValueMakeUndefined(context); | |
194 | } | |
195 | ||
196 | @end | |
c1582939 JF |
197 | |
198 | @implementation NSArray (Cyrver) | |
62ca2b82 | 199 | |
c1582939 JF |
200 | - (NSString *) cy$toJSON { |
201 | NSMutableString *json([[[NSMutableString alloc] init] autorelease]); | |
202 | [json appendString:@"["]; | |
203 | ||
204 | bool comma(false); | |
62ca2b82 | 205 | for (id object in self) { |
c1582939 JF |
206 | if (comma) |
207 | [json appendString:@","]; | |
208 | else | |
209 | comma = true; | |
210 | [json appendString:[object cy$toJSON]]; | |
211 | } | |
212 | ||
213 | [json appendString:@"]"]; | |
214 | return json; | |
62ca2b82 JF |
215 | } |
216 | ||
217 | @end | |
218 | ||
219 | @implementation NSDictionary (Cyrver) | |
220 | ||
221 | - (NSString *) cy$toJSON { | |
222 | NSMutableString *json([[[NSMutableString alloc] init] autorelease]); | |
7ba62cfd JF |
223 | [json appendString:@"("]; |
224 | [json appendString:@"{"]; | |
62ca2b82 JF |
225 | |
226 | bool comma(false); | |
227 | for (id key in self) { | |
228 | if (comma) | |
229 | [json appendString:@","]; | |
230 | else | |
231 | comma = true; | |
232 | [json appendString:[key cy$toJSON]]; | |
233 | [json appendString:@":"]; | |
234 | NSObject *object([self objectForKey:key]); | |
235 | [json appendString:[object cy$toJSON]]; | |
236 | } | |
237 | ||
238 | [json appendString:@"})"]; | |
239 | return json; | |
240 | } | |
241 | ||
242 | @end | |
c1582939 JF |
243 | |
244 | @implementation NSNumber (Cyrver) | |
62ca2b82 | 245 | |
c1582939 JF |
246 | - (NSString *) cy$toJSON { |
247 | return [self class] != NSCFBoolean_ ? [self stringValue] : [self boolValue] ? @"true" : @"false"; | |
62ca2b82 JF |
248 | } |
249 | ||
250 | - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { | |
251 | return [self class] != NSCFBoolean_ ? JSValueMakeNumber(context, [self doubleValue]) : JSValueMakeBoolean(context, [self boolValue]); | |
252 | } | |
253 | ||
254 | @end | |
c1582939 JF |
255 | |
256 | @implementation NSString (Cyrver) | |
62ca2b82 | 257 | |
c1582939 JF |
258 | - (NSString *) cy$toJSON { |
259 | CFMutableStringRef json(CFStringCreateMutableCopy(kCFAllocatorDefault, 0, (CFStringRef) self)); | |
260 | ||
c1582939 JF |
261 | CFStringFindAndReplace(json, CFSTR("\\"), CFSTR("\\\\"), CFRangeMake(0, CFStringGetLength(json)), 0); |
262 | CFStringFindAndReplace(json, CFSTR("\""), CFSTR("\\\""), CFRangeMake(0, CFStringGetLength(json)), 0); | |
263 | CFStringFindAndReplace(json, CFSTR("\t"), CFSTR("\\t"), CFRangeMake(0, CFStringGetLength(json)), 0); | |
264 | CFStringFindAndReplace(json, CFSTR("\r"), CFSTR("\\r"), CFRangeMake(0, CFStringGetLength(json)), 0); | |
265 | CFStringFindAndReplace(json, CFSTR("\n"), CFSTR("\\n"), CFRangeMake(0, CFStringGetLength(json)), 0); | |
266 | ||
267 | CFStringInsert(json, 0, CFSTR("\"")); | |
268 | CFStringAppend(json, CFSTR("\"")); | |
269 | ||
62ca2b82 JF |
270 | return [reinterpret_cast<const NSString *>(json) autorelease]; |
271 | } | |
272 | ||
273 | @end | |
274 | ||
b21525c7 | 275 | @interface CYJSObject : NSDictionary { |
62ca2b82 JF |
276 | JSObjectRef object_; |
277 | JSContextRef context_; | |
278 | } | |
279 | ||
280 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context; | |
281 | ||
282 | - (NSUInteger) count; | |
283 | - (id) objectForKey:(id)key; | |
284 | - (NSEnumerator *) keyEnumerator; | |
285 | - (void) setObject:(id)object forKey:(id)key; | |
286 | - (void) removeObjectForKey:(id)key; | |
287 | ||
288 | @end | |
c1582939 | 289 | |
b21525c7 | 290 | @interface CYJSArray : NSArray { |
c1582939 JF |
291 | JSObjectRef object_; |
292 | JSContextRef context_; | |
293 | } | |
294 | ||
295 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context; | |
296 | ||
297 | - (NSUInteger) count; | |
298 | - (id) objectAtIndex:(NSUInteger)index; | |
299 | ||
300 | @end | |
301 | ||
62ca2b82 | 302 | JSContextRef JSGetContext() { |
ea2d184c | 303 | return Context_; |
62ca2b82 JF |
304 | } |
305 | ||
ea2d184c JF |
306 | void CYThrow(JSContextRef context, JSValueRef value); |
307 | ||
b21525c7 | 308 | id CYCastNSObject(JSContextRef context, JSObjectRef object) { |
ea2d184c | 309 | if (JSValueIsObjectOfClass(context, object, joc_)) |
c1582939 | 310 | return reinterpret_cast<id>(JSObjectGetPrivate(object)); |
ea2d184c JF |
311 | JSValueRef exception(NULL); |
312 | bool array(JSValueIsInstanceOfConstructor(context, object, Array_, &exception)); | |
313 | CYThrow(context, exception); | |
314 | if (array) | |
b21525c7 JF |
315 | return [[[CYJSArray alloc] initWithJSObject:object inContext:context] autorelease]; |
316 | return [[[CYJSObject alloc] initWithJSObject:object inContext:context] autorelease]; | |
62ca2b82 JF |
317 | } |
318 | ||
319 | CFStringRef CYCopyCFString(JSStringRef value) { | |
320 | return JSStringCopyCFString(kCFAllocatorDefault, value); | |
321 | } | |
322 | ||
ea2d184c | 323 | CFStringRef CYCopyCFString(JSContextRef context, JSValueRef value) { |
62ca2b82 | 324 | JSValueRef exception(NULL); |
ea2d184c JF |
325 | JSStringRef string(JSValueToStringCopy(context, value, &exception)); |
326 | CYThrow(context, exception); | |
62ca2b82 JF |
327 | CFStringRef object(CYCopyCFString(string)); |
328 | JSStringRelease(string); | |
329 | return object; | |
c1582939 JF |
330 | } |
331 | ||
62ca2b82 JF |
332 | NSString *CYCastNSString(JSStringRef value) { |
333 | return [reinterpret_cast<const NSString *>(CYCopyCFString(value)) autorelease]; | |
334 | } | |
335 | ||
ea2d184c JF |
336 | CFTypeRef CYCopyCFType(JSContextRef context, JSValueRef value) { |
337 | JSType type(JSValueGetType(context, value)); | |
c1582939 JF |
338 | |
339 | switch (type) { | |
340 | case kJSTypeUndefined: | |
62ca2b82 | 341 | return CFRetain([WebUndefined undefined]); |
c1582939 JF |
342 | break; |
343 | ||
344 | case kJSTypeNull: | |
345 | return nil; | |
346 | break; | |
347 | ||
348 | case kJSTypeBoolean: | |
ea2d184c | 349 | return CFRetain(JSValueToBoolean(context, value) ? kCFBooleanTrue : kCFBooleanFalse); |
c1582939 JF |
350 | break; |
351 | ||
352 | case kJSTypeNumber: { | |
62ca2b82 | 353 | JSValueRef exception(NULL); |
ea2d184c JF |
354 | double number(JSValueToNumber(context, value, &exception)); |
355 | CYThrow(context, exception); | |
c1582939 JF |
356 | return CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, &number); |
357 | } break; | |
358 | ||
62ca2b82 | 359 | case kJSTypeString: |
ea2d184c | 360 | return CYCopyCFString(context, value); |
62ca2b82 | 361 | break; |
c1582939 JF |
362 | |
363 | case kJSTypeObject: | |
b21525c7 | 364 | return CFRetain((CFTypeRef) CYCastNSObject(context, (JSObjectRef) value)); |
c1582939 JF |
365 | break; |
366 | ||
367 | default: | |
368 | _assert(false); | |
c1582939 JF |
369 | break; |
370 | } | |
371 | } | |
372 | ||
62ca2b82 JF |
373 | NSArray *CYCastNSArray(JSPropertyNameArrayRef names) { |
374 | size_t size(JSPropertyNameArrayGetCount(names)); | |
375 | NSMutableArray *array([NSMutableArray arrayWithCapacity:size]); | |
376 | for (size_t index(0); index != size; ++index) | |
377 | [array addObject:CYCastNSString(JSPropertyNameArrayGetNameAtIndex(names, index))]; | |
378 | return array; | |
379 | } | |
380 | ||
ea2d184c JF |
381 | id CYCastNSObject(JSContextRef context, JSValueRef value) { |
382 | const NSObject *object(reinterpret_cast<const NSObject *>(CYCopyCFType(context, value))); | |
c1582939 JF |
383 | return object == nil ? nil : [object autorelease]; |
384 | } | |
385 | ||
ea2d184c | 386 | void CYThrow(JSContextRef context, JSValueRef value) { |
62ca2b82 JF |
387 | if (value == NULL) |
388 | return; | |
ea2d184c | 389 | @throw CYCastNSObject(context, value); |
62ca2b82 JF |
390 | } |
391 | ||
ea2d184c JF |
392 | JSValueRef CYCastJSValue(JSContextRef context, id value) { |
393 | return value == nil ? JSValueMakeNull(context) : [value cy$JSValueInContext:context]; | |
62ca2b82 JF |
394 | } |
395 | ||
ea2d184c | 396 | JSStringRef CYCopyJSString(id value) { |
62ca2b82 JF |
397 | return JSStringCreateWithCFString(reinterpret_cast<CFStringRef>([value description])); |
398 | } | |
399 | ||
ea2d184c JF |
400 | JSStringRef CYCopyJSString(const char *value) { |
401 | return JSStringCreateWithUTF8CString(value); | |
402 | } | |
403 | ||
7ba62cfd JF |
404 | JSStringRef CYCopyJSString(JSStringRef value) { |
405 | return JSStringRetain(value); | |
406 | } | |
407 | ||
408 | // XXX: this is not a safe handle | |
409 | class CYString { | |
410 | private: | |
411 | JSStringRef string_; | |
412 | ||
413 | public: | |
414 | template <typename Type_> | |
415 | CYString(Type_ value) { | |
416 | string_ = CYCopyJSString(value); | |
417 | } | |
418 | ||
419 | ~CYString() { | |
420 | JSStringRelease(string_); | |
421 | } | |
422 | ||
423 | operator JSStringRef() const { | |
424 | return string_; | |
425 | } | |
426 | }; | |
427 | ||
428 | void CYThrow(JSContextRef context, id error, JSValueRef *exception) { | |
429 | *exception = CYCastJSValue(context, error); | |
430 | } | |
431 | ||
b21525c7 | 432 | @implementation CYJSObject |
62ca2b82 JF |
433 | |
434 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { | |
435 | if ((self = [super init]) != nil) { | |
436 | object_ = object; | |
437 | context_ = context; | |
438 | } return self; | |
439 | } | |
440 | ||
441 | - (NSUInteger) count { | |
442 | JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_)); | |
443 | size_t size(JSPropertyNameArrayGetCount(names)); | |
444 | JSPropertyNameArrayRelease(names); | |
445 | return size; | |
446 | } | |
447 | ||
448 | - (id) objectForKey:(id)key { | |
449 | JSValueRef exception(NULL); | |
7ba62cfd | 450 | JSValueRef value(JSObjectGetProperty(context_, object_, CYString(key), &exception)); |
62ca2b82 JF |
451 | CYThrow(context_, exception); |
452 | return CYCastNSObject(context_, value); | |
453 | } | |
454 | ||
455 | - (NSEnumerator *) keyEnumerator { | |
456 | JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_)); | |
457 | NSEnumerator *enumerator([CYCastNSArray(names) objectEnumerator]); | |
458 | JSPropertyNameArrayRelease(names); | |
459 | return enumerator; | |
460 | } | |
461 | ||
462 | - (void) setObject:(id)object forKey:(id)key { | |
463 | JSValueRef exception(NULL); | |
7ba62cfd | 464 | JSObjectSetProperty(context_, object_, CYString(key), CYCastJSValue(context_, object), kJSPropertyAttributeNone, &exception); |
62ca2b82 JF |
465 | CYThrow(context_, exception); |
466 | } | |
467 | ||
468 | - (void) removeObjectForKey:(id)key { | |
469 | JSValueRef exception(NULL); | |
62ca2b82 | 470 | // XXX: this returns a bool |
7ba62cfd | 471 | JSObjectDeleteProperty(context_, object_, CYString(key), &exception); |
62ca2b82 JF |
472 | CYThrow(context_, exception); |
473 | } | |
474 | ||
475 | @end | |
476 | ||
b21525c7 | 477 | @implementation CYJSArray |
c1582939 JF |
478 | |
479 | - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { | |
480 | if ((self = [super init]) != nil) { | |
481 | object_ = object; | |
482 | context_ = context; | |
483 | } return self; | |
484 | } | |
485 | ||
486 | - (NSUInteger) count { | |
62ca2b82 JF |
487 | JSValueRef exception(NULL); |
488 | JSValueRef value(JSObjectGetProperty(context_, object_, length_, &exception)); | |
489 | CYThrow(context_, exception); | |
490 | double number(JSValueToNumber(context_, value, &exception)); | |
491 | CYThrow(context_, exception); | |
492 | return number; | |
c1582939 JF |
493 | } |
494 | ||
495 | - (id) objectAtIndex:(NSUInteger)index { | |
62ca2b82 JF |
496 | JSValueRef exception(NULL); |
497 | JSValueRef value(JSObjectGetPropertyAtIndex(context_, object_, index, &exception)); | |
498 | CYThrow(context_, exception); | |
499 | id object(CYCastNSObject(context_, value)); | |
c1582939 JF |
500 | return object == nil ? [NSNull null] : object; |
501 | } | |
502 | ||
503 | @end | |
504 | ||
ea2d184c JF |
505 | CFStringRef JSValueToJSONCopy(JSContextRef context, JSValueRef value) { |
506 | id object(CYCastNSObject(context, value)); | |
62ca2b82 | 507 | return reinterpret_cast<CFStringRef>([(object == nil ? @"null" : [object cy$toJSON]) retain]); |
c1582939 JF |
508 | } |
509 | ||
510 | static void OnData(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *value, void *info) { | |
511 | switch (type) { | |
512 | case kCFSocketDataCallBack: | |
513 | CFDataRef data(reinterpret_cast<CFDataRef>(value)); | |
514 | Client *client(reinterpret_cast<Client *>(info)); | |
515 | ||
516 | if (client->message_ == NULL) | |
517 | client->message_ = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, TRUE); | |
518 | ||
519 | if (!CFHTTPMessageAppendBytes(client->message_, CFDataGetBytePtr(data), CFDataGetLength(data))) | |
520 | CFLog(kCFLogLevelError, CFSTR("CFHTTPMessageAppendBytes()")); | |
521 | else if (CFHTTPMessageIsHeaderComplete(client->message_)) { | |
522 | CFURLRef url(CFHTTPMessageCopyRequestURL(client->message_)); | |
523 | Boolean absolute; | |
524 | CFStringRef path(CFURLCopyStrictPath(url, &absolute)); | |
525 | CFRelease(client->message_); | |
526 | ||
527 | CFStringRef code(CFURLCreateStringByReplacingPercentEscapes(kCFAllocatorDefault, path, CFSTR(""))); | |
528 | CFRelease(path); | |
529 | ||
530 | JSStringRef script(JSStringCreateWithCFString(code)); | |
531 | CFRelease(code); | |
532 | ||
62ca2b82 | 533 | JSValueRef result(JSEvaluateScript(JSGetContext(), script, NULL, NULL, 0, NULL)); |
c1582939 JF |
534 | JSStringRelease(script); |
535 | ||
536 | CFHTTPMessageRef response(CFHTTPMessageCreateResponse(kCFAllocatorDefault, 200, NULL, kCFHTTPVersion1_1)); | |
537 | CFHTTPMessageSetHeaderFieldValue(response, CFSTR("Content-Type"), CFSTR("application/json; charset=utf-8")); | |
538 | ||
62ca2b82 | 539 | CFStringRef json(JSValueToJSONCopy(JSGetContext(), result)); |
c1582939 JF |
540 | CFDataRef body(CFStringCreateExternalRepresentation(kCFAllocatorDefault, json, kCFStringEncodingUTF8, NULL)); |
541 | CFRelease(json); | |
542 | ||
543 | CFStringRef length(CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%u"), CFDataGetLength(body))); | |
544 | CFHTTPMessageSetHeaderFieldValue(response, CFSTR("Content-Length"), length); | |
545 | CFRelease(length); | |
546 | ||
547 | CFHTTPMessageSetBody(response, body); | |
548 | CFRelease(body); | |
549 | ||
550 | CFDataRef serialized(CFHTTPMessageCopySerializedMessage(response)); | |
551 | CFRelease(response); | |
552 | ||
553 | CFSocketSendData(socket, NULL, serialized, 0); | |
554 | CFRelease(serialized); | |
555 | ||
556 | CFRelease(url); | |
557 | } | |
558 | break; | |
559 | } | |
560 | } | |
561 | ||
562 | static void OnAccept(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *value, void *info) { | |
563 | switch (type) { | |
564 | case kCFSocketAcceptCallBack: | |
565 | Client *client(new Client()); | |
566 | ||
567 | client->message_ = NULL; | |
568 | ||
569 | CFSocketContext context; | |
570 | context.version = 0; | |
571 | context.info = client; | |
572 | context.retain = NULL; | |
573 | context.release = NULL; | |
574 | context.copyDescription = NULL; | |
575 | ||
576 | client->socket_ = CFSocketCreateWithNative(kCFAllocatorDefault, *reinterpret_cast<const CFSocketNativeHandle *>(value), kCFSocketDataCallBack, &OnData, &context); | |
577 | ||
578 | CFRunLoopAddSource(CFRunLoopGetCurrent(), CFSocketCreateRunLoopSource(kCFAllocatorDefault, client->socket_, 0), kCFRunLoopDefaultMode); | |
579 | break; | |
580 | } | |
581 | } | |
582 | ||
ea2d184c | 583 | static JSValueRef joc_getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef *exception) { |
c1582939 JF |
584 | return NULL; |
585 | } | |
586 | ||
ea2d184c JF |
587 | typedef id jocData; |
588 | ||
04450da0 | 589 | struct ptrData { |
b21525c7 | 590 | apr_pool_t *pool_; |
04450da0 | 591 | void *value_; |
b21525c7 | 592 | sig::Type type_; |
04450da0 JF |
593 | }; |
594 | ||
595 | static void ptr_finalize(JSObjectRef object) { | |
596 | ptrData *data(reinterpret_cast<ptrData *>(JSObjectGetPrivate(object))); | |
b21525c7 | 597 | apr_pool_destroy(data->pool_); |
04450da0 JF |
598 | } |
599 | ||
ea2d184c JF |
600 | static void joc_finalize(JSObjectRef object) { |
601 | id data(reinterpret_cast<jocData>(JSObjectGetPrivate(object))); | |
602 | [data release]; | |
603 | } | |
604 | ||
605 | static JSValueRef obc_getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef *exception) { | |
c1582939 JF |
606 | NSString *name([(NSString *) JSStringCopyCFString(kCFAllocatorDefault, propertyName) autorelease]); |
607 | if (Class _class = NSClassFromString(name)) | |
ea2d184c | 608 | return JSObjectMake(context, joc_, [_class retain]); |
c1582939 JF |
609 | return NULL; |
610 | } | |
611 | ||
ea2d184c JF |
612 | void CYSetProperty(JSContextRef context, JSObjectRef object, const char *name, JSValueRef value) { |
613 | JSValueRef exception(NULL); | |
7ba62cfd | 614 | JSObjectSetProperty(context, object, CYString(name), value, kJSPropertyAttributeNone, &exception); |
ea2d184c JF |
615 | CYThrow(context, exception); |
616 | } | |
617 | ||
618 | struct ffiData { | |
619 | apr_pool_t *pool_; | |
620 | void (*function_)(); | |
621 | const char *type_; | |
622 | sig::Signature signature_; | |
623 | ffi_cif cif_; | |
624 | }; | |
625 | ||
7ba62cfd JF |
626 | char *CYPoolCString(apr_pool_t *pool, JSStringRef value) { |
627 | size_t size(JSStringGetMaximumUTF8CStringSize(value)); | |
b21525c7 | 628 | char *string(new(pool) char[size]); |
7ba62cfd JF |
629 | JSStringGetUTF8CString(value, string, size); |
630 | JSStringRelease(value); | |
631 | return string; | |
632 | } | |
633 | ||
b21525c7 JF |
634 | // XXX: this macro is dangerous |
635 | #define CYCastCString(context, value) ({ \ | |
636 | JSValueRef exception(NULL); \ | |
637 | JSStringRef string(JSValueToStringCopy(context, value, &exception)); \ | |
638 | CYThrow(context, exception); \ | |
639 | size_t size(JSStringGetMaximumUTF8CStringSize(string)); \ | |
640 | char *utf8(reinterpret_cast<char *>(alloca(size))); \ | |
641 | JSStringGetUTF8CString(string, utf8, size); \ | |
642 | JSStringRelease(string); \ | |
643 | utf8; \ | |
644 | }) | |
645 | ||
7ba62cfd JF |
646 | SEL CYCastSEL(JSContextRef context, JSValueRef value) { |
647 | if (JSValueIsNull(context, value)) | |
648 | return NULL; | |
649 | else if (JSValueIsObjectOfClass(context, value, sel_)) | |
650 | return reinterpret_cast<SEL>(JSObjectGetPrivate((JSObjectRef) value)); | |
b21525c7 JF |
651 | else |
652 | return sel_registerName(CYCastCString(context, value)); | |
653 | } | |
654 | ||
655 | void *CYCastPointer(JSContextRef context, JSValueRef value) { | |
656 | switch (JSValueGetType(context, value)) { | |
657 | case kJSTypeNull: | |
658 | return NULL; | |
659 | break; | |
660 | ||
661 | case kJSTypeString: | |
662 | return dlsym(RTLD_DEFAULT, CYCastCString(context, value)); | |
663 | break; | |
664 | ||
665 | case kJSTypeObject: | |
666 | if (JSValueIsObjectOfClass(context, value, ptr_)) { | |
667 | ptrData *data(reinterpret_cast<ptrData *>(JSObjectGetPrivate((JSObjectRef) value))); | |
668 | return data->value_; | |
669 | } | |
670 | default: | |
671 | JSValueRef exception(NULL); | |
672 | double number(JSValueToNumber(context, value, &exception)); | |
673 | CYThrow(context, exception); | |
674 | return reinterpret_cast<void *>(static_cast<uintptr_t>(number)); | |
675 | break; | |
7ba62cfd JF |
676 | } |
677 | } | |
678 | ||
43cb3d68 | 679 | void CYPoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, void *data, JSValueRef value) { |
ea2d184c JF |
680 | switch (type->primitive) { |
681 | case sig::boolean_P: | |
682 | *reinterpret_cast<bool *>(data) = JSValueToBoolean(context, value); | |
683 | break; | |
684 | ||
43cb3d68 | 685 | #define CYPoolFFI_(primitive, native) \ |
ea2d184c | 686 | case sig::primitive ## _P: { \ |
7ba62cfd JF |
687 | JSValueRef exception(NULL); \ |
688 | double number(JSValueToNumber(context, value, &exception)); \ | |
689 | CYThrow(context, exception); \ | |
690 | *reinterpret_cast<native *>(data) = number; \ | |
ea2d184c JF |
691 | } break; |
692 | ||
43cb3d68 JF |
693 | CYPoolFFI_(uchar, unsigned char) |
694 | CYPoolFFI_(char, char) | |
695 | CYPoolFFI_(ushort, unsigned short) | |
696 | CYPoolFFI_(short, short) | |
697 | CYPoolFFI_(ulong, unsigned long) | |
698 | CYPoolFFI_(long, long) | |
699 | CYPoolFFI_(uint, unsigned int) | |
700 | CYPoolFFI_(int, int) | |
701 | CYPoolFFI_(ulonglong, unsigned long long) | |
702 | CYPoolFFI_(longlong, long long) | |
703 | CYPoolFFI_(float, float) | |
704 | CYPoolFFI_(double, double) | |
ea2d184c JF |
705 | |
706 | case sig::object_P: | |
707 | case sig::typename_P: | |
7ba62cfd JF |
708 | *reinterpret_cast<id *>(data) = CYCastNSObject(context, value); |
709 | break; | |
710 | ||
ea2d184c | 711 | case sig::selector_P: |
7ba62cfd JF |
712 | *reinterpret_cast<SEL *>(data) = CYCastSEL(context, value); |
713 | break; | |
ea2d184c | 714 | |
b21525c7 JF |
715 | case sig::pointer_P: |
716 | *reinterpret_cast<void **>(data) = CYCastPointer(context, value); | |
717 | break; | |
ea2d184c | 718 | |
7ba62cfd JF |
719 | case sig::string_P: { |
720 | JSValueRef exception(NULL); | |
721 | JSStringRef string(JSValueToStringCopy(context, value, &exception)); | |
722 | CYThrow(context, exception); | |
723 | size_t size(JSStringGetMaximumUTF8CStringSize(string)); | |
b21525c7 | 724 | char *utf8(new(pool) char[size]); |
7ba62cfd JF |
725 | JSStringGetUTF8CString(string, utf8, size); |
726 | JSStringRelease(string); | |
727 | *reinterpret_cast<char **>(data) = utf8; | |
728 | } break; | |
729 | ||
ea2d184c JF |
730 | case sig::struct_P: |
731 | goto fail; | |
732 | ||
733 | case sig::void_P: | |
734 | break; | |
735 | ||
736 | default: fail: | |
43cb3d68 | 737 | NSLog(@"CYPoolFFI(%c)\n", type->primitive); |
ea2d184c JF |
738 | _assert(false); |
739 | } | |
740 | } | |
741 | ||
43cb3d68 | 742 | JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, void *data) { |
ea2d184c JF |
743 | JSValueRef value; |
744 | ||
745 | switch (type->primitive) { | |
746 | case sig::boolean_P: | |
747 | value = JSValueMakeBoolean(context, *reinterpret_cast<bool *>(data)); | |
748 | break; | |
749 | ||
750 | #define CYFromFFI_(primitive, native) \ | |
751 | case sig::primitive ## _P: \ | |
752 | value = JSValueMakeNumber(context, *reinterpret_cast<native *>(data)); \ | |
753 | break; | |
754 | ||
755 | CYFromFFI_(uchar, unsigned char) | |
756 | CYFromFFI_(char, char) | |
757 | CYFromFFI_(ushort, unsigned short) | |
758 | CYFromFFI_(short, short) | |
759 | CYFromFFI_(ulong, unsigned long) | |
760 | CYFromFFI_(long, long) | |
761 | CYFromFFI_(uint, unsigned int) | |
762 | CYFromFFI_(int, int) | |
763 | CYFromFFI_(ulonglong, unsigned long long) | |
764 | CYFromFFI_(longlong, long long) | |
765 | CYFromFFI_(float, float) | |
766 | CYFromFFI_(double, double) | |
767 | ||
768 | case sig::object_P: | |
769 | case sig::typename_P: { | |
770 | value = CYCastJSValue(context, *reinterpret_cast<id *>(data)); | |
771 | } break; | |
772 | ||
7ba62cfd JF |
773 | case sig::selector_P: { |
774 | SEL sel(*reinterpret_cast<SEL *>(data)); | |
775 | value = sel == NULL ? JSValueMakeNull(context) : JSObjectMake(context, sel_, sel); | |
776 | } break; | |
777 | ||
778 | case sig::pointer_P: { | |
04450da0 | 779 | if (void *pointer = *reinterpret_cast<void **>(data)) { |
b21525c7 JF |
780 | apr_pool_t *pool; |
781 | apr_pool_create(&pool, NULL); | |
782 | ptrData *data(new(pool) ptrData()); | |
783 | data->pool_ = pool; | |
04450da0 JF |
784 | data->value_ = pointer; |
785 | value = JSObjectMake(context, ptr_, data); | |
786 | } else value = JSValueMakeNull(context); | |
7ba62cfd | 787 | } break; |
ea2d184c JF |
788 | |
789 | case sig::string_P: { | |
790 | char *utf8(*reinterpret_cast<char **>(data)); | |
7ba62cfd | 791 | value = utf8 == NULL ? JSValueMakeNull(context) : JSValueMakeString(context, CYString(utf8)); |
ea2d184c JF |
792 | } break; |
793 | ||
794 | case sig::struct_P: | |
795 | goto fail; | |
796 | ||
797 | case sig::void_P: | |
798 | value = NULL; | |
799 | break; | |
800 | ||
801 | default: fail: | |
802 | NSLog(@"CYFromFFI(%c)\n", type->primitive); | |
803 | _assert(false); | |
804 | } | |
805 | ||
806 | return value; | |
807 | } | |
808 | ||
7ba62cfd JF |
809 | class CYPool { |
810 | private: | |
811 | apr_pool_t *pool_; | |
ea2d184c | 812 | |
7ba62cfd JF |
813 | public: |
814 | CYPool() { | |
815 | apr_pool_create(&pool_, NULL); | |
816 | } | |
ea2d184c | 817 | |
7ba62cfd JF |
818 | ~CYPool() { |
819 | apr_pool_destroy(pool_); | |
820 | } | |
ea2d184c | 821 | |
7ba62cfd JF |
822 | operator apr_pool_t *() const { |
823 | return pool_; | |
824 | } | |
825 | }; | |
826 | ||
827 | static JSValueRef CYCallFunction(JSContextRef context, size_t count, const JSValueRef *arguments, JSValueRef *exception, sig::Signature *signature, ffi_cif *cif, void (*function)()) { | |
828 | @try { | |
85a33bf5 JF |
829 | if (count != signature->count - 1) |
830 | [NSException raise:NSInvalidArgumentException format:@"incorrect number of arguments to ffi function"]; | |
831 | ||
7ba62cfd JF |
832 | CYPool pool; |
833 | void *values[count]; | |
ea2d184c | 834 | |
7ba62cfd JF |
835 | for (unsigned index(0); index != count; ++index) { |
836 | sig::Element *element(&signature->elements[index + 1]); | |
b21525c7 | 837 | values[index] = new(pool) uint8_t[cif->arg_types[index]->size]; |
43cb3d68 | 838 | CYPoolFFI(pool, context, element->type, values[index], arguments[index]); |
7ba62cfd | 839 | } |
ea2d184c | 840 | |
7ba62cfd JF |
841 | uint8_t value[cif->rtype->size]; |
842 | ffi_call(cif, function, value, values); | |
843 | ||
43cb3d68 | 844 | return CYFromFFI(context, signature->elements[0].type, value); |
7ba62cfd JF |
845 | } @catch (id error) { |
846 | CYThrow(context, error, exception); | |
847 | return NULL; | |
ea2d184c | 848 | } |
7ba62cfd | 849 | } |
ea2d184c | 850 | |
04450da0 JF |
851 | bool stret(ffi_type *ffi_type) { |
852 | return ffi_type->type == FFI_TYPE_STRUCT && ( | |
853 | ffi_type->size > OBJC_MAX_STRUCT_BY_VALUE || | |
854 | struct_forward_array[ffi_type->size] != 0 | |
855 | ); | |
856 | } | |
857 | ||
7ba62cfd | 858 | static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { _pooled |
7ba62cfd | 859 | const char *type; |
ea2d184c JF |
860 | |
861 | @try { | |
85a33bf5 JF |
862 | if (count < 2) |
863 | [NSException raise:NSInvalidArgumentException format:@"too few arguments to objc_msgSend"]; | |
864 | ||
7ba62cfd JF |
865 | id self(CYCastNSObject(context, arguments[0])); |
866 | if (self == nil) | |
867 | return JSValueMakeNull(context); | |
7ba62cfd | 868 | |
85a33bf5 | 869 | SEL _cmd(CYCastSEL(context, arguments[1])); |
7ba62cfd | 870 | NSMethodSignature *method([self methodSignatureForSelector:_cmd]); |
85a33bf5 JF |
871 | if (method == nil) |
872 | [NSException raise:NSInvalidArgumentException format:@"unrecognized selector %s sent to object %p", sel_getName(_cmd), self]; | |
7ba62cfd JF |
873 | |
874 | type = [[method _typeString] UTF8String]; | |
ea2d184c | 875 | } @catch (id error) { |
7ba62cfd JF |
876 | CYThrow(context, error, exception); |
877 | return NULL; | |
ea2d184c JF |
878 | } |
879 | ||
7ba62cfd JF |
880 | CYPool pool; |
881 | ||
882 | sig::Signature signature; | |
883 | sig::Parse(pool, &signature, type); | |
ea2d184c | 884 | |
7ba62cfd | 885 | ffi_cif cif; |
b21525c7 | 886 | sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); |
7ba62cfd | 887 | |
04450da0 | 888 | void (*function)() = stret(cif.rtype) ? reinterpret_cast<void (*)()>(&objc_msgSend_stret) : reinterpret_cast<void (*)()>(&objc_msgSend); |
7ba62cfd JF |
889 | return CYCallFunction(context, count, arguments, exception, &signature, &cif, function); |
890 | } | |
891 | ||
892 | static JSValueRef ffi_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
893 | ffiData *data(reinterpret_cast<ffiData *>(JSObjectGetPrivate(object))); | |
7ba62cfd | 894 | return CYCallFunction(context, count, arguments, exception, &data->signature_, &data->cif_, data->function_); |
ea2d184c JF |
895 | } |
896 | ||
897 | static void ffi_finalize(JSObjectRef object) { | |
898 | ffiData *data(reinterpret_cast<ffiData *>(JSObjectGetPrivate(object))); | |
899 | apr_pool_destroy(data->pool_); | |
900 | } | |
901 | ||
b21525c7 | 902 | JSObjectRef CYMakeFunction(JSContextRef context, void (*function)(), const char *type) { |
ea2d184c JF |
903 | apr_pool_t *pool; |
904 | apr_pool_create(&pool, NULL); | |
905 | ||
b21525c7 | 906 | ffiData *data(new(pool) ffiData()); |
ea2d184c JF |
907 | |
908 | data->pool_ = pool; | |
909 | data->function_ = function; | |
910 | data->type_ = apr_pstrdup(pool, type); | |
911 | ||
912 | sig::Parse(pool, &data->signature_, type); | |
b21525c7 | 913 | sig::sig_ffi_cif(pool, &sig::ObjectiveC, &data->signature_, &data->cif_); |
ea2d184c | 914 | |
b21525c7 JF |
915 | return JSObjectMake(context, ffi_, data); |
916 | } | |
917 | ||
918 | JSObjectRef ffi(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { | |
919 | @try { | |
920 | if (count != 2) | |
921 | [NSException raise:NSInvalidArgumentException format:@"incorrect number of arguments to ffi constructor"]; | |
922 | void (*function)() = reinterpret_cast<void (*)()>(CYCastPointer(context, arguments[0])); | |
923 | const char *type(CYCastCString(context, arguments[1])); | |
924 | return CYMakeFunction(context, function, type); | |
925 | } @catch (id error) { | |
926 | CYThrow(context, error, exception); | |
927 | return NULL; | |
928 | } | |
ea2d184c JF |
929 | } |
930 | ||
04450da0 JF |
931 | JSValueRef ptr_getProperty_value(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef *exception) { |
932 | ptrData *data(reinterpret_cast<ptrData *>(JSObjectGetPrivate(object))); | |
933 | return JSValueMakeNumber(context, reinterpret_cast<uintptr_t>(data->value_)); | |
934 | } | |
935 | ||
936 | static JSStaticValue ptr_staticValues[2] = { | |
937 | {"value", &ptr_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete}, | |
938 | {NULL, NULL, NULL, 0} | |
939 | }; | |
940 | ||
7ba62cfd | 941 | MSInitialize { _pooled |
ea2d184c JF |
942 | apr_initialize(); |
943 | ||
c1582939 JF |
944 | NSCFBoolean_ = objc_getClass("NSCFBoolean"); |
945 | ||
946 | pid_t pid(getpid()); | |
947 | ||
948 | struct sockaddr_in address; | |
949 | address.sin_len = sizeof(address); | |
950 | address.sin_family = AF_INET; | |
951 | address.sin_addr.s_addr = INADDR_ANY; | |
952 | address.sin_port = htons(10000 + pid); | |
953 | ||
954 | CFDataRef data(CFDataCreate(kCFAllocatorDefault, reinterpret_cast<UInt8 *>(&address), sizeof(address))); | |
955 | ||
956 | CFSocketSignature signature; | |
957 | signature.protocolFamily = AF_INET; | |
958 | signature.socketType = SOCK_STREAM; | |
959 | signature.protocol = IPPROTO_TCP; | |
960 | signature.address = data; | |
961 | ||
962 | CFSocketRef socket(CFSocketCreateWithSocketSignature(kCFAllocatorDefault, &signature, kCFSocketAcceptCallBack, &OnAccept, NULL)); | |
963 | CFRunLoopAddSource(CFRunLoopGetCurrent(), CFSocketCreateRunLoopSource(kCFAllocatorDefault, socket, 0), kCFRunLoopDefaultMode); | |
964 | ||
965 | JSClassDefinition definition; | |
966 | ||
967 | definition = kJSClassDefinitionEmpty; | |
968 | definition.getProperty = &obc_getProperty; | |
969 | JSClassRef obc(JSClassCreate(&definition)); | |
970 | ||
ea2d184c | 971 | definition = kJSClassDefinitionEmpty; |
7ba62cfd | 972 | definition.className = "ffi"; |
ea2d184c JF |
973 | definition.callAsFunction = &ffi_callAsFunction; |
974 | definition.finalize = &ffi_finalize; | |
975 | ffi_ = JSClassCreate(&definition); | |
976 | ||
c1582939 | 977 | definition = kJSClassDefinitionEmpty; |
7ba62cfd | 978 | definition.className = "ptr"; |
04450da0 JF |
979 | definition.staticValues = ptr_staticValues; |
980 | definition.finalize = &ptr_finalize; | |
7ba62cfd JF |
981 | ptr_ = JSClassCreate(&definition); |
982 | ||
983 | definition = kJSClassDefinitionEmpty; | |
984 | definition.className = "sel"; | |
985 | sel_ = JSClassCreate(&definition); | |
986 | ||
987 | definition = kJSClassDefinitionEmpty; | |
988 | definition.className = "joc"; | |
c1582939 | 989 | definition.getProperty = &joc_getProperty; |
ea2d184c | 990 | definition.finalize = &joc_finalize; |
c1582939 JF |
991 | joc_ = JSClassCreate(&definition); |
992 | ||
ea2d184c JF |
993 | JSContextRef context(JSGlobalContextCreate(obc)); |
994 | Context_ = context; | |
995 | ||
996 | JSObjectRef global(JSContextGetGlobalObject(context)); | |
c1582939 | 997 | |
b21525c7 JF |
998 | CYSetProperty(context, global, "ffi", JSObjectMakeConstructor(context, ffi_, &ffi)); |
999 | ||
7ba62cfd | 1000 | #define CYSetFunction_(name, type) \ |
b21525c7 | 1001 | CYSetProperty(context, global, #name, CYMakeFunction(context, reinterpret_cast<void (*)()>(&name), type)) |
7ba62cfd | 1002 | |
04450da0 JF |
1003 | CYSetFunction_(class_addIvar, "B#*LC*"); |
1004 | CYSetFunction_(class_addMethod, "B#:^?*"); | |
1005 | CYSetFunction_(class_addProtocol, "B#@"); | |
1006 | CYSetFunction_(class_conformsToProtocol, "B#@"); | |
1007 | CYSetFunction_(class_copyIvarList, "^^{objc_ivar=}#^I"); | |
1008 | CYSetFunction_(class_copyMethodList, "^^{objc_method=}#^I"); | |
1009 | CYSetFunction_(class_copyPropertyList, "^^{objc_property=}#^I"); | |
1010 | CYSetFunction_(class_copyProtocolList, "^@#^I"); | |
7ba62cfd | 1011 | CYSetFunction_(class_createInstance, "@#L"); |
04450da0 JF |
1012 | CYSetFunction_(class_getClassMethod, "^{objc_method=}#:"); |
1013 | CYSetFunction_(class_getClassVariable, "^{objc_ivar=}#*"); | |
1014 | CYSetFunction_(class_getInstanceMethod, "^{objc_method=}#:"); | |
7ba62cfd | 1015 | CYSetFunction_(class_getInstanceSize, "L#"); |
04450da0 | 1016 | CYSetFunction_(class_getInstanceVariable, "^{objc_ivar=}#*"); |
7ba62cfd | 1017 | CYSetFunction_(class_getIvarLayout, "*#"); |
04450da0 JF |
1018 | CYSetFunction_(class_getMethodImplementation, "^?#:"); |
1019 | CYSetFunction_(class_getMethodImplementation_stret, "^?#:"); | |
7ba62cfd | 1020 | CYSetFunction_(class_getName, "*#"); |
04450da0 | 1021 | CYSetFunction_(class_getProperty, "^{objc_property=}#*"); |
7ba62cfd JF |
1022 | CYSetFunction_(class_getSuperclass, "##"); |
1023 | CYSetFunction_(class_getVersion, "i#"); | |
04450da0 | 1024 | CYSetFunction_(class_getWeakIvarLayout, "*#"); |
7ba62cfd | 1025 | CYSetFunction_(class_isMetaClass, "B#"); |
04450da0 | 1026 | CYSetFunction_(class_replaceMethod, "^?#:^?*"); |
7ba62cfd | 1027 | CYSetFunction_(class_respondsToSelector, "B#:"); |
04450da0 | 1028 | CYSetFunction_(class_setIvarLayout, "v#*"); |
7ba62cfd JF |
1029 | CYSetFunction_(class_setSuperclass, "###"); |
1030 | CYSetFunction_(class_setVersion, "v#i"); | |
04450da0 JF |
1031 | CYSetFunction_(class_setWeakIvarLayout, "v#*"); |
1032 | CYSetFunction_(ivar_getName, "*^{objc_ivar=}"); | |
1033 | CYSetFunction_(ivar_getOffset, "i^{objc_ivar=}"); | |
1034 | CYSetFunction_(ivar_getTypeEncoding, "*^{objc_ivar=}"); | |
1035 | CYSetFunction_(method_copyArgumentType, "^c^{objc_method=}I"); | |
1036 | CYSetFunction_(method_copyReturnType, "^c^{objc_method=}"); | |
1037 | CYSetFunction_(method_exchangeImplementations, "v^{objc_method=}^{objc_method=}"); | |
1038 | CYSetFunction_(method_getArgumentType, "v^{objc_method=}I^cL"); | |
1039 | CYSetFunction_(method_getImplementation, "^?^{objc_method=}"); | |
1040 | CYSetFunction_(method_getName, ":^{objc_method=}"); | |
1041 | CYSetFunction_(method_getNumberOfArguments, "I^{objc_method=}"); | |
1042 | CYSetFunction_(method_getReturnType, "v^{objc_method=}^cL"); | |
1043 | CYSetFunction_(method_getTypeEncoding, "*^{objc_method=}"); | |
1044 | CYSetFunction_(method_setImplementation, "^?^{objc_method=}^?"); | |
7ba62cfd | 1045 | CYSetFunction_(objc_allocateClassPair, "##*L"); |
04450da0 JF |
1046 | CYSetFunction_(objc_copyProtocolList, "^@^I"); |
1047 | CYSetFunction_(objc_duplicateClass, "##*L"); | |
7ba62cfd | 1048 | CYSetFunction_(objc_getClass, "#*"); |
04450da0 | 1049 | CYSetFunction_(objc_getClassList, "i^#i"); |
7ba62cfd JF |
1050 | CYSetFunction_(objc_getFutureClass, "#*"); |
1051 | CYSetFunction_(objc_getMetaClass, "@*"); | |
04450da0 | 1052 | CYSetFunction_(objc_getProtocol, "@*"); |
7ba62cfd JF |
1053 | CYSetFunction_(objc_getRequiredClass, "@*"); |
1054 | CYSetFunction_(objc_lookUpClass, "@*"); | |
1055 | CYSetFunction_(objc_registerClassPair, "v#"); | |
1056 | CYSetFunction_(objc_setFutureClass, "v#*"); | |
1057 | CYSetFunction_(object_copy, "@@L"); | |
1058 | CYSetFunction_(object_dispose, "@@"); | |
1059 | CYSetFunction_(object_getClass, "#@"); | |
1060 | CYSetFunction_(object_getClassName, "*@"); | |
04450da0 JF |
1061 | CYSetFunction_(object_getIndexedIvars, "^v@"); |
1062 | CYSetFunction_(object_getInstanceVariable, "^{objc_ivar=}@*^^v"); | |
1063 | CYSetFunction_(object_getIvar, "@@^{objc_ivar=}"); | |
7ba62cfd | 1064 | CYSetFunction_(object_setClass, "#@#"); |
04450da0 JF |
1065 | CYSetFunction_(object_setInstanceVariable, "^{objc_ivar=}@*^v"); |
1066 | CYSetFunction_(object_setIvar, "v@^{objc_ivar=}@"); | |
1067 | CYSetFunction_(property_getAttributes, "*^{objc_property=}"); | |
1068 | CYSetFunction_(property_getName, "*^{objc_property=}"); | |
1069 | CYSetFunction_(protocol_conformsToProtocol, "B@@"); | |
1070 | CYSetFunction_(protocol_copyMethodDescriptionList, "^{objc_method_description=:*}@BB^I"); | |
1071 | CYSetFunction_(protocol_copyPropertyList, "^{objc_property=}@^I"); | |
1072 | CYSetFunction_(protocol_copyProtocolList, "^@@^I"); | |
1073 | CYSetFunction_(protocol_getMethodDescription, "{objc_method_description=:*}@:BB"); | |
1074 | CYSetFunction_(protocol_getName, "*@"); | |
1075 | CYSetFunction_(protocol_getProperty, "^{objc_property=}@*BB"); | |
1076 | CYSetFunction_(protocol_isEqual, "B@@"); | |
7ba62cfd JF |
1077 | CYSetFunction_(sel_getName, "*:"); |
1078 | CYSetFunction_(sel_getUid, ":*"); | |
1079 | CYSetFunction_(sel_isEqual, "B::"); | |
1080 | CYSetFunction_(sel_registerName, ":*"); | |
1081 | ||
1082 | CYSetProperty(context, global, "objc_msgSend", JSObjectMakeFunctionWithCallback(context, CYString("objc_msgSend"), &$objc_msgSend)); | |
1083 | ||
1084 | CYSetProperty(context, global, "YES", JSValueMakeBoolean(context, true)); | |
85a33bf5 | 1085 | CYSetProperty(context, global, "NO", JSValueMakeBoolean(context, false)); |
7ba62cfd | 1086 | CYSetProperty(context, global, "nil", JSValueMakeNull(context)); |
c1582939 | 1087 | |
62ca2b82 JF |
1088 | name_ = JSStringCreateWithUTF8CString("name"); |
1089 | message_ = JSStringCreateWithUTF8CString("message"); | |
c1582939 JF |
1090 | length_ = JSStringCreateWithUTF8CString("length"); |
1091 | ||
62ca2b82 | 1092 | JSValueRef exception(NULL); |
7ba62cfd | 1093 | JSValueRef value(JSObjectGetProperty(JSGetContext(), global, CYString("Array"), &exception)); |
ea2d184c | 1094 | CYThrow(context, exception); |
62ca2b82 | 1095 | Array_ = JSValueToObject(JSGetContext(), value, &exception); |
ea2d184c | 1096 | CYThrow(context, exception); |
c1582939 | 1097 | } |