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