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