]> git.saurik.com Git - cycript.git/blame - ObjectiveC/Library.mm
Use Array.prototype, not Array, to look up functions.
[cycript.git] / ObjectiveC / Library.mm
CommitLineData
b3378a02
JF
1/* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2010 Jay Freeman (saurik)
e91fbe93
JF
3*/
4
b3378a02 5/* GNU Lesser General Public License, Version 3 {{{ */
e91fbe93 6/*
b3378a02
JF
7 * Cycript is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
e91fbe93 11 *
b3378a02
JF
12 * Cycript is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
e91fbe93 16 *
b3378a02
JF
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
19**/
e91fbe93
JF
20/* }}} */
21
aabea98c 22#ifdef __APPLE__
37954781 23#include "Struct.hpp"
aabea98c 24#endif
3c1c3635 25
37954781 26#include <Foundation/Foundation.h>
3c1c3635 27
2fd4c9a9
JF
28#include "ObjectiveC/Internal.hpp"
29
8ad1528b 30#include <objc/objc-api.h>
aabea98c 31
37954781 32#include "cycript.hpp"
3c1c3635 33
37954781 34#include "ObjectiveC/Internal.hpp"
3c1c3635 35
37954781
JF
36#ifdef __APPLE__
37#include <CoreFoundation/CoreFoundation.h>
37954781
JF
38#include <JavaScriptCore/JSStringRefCF.h>
39#include <WebKit/WebScriptObject.h>
0939f869 40#include <objc/runtime.h>
37954781 41#endif
3c1c3635 42
37954781
JF
43#include "Error.hpp"
44#include "JavaScript.hpp"
45#include "String.hpp"
0f3a21d5 46#include "Execute.hpp"
3c1c3635 47
aabea98c 48#include <cmath>
37954781 49#include <map>
3c1c3635 50
37954781
JF
51#define CYObjectiveTry_(context) { \
52 JSContextRef context_(context); \
53 try
54#define CYObjectiveTry { \
55 try
56#define CYObjectiveCatch \
57 catch (const CYException &error) { \
58 @throw CYCastNSObject(NULL, context_, error.CastJSValue(context_)); \
59 } \
3c1c3635
JF
60}
61
37954781
JF
62#define CYPoolTry { \
63 id _saved(nil); \
64 NSAutoreleasePool *_pool([[NSAutoreleasePool alloc] init]); \
65 @try
66#define CYPoolCatch(value) \
67 @catch (NSException *error) { \
68 _saved = [error retain]; \
69 throw CYJSError(context, CYCastJSValue(context, error)); \
70 return value; \
71 } @finally { \
72 [_pool release]; \
73 if (_saved != nil) \
74 [_saved autorelease]; \
75 } \
3c1c3635
JF
76}
77
b64ab4da
JF
78#define CYSadTry { \
79 @try
ea840434 80#define CYSadCatch(value) \
b64ab4da
JF
81 @catch (NSException *error ) { \
82 throw CYJSError(context, CYCastJSValue(context, error)); \
e0ddeff1 83 } return value; \
b64ab4da
JF
84}
85
37954781
JF
86#ifndef __APPLE__
87#define class_getSuperclass GSObjCSuper
aabea98c
JF
88#define class_getInstanceVariable GSCGetInstanceVariableDefinition
89#define class_getName GSNameFromClass
90
91#define class_removeMethods(cls, list) GSRemoveMethodList(cls, list, YES)
92
93#define ivar_getName(ivar) ((ivar)->ivar_name)
94#define ivar_getOffset(ivar) ((ivar)->ivar_offset)
95#define ivar_getTypeEncoding(ivar) ((ivar)->ivar_type)
96
97#define method_getName(method) ((method)->method_name)
98#define method_getImplementation(method) ((method)->method_imp)
99#define method_getTypeEncoding(method) ((method)->method_types)
100#define method_setImplementation(method, imp) ((void) ((method)->method_imp = (imp)))
101
ba4fa42f
JF
102#undef objc_getClass
103#define objc_getClass GSClassFromName
104
aabea98c
JF
105#define objc_getProtocol GSProtocolFromName
106
37954781 107#define object_getClass GSObjCClass
aabea98c
JF
108
109#define object_getInstanceVariable(object, name, value) ({ \
110 objc_ivar *ivar(class_getInstanceVariable(object_getClass(object), name)); \
9fe95800 111 _assert(value != NULL); \
ba4fa42f
JF
112 if (ivar != NULL) \
113 GSObjCGetVariable(object, ivar_getOffset(ivar), sizeof(void *), value); \
aabea98c
JF
114 ivar; \
115})
116
117#define object_setIvar(object, ivar, value) ({ \
118 void *data = (value); \
119 GSObjCSetVariable(object, ivar_getOffset(ivar), sizeof(void *), &data); \
120})
121
122#define protocol_getName(protocol) [(protocol) name]
37954781 123#endif
3c1c3635 124
37954781 125JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, Class super, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception);
3c1c3635 126
3c1c3635
JF
127/* Objective-C Pool Release {{{ */
128apr_status_t CYPoolRelease_(void *data) {
129 id object(reinterpret_cast<id>(data));
130 [object release];
131 return APR_SUCCESS;
132}
133
134id CYPoolRelease_(apr_pool_t *pool, id object) {
135 if (object == nil)
136 return nil;
137 else if (pool == NULL)
138 return [object autorelease];
139 else {
140 apr_pool_cleanup_register(pool, object, &CYPoolRelease_, &apr_pool_cleanup_null);
141 return object;
142 }
143}
144
145template <typename Type_>
146Type_ CYPoolRelease(apr_pool_t *pool, Type_ object) {
147 return (Type_) CYPoolRelease_(pool, (id) object);
148}
149/* }}} */
150/* Objective-C Strings {{{ */
151const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, NSString *value) {
152 if (pool == NULL)
153 return [value UTF8String];
154 else {
155 size_t size([value maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1);
156 char *string(new(pool) char[size]);
157 if (![value getCString:string maxLength:size encoding:NSUTF8StringEncoding])
37954781 158 throw CYJSError(context, "[NSString getCString:maxLength:encoding:] == NO");
3c1c3635
JF
159 return string;
160 }
161}
162
aabea98c 163JSStringRef CYCopyJSString(JSContextRef context, NSString *value) {
3c1c3635
JF
164#ifdef __APPLE__
165 return JSStringCreateWithCFString(reinterpret_cast<CFStringRef>(value));
166#else
167 CYPool pool;
aabea98c 168 return CYCopyJSString(CYPoolCString(pool, context, value));
3c1c3635
JF
169#endif
170}
171
8665da0c 172JSStringRef CYCopyJSString(JSContextRef context, NSObject *value) {
3c1c3635
JF
173 if (value == nil)
174 return NULL;
175 // XXX: this definition scares me; is anyone using this?!
176 NSString *string([value description]);
8665da0c 177 return CYCopyJSString(context, string);
3c1c3635
JF
178}
179
180NSString *CYCopyNSString(const CYUTF8String &value) {
181#ifdef __APPLE__
182 return (NSString *) CFStringCreateWithBytes(kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(value.data), value.size, kCFStringEncodingUTF8, true);
183#else
184 return [[NSString alloc] initWithBytes:value.data length:value.size encoding:NSUTF8StringEncoding];
185#endif
186}
187
aabea98c 188NSString *CYCopyNSString(JSContextRef context, JSStringRef value) {
3c1c3635
JF
189#ifdef __APPLE__
190 return (NSString *) JSStringCopyCFString(kCFAllocatorDefault, value);
191#else
aabea98c
JF
192 CYPool pool;
193 return CYCopyNSString(CYPoolUTF8String(pool, context, value));
3c1c3635
JF
194#endif
195}
196
197NSString *CYCopyNSString(JSContextRef context, JSValueRef value) {
aabea98c 198 return CYCopyNSString(context, CYJSString(context, value));
3c1c3635
JF
199}
200
201NSString *CYCastNSString(apr_pool_t *pool, const CYUTF8String &value) {
202 return CYPoolRelease(pool, CYCopyNSString(value));
203}
204
205NSString *CYCastNSString(apr_pool_t *pool, SEL sel) {
206 const char *name(sel_getName(sel));
207 return CYPoolRelease(pool, CYCopyNSString(CYUTF8String(name, strlen(name))));
208}
209
aabea98c
JF
210NSString *CYCastNSString(apr_pool_t *pool, JSContextRef context, JSStringRef value) {
211 return CYPoolRelease(pool, CYCopyNSString(context, value));
3c1c3635 212}
c239b9f8 213
3c1c3635
JF
214CYUTF8String CYCastUTF8String(NSString *value) {
215 NSData *data([value dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO]);
216 return CYUTF8String(reinterpret_cast<const char *>([data bytes]), [data length]);
217}
218/* }}} */
ff783f8c 219
ba4fa42f
JF
220JSValueRef CYCastJSValue(JSContextRef context, NSObject *value);
221
3c1c3635
JF
222void CYThrow(JSContextRef context, NSException *error, JSValueRef *exception) {
223 if (exception == NULL)
224 throw error;
225 *exception = CYCastJSValue(context, error);
226}
bce8339b 227
3c1c3635
JF
228size_t CYGetIndex(NSString *value) {
229 return CYGetIndex(CYCastUTF8String(value));
230}
ff783f8c 231
3c1c3635
JF
232bool CYGetOffset(apr_pool_t *pool, JSContextRef context, NSString *value, ssize_t &index) {
233 return CYGetOffset(CYPoolCString(pool, context, value), index);
234}
ff783f8c 235
3c1c3635
JF
236static JSClassRef Instance_;
237static JSClassRef Internal_;
238static JSClassRef Message_;
239static JSClassRef Messages_;
240static JSClassRef Selector_;
6732f195 241static JSClassRef StringInstance_;
3c1c3635 242static JSClassRef Super_;
ff783f8c 243
3c1c3635 244static JSClassRef ObjectiveC_Classes_;
aabea98c
JF
245static JSClassRef ObjectiveC_Protocols_;
246
247#ifdef __APPLE__
3c1c3635
JF
248static JSClassRef ObjectiveC_Image_Classes_;
249static JSClassRef ObjectiveC_Images_;
aabea98c 250#endif
ff783f8c 251
3c1c3635
JF
252#ifdef __APPLE__
253static Class NSCFBoolean_;
254static Class NSCFType_;
aaa4a269 255static Class NSGenericDeallocHandler_;
b5dd57dc
JF
256static Class NSMessageBuilder_;
257static Class NSZombie_;
c074e774
JF
258#else
259static Class NSBoolNumber_;
3c1c3635 260#endif
ff783f8c 261
3c1c3635
JF
262static Class NSArray_;
263static Class NSDictionary_;
4cb8aa43 264static Class NSString_;
3c1c3635 265static Class Object_;
b24eb750 266
3c1c3635
JF
267static Type_privateData *Object_type;
268static Type_privateData *Selector_type;
7b184c00
JF
269
270Type_privateData *Instance::GetType() const {
3c1c3635 271 return Object_type;
7b184c00
JF
272}
273
274Type_privateData *Selector_privateData::GetType() const {
3c1c3635 275 return Selector_type;
7b184c00 276}
ff783f8c 277
4cb8aa43
JF
278static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception);
279
3c1c3635
JF
280JSValueRef CYGetClassPrototype(JSContextRef context, id self) {
281 if (self == nil)
498c3570 282 return CYGetCachedObject(context, CYJSString("Instance_prototype"));
bd17e6f3 283
b64ab4da
JF
284 JSObjectRef global(CYGetGlobalObject(context));
285 JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s)));
286
287 char label[32];
288 sprintf(label, "i%p", self);
289 CYJSString name(label);
bd17e6f3 290
b64ab4da
JF
291 JSValueRef value(CYGetProperty(context, cy, name));
292 if (!JSValueIsUndefined(context, value))
3c1c3635 293 return value;
bd17e6f3 294
3c1c3635
JF
295 JSClassRef _class(NULL);
296 JSValueRef prototype;
bd17e6f3 297
3c1c3635 298 if (self == NSArray_)
498c3570 299 prototype = CYGetCachedObject(context, CYJSString("Array_prototype"));
3c1c3635 300 else if (self == NSDictionary_)
498c3570 301 prototype = CYGetCachedObject(context, CYJSString("Object_prototype"));
6732f195
JF
302 else if (self == NSString_)
303 prototype = CYGetCachedObject(context, CYJSString("StringInstance_prototype"));
304 else
3c1c3635 305 prototype = CYGetClassPrototype(context, class_getSuperclass(self));
bd17e6f3 306
6732f195 307 JSObjectRef object(JSObjectMake(context, _class, NULL));
3c1c3635 308 JSObjectSetPrototype(context, object, prototype);
b64ab4da 309 CYSetProperty(context, cy, name, object);
4cb8aa43 310
3c1c3635 311 return object;
bd17e6f3
JF
312}
313
3c1c3635
JF
314JSObjectRef Messages::Make(JSContextRef context, Class _class, bool array) {
315 JSObjectRef value(JSObjectMake(context, Messages_, new Messages(_class)));
316 if (_class == NSArray_)
317 array = true;
318 if (Class super = class_getSuperclass(_class))
319 JSObjectSetPrototype(context, value, Messages::Make(context, super, array));
320 /*else if (array)
321 JSObjectSetPrototype(context, value, Array_prototype_);*/
322 return value;
323}
dc68b74c 324
3c1c3635
JF
325JSObjectRef Internal::Make(JSContextRef context, id object, JSObjectRef owner) {
326 return JSObjectMake(context, Internal_, new Internal(object, context, owner));
327}
f33b048a 328
3c1c3635
JF
329namespace cy {
330JSObjectRef Super::Make(JSContextRef context, id object, Class _class) {
331 JSObjectRef value(JSObjectMake(context, Super_, new Super(object, _class)));
332 return value;
333} }
f33b048a 334
3c1c3635
JF
335JSObjectRef Instance::Make(JSContextRef context, id object, Flags flags) {
336 JSObjectRef value(JSObjectMake(context, Instance_, new Instance(object, flags)));
337 JSObjectSetPrototype(context, value, CYGetClassPrototype(context, object_getClass(object)));
338 return value;
339}
4e39dc0b 340
3c1c3635
JF
341Instance::~Instance() {
342 if ((flags_ & Transient) == 0)
343 // XXX: does this handle background threads correctly?
344 // XXX: this simply does not work on the console because I'm stupid
345 [GetValue() performSelector:@selector(release) withObject:nil afterDelay:0];
346}
f33b048a 347
dc68b74c 348struct Message_privateData :
37954781 349 cy::Functor
dc68b74c
JF
350{
351 SEL sel_;
352
353 Message_privateData(SEL sel, const char *type, IMP value = NULL) :
37954781 354 cy::Functor(type, reinterpret_cast<void (*)()>(value)),
dc68b74c
JF
355 sel_(sel)
356 {
357 }
358};
359
f7c38a29 360JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient) {
2b52f27e
JF
361 Instance::Flags flags;
362
363 if (transient)
364 flags = Instance::Transient;
365 else {
366 flags = Instance::None;
478d4ed0 367 object = [object retain];
2b52f27e
JF
368 }
369
370 return Instance::Make(context, object, flags);
478d4ed0 371}
0c862573 372
107e3ed0 373@interface NSMethodSignature (Cycript)
7ba62cfd
JF
374- (NSString *) _typeString;
375@end
376
107e3ed0 377@interface NSObject (Cycript)
b4aa79af 378
61933e16 379- (JSValueRef) cy$JSValueInContext:(JSContextRef)context;
b4aa79af
JF
380- (JSType) cy$JSType;
381
382- (NSObject *) cy$toJSON:(NSString *)key;
383- (NSString *) cy$toCYON;
f33b048a 384- (NSString *) cy$toKey;
b4aa79af 385
7b184c00 386- (bool) cy$hasProperty:(NSString *)name;
cc103044
JF
387- (NSObject *) cy$getProperty:(NSString *)name;
388- (bool) cy$setProperty:(NSString *)name to:(NSObject *)value;
389- (bool) cy$deleteProperty:(NSString *)name;
8665da0c 390- (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context;
b4aa79af 391
3e264692
JF
392+ (bool) cy$hasImplicitProperties;
393
c1582939
JF
394@end
395
f7c38a29
JF
396@protocol Cycript
397- (JSValueRef) cy$JSValueInContext:(JSContextRef)context;
398@end
88c977fa 399
520c130f
JF
400NSString *CYCastNSCYON(id value) {
401 NSString *string;
402
403 if (value == nil)
404 string = @"nil";
405 else {
406 Class _class(object_getClass(value));
407 SEL sel(@selector(cy$toCYON));
408
409 if (objc_method *toCYON = class_getInstanceMethod(_class, sel))
410 string = reinterpret_cast<NSString *(*)(id, SEL)>(method_getImplementation(toCYON))(value, sel);
411 else if (objc_method *methodSignatureForSelector = class_getInstanceMethod(_class, @selector(methodSignatureForSelector:))) {
412 if (reinterpret_cast<NSMethodSignature *(*)(id, SEL, SEL)>(method_getImplementation(methodSignatureForSelector))(value, @selector(methodSignatureForSelector:), sel) != nil)
413 string = [value cy$toCYON];
414 else goto fail;
415 } else fail: {
b5dd57dc
JF
416 if (false);
417#ifdef __APPLE__
418 else if (value == NSZombie_)
520c130f
JF
419 string = @"_NSZombie_";
420 else if (_class == NSZombie_)
421 string = [NSString stringWithFormat:@"<_NSZombie_: %p>", value];
422 // XXX: frowny /in/ the pants
aaa4a269 423 else if (value == NSGenericDeallocHandler_ || value == NSMessageBuilder_ || value == Object_)
520c130f 424 string = nil;
b5dd57dc 425#endif
520c130f
JF
426 else
427 string = [NSString stringWithFormat:@"%@", value];
428 }
429
430 // XXX: frowny pants
431 if (string == nil)
432 string = @"undefined";
433 }
434
435 return string;
436}
520c130f 437
cbaa5f0f 438#ifdef __APPLE__
4afefdd9
JF
439struct PropertyAttributes {
440 CYPool pool_;
441
442 const char *name;
443
444 const char *variable;
445
446 const char *getter_;
447 const char *setter_;
448
449 bool readonly;
450 bool copy;
451 bool retain;
452 bool nonatomic;
453 bool dynamic;
454 bool weak;
455 bool garbage;
456
457 PropertyAttributes(objc_property_t property) :
458 variable(NULL),
459 getter_(NULL),
460 setter_(NULL),
461 readonly(false),
462 copy(false),
463 retain(false),
464 nonatomic(false),
465 dynamic(false),
466 weak(false),
467 garbage(false)
468 {
469 name = property_getName(property);
470 const char *attributes(property_getAttributes(property));
471
472 for (char *state, *token(apr_strtok(apr_pstrdup(pool_, attributes), ",", &state)); token != NULL; token = apr_strtok(NULL, ",", &state)) {
473 switch (*token) {
474 case 'R': readonly = true; break;
475 case 'C': copy = true; break;
476 case '&': retain = true; break;
477 case 'N': nonatomic = true; break;
478 case 'G': getter_ = token + 1; break;
479 case 'S': setter_ = token + 1; break;
480 case 'V': variable = token + 1; break;
481 }
482 }
483
484 /*if (variable == NULL) {
485 variable = property_getName(property);
486 size_t size(strlen(variable));
487 char *name(new(pool_) char[size + 2]);
488 name[0] = '_';
489 memcpy(name + 1, variable, size);
490 name[size + 1] = '\0';
491 variable = name;
492 }*/
493 }
494
495 const char *Getter() {
496 if (getter_ == NULL)
497 getter_ = apr_pstrdup(pool_, name);
498 return getter_;
499 }
500
501 const char *Setter() {
502 if (setter_ == NULL && !readonly) {
503 size_t length(strlen(name));
504
505 char *temp(new(pool_) char[length + 5]);
506 temp[0] = 's';
507 temp[1] = 'e';
508 temp[2] = 't';
509
3c1c3635
JF
510 if (length != 0) {
511 temp[3] = toupper(name[0]);
512 memcpy(temp + 4, name + 1, length - 1);
513 }
514
515 temp[length + 3] = ':';
516 temp[length + 4] = '\0';
517 setter_ = temp;
518 }
519
520 return setter_;
521 }
522
523};
524#endif
525
526#ifdef __APPLE__
527NSObject *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) {
528 return [(NSString *) CFCopyDescription((CFTypeRef) self) autorelease];
529}
530#endif
531
532#ifndef __APPLE__
533@interface CYWebUndefined : NSObject {
534}
535
536+ (CYWebUndefined *) undefined;
537
538@end
539
540@implementation CYWebUndefined
541
542+ (CYWebUndefined *) undefined {
543 static CYWebUndefined *instance_([[CYWebUndefined alloc] init]);
544 return instance_;
545}
546
547@end
548
549#define WebUndefined CYWebUndefined
550#endif
551
552/* Bridge: CYJSObject {{{ */
553@interface CYJSObject : NSMutableDictionary {
554 JSObjectRef object_;
555 JSContextRef context_;
556}
557
558- (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context;
559
560- (NSObject *) cy$toJSON:(NSString *)key;
561
562- (NSUInteger) count;
563- (id) objectForKey:(id)key;
564- (NSEnumerator *) keyEnumerator;
565- (void) setObject:(id)object forKey:(id)key;
566- (void) removeObjectForKey:(id)key;
567
568@end
569/* }}} */
570/* Bridge: CYJSArray {{{ */
571@interface CYJSArray : NSMutableArray {
572 JSObjectRef object_;
573 JSContextRef context_;
574}
575
576- (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context;
577
578- (NSUInteger) count;
579- (id) objectAtIndex:(NSUInteger)index;
580
581- (void) addObject:(id)anObject;
582- (void) insertObject:(id)anObject atIndex:(NSUInteger)index;
583- (void) removeLastObject;
584- (void) removeObjectAtIndex:(NSUInteger)index;
585- (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
586
587@end
588/* }}} */
4afefdd9 589
3c1c3635 590NSObject *CYCastNSObject_(apr_pool_t *pool, JSContextRef context, JSObjectRef object) {
498c3570 591 JSObjectRef Array(CYGetCachedObject(context, Array_s));
3c1c3635 592 JSValueRef exception(NULL);
498c3570 593 bool array(JSValueIsInstanceOfConstructor(context, object, Array, &exception));
3c1c3635
JF
594 CYThrow(context, exception);
595 id value(array ? [CYJSArray alloc] : [CYJSObject alloc]);
596 return CYPoolRelease(pool, [value initWithJSObject:object inContext:context]);
597}
4afefdd9 598
3c1c3635
JF
599NSObject *CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSObjectRef object) {
600 if (!JSValueIsObjectOfClass(context, object, Instance_))
601 return CYCastNSObject_(pool, context, object);
602 else {
603 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
604 return internal->GetValue();
4afefdd9 605 }
3c1c3635 606}
4afefdd9 607
3c1c3635
JF
608NSNumber *CYCopyNSNumber(JSContextRef context, JSValueRef value) {
609 return [[NSNumber alloc] initWithDouble:CYCastDouble(context, value)];
610}
4afefdd9 611
c074e774
JF
612#ifndef __APPLE__
613@interface NSBoolNumber : NSNumber {
614}
615@end
616#endif
617
3c1c3635
JF
618id CYNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value, bool cast) {
619 id object;
620 bool copy;
621
622 switch (JSType type = JSValueGetType(context, value)) {
623 case kJSTypeUndefined:
624 object = [WebUndefined undefined];
625 copy = false;
626 break;
627
628 case kJSTypeNull:
629 return NULL;
630 break;
631
632 case kJSTypeBoolean:
cbaa5f0f 633#ifdef __APPLE__
3c1c3635
JF
634 object = (id) (CYCastBool(context, value) ? kCFBooleanTrue : kCFBooleanFalse);
635 copy = false;
636#else
c074e774 637 object = [[NSBoolNumber alloc] initWithBool:CYCastBool(context, value)];
3c1c3635 638 copy = true;
b24eb750 639#endif
3c1c3635 640 break;
c239b9f8 641
3c1c3635
JF
642 case kJSTypeNumber:
643 object = CYCopyNSNumber(context, value);
644 copy = true;
645 break;
993f82f8 646
3c1c3635
JF
647 case kJSTypeString:
648 object = CYCopyNSString(context, value);
649 copy = true;
650 break;
993f82f8 651
3c1c3635
JF
652 case kJSTypeObject:
653 // XXX: this might could be more efficient
654 object = CYCastNSObject(pool, context, (JSObjectRef) value);
655 copy = false;
656 break;
993f82f8 657
3c1c3635 658 default:
37954781 659 throw CYJSError(context, "JSValueGetType() == 0x%x", type);
3c1c3635
JF
660 break;
661 }
993f82f8 662
3c1c3635
JF
663 if (cast != copy)
664 return object;
665 else if (copy)
666 return CYPoolRelease(pool, object);
667 else
668 return [object retain];
993f82f8
JF
669}
670
3c1c3635
JF
671NSObject *CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
672 return CYNSObject(pool, context, value, true);
673}
993f82f8 674
3c1c3635
JF
675NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
676 return CYNSObject(pool, context, value, false);
677}
993f82f8 678
365abb0a 679/* Bridge: NSArray {{{ */
107e3ed0 680@implementation NSArray (Cycript)
62ca2b82 681
b4aa79af 682- (NSString *) cy$toCYON {
c1582939
JF
683 NSMutableString *json([[[NSMutableString alloc] init] autorelease]);
684 [json appendString:@"["];
685
686 bool comma(false);
cbaa5f0f 687#ifdef __APPLE__
62ca2b82 688 for (id object in self) {
cbaa5f0f 689#else
cbaa5f0f 690 for (size_t index(0), count([self count]); index != count; ++index) {
af4272e6 691 id object([self objectAtIndex:index]);
cbaa5f0f 692#endif
c1582939
JF
693 if (comma)
694 [json appendString:@","];
695 else
696 comma = true;
7b184c00 697 if (object == nil || [object cy$JSType] != kJSTypeUndefined)
520c130f 698 [json appendString:CYCastNSCYON(object)];
6b8a9500
JF
699 else {
700 [json appendString:@","];
701 comma = false;
702 }
c1582939
JF
703 }
704
705 [json appendString:@"]"];
706 return json;
62ca2b82
JF
707}
708
7b184c00
JF
709- (bool) cy$hasProperty:(NSString *)name {
710 if ([name isEqualToString:@"length"])
711 return true;
712
520c130f 713 size_t index(CYGetIndex(name));
faf69207 714 if (index == _not(size_t) || index >= [self count])
7b184c00
JF
715 return [super cy$hasProperty:name];
716 else
717 return true;
718}
719
cc103044 720- (NSObject *) cy$getProperty:(NSString *)name {
cbaa5f0f
JF
721 if ([name isEqualToString:@"length"]) {
722 NSUInteger count([self count]);
723#ifdef __APPLE__
724 return [NSNumber numberWithUnsignedInteger:count];
725#else
726 return [NSNumber numberWithUnsignedInt:count];
727#endif
728 }
4afefdd9 729
520c130f 730 size_t index(CYGetIndex(name));
faf69207 731 if (index == _not(size_t) || index >= [self count])
cc103044
JF
732 return [super cy$getProperty:name];
733 else
734 return [self objectAtIndex:index];
735}
736
8665da0c
JF
737- (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context {
738 [super cy$getPropertyNames:names inContext:context];
d3760804
JF
739
740 for (size_t index(0), count([self count]); index != count; ++index) {
741 id object([self objectAtIndex:index]);
742 if (object == nil || [object cy$JSType] != kJSTypeUndefined) {
743 char name[32];
744 sprintf(name, "%zu", index);
745 JSPropertyNameAccumulatorAddName(names, CYJSString(name));
746 }
747 }
748}
749
334bdca2
JF
750+ (bool) cy$hasImplicitProperties {
751 return false;
752}
753
cc103044 754@end
365abb0a 755/* }}} */
c074e774
JF
756/* Bridge: NSBoolNumber {{{ */
757#ifndef __APPLE__
758@implementation NSBoolNumber (Cycript)
759
760- (JSType) cy$JSType {
761 return kJSTypeBoolean;
762}
763
764- (NSObject *) cy$toJSON:(NSString *)key {
765 return self;
766}
767
768- (NSString *) cy$toCYON {
769 return [self boolValue] ? @"true" : @"false";
770}
771
772- (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) {
773 return CYCastJSValue(context, (bool) [self boolValue]);
774} CYObjectiveCatch }
775
776@end
777#endif
778/* }}} */
365abb0a
JF
779/* Bridge: NSDictionary {{{ */
780@implementation NSDictionary (Cycript)
781
782- (NSString *) cy$toCYON {
783 NSMutableString *json([[[NSMutableString alloc] init] autorelease]);
784 [json appendString:@"{"];
785
786 bool comma(false);
cbaa5f0f 787#ifdef __APPLE__
0226d428 788 for (NSObject *key in self) {
cbaa5f0f
JF
789#else
790 NSEnumerator *keys([self keyEnumerator]);
0226d428 791 while (NSObject *key = [keys nextObject]) {
cbaa5f0f 792#endif
365abb0a
JF
793 if (comma)
794 [json appendString:@","];
795 else
796 comma = true;
797 [json appendString:[key cy$toKey]];
798 [json appendString:@":"];
799 NSObject *object([self objectForKey:key]);
520c130f 800 [json appendString:CYCastNSCYON(object)];
365abb0a 801 }
cc103044 802
365abb0a
JF
803 [json appendString:@"}"];
804 return json;
805}
806
807- (bool) cy$hasProperty:(NSString *)name {
808 return [self objectForKey:name] != nil;
809}
810
811- (NSObject *) cy$getProperty:(NSString *)name {
812 return [self objectForKey:name];
813}
814
8665da0c
JF
815- (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context {
816 [super cy$getPropertyNames:names inContext:context];
d3760804
JF
817
818#ifdef __APPLE__
0226d428 819 for (NSObject *key in self) {
d3760804
JF
820#else
821 NSEnumerator *keys([self keyEnumerator]);
0226d428 822 while (NSObject *key = [keys nextObject]) {
d3760804 823#endif
8665da0c 824 JSPropertyNameAccumulatorAddName(names, CYJSString(context, key));
d3760804
JF
825 }
826}
827
3e264692
JF
828+ (bool) cy$hasImplicitProperties {
829 return false;
830}
831
365abb0a
JF
832@end
833/* }}} */
834/* Bridge: NSMutableArray {{{ */
cc103044
JF
835@implementation NSMutableArray (Cycript)
836
837- (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
faf69207
JF
838 if ([name isEqualToString:@"length"]) {
839 // XXX: is this not intelligent?
cbaa5f0f
JF
840 NSNumber *number(reinterpret_cast<NSNumber *>(value));
841#ifdef __APPLE__
842 NSUInteger size([number unsignedIntegerValue]);
843#else
844 NSUInteger size([number unsignedIntValue]);
845#endif
faf69207
JF
846 NSUInteger count([self count]);
847 if (size < count)
848 [self removeObjectsInRange:NSMakeRange(size, count - size)];
849 else if (size != count) {
850 WebUndefined *undefined([WebUndefined undefined]);
851 for (size_t i(count); i != size; ++i)
852 [self addObject:undefined];
853 }
854 return true;
855 }
856
520c130f 857 size_t index(CYGetIndex(name));
faf69207 858 if (index == _not(size_t))
cc103044 859 return [super cy$setProperty:name to:value];
faf69207
JF
860
861 id object(value ?: [NSNull null]);
862
863 size_t count([self count]);
864 if (index < count)
865 [self replaceObjectAtIndex:index withObject:object];
cc103044 866 else {
faf69207
JF
867 if (index != count) {
868 WebUndefined *undefined([WebUndefined undefined]);
869 for (size_t i(count); i != index; ++i)
870 [self addObject:undefined];
871 }
872
873 [self addObject:object];
cc103044 874 }
faf69207 875
365abb0a 876 return true;
7b184c00
JF
877}
878
365abb0a 879- (bool) cy$deleteProperty:(NSString *)name {
520c130f 880 size_t index(CYGetIndex(name));
365abb0a
JF
881 if (index == _not(size_t) || index >= [self count])
882 return [super cy$deleteProperty:name];
883 [self replaceObjectAtIndex:index withObject:[WebUndefined undefined]];
884 return true;
cc103044
JF
885}
886
887@end
365abb0a
JF
888/* }}} */
889/* Bridge: NSMutableDictionary {{{ */
cc103044
JF
890@implementation NSMutableDictionary (Cycript)
891
892- (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
b6ea08b6 893 [self setObject:(value ?: [NSNull null]) forKey:name];
cc103044
JF
894 return true;
895}
896
897- (bool) cy$deleteProperty:(NSString *)name {
898 if ([self objectForKey:name] == nil)
899 return false;
900 else {
901 [self removeObjectForKey:name];
902 return true;
903 }
904}
905
62ca2b82 906@end
365abb0a
JF
907/* }}} */
908/* Bridge: NSNumber {{{ */
107e3ed0 909@implementation NSNumber (Cycript)
62ca2b82 910
b4aa79af 911- (JSType) cy$JSType {
b53b30c1 912#ifdef __APPLE__
b4aa79af 913 // XXX: this just seems stupid
b53b30c1
JF
914 if ([self class] == NSCFBoolean_)
915 return kJSTypeBoolean;
916#endif
917 return kJSTypeNumber;
b4aa79af
JF
918}
919
920- (NSObject *) cy$toJSON:(NSString *)key {
921 return self;
922}
923
924- (NSString *) cy$toCYON {
925 return [self cy$JSType] != kJSTypeBoolean ? [self stringValue] : [self boolValue] ? @"true" : @"false";
62ca2b82
JF
926}
927
37954781 928- (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) {
b4aa79af 929 return [self cy$JSType] != kJSTypeBoolean ? CYCastJSValue(context, [self doubleValue]) : CYCastJSValue(context, [self boolValue]);
3c1c3635 930} CYObjectiveCatch }
62ca2b82
JF
931
932@end
365abb0a
JF
933/* }}} */
934/* Bridge: NSNull {{{ */
935@implementation NSNull (Cycript)
936
937- (JSType) cy$JSType {
938 return kJSTypeNull;
939}
940
941- (NSObject *) cy$toJSON:(NSString *)key {
942 return self;
943}
944
945- (NSString *) cy$toCYON {
946 return @"null";
947}
948
949@end
950/* }}} */
951/* Bridge: NSObject {{{ */
952@implementation NSObject (Cycript)
953
37954781 954- (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) {
365abb0a 955 return CYMakeInstance(context, self, false);
3c1c3635 956} CYObjectiveCatch }
365abb0a
JF
957
958- (JSType) cy$JSType {
959 return kJSTypeObject;
960}
961
962- (NSObject *) cy$toJSON:(NSString *)key {
963 return [self description];
964}
965
966- (NSString *) cy$toCYON {
967 return [[self cy$toJSON:@""] cy$toCYON];
968}
969
970- (NSString *) cy$toKey {
971 return [self cy$toCYON];
972}
973
974- (bool) cy$hasProperty:(NSString *)name {
975 return false;
976}
977
978- (NSObject *) cy$getProperty:(NSString *)name {
979 return nil;
980}
981
982- (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
983 return false;
984}
985
986- (bool) cy$deleteProperty:(NSString *)name {
987 return false;
988}
989
8665da0c 990- (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context {
d3760804
JF
991}
992
3e264692
JF
993+ (bool) cy$hasImplicitProperties {
994 return true;
995}
996
365abb0a
JF
997@end
998/* }}} */
999/* Bridge: NSProxy {{{ */
1000@implementation NSProxy (Cycript)
1001
1002- (NSObject *) cy$toJSON:(NSString *)key {
1003 return [self description];
1004}
1005
1006- (NSString *) cy$toCYON {
1007 return [[self cy$toJSON:@""] cy$toCYON];
1008}
c1582939 1009
365abb0a
JF
1010@end
1011/* }}} */
1012/* Bridge: NSString {{{ */
107e3ed0 1013@implementation NSString (Cycript)
62ca2b82 1014
b4aa79af
JF
1015- (JSType) cy$JSType {
1016 return kJSTypeString;
1017}
1018
1019- (NSObject *) cy$toJSON:(NSString *)key {
1020 return self;
1021}
1022
3c1c3635
JF
1023- (NSString *) cy$toCYON {
1024 std::ostringstream str;
1025 CYUTF8String string(CYCastUTF8String(self));
1026 CYStringify(str, string.data, string.size);
1027 std::string value(str.str());
1028 return CYCastNSString(NULL, CYUTF8String(value.c_str(), value.size()));
1029}
b09da87b 1030
3c1c3635
JF
1031- (NSString *) cy$toKey {
1032 if (CYIsKey(CYCastUTF8String(self)))
1033 return self;
1034 return [self cy$toCYON];
1035}
b09da87b 1036
6aa16f29
JF
1037- (bool) cy$hasProperty:(NSString *)name {
1038 if ([name isEqualToString:@"length"])
1039 return true;
1040
1041 size_t index(CYGetIndex(name));
1042 if (index == _not(size_t) || index >= [self length])
1043 return [super cy$hasProperty:name];
1044 else
1045 return true;
1046}
1047
1048- (NSObject *) cy$getProperty:(NSString *)name {
1049 if ([name isEqualToString:@"length"]) {
1050 NSUInteger count([self length]);
1051#ifdef __APPLE__
1052 return [NSNumber numberWithUnsignedInteger:count];
1053#else
1054 return [NSNumber numberWithUnsignedInt:count];
1055#endif
1056 }
1057
1058 size_t index(CYGetIndex(name));
1059 if (index == _not(size_t) || index >= [self length])
1060 return [super cy$getProperty:name];
1061 else
1062 return [self substringWithRange:NSMakeRange(index, 1)];
1063}
1064
1065- (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context {
1066 [super cy$getPropertyNames:names inContext:context];
1067
1068 for (size_t index(0), length([self length]); index != length; ++index) {
1069 char name[32];
1070 sprintf(name, "%zu", index);
1071 JSPropertyNameAccumulatorAddName(names, CYJSString(name));
1072 }
1073}
1074
1075// XXX: this might be overly restrictive for NSString; I think I need a half-way between /injecting/ implicit properties and /accepting/ implicit properties
1076+ (bool) cy$hasImplicitProperties {
1077 return false;
1078}
1079
3c1c3635
JF
1080@end
1081/* }}} */
1082/* Bridge: WebUndefined {{{ */
1083@implementation WebUndefined (Cycript)
b09da87b 1084
3c1c3635
JF
1085- (JSType) cy$JSType {
1086 return kJSTypeUndefined;
b09da87b
JF
1087}
1088
3c1c3635
JF
1089- (NSObject *) cy$toJSON:(NSString *)key {
1090 return self;
b09da87b 1091}
3c1c3635
JF
1092
1093- (NSString *) cy$toCYON {
1094 return @"undefined";
c1582939
JF
1095}
1096
37954781 1097- (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) {
3c1c3635
JF
1098 return CYJSUndefined(context);
1099} CYObjectiveCatch }
1100
1101@end
1102/* }}} */
1103
cacd1a88 1104static bool CYIsClass(id self) {
aabea98c 1105#ifdef __APPLE__
cacd1a88
JF
1106 // XXX: this is a lame object_isClass
1107 return class_getInstanceMethod(object_getClass(self), @selector(alloc)) != NULL;
aabea98c
JF
1108#else
1109 return GSObjCIsClass(self);
1110#endif
cacd1a88
JF
1111}
1112
1113Class CYCastClass(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
1114 id self(CYCastNSObject(pool, context, value));
1115 if (CYIsClass(self))
1116 return (Class) self;
37954781 1117 throw CYJSError(context, "got something that is not a Class");
cacd1a88
JF
1118 return NULL;
1119}
1120
aabea98c 1121NSArray *CYCastNSArray(JSContextRef context, JSPropertyNameArrayRef names) {
b09da87b 1122 CYPool pool;
62ca2b82
JF
1123 size_t size(JSPropertyNameArrayGetCount(names));
1124 NSMutableArray *array([NSMutableArray arrayWithCapacity:size]);
1125 for (size_t index(0); index != size; ++index)
aabea98c 1126 [array addObject:CYCastNSString(pool, context, JSPropertyNameArrayGetNameAtIndex(names, index))];
62ca2b82
JF
1127 return array;
1128}
62ca2b82 1129
ba4fa42f 1130JSValueRef CYCastJSValue(JSContextRef context, NSObject *value) { CYPoolTry {
f7c38a29
JF
1131 if (value == nil)
1132 return CYJSNull(context);
1133 else if ([value respondsToSelector:@selector(cy$JSValueInContext:)])
1134 return [value cy$JSValueInContext:context];
1135 else
1136 return CYMakeInstance(context, value, false);
7c6c5b0a 1137} CYPoolCatch(NULL) return /*XXX*/ NULL; }
af4272e6 1138
b21525c7 1139@implementation CYJSObject
62ca2b82 1140
3c1c3635 1141- (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { CYObjectiveTry {
62ca2b82
JF
1142 if ((self = [super init]) != nil) {
1143 object_ = object;
bc60fb46
JF
1144 context_ = CYGetJSContext(context);
1145 //XXX:JSGlobalContextRetain(context_);
75b0a457 1146 JSValueProtect(context_, object_);
62ca2b82 1147 } return self;
3c1c3635 1148} CYObjectiveCatch }
62ca2b82 1149
3c1c3635 1150- (void) dealloc { CYObjectiveTry {
75b0a457 1151 JSValueUnprotect(context_, object_);
bc60fb46 1152 //XXX:JSGlobalContextRelease(context_);
75b0a457 1153 [super dealloc];
3c1c3635 1154} CYObjectiveCatch }
75b0a457 1155
3c1c3635 1156- (NSObject *) cy$toJSON:(NSString *)key { CYObjectiveTry {
00b9328f 1157 JSValueRef toJSON(CYGetProperty(context_, object_, toJSON_s));
b4aa79af
JF
1158 if (!CYIsCallable(context_, toJSON))
1159 return [super cy$toJSON:key];
1160 else {
1161 JSValueRef arguments[1] = {CYCastJSValue(context_, key)};
1162 JSValueRef value(CYCallAsFunction(context_, (JSObjectRef) toJSON, object_, 1, arguments));
1163 // XXX: do I really want an NSNull here?!
1164 return CYCastNSObject(NULL, context_, value) ?: [NSNull null];
1165 }
3c1c3635 1166} CYObjectiveCatch }
b4aa79af 1167
3c1c3635 1168- (NSString *) cy$toCYON { CYObjectiveTry {
af4272e6
JF
1169 CYPool pool;
1170 JSValueRef exception(NULL);
1171 const char *cyon(CYPoolCCYON(pool, context_, object_));
1172 CYThrow(context_, exception);
8aa3e970
JF
1173 if (cyon == NULL)
1174 return [super cy$toCYON];
1175 else
1176 return [NSString stringWithUTF8String:cyon];
3c1c3635 1177} CYObjectiveCatch }
b4aa79af 1178
3c1c3635 1179- (NSUInteger) count { CYObjectiveTry {
62ca2b82
JF
1180 JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_));
1181 size_t size(JSPropertyNameArrayGetCount(names));
1182 JSPropertyNameArrayRelease(names);
1183 return size;
3c1c3635 1184} CYObjectiveCatch }
62ca2b82 1185
3c1c3635 1186- (id) objectForKey:(id)key { CYObjectiveTry {
0226d428 1187 JSValueRef value(CYGetProperty(context_, object_, CYJSString(context_, (NSObject *) key)));
d0a00196
JF
1188 if (JSValueIsUndefined(context_, value))
1189 return nil;
1190 return CYCastNSObject(NULL, context_, value) ?: [NSNull null];
3c1c3635 1191} CYObjectiveCatch }
62ca2b82 1192
3c1c3635 1193- (NSEnumerator *) keyEnumerator { CYObjectiveTry {
62ca2b82 1194 JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_));
ec599edf 1195 NSEnumerator *enumerator([CYCastNSArray(context_, names) objectEnumerator]);
62ca2b82
JF
1196 JSPropertyNameArrayRelease(names);
1197 return enumerator;
3c1c3635 1198} CYObjectiveCatch }
62ca2b82 1199
3c1c3635 1200- (void) setObject:(id)object forKey:(id)key { CYObjectiveTry {
0226d428 1201 CYSetProperty(context_, object_, CYJSString(context_, (NSObject *) key), CYCastJSValue(context_, (NSString *) object));
3c1c3635 1202} CYObjectiveCatch }
62ca2b82 1203
3c1c3635 1204- (void) removeObjectForKey:(id)key { CYObjectiveTry {
62ca2b82 1205 JSValueRef exception(NULL);
0226d428 1206 (void) JSObjectDeleteProperty(context_, object_, CYJSString(context_, (NSObject *) key), &exception);
62ca2b82 1207 CYThrow(context_, exception);
3c1c3635 1208} CYObjectiveCatch }
62ca2b82
JF
1209
1210@end
1211
b21525c7 1212@implementation CYJSArray
c1582939 1213
3c1c3635 1214- (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { CYObjectiveTry {
c1582939
JF
1215 if ((self = [super init]) != nil) {
1216 object_ = object;
bc60fb46
JF
1217 context_ = CYGetJSContext(context);
1218 //XXX:JSGlobalContextRetain(context_);
75b0a457 1219 JSValueProtect(context_, object_);
c1582939 1220 } return self;
3c1c3635 1221} CYObjectiveCatch }
c1582939 1222
3c1c3635 1223- (void) dealloc { CYObjectiveTry {
75b0a457 1224 JSValueUnprotect(context_, object_);
bc60fb46 1225 //XXX:JSGlobalContextRelease(context_);
75b0a457 1226 [super dealloc];
3c1c3635 1227} CYObjectiveCatch }
75b0a457 1228
3c1c3635 1229- (NSUInteger) count { CYObjectiveTry {
00b9328f 1230 return CYCastDouble(context_, CYGetProperty(context_, object_, length_s));
3c1c3635 1231} CYObjectiveCatch }
c1582939 1232
3c1c3635 1233- (id) objectAtIndex:(NSUInteger)index { CYObjectiveTry {
9a2db8b2
JF
1234 size_t bounds([self count]);
1235 if (index >= bounds)
1236 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray objectAtIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
62ca2b82
JF
1237 JSValueRef exception(NULL);
1238 JSValueRef value(JSObjectGetPropertyAtIndex(context_, object_, index, &exception));
1239 CYThrow(context_, exception);
478d4ed0 1240 return CYCastNSObject(NULL, context_, value) ?: [NSNull null];
3c1c3635 1241} CYObjectiveCatch }
c1582939 1242
3c1c3635 1243- (void) addObject:(id)object { CYObjectiveTry {
faf69207
JF
1244 JSValueRef exception(NULL);
1245 JSValueRef arguments[1];
ba4fa42f 1246 arguments[0] = CYCastJSValue(context_, (NSObject *) object);
9772d9bf 1247 JSObjectRef Array(CYGetCachedObject(context_, CYJSString("Array_prototype")));
498c3570 1248 JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array, push_s)), object_, 1, arguments, &exception);
faf69207 1249 CYThrow(context_, exception);
3c1c3635 1250} CYObjectiveCatch }
faf69207 1251
3c1c3635 1252- (void) insertObject:(id)object atIndex:(NSUInteger)index { CYObjectiveTry {
9a2db8b2
JF
1253 size_t bounds([self count] + 1);
1254 if (index >= bounds)
1255 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray insertObject:atIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
faf69207
JF
1256 JSValueRef exception(NULL);
1257 JSValueRef arguments[3];
1258 arguments[0] = CYCastJSValue(context_, index);
1259 arguments[1] = CYCastJSValue(context_, 0);
ba4fa42f 1260 arguments[2] = CYCastJSValue(context_, (NSObject *) object);
9772d9bf 1261 JSObjectRef Array(CYGetCachedObject(context_, CYJSString("Array_prototype")));
498c3570 1262 JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array, splice_s)), object_, 3, arguments, &exception);
3c1c3635
JF
1263 CYThrow(context_, exception);
1264} CYObjectiveCatch }
af4272e6 1265
3c1c3635
JF
1266- (void) removeLastObject { CYObjectiveTry {
1267 JSValueRef exception(NULL);
9772d9bf 1268 JSObjectRef Array(CYGetCachedObject(context_, CYJSString("Array_prototype")));
498c3570 1269 JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array, pop_s)), object_, 0, NULL, &exception);
3c1c3635
JF
1270 CYThrow(context_, exception);
1271} CYObjectiveCatch }
af4272e6 1272
3c1c3635
JF
1273- (void) removeObjectAtIndex:(NSUInteger)index { CYObjectiveTry {
1274 size_t bounds([self count]);
1275 if (index >= bounds)
1276 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray removeObjectAtIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
1277 JSValueRef exception(NULL);
1278 JSValueRef arguments[2];
1279 arguments[0] = CYCastJSValue(context_, index);
1280 arguments[1] = CYCastJSValue(context_, 1);
9772d9bf 1281 JSObjectRef Array(CYGetCachedObject(context_, CYJSString("Array_prototype")));
498c3570 1282 JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array, splice_s)), object_, 2, arguments, &exception);
3c1c3635
JF
1283 CYThrow(context_, exception);
1284} CYObjectiveCatch }
1285
1286- (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)object { CYObjectiveTry {
1287 size_t bounds([self count]);
1288 if (index >= bounds)
1289 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray replaceObjectAtIndex:withObject:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
ba4fa42f 1290 CYSetProperty(context_, object_, index, CYCastJSValue(context_, (NSObject *) object));
3c1c3635
JF
1291} CYObjectiveCatch }
1292
1293@end
af4272e6 1294
18401062 1295// XXX: use objc_getAssociatedObject and objc_setAssociatedObject on 10.6
61933e16
JF
1296struct CYInternal :
1297 CYData
1298{
1299 JSObjectRef object_;
1300
1301 CYInternal() :
1302 object_(NULL)
1303 {
1304 }
1305
1306 ~CYInternal() {
1307 // XXX: delete object_? ;(
1308 }
1309
1310 static CYInternal *Get(id self) {
1311 CYInternal *internal(NULL);
1312 if (object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal)) == NULL) {
1313 // XXX: do something epic? ;P
1314 }
1315
1316 return internal;
1317 }
1318
1319 static CYInternal *Set(id self) {
1320 CYInternal *internal(NULL);
aabea98c 1321 if (objc_ivar *ivar = object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal))) {
61933e16
JF
1322 if (internal == NULL) {
1323 internal = new CYInternal();
1324 object_setIvar(self, ivar, reinterpret_cast<id>(internal));
1325 }
1326 } else {
1327 // XXX: do something epic? ;P
1328 }
1329
1330 return internal;
1331 }
1332
7b184c00
JF
1333 bool HasProperty(JSContextRef context, JSStringRef name) {
1334 if (object_ == NULL)
1335 return false;
1336 return JSObjectHasProperty(context, object_, name);
1337 }
1338
61933e16
JF
1339 JSValueRef GetProperty(JSContextRef context, JSStringRef name) {
1340 if (object_ == NULL)
1341 return NULL;
1342 return CYGetProperty(context, object_, name);
1343 }
1344
1345 void SetProperty(JSContextRef context, JSStringRef name, JSValueRef value) {
1346 if (object_ == NULL)
1347 object_ = JSObjectMake(context, NULL, NULL);
1348 CYSetProperty(context, object_, name, value);
1349 }
1350};
1351
534fb6da 1352static JSObjectRef CYMakeSelector(JSContextRef context, SEL sel) {
9e562cfc
JF
1353 Selector_privateData *internal(new Selector_privateData(sel));
1354 return JSObjectMake(context, Selector_, internal);
dea834b0 1355}
b09da87b 1356
534fb6da 1357static SEL CYCastSEL(JSContextRef context, JSValueRef value) {
4afefdd9 1358 if (JSValueIsObjectOfClass(context, value, Selector_)) {
9e562cfc
JF
1359 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate((JSObjectRef) value)));
1360 return reinterpret_cast<SEL>(internal->value_);
4afefdd9
JF
1361 } else
1362 return CYCastPointer<SEL>(context, value);
1363}
1364
b64ab4da 1365void *CYObjectiveC_ExecuteStart(JSContextRef context) { CYSadTry {
3c1c3635 1366 return (void *) [[NSAutoreleasePool alloc] init];
ea840434 1367} CYSadCatch(NULL) }
ea2d184c 1368
b64ab4da 1369void CYObjectiveC_ExecuteEnd(JSContextRef context, void *handle) { CYSadTry {
3c1c3635 1370 return [(NSAutoreleasePool *) handle release];
ea840434 1371} CYSadCatch() }
ea2d184c 1372
f0d43c71 1373JSValueRef CYObjectiveC_RuntimeProperty(JSContextRef context, CYUTF8String name) { CYPoolTry {
3c1c3635
JF
1374 if (name == "nil")
1375 return Instance::Make(context, nil);
1376 if (Class _class = objc_getClass(name.data))
1377 return CYMakeInstance(context, _class, true);
64b8d29f
JF
1378 if (Protocol *protocol = objc_getProtocol(name.data))
1379 return CYMakeInstance(context, protocol, true);
3c1c3635 1380 return NULL;
7c6c5b0a 1381} CYPoolCatch(NULL) return /*XXX*/ NULL; }
d3760804 1382
b64ab4da 1383static void CYObjectiveC_CallFunction(JSContextRef context, ffi_cif *cif, void (*function)(), uint8_t *value, void **values) { CYSadTry {
d3760804 1384 ffi_call(cif, function, value, values);
ea840434 1385} CYSadCatch() }
ea2d184c 1386
b64ab4da 1387static bool CYObjectiveC_PoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { CYSadTry {
3c1c3635 1388 switch (type->primitive) {
ecf94af8
JF
1389 // XXX: do something epic about blocks
1390 case sig::block_P:
ea2d184c
JF
1391 case sig::object_P:
1392 case sig::typename_P:
b09da87b 1393 *reinterpret_cast<id *>(data) = CYCastNSObject(pool, context, value);
7ba62cfd
JF
1394 break;
1395
ea2d184c 1396 case sig::selector_P:
7ba62cfd
JF
1397 *reinterpret_cast<SEL *>(data) = CYCastSEL(context, value);
1398 break;
ea2d184c 1399
bd17e6f3 1400 default:
3c1c3635 1401 return false;
ea2d184c 1402 }
ea2d184c 1403
3c1c3635 1404 return true;
ea840434 1405} CYSadCatch(false) }
ea2d184c 1406
f0d43c71 1407static JSValueRef CYObjectiveC_FromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) { CYPoolTry {
ea2d184c 1408 switch (type->primitive) {
ecf94af8
JF
1409 // XXX: do something epic about blocks
1410 case sig::block_P:
3c1c3635 1411 case sig::object_P:
ba4fa42f 1412 if (NSObject *object = *reinterpret_cast<NSObject **>(data)) {
3c1c3635 1413 JSValueRef value(CYCastJSValue(context, object));
2b52f27e
JF
1414 if (initialize)
1415 [object release];
3c1c3635 1416 return value;
2b52f27e 1417 } else goto null;
dea834b0 1418
b09da87b 1419 case sig::typename_P:
3c1c3635 1420 return CYMakeInstance(context, *reinterpret_cast<Class *>(data), true);
b09da87b 1421
dea834b0
JF
1422 case sig::selector_P:
1423 if (SEL sel = *reinterpret_cast<SEL *>(data))
3c1c3635 1424 return CYMakeSelector(context, sel);
f610e1a0 1425 else goto null;
f610e1a0
JF
1426
1427 null:
3c1c3635 1428 return CYJSNull(context);
bd17e6f3 1429 default:
3c1c3635 1430 return NULL;
ea2d184c 1431 }
7c6c5b0a 1432} CYPoolCatch(NULL) return /*XXX*/ NULL; }
ea2d184c 1433
8a199b13 1434static bool CYImplements(id object, Class _class, SEL selector, bool devoid) {
39bb4b6a 1435 if (objc_method *method = class_getInstanceMethod(_class, selector)) {
8a199b13
JF
1436 if (!devoid)
1437 return true;
7c6c5b0a 1438#if OBJC_API_VERSION >= 2
8a199b13
JF
1439 char type[16];
1440 method_getReturnType(method, type, sizeof(type));
b5dd57dc
JF
1441#else
1442 const char *type(method_getTypeEncoding(method));
1443#endif
8a199b13
JF
1444 if (type[0] != 'v')
1445 return true;
1446 }
1447
7b184c00 1448 // XXX: possibly use a more "awesome" check?
8a199b13 1449 return false;
7b184c00
JF
1450}
1451
37954781 1452static const char *CYPoolTypeEncoding(apr_pool_t *pool, JSContextRef context, SEL sel, objc_method *method) {
dc68b74c
JF
1453 if (method != NULL)
1454 return method_getTypeEncoding(method);
856b8cd0
JF
1455
1456 const char *name(sel_getName(sel));
2f51d6ab 1457 size_t length(strlen(name));
856b8cd0 1458
2f51d6ab
JF
1459 char keyed[length + 2];
1460 keyed[0] = '6';
1461 keyed[length + 1] = '\0';
1462 memcpy(keyed + 1, name, length);
dc68b74c 1463
2f51d6ab
JF
1464 if (CYBridgeEntry *entry = CYBridgeHash(keyed, length + 1))
1465 return entry->value_;
dc68b74c 1466
3c1c3635 1467 return NULL;
dc68b74c
JF
1468}
1469
3c1c3635
JF
1470static void MessageClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) {
1471 Closure_privateData *internal(reinterpret_cast<Closure_privateData *>(arg));
dc68b74c 1472
3c1c3635 1473 JSContextRef context(internal->context_);
dc68b74c 1474
3c1c3635
JF
1475 size_t count(internal->cif_.nargs);
1476 JSValueRef values[count];
1477
1478 for (size_t index(0); index != count; ++index)
1479 values[index] = CYFromFFI(context, internal->signature_.elements[1 + index].type, internal->cif_.arg_types[index], arguments[index]);
1480
1481 JSObjectRef _this(CYCastJSObject(context, values[0]));
1482
1483 JSValueRef value(CYCallAsFunction(context, internal->function_, _this, count - 2, values + 2));
1484 CYPoolFFI(NULL, context, internal->signature_.elements[0].type, internal->cif_.rtype, result, value);
dc68b74c
JF
1485}
1486
1487static JSObjectRef CYMakeMessage(JSContextRef context, SEL sel, IMP imp, const char *type) {
1488 Message_privateData *internal(new Message_privateData(sel, type, imp));
1489 return JSObjectMake(context, Message_, internal);
1490}
1491
1492static IMP CYMakeMessage(JSContextRef context, JSValueRef value, const char *type) {
1493 JSObjectRef function(CYCastJSObject(context, value));
1494 Closure_privateData *internal(CYMakeFunctor_(context, function, type, &MessageClosure_));
2fd4c9a9 1495 // XXX: see notes in Library.cpp about needing to leak
dc68b74c
JF
1496 return reinterpret_cast<IMP>(internal->GetValue());
1497}
1498
365abb0a
JF
1499static bool Messages_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
1500 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
dc68b74c
JF
1501 Class _class(internal->GetValue());
1502
1503 CYPool pool;
3c1c3635 1504 const char *name(CYPoolCString(pool, context, property));
dc68b74c
JF
1505
1506 if (SEL sel = sel_getUid(name))
1507 if (class_getInstanceMethod(_class, sel) != NULL)
1508 return true;
1509
1510 return false;
1511}
1512
365abb0a
JF
1513static JSValueRef Messages_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
1514 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
dc68b74c
JF
1515 Class _class(internal->GetValue());
1516
1517 CYPool pool;
3c1c3635 1518 const char *name(CYPoolCString(pool, context, property));
dc68b74c
JF
1519
1520 if (SEL sel = sel_getUid(name))
39bb4b6a 1521 if (objc_method *method = class_getInstanceMethod(_class, sel))
dc68b74c
JF
1522 return CYMakeMessage(context, sel, method_getImplementation(method), method_getTypeEncoding(method));
1523
1524 return NULL;
1525}
1526
365abb0a
JF
1527static bool Messages_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
1528 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
dc68b74c
JF
1529 Class _class(internal->GetValue());
1530
1531 CYPool pool;
3c1c3635 1532 const char *name(CYPoolCString(pool, context, property));
dc68b74c
JF
1533
1534 SEL sel(sel_registerName(name));
1535
39bb4b6a 1536 objc_method *method(class_getInstanceMethod(_class, sel));
dc68b74c
JF
1537
1538 const char *type;
1539 IMP imp;
1540
1541 if (JSValueIsObjectOfClass(context, value, Message_)) {
1542 Message_privateData *message(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate((JSObjectRef) value)));
1543 type = sig::Unparse(pool, &message->signature_);
1544 imp = reinterpret_cast<IMP>(message->GetValue());
1545 } else {
37954781 1546 type = CYPoolTypeEncoding(pool, context, sel, method);
dc68b74c
JF
1547 imp = CYMakeMessage(context, value, type);
1548 }
1549
1550 if (method != NULL)
1551 method_setImplementation(method, imp);
b5dd57dc
JF
1552 else {
1553#ifdef GNU_RUNTIME
1554 GSMethodList list(GSAllocMethodList(1));
1555 GSAppendMethodToList(list, sel, type, imp, YES);
1556 GSAddMethodList(_class, list, YES);
1557 GSFlushMethodCacheForClass(_class);
1558#else
1559 class_addMethod(_class, sel, imp, type);
1560#endif
1561 }
dc68b74c
JF
1562
1563 return true;
1564}
1565
7c6c5b0a 1566#if 0 && OBJC_API_VERSION < 2
365abb0a
JF
1567static bool Messages_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
1568 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
dc68b74c
JF
1569 Class _class(internal->GetValue());
1570
1571 CYPool pool;
aabea98c 1572 const char *name(CYPoolCString(pool, context, property));
dc68b74c
JF
1573
1574 if (SEL sel = sel_getUid(name))
39bb4b6a 1575 if (objc_method *method = class_getInstanceMethod(_class, sel)) {
dc68b74c
JF
1576 objc_method_list list = {NULL, 1, {method}};
1577 class_removeMethods(_class, &list);
1578 return true;
1579 }
1580
1581 return false;
1582}
1583#endif
1584
365abb0a
JF
1585static void Messages_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1586 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
dc68b74c
JF
1587 Class _class(internal->GetValue());
1588
7c6c5b0a 1589#if OBJC_API_VERSION >= 2
dc68b74c 1590 unsigned int size;
39bb4b6a 1591 objc_method **data(class_copyMethodList(_class, &size));
dc68b74c
JF
1592 for (size_t i(0); i != size; ++i)
1593 JSPropertyNameAccumulatorAddName(names, CYJSString(sel_getName(method_getName(data[i]))));
1594 free(data);
aabea98c
JF
1595#else
1596 for (objc_method_list *methods(_class->methods); methods != NULL; methods = methods->method_next)
1597 for (int i(0); i != methods->method_count; ++i)
1598 JSPropertyNameAccumulatorAddName(names, CYJSString(sel_getName(method_getName(&methods->method_list[i]))));
1599#endif
dc68b74c
JF
1600}
1601
3e264692
JF
1602static bool CYHasImplicitProperties(Class _class) {
1603 // XXX: this is an evil hack to deal with NSProxy; fix elsewhere
1604 if (!CYImplements(_class, object_getClass(_class), @selector(cy$hasImplicitProperties), false))
1605 return true;
1606 return [_class cy$hasImplicitProperties];
1607}
1608
7b184c00
JF
1609static bool Instance_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
1610 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1611 id self(internal->GetValue());
1612
1613 if (JSStringIsEqualToUTF8CString(property, "$cyi"))
1614 return true;
1615
c239b9f8 1616 CYPool pool;
aabea98c 1617 NSString *name(CYCastNSString(pool, context, property));
7b184c00
JF
1618
1619 if (CYInternal *internal = CYInternal::Get(self))
1620 if (internal->HasProperty(context, property))
1621 return true;
1622
365abb0a
JF
1623 Class _class(object_getClass(self));
1624
7b184c00 1625 CYPoolTry {
365abb0a
JF
1626 // XXX: this is an evil hack to deal with NSProxy; fix elsewhere
1627 if (CYImplements(self, _class, @selector(cy$hasProperty:), false))
1628 if ([self cy$hasProperty:name])
1629 return true;
7b184c00
JF
1630 } CYPoolCatch(false)
1631
3c1c3635 1632 const char *string(CYPoolCString(pool, context, name));
7b184c00 1633
aabea98c 1634#ifdef __APPLE__
7b184c00
JF
1635 if (class_getProperty(_class, string) != NULL)
1636 return true;
aabea98c 1637#endif
7b184c00 1638
3e264692
JF
1639 if (CYHasImplicitProperties(_class))
1640 if (SEL sel = sel_getUid(string))
1641 if (CYImplements(self, _class, sel, true))
1642 return true;
7b184c00
JF
1643
1644 return false;
1645}
1646
3c1c3635 1647static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
7b184c00
JF
1648 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1649 id self(internal->GetValue());
1650
1651 if (JSStringIsEqualToUTF8CString(property, "$cyi"))
1652 return Internal::Make(context, self, object);
c239b9f8 1653
3c1c3635 1654 CYPool pool;
aabea98c 1655 NSString *name(CYCastNSString(pool, context, property));
c239b9f8 1656
3c1c3635
JF
1657 if (CYInternal *internal = CYInternal::Get(self))
1658 if (JSValueRef value = internal->GetProperty(context, property))
1659 return value;
c239b9f8 1660
3c1c3635
JF
1661 CYPoolTry {
1662 if (NSObject *data = [self cy$getProperty:name])
1663 return CYCastJSValue(context, data);
1664 } CYPoolCatch(NULL)
c239b9f8 1665
3c1c3635
JF
1666 const char *string(CYPoolCString(pool, context, name));
1667 Class _class(object_getClass(self));
c239b9f8 1668
cbaa5f0f 1669#ifdef __APPLE__
3c1c3635
JF
1670 if (objc_property_t property = class_getProperty(_class, string)) {
1671 PropertyAttributes attributes(property);
1672 SEL sel(sel_registerName(attributes.Getter()));
1673 return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception);
1674 }
cbaa5f0f 1675#endif
c239b9f8 1676
3e264692
JF
1677 if (CYHasImplicitProperties(_class))
1678 if (SEL sel = sel_getUid(string))
1679 if (CYImplements(self, _class, sel, true))
1680 return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception);
8953777c 1681
3c1c3635
JF
1682 return NULL;
1683} CYCatch }
c239b9f8 1684
3c1c3635 1685static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
dc68b74c
JF
1686 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1687 id self(internal->GetValue());
1688
c239b9f8
JF
1689 CYPool pool;
1690
aabea98c 1691 NSString *name(CYCastNSString(pool, context, property));
3c1c3635 1692 NSObject *data(CYCastNSObject(pool, context, value));
8953777c 1693
3c1c3635
JF
1694 CYPoolTry {
1695 if ([self cy$setProperty:name to:data])
c239b9f8 1696 return true;
3c1c3635 1697 } CYPoolCatch(NULL)
dc68b74c 1698
3c1c3635 1699 const char *string(CYPoolCString(pool, context, name));
c239b9f8
JF
1700 Class _class(object_getClass(self));
1701
8aa3e970 1702#ifdef __APPLE__
3c1c3635
JF
1703 if (objc_property_t property = class_getProperty(_class, string)) {
1704 PropertyAttributes attributes(property);
1705 if (const char *setter = attributes.Setter()) {
1706 SEL sel(sel_registerName(setter));
1707 JSValueRef arguments[1] = {value};
1708 CYSendMessage(pool, context, self, NULL, sel, 1, arguments, false, exception);
9b5527f0
JF
1709 return true;
1710 }
3c1c3635 1711 }
b24eb750 1712#endif
c239b9f8 1713
3c1c3635 1714 size_t length(strlen(string));
9e20b0b7 1715
3c1c3635 1716 char set[length + 5];
f33b048a 1717
3c1c3635
JF
1718 set[0] = 's';
1719 set[1] = 'e';
1720 set[2] = 't';
f33b048a 1721
3c1c3635
JF
1722 if (string[0] != '\0') {
1723 set[3] = toupper(string[0]);
1724 memcpy(set + 4, string + 1, length - 1);
e0dc20ec 1725 }
bd17e6f3 1726
3c1c3635
JF
1727 set[length + 3] = ':';
1728 set[length + 4] = '\0';
9b5527f0 1729
3c1c3635
JF
1730 if (SEL sel = sel_getUid(set))
1731 if (CYImplements(self, _class, sel, false)) {
1732 JSValueRef arguments[1] = {value};
1733 CYSendMessage(pool, context, self, NULL, sel, 1, arguments, false, exception);
1734 }
f37b3d2b 1735
3c1c3635
JF
1736 if (CYInternal *internal = CYInternal::Set(self)) {
1737 internal->SetProperty(context, property, value);
f37b3d2b 1738 return true;
3c1c3635 1739 }
f37b3d2b 1740
3c1c3635
JF
1741 return false;
1742} CYCatch }
9b5527f0 1743
3c1c3635
JF
1744static bool Instance_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1745 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1746 id self(internal->GetValue());
9b5527f0 1747
3c1c3635 1748 CYPoolTry {
aabea98c 1749 NSString *name(CYCastNSString(NULL, context, property));
3c1c3635
JF
1750 return [self cy$deleteProperty:name];
1751 } CYPoolCatch(NULL)
7c6c5b0a 1752} CYCatch return /*XXX*/ NULL; }
9b5527f0 1753
ec18fc0e
JF
1754static void Instance_getPropertyNames_message(JSPropertyNameAccumulatorRef names, objc_method *method) {
1755 const char *name(sel_getName(method_getName(method)));
1756 if (strchr(name, ':') != NULL)
1757 return;
1758
1759 const char *type(method_getTypeEncoding(method));
1760 if (type == NULL || *type == '\0' || *type == 'v')
1761 return;
1762
1763 JSPropertyNameAccumulatorAddName(names, CYJSString(name));
1764}
1765
3c1c3635
JF
1766static void Instance_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1767 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1768 id self(internal->GetValue());
9b5527f0 1769
3c1c3635
JF
1770 CYPool pool;
1771 Class _class(object_getClass(self));
9b5527f0 1772
3c1c3635
JF
1773#ifdef __APPLE__
1774 {
1775 unsigned int size;
1776 objc_property_t *data(class_copyPropertyList(_class, &size));
1777 for (size_t i(0); i != size; ++i)
1778 JSPropertyNameAccumulatorAddName(names, CYJSString(property_getName(data[i])));
1779 free(data);
1780 }
1781#endif
d3760804 1782
326a9dba
JF
1783 if (CYHasImplicitProperties(_class))
1784 for (Class current(_class); current != nil; current = class_getSuperclass(current)) {
ec18fc0e 1785#if OBJC_API_VERSION >= 2
326a9dba
JF
1786 unsigned int size;
1787 objc_method **data(class_copyMethodList(current, &size));
1788 for (size_t i(0); i != size; ++i)
1789 Instance_getPropertyNames_message(names, data[i]);
1790 free(data);
ec18fc0e 1791#else
326a9dba
JF
1792 for (objc_method_list *methods(current->methods); methods != NULL; methods = methods->method_next)
1793 for (int i(0); i != methods->method_count; ++i)
1794 Instance_getPropertyNames_message(names, &methods->method_list[i]);
ec18fc0e 1795#endif
326a9dba 1796 }
ec18fc0e 1797
d3760804
JF
1798 CYPoolTry {
1799 // XXX: this is an evil hack to deal with NSProxy; fix elsewhere
8665da0c
JF
1800 if (CYImplements(self, _class, @selector(cy$getPropertyNames:inContext:), false))
1801 [self cy$getPropertyNames:names inContext:context];
d3760804 1802 } CYPoolCatch()
9b5527f0
JF
1803}
1804
3c1c3635
JF
1805static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1806 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1807 JSObjectRef value(Instance::Make(context, [internal->GetValue() alloc], Instance::Uninitialized));
1808 return value;
1809} CYCatch }
bd17e6f3 1810
3c1c3635
JF
1811static bool Instance_hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef instance, JSValueRef *exception) { CYTry {
1812 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) constructor)));
1813 Class _class(internal->GetValue());
1814 if (!CYIsClass(_class))
1815 return false;
bd17e6f3 1816
3c1c3635
JF
1817 if (JSValueIsObjectOfClass(context, instance, Instance_)) {
1818 Instance *linternal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) instance)));
1819 // XXX: this isn't always safe
1820 return [linternal->GetValue() isKindOfClass:_class];
1821 }
ff783f8c 1822
3c1c3635
JF
1823 return false;
1824} CYCatch }
bd17e6f3 1825
3c1c3635
JF
1826static bool Internal_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
1827 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
f33b048a 1828 CYPool pool;
bd17e6f3 1829
3c1c3635
JF
1830 id self(internal->GetValue());
1831 const char *name(CYPoolCString(pool, context, property));
520c130f 1832
3c1c3635 1833 if (object_getInstanceVariable(self, name, NULL) != NULL)
bd17e6f3 1834 return true;
3c1c3635
JF
1835
1836 return false;
bd17e6f3
JF
1837}
1838
3c1c3635
JF
1839static JSValueRef Internal_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1840 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1841 CYPool pool;
930aa21b 1842
3c1c3635
JF
1843 id self(internal->GetValue());
1844 const char *name(CYPoolCString(pool, context, property));
f33b048a 1845
aabea98c 1846 if (objc_ivar *ivar = object_getInstanceVariable(self, name, NULL)) {
3c1c3635 1847 Type_privateData type(pool, ivar_getTypeEncoding(ivar));
b81e7fb6 1848 // XXX: if this fails and throws an exception the person we are throwing it to gets the wrong exception
3c1c3635
JF
1849 return CYFromFFI(context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar));
1850 }
f33b048a 1851
3c1c3635
JF
1852 return NULL;
1853} CYCatch }
283e7e33 1854
3c1c3635
JF
1855static bool Internal_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
1856 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1857 CYPool pool;
283e7e33 1858
3c1c3635
JF
1859 id self(internal->GetValue());
1860 const char *name(CYPoolCString(pool, context, property));
283e7e33 1861
aabea98c 1862 if (objc_ivar *ivar = object_getInstanceVariable(self, name, NULL)) {
3c1c3635
JF
1863 Type_privateData type(pool, ivar_getTypeEncoding(ivar));
1864 CYPoolFFI(pool, context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar), value);
1865 return true;
283e7e33 1866 }
f33b048a 1867
3c1c3635
JF
1868 return false;
1869} CYCatch }
85a33bf5 1870
3c1c3635
JF
1871static void Internal_getPropertyNames_(Class _class, JSPropertyNameAccumulatorRef names) {
1872 if (Class super = class_getSuperclass(_class))
1873 Internal_getPropertyNames_(super, names);
ea2d184c 1874
7c6c5b0a 1875#if OBJC_API_VERSION >= 2
3c1c3635 1876 unsigned int size;
aabea98c 1877 objc_ivar **data(class_copyIvarList(_class, &size));
3c1c3635
JF
1878 for (size_t i(0); i != size; ++i)
1879 JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(data[i])));
1880 free(data);
b5dd57dc
JF
1881#else
1882 if (objc_ivar_list *ivars = _class->ivars)
1883 for (int i(0); i != ivars->ivar_count; ++i)
1884 JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(&ivars->ivar_list[i])));
1885#endif
3c1c3635
JF
1886}
1887
1888static void Internal_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1889 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1890 CYPool pool;
ea2d184c 1891
3c1c3635
JF
1892 id self(internal->GetValue());
1893 Class _class(object_getClass(self));
7ba62cfd 1894
3c1c3635 1895 Internal_getPropertyNames_(_class, names);
7ba62cfd 1896}
ea2d184c 1897
3c1c3635
JF
1898static JSValueRef Internal_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
1899 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1900 return internal->GetOwner();
c239b9f8
JF
1901}
1902
3c1c3635
JF
1903static JSValueRef ObjectiveC_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1904 CYPool pool;
aabea98c 1905 NSString *name(CYCastNSString(pool, context, property));
3c1c3635
JF
1906 if (Class _class = NSClassFromString(name))
1907 return CYMakeInstance(context, _class, true);
1908 return NULL;
1909} CYCatch }
1910
c239b9f8 1911static void ObjectiveC_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
aabea98c 1912#ifdef __APPLE__
c239b9f8
JF
1913 size_t size(objc_getClassList(NULL, 0));
1914 Class *data(reinterpret_cast<Class *>(malloc(sizeof(Class) * size)));
1915
1916 get:
1917 size_t writ(objc_getClassList(data, size));
1918 if (size < writ) {
1919 size = writ;
1920 if (Class *copy = reinterpret_cast<Class *>(realloc(data, sizeof(Class) * writ))) {
1921 data = copy;
1922 goto get;
1923 } else goto done;
1924 }
1925
1926 for (size_t i(0); i != writ; ++i)
1927 JSPropertyNameAccumulatorAddName(names, CYJSString(class_getName(data[i])));
1928
1929 done:
1930 free(data);
aabea98c
JF
1931#else
1932 void *state(NULL);
1933 while (Class _class = objc_next_class(&state))
1934 JSPropertyNameAccumulatorAddName(names, CYJSString(class_getName(_class)));
1935#endif
c239b9f8
JF
1936}
1937
7c6c5b0a 1938#if OBJC_API_VERSION >= 2
3c1c3635 1939static JSValueRef ObjectiveC_Image_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
c239b9f8
JF
1940 const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object)));
1941
3c1c3635
JF
1942 CYPool pool;
1943 const char *name(CYPoolCString(pool, context, property));
1944 unsigned int size;
1945 const char **data(objc_copyClassNamesForImage(internal, &size));
1946 JSValueRef value;
1947 for (size_t i(0); i != size; ++i)
1948 if (strcmp(name, data[i]) == 0) {
1949 if (Class _class = objc_getClass(name)) {
1950 value = CYMakeInstance(context, _class, true);
1951 goto free;
1952 } else
1953 break;
1954 }
1955 value = NULL;
1956 free:
1957 free(data);
1958 return value;
1959} CYCatch }
c239b9f8
JF
1960
1961static void ObjectiveC_Image_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1962 const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object)));
1963 unsigned int size;
1964 const char **data(objc_copyClassNamesForImage(internal, &size));
1965 for (size_t i(0); i != size; ++i)
1966 JSPropertyNameAccumulatorAddName(names, CYJSString(data[i]));
1967 free(data);
1968}
1969
3c1c3635
JF
1970static JSValueRef ObjectiveC_Images_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1971 CYPool pool;
1972 const char *name(CYPoolCString(pool, context, property));
1973 unsigned int size;
1974 const char **data(objc_copyImageNames(&size));
1975 for (size_t i(0); i != size; ++i)
1976 if (strcmp(name, data[i]) == 0) {
1977 name = data[i];
1978 goto free;
1979 }
1980 name = NULL;
1981 free:
1982 free(data);
1983 if (name == NULL)
1984 return NULL;
1985 JSObjectRef value(JSObjectMake(context, NULL, NULL));
1986 CYSetProperty(context, value, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Image_Classes_, const_cast<char *>(name)));
1987 return value;
1988} CYCatch }
c239b9f8
JF
1989
1990static void ObjectiveC_Images_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1991 unsigned int size;
1992 const char **data(objc_copyImageNames(&size));
1993 for (size_t i(0); i != size; ++i)
1994 JSPropertyNameAccumulatorAddName(names, CYJSString(data[i]));
1995 free(data);
1996}
aabea98c 1997#endif
c239b9f8 1998
3c1c3635
JF
1999static JSValueRef ObjectiveC_Protocols_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2000 CYPool pool;
aabea98c
JF
2001 const char *name(CYPoolCString(pool, context, property));
2002 if (Protocol *protocol = objc_getProtocol(name))
3c1c3635
JF
2003 return CYMakeInstance(context, protocol, true);
2004 return NULL;
2005} CYCatch }
c239b9f8
JF
2006
2007static void ObjectiveC_Protocols_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
7c6c5b0a 2008#if OBJC_API_VERSION >= 2
c239b9f8
JF
2009 unsigned int size;
2010 Protocol **data(objc_copyProtocolList(&size));
2011 for (size_t i(0); i != size; ++i)
2012 JSPropertyNameAccumulatorAddName(names, CYJSString(protocol_getName(data[i])));
2013 free(data);
aabea98c
JF
2014#else
2015 // XXX: fix this!
2016#endif
c239b9f8 2017}
707bcb93 2018
aabea98c 2019#ifdef __APPLE__
534fb6da 2020static bool stret(ffi_type *ffi_type) {
04450da0
JF
2021 return ffi_type->type == FFI_TYPE_STRUCT && (
2022 ffi_type->size > OBJC_MAX_STRUCT_BY_VALUE ||
2023 struct_forward_array[ffi_type->size] != 0
2024 );
2025}
aabea98c 2026#endif
b09da87b 2027
3c1c3635 2028JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, Class _class, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception) { CYTry {
7ba62cfd 2029 const char *type;
ea2d184c 2030
cacd1a88
JF
2031 if (_class == NULL)
2032 _class = object_getClass(self);
2033
aabea98c
JF
2034 IMP imp;
2035
2036 if (objc_method *method = class_getInstanceMethod(_class, _cmd)) {
2037 imp = method_getImplementation(method);
4afefdd9 2038 type = method_getTypeEncoding(method);
aabea98c
JF
2039 } else {
2040 imp = NULL;
2041
3c1c3635
JF
2042 CYPoolTry {
2043 NSMethodSignature *method([self methodSignatureForSelector:_cmd]);
2044 if (method == nil)
37954781 2045 throw CYJSError(context, "unrecognized selector %s sent to object %p", sel_getName(_cmd), self);
3c1c3635
JF
2046 type = CYPoolCString(pool, context, [method _typeString]);
2047 } CYPoolCatch(NULL)
4afefdd9
JF
2048 }
2049
2050 void *setup[2];
f623c5d8 2051 setup[0] = &self;
4afefdd9
JF
2052 setup[1] = &_cmd;
2053
2054 sig::Signature signature;
f33b048a 2055 sig::Parse(pool, &signature, type, &Structor_);
4afefdd9
JF
2056
2057 ffi_cif cif;
2058 sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif);
2059
f623c5d8 2060 if (imp == NULL) {
aabea98c
JF
2061#ifdef __APPLE__
2062 if (stret(cif.rtype))
2063 imp = class_getMethodImplementation_stret(_class, _cmd);
2064 else
2065 imp = class_getMethodImplementation(_class, _cmd);
2066#else
f623c5d8 2067 objc_super super = {self, _class};
aabea98c
JF
2068 imp = objc_msg_lookup_super(&super, _cmd);
2069#endif
f623c5d8 2070 }
aabea98c
JF
2071
2072 void (*function)() = reinterpret_cast<void (*)()>(imp);
2b52f27e 2073 return CYCallFunction(pool, context, 2, setup, count, arguments, initialize, exception, &signature, &cif, function);
3c1c3635 2074} CYCatch }
367eebb1 2075
3c1c3635
JF
2076static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2077 if (count < 2)
37954781 2078 throw CYJSError(context, "too few arguments to objc_msgSend");
367eebb1 2079
478d4ed0
JF
2080 CYPool pool;
2081
2b52f27e
JF
2082 bool uninitialized;
2083
4afefdd9
JF
2084 id self;
2085 SEL _cmd;
cacd1a88 2086 Class _class;
4afefdd9 2087
3c1c3635
JF
2088 if (JSValueIsObjectOfClass(context, arguments[0], Super_)) {
2089 cy::Super *internal(reinterpret_cast<cy::Super *>(JSObjectGetPrivate((JSObjectRef) arguments[0])));
2090 self = internal->GetValue();
2091 _class = internal->class_;;
2092 uninitialized = false;
2093 } else if (JSValueIsObjectOfClass(context, arguments[0], Instance_)) {
2094 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) arguments[0])));
2095 self = internal->GetValue();
2096 _class = nil;
2097 uninitialized = internal->IsUninitialized();
2098 if (uninitialized)
2099 internal->value_ = nil;
2100 } else {
2101 self = CYCastNSObject(pool, context, arguments[0]);
2102 _class = nil;
2103 uninitialized = false;
2104 }
2b52f27e 2105
3c1c3635
JF
2106 if (self == nil)
2107 return CYJSNull(context);
7ba62cfd 2108
3c1c3635 2109 _cmd = CYCastSEL(context, arguments[1]);
ea2d184c 2110
cacd1a88 2111 return CYSendMessage(pool, context, self, _class, _cmd, count - 2, arguments + 2, uninitialized, exception);
3c1c3635 2112} CYCatch }
7ba62cfd 2113
365abb0a 2114/* Hook: objc_registerClassPair {{{ */
8ad1528b 2115#if defined(__APPLE__) && defined(__arm__) && 0
365abb0a
JF
2116// XXX: replace this with associated objects
2117
272c3da3 2118MSHook(void, CYDealloc, id self, SEL sel) {
61933e16
JF
2119 CYInternal *internal;
2120 object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal));
2121 if (internal != NULL)
2122 delete internal;
272c3da3 2123 _CYDealloc(self, sel);
61933e16 2124}
f7c38a29 2125
61933e16
JF
2126MSHook(void, objc_registerClassPair, Class _class) {
2127 Class super(class_getSuperclass(_class));
2128 if (super == NULL || class_getInstanceVariable(super, "cy$internal_") == NULL) {
2129 class_addIvar(_class, "cy$internal_", sizeof(CYInternal *), log2(sizeof(CYInternal *)), "^{CYInternal}");
272c3da3 2130 MSHookMessage(_class, @selector(dealloc), MSHake(CYDealloc));
f7c38a29
JF
2131 }
2132
61933e16 2133 _objc_registerClassPair(_class);
f7c38a29
JF
2134}
2135
3c1c3635
JF
2136static JSValueRef objc_registerClassPair_(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2137 if (count != 1)
37954781 2138 throw CYJSError(context, "incorrect number of arguments to objc_registerClassPair");
3c1c3635
JF
2139 CYPool pool;
2140 NSObject *value(CYCastNSObject(pool, context, arguments[0]));
2141 if (value == NULL || !CYIsClass(value))
37954781 2142 throw CYJSError(context, "incorrect number of arguments to objc_registerClassPair");
3c1c3635
JF
2143 Class _class((Class) value);
2144 $objc_registerClassPair(_class);
d447cc5e 2145 return CYJSUndefined(context);
3c1c3635 2146} CYCatch }
b5dd57dc 2147#endif
3c1c3635 2148/* }}} */
d447cc5e 2149
dea834b0
JF
2150static JSValueRef Selector_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2151 JSValueRef setup[count + 2];
2152 setup[0] = _this;
2153 setup[1] = object;
4afefdd9 2154 memcpy(setup + 2, arguments, sizeof(JSValueRef) * count);
dea834b0
JF
2155 return $objc_msgSend(context, NULL, NULL, count + 2, setup, exception);
2156}
2157
dc68b74c
JF
2158static JSValueRef Message_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2159 CYPool pool;
2160 Message_privateData *internal(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate(object)));
2161
2162 // XXX: handle Instance::Uninitialized?
2163 id self(CYCastNSObject(pool, context, _this));
2164
2165 void *setup[2];
2166 setup[0] = &self;
2167 setup[1] = &internal->sel_;
2168
2169 return CYCallFunction(pool, context, 2, setup, count, arguments, false, exception, &internal->signature_, &internal->cif_, internal->GetValue());
2170}
2171
3c1c3635
JF
2172static JSObjectRef Super_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2173 if (count != 2)
37954781 2174 throw CYJSError(context, "incorrect number of arguments to Super constructor");
4afefdd9 2175 CYPool pool;
3c1c3635
JF
2176 id self(CYCastNSObject(pool, context, arguments[0]));
2177 Class _class(CYCastClass(pool, context, arguments[1]));
2178 return cy::Super::Make(context, self, _class);
2179} CYCatch }
bce8339b 2180
3c1c3635
JF
2181static JSObjectRef Selector_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2182 if (count != 1)
37954781
JF
2183 throw CYJSError(context, "incorrect number of arguments to Selector constructor");
2184 CYPool pool;
2185 const char *name(CYPoolCString(pool, context, arguments[0]));
3c1c3635
JF
2186 return CYMakeSelector(context, sel_registerName(name));
2187} CYCatch }
7b184c00 2188
3c1c3635
JF
2189static JSObjectRef Instance_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2190 if (count > 1)
37954781 2191 throw CYJSError(context, "incorrect number of arguments to Instance constructor");
3c1c3635 2192 id self(count == 0 ? nil : CYCastPointer<id>(context, arguments[0]));
15024d7e 2193 return CYMakeInstance(context, self, false);
3c1c3635 2194} CYCatch }
ea2d184c 2195
534fb6da 2196static JSValueRef CYValue_getProperty_value(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
61933e16
JF
2197 CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(object)));
2198 return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_));
04450da0
JF
2199}
2200
7b184c00
JF
2201static JSValueRef CYValue_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2202 CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this)));
2203 Type_privateData *typical(internal->GetType());
2204
2205 sig::Type *type;
2206 ffi_type *ffi;
2207
2208 if (typical == NULL) {
2209 type = NULL;
2210 ffi = NULL;
2211 } else {
2212 type = typical->type_;
2213 ffi = typical->ffi_;
2214 }
2215
53ba31e9 2216 return CYMakePointer(context, &internal->value_, _not(size_t), type, ffi, object);
7b184c00 2217}
4afefdd9 2218
dc68b74c
JF
2219static JSValueRef Instance_getProperty_constructor(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
2220 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
ec599edf 2221 return Instance::Make(context, (id) object_getClass(internal->GetValue()));
dc68b74c
JF
2222}
2223
2f1295ff 2224static JSValueRef Instance_getProperty_prototype(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
365abb0a
JF
2225 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
2226 id self(internal->GetValue());
2227 if (!CYIsClass(self))
2228 return CYJSUndefined(context);
3c1c3635
JF
2229 return CYGetClassPrototype(context, self);
2230} CYCatch }
365abb0a
JF
2231
2232static JSValueRef Instance_getProperty_messages(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
dc68b74c
JF
2233 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
2234 id self(internal->GetValue());
b5dd57dc 2235 if (!CYIsClass(self))
dc68b74c 2236 return CYJSUndefined(context);
b5dd57dc 2237 return Messages::Make(context, (Class) self);
dc68b74c
JF
2238}
2239
3c1c3635 2240static JSValueRef Instance_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
365abb0a
JF
2241 if (!JSValueIsObjectOfClass(context, _this, Instance_))
2242 return NULL;
2243
7b184c00 2244 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
8665da0c 2245 return CYCastJSValue(context, CYJSString(context, CYCastNSCYON(internal->GetValue())));
3c1c3635 2246} CYCatch }
7b184c00 2247
3c1c3635 2248static JSValueRef Instance_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
365abb0a
JF
2249 if (!JSValueIsObjectOfClass(context, _this, Instance_))
2250 return NULL;
2251
7b184c00
JF
2252 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2253
3c1c3635 2254 CYPoolTry {
b5dd57dc
JF
2255 NSString *key;
2256 if (count == 0)
2257 key = nil;
2258 else
2259 key = CYCastNSString(NULL, context, CYJSString(context, arguments[0]));
3c1c3635 2260 // XXX: check for support of cy$toJSON?
8665da0c 2261 return CYCastJSValue(context, CYJSString(context, [internal->GetValue() cy$toJSON:key]));
3c1c3635 2262 } CYPoolCatch(NULL)
7c6c5b0a 2263} CYCatch return /*XXX*/ NULL; }
b4aa79af 2264
20ded97a 2265#if 0
c30687a7
JF
2266static JSValueRef Instance_callAsFunction_valueOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2267 if (!JSValueIsObjectOfClass(context, _this, Instance_))
2268 return NULL;
2269
2270 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2271 return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->GetValue()));
2272} CYCatch return /*XXX*/ NULL; }
20ded97a
JF
2273#endif
2274
2275static JSValueRef Instance_callAsFunction_toPointer(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2276 if (!JSValueIsObjectOfClass(context, _this, Instance_))
2277 return NULL;
2278
2279 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2280 // XXX: but... but... THIS ISN'T A POINTER! :(
2281 return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->GetValue()));
2282} CYCatch return /*XXX*/ NULL; }
c30687a7 2283
3c1c3635 2284static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
365abb0a
JF
2285 if (!JSValueIsObjectOfClass(context, _this, Instance_))
2286 return NULL;
2287
9e562cfc 2288 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
7b184c00 2289
7db9993a
JF
2290 id value(internal->GetValue());
2291 if (value == nil)
2292 return CYCastJSValue(context, "nil");
2293
3c1c3635
JF
2294 CYPoolTry {
2295 // XXX: this seems like a stupid implementation; what if it crashes? why not use the CYONifier backend?
8665da0c 2296 return CYCastJSValue(context, CYJSString(context, [internal->GetValue() description]));
3c1c3635 2297 } CYPoolCatch(NULL)
7c6c5b0a 2298} CYCatch return /*XXX*/ NULL; }
478d4ed0 2299
3c1c3635 2300static JSValueRef Selector_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
9e562cfc 2301 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
3c1c3635
JF
2302 return CYCastJSValue(context, sel_getName(internal->GetValue()));
2303} CYCatch }
478d4ed0 2304
b4aa79af
JF
2305static JSValueRef Selector_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2306 return Selector_callAsFunction_toString(context, object, _this, count, arguments, exception);
2307}
2308
3c1c3635 2309static JSValueRef Selector_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
7b184c00
JF
2310 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
2311 const char *name(sel_getName(internal->GetValue()));
2312
3c1c3635 2313 CYPoolTry {
95678376
JF
2314 NSString *string([NSString stringWithFormat:@"@selector(%s)", name]);
2315 return CYCastJSValue(context, CYJSString(context, string));
3c1c3635 2316 } CYPoolCatch(NULL)
7c6c5b0a 2317} CYCatch return /*XXX*/ NULL; }
e5bc40db 2318
3c1c3635
JF
2319static JSValueRef Selector_callAsFunction_type(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2320 if (count != 1)
37954781
JF
2321 throw CYJSError(context, "incorrect number of arguments to Selector.type");
2322
3c1c3635
JF
2323 CYPool pool;
2324 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
37954781
JF
2325 SEL sel(internal->GetValue());
2326
2327 objc_method *method;
2328 if (Class _class = CYCastClass(pool, context, arguments[0]))
2329 method = class_getInstanceMethod(_class, sel);
2330 else
2331 method = NULL;
2332
2333 if (const char *type = CYPoolTypeEncoding(pool, context, sel, method))
2334 return CYCastJSValue(context, CYJSString(type));
e5bc40db 2335
3c1c3635
JF
2336 return CYJSNull(context);
2337} CYCatch }
e5bc40db 2338
856b8cd0 2339static JSStaticValue Selector_staticValues[2] = {
61933e16 2340 {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete},
04450da0
JF
2341 {NULL, NULL, NULL, 0}
2342};
953647c1 2343
365abb0a 2344static JSStaticValue Instance_staticValues[5] = {
9e562cfc 2345 {"constructor", &Instance_getProperty_constructor, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
365abb0a 2346 {"messages", &Instance_getProperty_messages, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2f1295ff 2347 {"prototype", &Instance_getProperty_prototype, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
9e562cfc 2348 {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
9b5527f0
JF
2349 {NULL, NULL, NULL, 0}
2350};
2351
c30687a7 2352static JSStaticFunction Instance_staticFunctions[6] = {
7b184c00 2353 {"$cya", &CYValue_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
b4aa79af
JF
2354 {"toCYON", &Instance_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2355 {"toJSON", &Instance_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
20ded97a
JF
2356 //{"valueOf", &Instance_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2357 {"toPointer", &Instance_callAsFunction_toPointer, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
478d4ed0
JF
2358 {"toString", &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2359 {NULL, NULL, 0}
2360};
2361
9b5527f0
JF
2362static JSStaticFunction Internal_staticFunctions[2] = {
2363 {"$cya", &Internal_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2364 {NULL, NULL, 0}
2365};
2366
b4aa79af
JF
2367static JSStaticFunction Selector_staticFunctions[5] = {
2368 {"toCYON", &Selector_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2369 {"toJSON", &Selector_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
478d4ed0 2370 {"toString", &Selector_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
b09da87b
JF
2371 {"type", &Selector_callAsFunction_type, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2372 {NULL, NULL, 0}
2373};
ea2d184c 2374
6732f195
JF
2375static JSStaticFunction StringInstance_staticFunctions[2] = {
2376 //{"valueOf", &Instance_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2377 {"toString", &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2378 {NULL, NULL, 0}
2379};
2380
3c9f7e22 2381void CYObjectiveC_Initialize() { /*XXX*/ JSContextRef context(NULL); CYPoolTry {
37954781
JF
2382 apr_pool_t *pool(CYGetGlobalPool());
2383
807a88be
JF
2384 Object_type = new(pool) Type_privateData("@");
2385 Selector_type = new(pool) Type_privateData(":");
7b184c00 2386
b53b30c1 2387#ifdef __APPLE__
c1582939 2388 NSCFBoolean_ = objc_getClass("NSCFBoolean");
c239b9f8 2389 NSCFType_ = objc_getClass("NSCFType");
aaa4a269 2390 NSGenericDeallocHandler_ = objc_getClass("__NSGenericDeallocHandler");
b5dd57dc
JF
2391 NSMessageBuilder_ = objc_getClass("NSMessageBuilder");
2392 NSZombie_ = objc_getClass("_NSZombie_");
c074e774
JF
2393#else
2394 NSBoolNumber_ = objc_getClass("NSBoolNumber");
b53b30c1
JF
2395#endif
2396
2397 NSArray_ = objc_getClass("NSArray");
b5dd57dc 2398 NSDictionary_ = objc_getClass("NSDictionary");
4cb8aa43 2399 NSString_ = objc_getClass("NSString");
c239b9f8 2400 Object_ = objc_getClass("Object");
3c1c3635
JF
2401
2402 JSClassDefinition definition;
2403
2404 definition = kJSClassDefinitionEmpty;
2405 definition.className = "Instance";
2406 definition.staticValues = Instance_staticValues;
2407 definition.staticFunctions = Instance_staticFunctions;
2408 definition.hasProperty = &Instance_hasProperty;
2409 definition.getProperty = &Instance_getProperty;
2410 definition.setProperty = &Instance_setProperty;
2411 definition.deleteProperty = &Instance_deleteProperty;
2412 definition.getPropertyNames = &Instance_getPropertyNames;
2413 definition.callAsConstructor = &Instance_callAsConstructor;
2414 definition.hasInstance = &Instance_hasInstance;
37954781 2415 definition.finalize = &CYFinalize;
3c1c3635
JF
2416 Instance_ = JSClassCreate(&definition);
2417
2418 definition = kJSClassDefinitionEmpty;
2419 definition.className = "Internal";
2420 definition.staticFunctions = Internal_staticFunctions;
2421 definition.hasProperty = &Internal_hasProperty;
2422 definition.getProperty = &Internal_getProperty;
2423 definition.setProperty = &Internal_setProperty;
2424 definition.getPropertyNames = &Internal_getPropertyNames;
37954781 2425 definition.finalize = &CYFinalize;
3c1c3635
JF
2426 Internal_ = JSClassCreate(&definition);
2427
2428 definition = kJSClassDefinitionEmpty;
2429 definition.className = "Message";
37954781 2430 definition.staticFunctions = cy::Functor::StaticFunctions;
3c1c3635 2431 definition.callAsFunction = &Message_callAsFunction;
37954781 2432 definition.finalize = &CYFinalize;
3c1c3635
JF
2433 Message_ = JSClassCreate(&definition);
2434
2435 definition = kJSClassDefinitionEmpty;
2436 definition.className = "Messages";
2437 definition.hasProperty = &Messages_hasProperty;
2438 definition.getProperty = &Messages_getProperty;
2439 definition.setProperty = &Messages_setProperty;
7c6c5b0a 2440#if 0 && OBJC_API_VERSION < 2
3c1c3635
JF
2441 definition.deleteProperty = &Messages_deleteProperty;
2442#endif
2443 definition.getPropertyNames = &Messages_getPropertyNames;
37954781 2444 definition.finalize = &CYFinalize;
3c1c3635
JF
2445 Messages_ = JSClassCreate(&definition);
2446
2447 definition = kJSClassDefinitionEmpty;
2448 definition.className = "Selector";
2449 definition.staticValues = Selector_staticValues;
2450 definition.staticFunctions = Selector_staticFunctions;
2451 definition.callAsFunction = &Selector_callAsFunction;
37954781 2452 definition.finalize = &CYFinalize;
3c1c3635
JF
2453 Selector_ = JSClassCreate(&definition);
2454
6732f195
JF
2455 definition = kJSClassDefinitionEmpty;
2456 definition.className = "StringInstance";
2457 definition.staticFunctions = StringInstance_staticFunctions;
2458 StringInstance_ = JSClassCreate(&definition);
2459
3c1c3635
JF
2460 definition = kJSClassDefinitionEmpty;
2461 definition.className = "Super";
2462 definition.staticFunctions = Internal_staticFunctions;
37954781 2463 definition.finalize = &CYFinalize;
3c1c3635
JF
2464 Super_ = JSClassCreate(&definition);
2465
2466 definition = kJSClassDefinitionEmpty;
2467 definition.className = "ObjectiveC::Classes";
2468 definition.getProperty = &ObjectiveC_Classes_getProperty;
2469 definition.getPropertyNames = &ObjectiveC_Classes_getPropertyNames;
2470 ObjectiveC_Classes_ = JSClassCreate(&definition);
2471
7c6c5b0a 2472#if OBJC_API_VERSION >= 2
3c1c3635
JF
2473 definition = kJSClassDefinitionEmpty;
2474 definition.className = "ObjectiveC::Images";
2475 definition.getProperty = &ObjectiveC_Images_getProperty;
2476 definition.getPropertyNames = &ObjectiveC_Images_getPropertyNames;
2477 ObjectiveC_Images_ = JSClassCreate(&definition);
2478
2479 definition = kJSClassDefinitionEmpty;
2480 definition.className = "ObjectiveC::Image::Classes";
2481 definition.getProperty = &ObjectiveC_Image_Classes_getProperty;
2482 definition.getPropertyNames = &ObjectiveC_Image_Classes_getPropertyNames;
2483 ObjectiveC_Image_Classes_ = JSClassCreate(&definition);
3c9f7e22 2484#endif
3c1c3635 2485
3c9f7e22
JF
2486 definition = kJSClassDefinitionEmpty;
2487 definition.className = "ObjectiveC::Protocols";
2488 definition.getProperty = &ObjectiveC_Protocols_getProperty;
2489 definition.getPropertyNames = &ObjectiveC_Protocols_getPropertyNames;
2490 ObjectiveC_Protocols_ = JSClassCreate(&definition);
2491
8ad1528b 2492#if defined(__APPLE__) && defined(__arm__) && 0
3c9f7e22
JF
2493 MSHookFunction(&objc_registerClassPair, MSHake(objc_registerClassPair));
2494#endif
2495
2496#ifdef __APPLE__
2497 class_addMethod(NSCFType_, @selector(cy$toJSON:), reinterpret_cast<IMP>(&NSCFType$cy$toJSON), "@12@0:4@8");
2498#endif
2499} CYPoolCatch() }
2500
2501void CYObjectiveC_SetupContext(JSContextRef context) { CYPoolTry {
2502 JSObjectRef global(CYGetGlobalObject(context));
498c3570 2503 JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s)));
2385c806
JF
2504 JSObjectRef cycript(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Cycript"))));
2505 JSObjectRef all(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("all"))));
3c9f7e22
JF
2506
2507 JSObjectRef ObjectiveC(JSObjectMake(context, NULL, NULL));
2385c806 2508 CYSetProperty(context, cycript, CYJSString("ObjectiveC"), ObjectiveC);
3c9f7e22
JF
2509
2510 CYSetProperty(context, ObjectiveC, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Classes_, NULL));
2511 CYSetProperty(context, ObjectiveC, CYJSString("protocols"), JSObjectMake(context, ObjectiveC_Protocols_, NULL));
2512
2513#if OBJC_API_VERSION >= 2
3c1c3635 2514 CYSetProperty(context, ObjectiveC, CYJSString("images"), JSObjectMake(context, ObjectiveC_Images_, NULL));
aabea98c 2515#endif
3c1c3635
JF
2516
2517 JSObjectRef Instance(JSObjectMakeConstructor(context, Instance_, &Instance_new));
2518 JSObjectRef Message(JSObjectMakeConstructor(context, Message_, NULL));
2519 JSObjectRef Selector(JSObjectMakeConstructor(context, Selector_, &Selector_new));
6732f195 2520 JSObjectRef StringInstance(JSObjectMakeConstructor(context, StringInstance_, NULL));
3c1c3635
JF
2521 JSObjectRef Super(JSObjectMakeConstructor(context, Super_, &Super_new));
2522
498c3570
JF
2523 JSObjectRef Instance_prototype(CYCastJSObject(context, CYGetProperty(context, Instance, prototype_s)));
2524 CYSetProperty(context, cy, CYJSString("Instance_prototype"), Instance_prototype);
3c1c3635 2525
6732f195
JF
2526 JSObjectRef StringInstance_prototype(CYCastJSObject(context, CYGetProperty(context, StringInstance, prototype_s)));
2527 CYSetProperty(context, cy, CYJSString("StringInstance_prototype"), StringInstance_prototype);
2528
2529 JSObjectRef String_prototype(CYGetCachedObject(context, CYJSString("String_prototype")));
2530 JSObjectSetPrototype(context, StringInstance_prototype, String_prototype);
2531
2385c806
JF
2532 CYSetProperty(context, cycript, CYJSString("Instance"), Instance);
2533 CYSetProperty(context, cycript, CYJSString("Selector"), Selector);
2534 CYSetProperty(context, cycript, CYJSString("Super"), Super);
3c1c3635 2535
8ad1528b 2536#if defined(__APPLE__) && defined(__arm__) && 0
98ea05a3 2537 CYSetProperty(context, all, CYJSString("objc_registerClassPair"), &objc_registerClassPair_, kJSPropertyAttributeDontEnum);
b5dd57dc
JF
2538#endif
2539
98ea05a3 2540 CYSetProperty(context, all, CYJSString("objc_msgSend"), &$objc_msgSend, kJSPropertyAttributeDontEnum);
3c1c3635 2541
498c3570
JF
2542 JSObjectRef Function_prototype(CYGetCachedObject(context, CYJSString("Function_prototype")));
2543 JSObjectSetPrototype(context, CYCastJSObject(context, CYGetProperty(context, Message, prototype_s)), Function_prototype);
2544 JSObjectSetPrototype(context, CYCastJSObject(context, CYGetProperty(context, Selector, prototype_s)), Function_prototype);
3c9f7e22 2545} CYPoolCatch() }
b5dd57dc
JF
2546
2547static CYHooks CYObjectiveCHooks = {
2548 &CYObjectiveC_ExecuteStart,
2549 &CYObjectiveC_ExecuteEnd,
2550 &CYObjectiveC_RuntimeProperty,
2551 &CYObjectiveC_CallFunction,
3c9f7e22 2552 &CYObjectiveC_Initialize,
b5dd57dc
JF
2553 &CYObjectiveC_SetupContext,
2554 &CYObjectiveC_PoolFFI,
2555 &CYObjectiveC_FromFFI,
2556};
2557
2558struct CYObjectiveC {
2559 CYObjectiveC() {
2560 hooks_ = &CYObjectiveCHooks;
6faa533c
DWT
2561 // XXX: evil magic juju to make this actually take effect on a Mac when compiled with autoconf/libtool doom!
2562 _assert(hooks_ != NULL);
b5dd57dc
JF
2563 }
2564} CYObjectiveC;