1 #if defined(__APPLE__) && defined(__arm__)
4 #include <objc/objc-api.h>
7 #include "ObjectiveC/Internal.hpp"
13 #include <Foundation/Foundation.h>
15 #include <objc/Protocol.h>
17 #include "cycript.hpp"
19 #include "ObjectiveC/Internal.hpp"
22 #include <CoreFoundation/CoreFoundation.h>
23 #include <JavaScriptCore/JSStringRefCF.h>
24 #include <WebKit/WebScriptObject.h>
28 #include "JavaScript.hpp"
34 #define CYObjectiveTry_(context) { \
35 JSContextRef context_(context); \
37 #define CYObjectiveTry { \
39 #define CYObjectiveCatch \
40 catch (const CYException &error) { \
41 @throw CYCastNSObject(NULL, context_, error.CastJSValue(context_)); \
47 NSAutoreleasePool *_pool([[NSAutoreleasePool alloc] init]); \
49 #define CYPoolCatch(value) \
50 @catch (NSException *error) { \
51 _saved = [error retain]; \
52 throw CYJSError(context, CYCastJSValue(context, error)); \
57 [_saved autorelease]; \
62 #define class_getSuperclass GSObjCSuper
63 #define class_getInstanceVariable GSCGetInstanceVariableDefinition
64 #define class_getName GSNameFromClass
66 #define class_removeMethods(cls, list) GSRemoveMethodList(cls, list, YES)
68 #define ivar_getName(ivar) ((ivar)->ivar_name)
69 #define ivar_getOffset(ivar) ((ivar)->ivar_offset)
70 #define ivar_getTypeEncoding(ivar) ((ivar)->ivar_type)
72 #define method_getName(method) ((method)->method_name)
73 #define method_getImplementation(method) ((method)->method_imp)
74 #define method_getTypeEncoding(method) ((method)->method_types)
75 #define method_setImplementation(method, imp) ((void) ((method)->method_imp = (imp)))
77 #define objc_getProtocol GSProtocolFromName
79 #define object_getClass GSObjCClass
81 #define object_getInstanceVariable(object, name, value) ({ \
82 objc_ivar *ivar(class_getInstanceVariable(object_getClass(object), name)); \
83 GSObjCGetVariable(object, ivar_getOffset(ivar), sizeof(void *), value); \
87 #define object_setIvar(object, ivar, value) ({ \
88 void *data = (value); \
89 GSObjCSetVariable(object, ivar_getOffset(ivar), sizeof(void *), &data); \
92 #define protocol_getName(protocol) [(protocol) name]
95 JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, Class super, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception);
97 extern sqlite3 *Bridge_;
99 /* Objective-C Pool Release {{{ */
100 apr_status_t CYPoolRelease_(void *data) {
101 id object(reinterpret_cast<id>(data));
106 id CYPoolRelease_(apr_pool_t *pool, id object) {
109 else if (pool == NULL)
110 return [object autorelease];
112 apr_pool_cleanup_register(pool, object, &CYPoolRelease_, &apr_pool_cleanup_null);
117 template <typename Type_>
118 Type_ CYPoolRelease(apr_pool_t *pool, Type_ object) {
119 return (Type_) CYPoolRelease_(pool, (id) object);
122 /* Objective-C Strings {{{ */
123 const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, NSString *value) {
125 return [value UTF8String];
127 size_t size([value maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1);
128 char *string(new(pool) char[size]);
129 if (![value getCString:string maxLength:size encoding:NSUTF8StringEncoding])
130 throw CYJSError(context, "[NSString getCString:maxLength:encoding:] == NO");
135 JSStringRef CYCopyJSString(JSContextRef context, NSString *value) {
137 return JSStringCreateWithCFString(reinterpret_cast<CFStringRef>(value));
140 return CYCopyJSString(CYPoolCString(pool, context, value));
144 JSStringRef CYCopyJSString(JSContextRef context, NSObject *value) {
147 // XXX: this definition scares me; is anyone using this?!
148 NSString *string([value description]);
149 return CYCopyJSString(context, string);
152 NSString *CYCopyNSString(const CYUTF8String &value) {
154 return (NSString *) CFStringCreateWithBytes(kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(value.data), value.size, kCFStringEncodingUTF8, true);
156 return [[NSString alloc] initWithBytes:value.data length:value.size encoding:NSUTF8StringEncoding];
160 NSString *CYCopyNSString(JSContextRef context, JSStringRef value) {
162 return (NSString *) JSStringCopyCFString(kCFAllocatorDefault, value);
165 return CYCopyNSString(CYPoolUTF8String(pool, context, value));
169 NSString *CYCopyNSString(JSContextRef context, JSValueRef value) {
170 return CYCopyNSString(context, CYJSString(context, value));
173 NSString *CYCastNSString(apr_pool_t *pool, const CYUTF8String &value) {
174 return CYPoolRelease(pool, CYCopyNSString(value));
177 NSString *CYCastNSString(apr_pool_t *pool, SEL sel) {
178 const char *name(sel_getName(sel));
179 return CYPoolRelease(pool, CYCopyNSString(CYUTF8String(name, strlen(name))));
182 NSString *CYCastNSString(apr_pool_t *pool, JSContextRef context, JSStringRef value) {
183 return CYPoolRelease(pool, CYCopyNSString(context, value));
186 CYUTF8String CYCastUTF8String(NSString *value) {
187 NSData *data([value dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO]);
188 return CYUTF8String(reinterpret_cast<const char *>([data bytes]), [data length]);
192 void CYThrow(JSContextRef context, NSException *error, JSValueRef *exception) {
193 if (exception == NULL)
195 *exception = CYCastJSValue(context, error);
198 size_t CYGetIndex(NSString *value) {
199 return CYGetIndex(CYCastUTF8String(value));
202 bool CYGetOffset(apr_pool_t *pool, JSContextRef context, NSString *value, ssize_t &index) {
203 return CYGetOffset(CYPoolCString(pool, context, value), index);
206 static JSClassRef Instance_;
207 static JSClassRef Internal_;
208 static JSClassRef Message_;
209 static JSClassRef Messages_;
210 static JSClassRef Selector_;
211 static JSClassRef Super_;
213 static JSClassRef ObjectiveC_Classes_;
214 static JSClassRef ObjectiveC_Protocols_;
217 static JSClassRef ObjectiveC_Image_Classes_;
218 static JSClassRef ObjectiveC_Images_;
221 static JSObjectRef Instance_prototype_;
224 static Class NSCFBoolean_;
225 static Class NSCFType_;
226 static Class NSMessageBuilder_;
227 static Class NSZombie_;
230 static Class NSArray_;
231 static Class NSDictionary_;
232 static Class Object_;
234 static Type_privateData *Object_type;
235 static Type_privateData *Selector_type;
237 Type_privateData *Instance::GetType() const {
241 Type_privateData *Selector_privateData::GetType() const {
242 return Selector_type;
245 // XXX: trick this out with associated objects!
246 JSValueRef CYGetClassPrototype(JSContextRef context, id self) {
248 return Instance_prototype_;
250 // XXX: I need to think through multi-context
251 typedef std::map<id, JSValueRef> CacheMap;
252 static CacheMap cache_;
254 JSValueRef &value(cache_[self]);
258 JSClassRef _class(NULL);
259 JSValueRef prototype;
261 if (self == NSArray_)
262 prototype = Array_prototype_;
263 else if (self == NSDictionary_)
264 prototype = Object_prototype_;
266 prototype = CYGetClassPrototype(context, class_getSuperclass(self));
268 JSObjectRef object(JSObjectMake(context, _class, NULL));
269 JSObjectSetPrototype(context, object, prototype);
271 JSValueProtect(context, object);
276 JSObjectRef Messages::Make(JSContextRef context, Class _class, bool array) {
277 JSObjectRef value(JSObjectMake(context, Messages_, new Messages(_class)));
278 if (_class == NSArray_)
280 if (Class super = class_getSuperclass(_class))
281 JSObjectSetPrototype(context, value, Messages::Make(context, super, array));
283 JSObjectSetPrototype(context, value, Array_prototype_);*/
287 JSObjectRef Internal::Make(JSContextRef context, id object, JSObjectRef owner) {
288 return JSObjectMake(context, Internal_, new Internal(object, context, owner));
292 JSObjectRef Super::Make(JSContextRef context, id object, Class _class) {
293 JSObjectRef value(JSObjectMake(context, Super_, new Super(object, _class)));
297 JSObjectRef Instance::Make(JSContextRef context, id object, Flags flags) {
298 JSObjectRef value(JSObjectMake(context, Instance_, new Instance(object, flags)));
299 JSObjectSetPrototype(context, value, CYGetClassPrototype(context, object_getClass(object)));
303 Instance::~Instance() {
304 if ((flags_ & Transient) == 0)
305 // XXX: does this handle background threads correctly?
306 // XXX: this simply does not work on the console because I'm stupid
307 [GetValue() performSelector:@selector(release) withObject:nil afterDelay:0];
310 struct Message_privateData :
315 Message_privateData(SEL sel, const char *type, IMP value = NULL) :
316 cy::Functor(type, reinterpret_cast<void (*)()>(value)),
322 JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient) {
323 Instance::Flags flags;
326 flags = Instance::Transient;
328 flags = Instance::None;
329 object = [object retain];
332 return Instance::Make(context, object, flags);
335 @interface NSMethodSignature (Cycript)
336 - (NSString *) _typeString;
339 @interface NSObject (Cycript)
341 - (JSValueRef) cy$JSValueInContext:(JSContextRef)context;
342 - (JSType) cy$JSType;
344 - (NSObject *) cy$toJSON:(NSString *)key;
345 - (NSString *) cy$toCYON;
346 - (NSString *) cy$toKey;
348 - (bool) cy$hasProperty:(NSString *)name;
349 - (NSObject *) cy$getProperty:(NSString *)name;
350 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value;
351 - (bool) cy$deleteProperty:(NSString *)name;
352 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context;
357 - (JSValueRef) cy$JSValueInContext:(JSContextRef)context;
360 NSString *CYCastNSCYON(id value) {
366 Class _class(object_getClass(value));
367 SEL sel(@selector(cy$toCYON));
369 if (objc_method *toCYON = class_getInstanceMethod(_class, sel))
370 string = reinterpret_cast<NSString *(*)(id, SEL)>(method_getImplementation(toCYON))(value, sel);
371 else if (objc_method *methodSignatureForSelector = class_getInstanceMethod(_class, @selector(methodSignatureForSelector:))) {
372 if (reinterpret_cast<NSMethodSignature *(*)(id, SEL, SEL)>(method_getImplementation(methodSignatureForSelector))(value, @selector(methodSignatureForSelector:), sel) != nil)
373 string = [value cy$toCYON];
378 else if (value == NSZombie_)
379 string = @"_NSZombie_";
380 else if (_class == NSZombie_)
381 string = [NSString stringWithFormat:@"<_NSZombie_: %p>", value];
382 // XXX: frowny /in/ the pants
383 else if (value == NSMessageBuilder_ || value == Object_)
387 string = [NSString stringWithFormat:@"%@", value];
392 string = @"undefined";
399 struct PropertyAttributes {
404 const char *variable;
417 PropertyAttributes(objc_property_t property) :
429 name = property_getName(property);
430 const char *attributes(property_getAttributes(property));
432 for (char *state, *token(apr_strtok(apr_pstrdup(pool_, attributes), ",", &state)); token != NULL; token = apr_strtok(NULL, ",", &state)) {
434 case 'R': readonly = true; break;
435 case 'C': copy = true; break;
436 case '&': retain = true; break;
437 case 'N': nonatomic = true; break;
438 case 'G': getter_ = token + 1; break;
439 case 'S': setter_ = token + 1; break;
440 case 'V': variable = token + 1; break;
444 /*if (variable == NULL) {
445 variable = property_getName(property);
446 size_t size(strlen(variable));
447 char *name(new(pool_) char[size + 2]);
449 memcpy(name + 1, variable, size);
450 name[size + 1] = '\0';
455 const char *Getter() {
457 getter_ = apr_pstrdup(pool_, name);
461 const char *Setter() {
462 if (setter_ == NULL && !readonly) {
463 size_t length(strlen(name));
465 char *temp(new(pool_) char[length + 5]);
471 temp[3] = toupper(name[0]);
472 memcpy(temp + 4, name + 1, length - 1);
475 temp[length + 3] = ':';
476 temp[length + 4] = '\0';
487 NSObject *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) {
488 return [(NSString *) CFCopyDescription((CFTypeRef) self) autorelease];
493 @interface CYWebUndefined : NSObject {
496 + (CYWebUndefined *) undefined;
500 @implementation CYWebUndefined
502 + (CYWebUndefined *) undefined {
503 static CYWebUndefined *instance_([[CYWebUndefined alloc] init]);
509 #define WebUndefined CYWebUndefined
512 /* Bridge: CYJSObject {{{ */
513 @interface CYJSObject : NSMutableDictionary {
515 JSContextRef context_;
518 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context;
520 - (NSObject *) cy$toJSON:(NSString *)key;
522 - (NSUInteger) count;
523 - (id) objectForKey:(id)key;
524 - (NSEnumerator *) keyEnumerator;
525 - (void) setObject:(id)object forKey:(id)key;
526 - (void) removeObjectForKey:(id)key;
530 /* Bridge: CYJSArray {{{ */
531 @interface CYJSArray : NSMutableArray {
533 JSContextRef context_;
536 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context;
538 - (NSUInteger) count;
539 - (id) objectAtIndex:(NSUInteger)index;
541 - (void) addObject:(id)anObject;
542 - (void) insertObject:(id)anObject atIndex:(NSUInteger)index;
543 - (void) removeLastObject;
544 - (void) removeObjectAtIndex:(NSUInteger)index;
545 - (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
550 NSObject *CYCastNSObject_(apr_pool_t *pool, JSContextRef context, JSObjectRef object) {
551 JSValueRef exception(NULL);
552 bool array(JSValueIsInstanceOfConstructor(context, object, Array_, &exception));
553 CYThrow(context, exception);
554 id value(array ? [CYJSArray alloc] : [CYJSObject alloc]);
555 return CYPoolRelease(pool, [value initWithJSObject:object inContext:context]);
558 NSObject *CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSObjectRef object) {
559 if (!JSValueIsObjectOfClass(context, object, Instance_))
560 return CYCastNSObject_(pool, context, object);
562 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
563 return internal->GetValue();
567 NSNumber *CYCopyNSNumber(JSContextRef context, JSValueRef value) {
568 return [[NSNumber alloc] initWithDouble:CYCastDouble(context, value)];
571 id CYNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value, bool cast) {
575 switch (JSType type = JSValueGetType(context, value)) {
576 case kJSTypeUndefined:
577 object = [WebUndefined undefined];
587 object = (id) (CYCastBool(context, value) ? kCFBooleanTrue : kCFBooleanFalse);
590 object = [[NSNumber alloc] initWithBool:CYCastBool(context, value)];
596 object = CYCopyNSNumber(context, value);
601 object = CYCopyNSString(context, value);
606 // XXX: this might could be more efficient
607 object = CYCastNSObject(pool, context, (JSObjectRef) value);
612 throw CYJSError(context, "JSValueGetType() == 0x%x", type);
619 return CYPoolRelease(pool, object);
621 return [object retain];
624 NSObject *CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
625 return CYNSObject(pool, context, value, true);
628 NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
629 return CYNSObject(pool, context, value, false);
632 /* Bridge: NSArray {{{ */
633 @implementation NSArray (Cycript)
635 - (NSString *) cy$toCYON {
636 NSMutableString *json([[[NSMutableString alloc] init] autorelease]);
637 [json appendString:@"["];
641 for (id object in self) {
643 for (size_t index(0), count([self count]); index != count; ++index) {
644 id object([self objectAtIndex:index]);
647 [json appendString:@","];
650 if (object == nil || [object cy$JSType] != kJSTypeUndefined)
651 [json appendString:CYCastNSCYON(object)];
653 [json appendString:@","];
658 [json appendString:@"]"];
662 - (bool) cy$hasProperty:(NSString *)name {
663 if ([name isEqualToString:@"length"])
666 size_t index(CYGetIndex(name));
667 if (index == _not(size_t) || index >= [self count])
668 return [super cy$hasProperty:name];
673 - (NSObject *) cy$getProperty:(NSString *)name {
674 if ([name isEqualToString:@"length"]) {
675 NSUInteger count([self count]);
677 return [NSNumber numberWithUnsignedInteger:count];
679 return [NSNumber numberWithUnsignedInt:count];
683 size_t index(CYGetIndex(name));
684 if (index == _not(size_t) || index >= [self count])
685 return [super cy$getProperty:name];
687 return [self objectAtIndex:index];
690 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context {
691 [super cy$getPropertyNames:names inContext:context];
693 for (size_t index(0), count([self count]); index != count; ++index) {
694 id object([self objectAtIndex:index]);
695 if (object == nil || [object cy$JSType] != kJSTypeUndefined) {
697 sprintf(name, "%zu", index);
698 JSPropertyNameAccumulatorAddName(names, CYJSString(name));
705 /* Bridge: NSDictionary {{{ */
706 @implementation NSDictionary (Cycript)
708 - (NSString *) cy$toCYON {
709 NSMutableString *json([[[NSMutableString alloc] init] autorelease]);
710 [json appendString:@"{"];
714 for (id key in self) {
716 NSEnumerator *keys([self keyEnumerator]);
717 while (id key = [keys nextObject]) {
720 [json appendString:@","];
723 [json appendString:[key cy$toKey]];
724 [json appendString:@":"];
725 NSObject *object([self objectForKey:key]);
726 [json appendString:CYCastNSCYON(object)];
729 [json appendString:@"}"];
733 - (bool) cy$hasProperty:(NSString *)name {
734 return [self objectForKey:name] != nil;
737 - (NSObject *) cy$getProperty:(NSString *)name {
738 return [self objectForKey:name];
741 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context {
742 [super cy$getPropertyNames:names inContext:context];
745 for (NSString *key in self) {
747 NSEnumerator *keys([self keyEnumerator]);
748 while (NSString *key = [keys nextObject]) {
750 JSPropertyNameAccumulatorAddName(names, CYJSString(context, key));
756 /* Bridge: NSMutableArray {{{ */
757 @implementation NSMutableArray (Cycript)
759 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
760 if ([name isEqualToString:@"length"]) {
761 // XXX: is this not intelligent?
762 NSNumber *number(reinterpret_cast<NSNumber *>(value));
764 NSUInteger size([number unsignedIntegerValue]);
766 NSUInteger size([number unsignedIntValue]);
768 NSUInteger count([self count]);
770 [self removeObjectsInRange:NSMakeRange(size, count - size)];
771 else if (size != count) {
772 WebUndefined *undefined([WebUndefined undefined]);
773 for (size_t i(count); i != size; ++i)
774 [self addObject:undefined];
779 size_t index(CYGetIndex(name));
780 if (index == _not(size_t))
781 return [super cy$setProperty:name to:value];
783 id object(value ?: [NSNull null]);
785 size_t count([self count]);
787 [self replaceObjectAtIndex:index withObject:object];
789 if (index != count) {
790 WebUndefined *undefined([WebUndefined undefined]);
791 for (size_t i(count); i != index; ++i)
792 [self addObject:undefined];
795 [self addObject:object];
801 - (bool) cy$deleteProperty:(NSString *)name {
802 size_t index(CYGetIndex(name));
803 if (index == _not(size_t) || index >= [self count])
804 return [super cy$deleteProperty:name];
805 [self replaceObjectAtIndex:index withObject:[WebUndefined undefined]];
811 /* Bridge: NSMutableDictionary {{{ */
812 @implementation NSMutableDictionary (Cycript)
814 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
815 [self setObject:(value ?: [NSNull null]) forKey:name];
819 - (bool) cy$deleteProperty:(NSString *)name {
820 if ([self objectForKey:name] == nil)
823 [self removeObjectForKey:name];
830 /* Bridge: NSNumber {{{ */
831 @implementation NSNumber (Cycript)
833 - (JSType) cy$JSType {
835 // XXX: this just seems stupid
836 if ([self class] == NSCFBoolean_)
837 return kJSTypeBoolean;
839 return kJSTypeNumber;
842 - (NSObject *) cy$toJSON:(NSString *)key {
846 - (NSString *) cy$toCYON {
847 return [self cy$JSType] != kJSTypeBoolean ? [self stringValue] : [self boolValue] ? @"true" : @"false";
850 - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) {
851 return [self cy$JSType] != kJSTypeBoolean ? CYCastJSValue(context, [self doubleValue]) : CYCastJSValue(context, [self boolValue]);
856 /* Bridge: NSNull {{{ */
857 @implementation NSNull (Cycript)
859 - (JSType) cy$JSType {
863 - (NSObject *) cy$toJSON:(NSString *)key {
867 - (NSString *) cy$toCYON {
873 /* Bridge: NSObject {{{ */
874 @implementation NSObject (Cycript)
876 - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) {
877 return CYMakeInstance(context, self, false);
880 - (JSType) cy$JSType {
881 return kJSTypeObject;
884 - (NSObject *) cy$toJSON:(NSString *)key {
885 return [self description];
888 - (NSString *) cy$toCYON {
889 return [[self cy$toJSON:@""] cy$toCYON];
892 - (NSString *) cy$toKey {
893 return [self cy$toCYON];
896 - (bool) cy$hasProperty:(NSString *)name {
900 - (NSObject *) cy$getProperty:(NSString *)name {
904 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
908 - (bool) cy$deleteProperty:(NSString *)name {
912 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context {
917 /* Bridge: NSProxy {{{ */
918 @implementation NSProxy (Cycript)
920 - (NSObject *) cy$toJSON:(NSString *)key {
921 return [self description];
924 - (NSString *) cy$toCYON {
925 return [[self cy$toJSON:@""] cy$toCYON];
930 /* Bridge: NSString {{{ */
931 @implementation NSString (Cycript)
933 - (JSType) cy$JSType {
934 return kJSTypeString;
937 - (NSObject *) cy$toJSON:(NSString *)key {
941 - (NSString *) cy$toCYON {
942 std::ostringstream str;
943 CYUTF8String string(CYCastUTF8String(self));
944 CYStringify(str, string.data, string.size);
945 std::string value(str.str());
946 return CYCastNSString(NULL, CYUTF8String(value.c_str(), value.size()));
949 - (NSString *) cy$toKey {
950 if (CYIsKey(CYCastUTF8String(self)))
952 return [self cy$toCYON];
957 /* Bridge: WebUndefined {{{ */
958 @implementation WebUndefined (Cycript)
960 - (JSType) cy$JSType {
961 return kJSTypeUndefined;
964 - (NSObject *) cy$toJSON:(NSString *)key {
968 - (NSString *) cy$toCYON {
972 - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) {
973 return CYJSUndefined(context);
979 static bool CYIsClass(id self) {
981 // XXX: this is a lame object_isClass
982 return class_getInstanceMethod(object_getClass(self), @selector(alloc)) != NULL;
984 return GSObjCIsClass(self);
988 Class CYCastClass(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
989 id self(CYCastNSObject(pool, context, value));
992 throw CYJSError(context, "got something that is not a Class");
996 NSArray *CYCastNSArray(JSContextRef context, JSPropertyNameArrayRef names) {
998 size_t size(JSPropertyNameArrayGetCount(names));
999 NSMutableArray *array([NSMutableArray arrayWithCapacity:size]);
1000 for (size_t index(0); index != size; ++index)
1001 [array addObject:CYCastNSString(pool, context, JSPropertyNameArrayGetNameAtIndex(names, index))];
1005 JSValueRef CYCastJSValue(JSContextRef context, id value) { CYPoolTry {
1007 return CYJSNull(context);
1008 else if ([value respondsToSelector:@selector(cy$JSValueInContext:)])
1009 return [value cy$JSValueInContext:context];
1011 return CYMakeInstance(context, value, false);
1012 } CYPoolCatch(NULL) return /*XXX*/ NULL; }
1014 @implementation CYJSObject
1016 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { CYObjectiveTry {
1017 if ((self = [super init]) != nil) {
1020 JSValueProtect(context_, object_);
1022 } CYObjectiveCatch }
1024 - (void) dealloc { CYObjectiveTry {
1025 JSValueUnprotect(context_, object_);
1027 } CYObjectiveCatch }
1029 - (NSObject *) cy$toJSON:(NSString *)key { CYObjectiveTry {
1030 JSValueRef toJSON(CYGetProperty(context_, object_, toJSON_));
1031 if (!CYIsCallable(context_, toJSON))
1032 return [super cy$toJSON:key];
1034 JSValueRef arguments[1] = {CYCastJSValue(context_, key)};
1035 JSValueRef value(CYCallAsFunction(context_, (JSObjectRef) toJSON, object_, 1, arguments));
1036 // XXX: do I really want an NSNull here?!
1037 return CYCastNSObject(NULL, context_, value) ?: [NSNull null];
1039 } CYObjectiveCatch }
1041 - (NSString *) cy$toCYON { CYObjectiveTry {
1043 JSValueRef exception(NULL);
1044 const char *cyon(CYPoolCCYON(pool, context_, object_));
1045 CYThrow(context_, exception);
1047 return [super cy$toCYON];
1049 return [NSString stringWithUTF8String:cyon];
1050 } CYObjectiveCatch }
1052 - (NSUInteger) count { CYObjectiveTry {
1053 JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_));
1054 size_t size(JSPropertyNameArrayGetCount(names));
1055 JSPropertyNameArrayRelease(names);
1057 } CYObjectiveCatch }
1059 - (id) objectForKey:(id)key { CYObjectiveTry {
1060 // XXX: are NSDictionary keys always NSString *?
1061 JSValueRef value(CYGetProperty(context_, object_, CYJSString(context_, (NSString *) key)));
1062 if (JSValueIsUndefined(context_, value))
1064 return CYCastNSObject(NULL, context_, value) ?: [NSNull null];
1065 } CYObjectiveCatch }
1067 - (NSEnumerator *) keyEnumerator { CYObjectiveTry {
1068 JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_));
1069 NSEnumerator *enumerator([CYCastNSArray(context_, names) objectEnumerator]);
1070 JSPropertyNameArrayRelease(names);
1072 } CYObjectiveCatch }
1074 - (void) setObject:(id)object forKey:(id)key { CYObjectiveTry {
1075 // XXX: are NSDictionary keys always NSString *?
1076 CYSetProperty(context_, object_, CYJSString(context_, (NSString *) key), CYCastJSValue(context_, object));
1077 } CYObjectiveCatch }
1079 - (void) removeObjectForKey:(id)key { CYObjectiveTry {
1080 JSValueRef exception(NULL);
1081 // XXX: are NSDictionary keys always NSString *?
1082 (void) JSObjectDeleteProperty(context_, object_, CYJSString(context_, (NSString *) key), &exception);
1083 CYThrow(context_, exception);
1084 } CYObjectiveCatch }
1088 @implementation CYJSArray
1090 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { CYObjectiveTry {
1091 if ((self = [super init]) != nil) {
1094 JSValueProtect(context_, object_);
1096 } CYObjectiveCatch }
1098 - (void) dealloc { CYObjectiveTry {
1099 JSValueUnprotect(context_, object_);
1101 } CYObjectiveCatch }
1103 - (NSUInteger) count { CYObjectiveTry {
1104 return CYCastDouble(context_, CYGetProperty(context_, object_, length_));
1105 } CYObjectiveCatch }
1107 - (id) objectAtIndex:(NSUInteger)index { CYObjectiveTry {
1108 size_t bounds([self count]);
1109 if (index >= bounds)
1110 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray objectAtIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
1111 JSValueRef exception(NULL);
1112 JSValueRef value(JSObjectGetPropertyAtIndex(context_, object_, index, &exception));
1113 CYThrow(context_, exception);
1114 return CYCastNSObject(NULL, context_, value) ?: [NSNull null];
1115 } CYObjectiveCatch }
1117 - (void) addObject:(id)object { CYObjectiveTry {
1118 JSValueRef exception(NULL);
1119 JSValueRef arguments[1];
1120 arguments[0] = CYCastJSValue(context_, object);
1121 JSObjectCallAsFunction(context_, Array_push_, object_, 1, arguments, &exception);
1122 CYThrow(context_, exception);
1123 } CYObjectiveCatch }
1125 - (void) insertObject:(id)object atIndex:(NSUInteger)index { CYObjectiveTry {
1126 size_t bounds([self count] + 1);
1127 if (index >= bounds)
1128 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray insertObject:atIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
1129 JSValueRef exception(NULL);
1130 JSValueRef arguments[3];
1131 arguments[0] = CYCastJSValue(context_, index);
1132 arguments[1] = CYCastJSValue(context_, 0);
1133 arguments[2] = CYCastJSValue(context_, object);
1134 JSObjectCallAsFunction(context_, Array_splice_, object_, 3, arguments, &exception);
1135 CYThrow(context_, exception);
1136 } CYObjectiveCatch }
1138 - (void) removeLastObject { CYObjectiveTry {
1139 JSValueRef exception(NULL);
1140 JSObjectCallAsFunction(context_, Array_pop_, object_, 0, NULL, &exception);
1141 CYThrow(context_, exception);
1142 } CYObjectiveCatch }
1144 - (void) removeObjectAtIndex:(NSUInteger)index { CYObjectiveTry {
1145 size_t bounds([self count]);
1146 if (index >= bounds)
1147 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray removeObjectAtIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
1148 JSValueRef exception(NULL);
1149 JSValueRef arguments[2];
1150 arguments[0] = CYCastJSValue(context_, index);
1151 arguments[1] = CYCastJSValue(context_, 1);
1152 JSObjectCallAsFunction(context_, Array_splice_, object_, 2, arguments, &exception);
1153 CYThrow(context_, exception);
1154 } CYObjectiveCatch }
1156 - (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)object { CYObjectiveTry {
1157 size_t bounds([self count]);
1158 if (index >= bounds)
1159 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray replaceObjectAtIndex:withObject:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
1160 CYSetProperty(context_, object_, index, CYCastJSValue(context_, object));
1161 } CYObjectiveCatch }
1165 // XXX: use objc_getAssociatedObject and objc_setAssociatedObject on 10.6
1169 JSObjectRef object_;
1177 // XXX: delete object_? ;(
1180 static CYInternal *Get(id self) {
1181 CYInternal *internal(NULL);
1182 if (object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal)) == NULL) {
1183 // XXX: do something epic? ;P
1189 static CYInternal *Set(id self) {
1190 CYInternal *internal(NULL);
1191 if (objc_ivar *ivar = object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal))) {
1192 if (internal == NULL) {
1193 internal = new CYInternal();
1194 object_setIvar(self, ivar, reinterpret_cast<id>(internal));
1197 // XXX: do something epic? ;P
1203 bool HasProperty(JSContextRef context, JSStringRef name) {
1204 if (object_ == NULL)
1206 return JSObjectHasProperty(context, object_, name);
1209 JSValueRef GetProperty(JSContextRef context, JSStringRef name) {
1210 if (object_ == NULL)
1212 return CYGetProperty(context, object_, name);
1215 void SetProperty(JSContextRef context, JSStringRef name, JSValueRef value) {
1216 if (object_ == NULL)
1217 object_ = JSObjectMake(context, NULL, NULL);
1218 CYSetProperty(context, object_, name, value);
1222 static JSObjectRef CYMakeSelector(JSContextRef context, SEL sel) {
1223 Selector_privateData *internal(new Selector_privateData(sel));
1224 return JSObjectMake(context, Selector_, internal);
1227 static SEL CYCastSEL(JSContextRef context, JSValueRef value) {
1228 if (JSValueIsObjectOfClass(context, value, Selector_)) {
1229 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate((JSObjectRef) value)));
1230 return reinterpret_cast<SEL>(internal->value_);
1232 return CYCastPointer<SEL>(context, value);
1235 void *CYObjectiveC_ExecuteStart(JSContextRef context) {
1236 // XXX: deal with exceptions!
1237 return (void *) [[NSAutoreleasePool alloc] init];
1240 void CYObjectiveC_ExecuteEnd(JSContextRef context, void *handle) {
1241 // XXX: deal with exceptions!
1242 return [(NSAutoreleasePool *) handle release];
1245 JSValueRef CYObjectiveC_RuntimeProperty(JSContextRef context, CYUTF8String name) { CYPoolTry {
1247 return Instance::Make(context, nil);
1248 if (Class _class = objc_getClass(name.data))
1249 return CYMakeInstance(context, _class, true);
1251 } CYPoolCatch(NULL) return /*XXX*/ NULL; }
1253 static void CYObjectiveC_CallFunction(JSContextRef context, ffi_cif *cif, void (*function)(), uint8_t *value, void **values) { CYPoolTry {
1254 ffi_call(cif, function, value, values);
1257 static bool CYObjectiveC_PoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { CYPoolTry {
1258 switch (type->primitive) {
1260 case sig::typename_P:
1261 *reinterpret_cast<id *>(data) = CYCastNSObject(pool, context, value);
1264 case sig::selector_P:
1265 *reinterpret_cast<SEL *>(data) = CYCastSEL(context, value);
1273 } CYPoolCatch(false) return /*XXX*/ NULL; }
1275 static JSValueRef CYObjectiveC_FromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) { CYPoolTry {
1276 switch (type->primitive) {
1278 if (id object = *reinterpret_cast<id *>(data)) {
1279 JSValueRef value(CYCastJSValue(context, object));
1285 case sig::typename_P:
1286 return CYMakeInstance(context, *reinterpret_cast<Class *>(data), true);
1288 case sig::selector_P:
1289 if (SEL sel = *reinterpret_cast<SEL *>(data))
1290 return CYMakeSelector(context, sel);
1294 return CYJSNull(context);
1298 } CYPoolCatch(NULL) return /*XXX*/ NULL; }
1300 static bool CYImplements(id object, Class _class, SEL selector, bool devoid) {
1301 if (objc_method *method = class_getInstanceMethod(_class, selector)) {
1304 #if OBJC_API_VERSION >= 2
1306 method_getReturnType(method, type, sizeof(type));
1308 const char *type(method_getTypeEncoding(method));
1314 // XXX: possibly use a more "awesome" check?
1318 static const char *CYPoolTypeEncoding(apr_pool_t *pool, JSContextRef context, SEL sel, objc_method *method) {
1320 return method_getTypeEncoding(method);
1322 const char *name(sel_getName(sel));
1324 sqlite3_stmt *statement;
1326 _sqlcall(sqlite3_prepare(Bridge_,
1328 "\"bridge\".\"value\" "
1331 " \"bridge\".\"mode\" = -1 and"
1332 " \"bridge\".\"name\" = ?"
1334 , -1, &statement, NULL));
1337 _sqlcall(sqlite3_bind_text(statement, 1, name, -1, SQLITE_STATIC));
1340 if (_sqlcall(sqlite3_step(statement)) == SQLITE_DONE) {
1345 value = sqlite3_column_pooled(pool, statement, 0);
1348 _sqlcall(sqlite3_finalize(statement));
1356 static void MessageClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) {
1357 Closure_privateData *internal(reinterpret_cast<Closure_privateData *>(arg));
1359 JSContextRef context(internal->context_);
1361 size_t count(internal->cif_.nargs);
1362 JSValueRef values[count];
1364 for (size_t index(0); index != count; ++index)
1365 values[index] = CYFromFFI(context, internal->signature_.elements[1 + index].type, internal->cif_.arg_types[index], arguments[index]);
1367 JSObjectRef _this(CYCastJSObject(context, values[0]));
1369 JSValueRef value(CYCallAsFunction(context, internal->function_, _this, count - 2, values + 2));
1370 CYPoolFFI(NULL, context, internal->signature_.elements[0].type, internal->cif_.rtype, result, value);
1373 static JSObjectRef CYMakeMessage(JSContextRef context, SEL sel, IMP imp, const char *type) {
1374 Message_privateData *internal(new Message_privateData(sel, type, imp));
1375 return JSObjectMake(context, Message_, internal);
1378 static IMP CYMakeMessage(JSContextRef context, JSValueRef value, const char *type) {
1379 JSObjectRef function(CYCastJSObject(context, value));
1380 Closure_privateData *internal(CYMakeFunctor_(context, function, type, &MessageClosure_));
1381 return reinterpret_cast<IMP>(internal->GetValue());
1384 static bool Messages_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
1385 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1386 Class _class(internal->GetValue());
1389 const char *name(CYPoolCString(pool, context, property));
1391 if (SEL sel = sel_getUid(name))
1392 if (class_getInstanceMethod(_class, sel) != NULL)
1398 static JSValueRef Messages_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
1399 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1400 Class _class(internal->GetValue());
1403 const char *name(CYPoolCString(pool, context, property));
1405 if (SEL sel = sel_getUid(name))
1406 if (objc_method *method = class_getInstanceMethod(_class, sel))
1407 return CYMakeMessage(context, sel, method_getImplementation(method), method_getTypeEncoding(method));
1412 static bool Messages_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
1413 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1414 Class _class(internal->GetValue());
1417 const char *name(CYPoolCString(pool, context, property));
1419 SEL sel(sel_registerName(name));
1421 objc_method *method(class_getInstanceMethod(_class, sel));
1426 if (JSValueIsObjectOfClass(context, value, Message_)) {
1427 Message_privateData *message(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate((JSObjectRef) value)));
1428 type = sig::Unparse(pool, &message->signature_);
1429 imp = reinterpret_cast<IMP>(message->GetValue());
1431 type = CYPoolTypeEncoding(pool, context, sel, method);
1432 imp = CYMakeMessage(context, value, type);
1436 method_setImplementation(method, imp);
1439 GSMethodList list(GSAllocMethodList(1));
1440 GSAppendMethodToList(list, sel, type, imp, YES);
1441 GSAddMethodList(_class, list, YES);
1442 GSFlushMethodCacheForClass(_class);
1444 class_addMethod(_class, sel, imp, type);
1451 #if 0 && OBJC_API_VERSION < 2
1452 static bool Messages_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
1453 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1454 Class _class(internal->GetValue());
1457 const char *name(CYPoolCString(pool, context, property));
1459 if (SEL sel = sel_getUid(name))
1460 if (objc_method *method = class_getInstanceMethod(_class, sel)) {
1461 objc_method_list list = {NULL, 1, {method}};
1462 class_removeMethods(_class, &list);
1470 static void Messages_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1471 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1472 Class _class(internal->GetValue());
1474 #if OBJC_API_VERSION >= 2
1476 objc_method **data(class_copyMethodList(_class, &size));
1477 for (size_t i(0); i != size; ++i)
1478 JSPropertyNameAccumulatorAddName(names, CYJSString(sel_getName(method_getName(data[i]))));
1481 for (objc_method_list *methods(_class->methods); methods != NULL; methods = methods->method_next)
1482 for (int i(0); i != methods->method_count; ++i)
1483 JSPropertyNameAccumulatorAddName(names, CYJSString(sel_getName(method_getName(&methods->method_list[i]))));
1487 static bool Instance_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
1488 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1489 id self(internal->GetValue());
1491 if (JSStringIsEqualToUTF8CString(property, "$cyi"))
1495 NSString *name(CYCastNSString(pool, context, property));
1497 if (CYInternal *internal = CYInternal::Get(self))
1498 if (internal->HasProperty(context, property))
1501 Class _class(object_getClass(self));
1504 // XXX: this is an evil hack to deal with NSProxy; fix elsewhere
1505 if (CYImplements(self, _class, @selector(cy$hasProperty:), false))
1506 if ([self cy$hasProperty:name])
1508 } CYPoolCatch(false)
1510 const char *string(CYPoolCString(pool, context, name));
1513 if (class_getProperty(_class, string) != NULL)
1517 if (SEL sel = sel_getUid(string))
1518 if (CYImplements(self, _class, sel, true))
1524 static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1525 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1526 id self(internal->GetValue());
1528 if (JSStringIsEqualToUTF8CString(property, "$cyi"))
1529 return Internal::Make(context, self, object);
1532 NSString *name(CYCastNSString(pool, context, property));
1534 if (CYInternal *internal = CYInternal::Get(self))
1535 if (JSValueRef value = internal->GetProperty(context, property))
1539 if (NSObject *data = [self cy$getProperty:name])
1540 return CYCastJSValue(context, data);
1543 const char *string(CYPoolCString(pool, context, name));
1544 Class _class(object_getClass(self));
1547 if (objc_property_t property = class_getProperty(_class, string)) {
1548 PropertyAttributes attributes(property);
1549 SEL sel(sel_registerName(attributes.Getter()));
1550 return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception);
1554 if (SEL sel = sel_getUid(string))
1555 if (CYImplements(self, _class, sel, true))
1556 return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception);
1561 static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
1562 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1563 id self(internal->GetValue());
1567 NSString *name(CYCastNSString(pool, context, property));
1568 NSObject *data(CYCastNSObject(pool, context, value));
1571 if ([self cy$setProperty:name to:data])
1575 const char *string(CYPoolCString(pool, context, name));
1576 Class _class(object_getClass(self));
1579 if (objc_property_t property = class_getProperty(_class, string)) {
1580 PropertyAttributes attributes(property);
1581 if (const char *setter = attributes.Setter()) {
1582 SEL sel(sel_registerName(setter));
1583 JSValueRef arguments[1] = {value};
1584 CYSendMessage(pool, context, self, NULL, sel, 1, arguments, false, exception);
1590 size_t length(strlen(string));
1592 char set[length + 5];
1598 if (string[0] != '\0') {
1599 set[3] = toupper(string[0]);
1600 memcpy(set + 4, string + 1, length - 1);
1603 set[length + 3] = ':';
1604 set[length + 4] = '\0';
1606 if (SEL sel = sel_getUid(set))
1607 if (CYImplements(self, _class, sel, false)) {
1608 JSValueRef arguments[1] = {value};
1609 CYSendMessage(pool, context, self, NULL, sel, 1, arguments, false, exception);
1612 if (CYInternal *internal = CYInternal::Set(self)) {
1613 internal->SetProperty(context, property, value);
1620 static bool Instance_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1621 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1622 id self(internal->GetValue());
1625 NSString *name(CYCastNSString(NULL, context, property));
1626 return [self cy$deleteProperty:name];
1628 } CYCatch return /*XXX*/ NULL; }
1630 static void Instance_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1631 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1632 id self(internal->GetValue());
1635 Class _class(object_getClass(self));
1640 objc_property_t *data(class_copyPropertyList(_class, &size));
1641 for (size_t i(0); i != size; ++i)
1642 JSPropertyNameAccumulatorAddName(names, CYJSString(property_getName(data[i])));
1648 // XXX: this is an evil hack to deal with NSProxy; fix elsewhere
1649 if (CYImplements(self, _class, @selector(cy$getPropertyNames:inContext:), false))
1650 [self cy$getPropertyNames:names inContext:context];
1654 static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1655 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1656 JSObjectRef value(Instance::Make(context, [internal->GetValue() alloc], Instance::Uninitialized));
1660 static bool Instance_hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef instance, JSValueRef *exception) { CYTry {
1661 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) constructor)));
1662 Class _class(internal->GetValue());
1663 if (!CYIsClass(_class))
1666 if (JSValueIsObjectOfClass(context, instance, Instance_)) {
1667 Instance *linternal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) instance)));
1668 // XXX: this isn't always safe
1669 return [linternal->GetValue() isKindOfClass:_class];
1675 static bool Internal_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
1676 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1679 id self(internal->GetValue());
1680 const char *name(CYPoolCString(pool, context, property));
1682 if (object_getInstanceVariable(self, name, NULL) != NULL)
1688 static JSValueRef Internal_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1689 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1692 id self(internal->GetValue());
1693 const char *name(CYPoolCString(pool, context, property));
1695 if (objc_ivar *ivar = object_getInstanceVariable(self, name, NULL)) {
1696 Type_privateData type(pool, ivar_getTypeEncoding(ivar));
1697 return CYFromFFI(context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar));
1703 static bool Internal_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
1704 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1707 id self(internal->GetValue());
1708 const char *name(CYPoolCString(pool, context, property));
1710 if (objc_ivar *ivar = object_getInstanceVariable(self, name, NULL)) {
1711 Type_privateData type(pool, ivar_getTypeEncoding(ivar));
1712 CYPoolFFI(pool, context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar), value);
1719 static void Internal_getPropertyNames_(Class _class, JSPropertyNameAccumulatorRef names) {
1720 if (Class super = class_getSuperclass(_class))
1721 Internal_getPropertyNames_(super, names);
1723 #if OBJC_API_VERSION >= 2
1725 objc_ivar **data(class_copyIvarList(_class, &size));
1726 for (size_t i(0); i != size; ++i)
1727 JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(data[i])));
1730 if (objc_ivar_list *ivars = _class->ivars)
1731 for (int i(0); i != ivars->ivar_count; ++i)
1732 JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(&ivars->ivar_list[i])));
1736 static void Internal_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1737 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1740 id self(internal->GetValue());
1741 Class _class(object_getClass(self));
1743 Internal_getPropertyNames_(_class, names);
1746 static JSValueRef Internal_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
1747 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1748 return internal->GetOwner();
1751 static JSValueRef ObjectiveC_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1753 NSString *name(CYCastNSString(pool, context, property));
1754 if (Class _class = NSClassFromString(name))
1755 return CYMakeInstance(context, _class, true);
1759 static void ObjectiveC_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1761 size_t size(objc_getClassList(NULL, 0));
1762 Class *data(reinterpret_cast<Class *>(malloc(sizeof(Class) * size)));
1765 size_t writ(objc_getClassList(data, size));
1768 if (Class *copy = reinterpret_cast<Class *>(realloc(data, sizeof(Class) * writ))) {
1774 for (size_t i(0); i != writ; ++i)
1775 JSPropertyNameAccumulatorAddName(names, CYJSString(class_getName(data[i])));
1781 while (Class _class = objc_next_class(&state))
1782 JSPropertyNameAccumulatorAddName(names, CYJSString(class_getName(_class)));
1786 #if OBJC_API_VERSION >= 2
1787 static JSValueRef ObjectiveC_Image_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1788 const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object)));
1791 const char *name(CYPoolCString(pool, context, property));
1793 const char **data(objc_copyClassNamesForImage(internal, &size));
1795 for (size_t i(0); i != size; ++i)
1796 if (strcmp(name, data[i]) == 0) {
1797 if (Class _class = objc_getClass(name)) {
1798 value = CYMakeInstance(context, _class, true);
1809 static void ObjectiveC_Image_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1810 const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object)));
1812 const char **data(objc_copyClassNamesForImage(internal, &size));
1813 for (size_t i(0); i != size; ++i)
1814 JSPropertyNameAccumulatorAddName(names, CYJSString(data[i]));
1818 static JSValueRef ObjectiveC_Images_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1820 const char *name(CYPoolCString(pool, context, property));
1822 const char **data(objc_copyImageNames(&size));
1823 for (size_t i(0); i != size; ++i)
1824 if (strcmp(name, data[i]) == 0) {
1833 JSObjectRef value(JSObjectMake(context, NULL, NULL));
1834 CYSetProperty(context, value, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Image_Classes_, const_cast<char *>(name)));
1838 static void ObjectiveC_Images_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1840 const char **data(objc_copyImageNames(&size));
1841 for (size_t i(0); i != size; ++i)
1842 JSPropertyNameAccumulatorAddName(names, CYJSString(data[i]));
1847 static JSValueRef ObjectiveC_Protocols_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1849 const char *name(CYPoolCString(pool, context, property));
1850 if (Protocol *protocol = objc_getProtocol(name))
1851 return CYMakeInstance(context, protocol, true);
1855 static void ObjectiveC_Protocols_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1856 #if OBJC_API_VERSION >= 2
1858 Protocol **data(objc_copyProtocolList(&size));
1859 for (size_t i(0); i != size; ++i)
1860 JSPropertyNameAccumulatorAddName(names, CYJSString(protocol_getName(data[i])));
1868 static bool stret(ffi_type *ffi_type) {
1869 return ffi_type->type == FFI_TYPE_STRUCT && (
1870 ffi_type->size > OBJC_MAX_STRUCT_BY_VALUE ||
1871 struct_forward_array[ffi_type->size] != 0
1876 JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, Class _class, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception) { CYTry {
1880 _class = object_getClass(self);
1884 if (objc_method *method = class_getInstanceMethod(_class, _cmd)) {
1885 imp = method_getImplementation(method);
1886 type = method_getTypeEncoding(method);
1891 NSMethodSignature *method([self methodSignatureForSelector:_cmd]);
1893 throw CYJSError(context, "unrecognized selector %s sent to object %p", sel_getName(_cmd), self);
1894 type = CYPoolCString(pool, context, [method _typeString]);
1902 sig::Signature signature;
1903 sig::Parse(pool, &signature, type, &Structor_);
1906 sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif);
1910 if (stret(cif.rtype))
1911 imp = class_getMethodImplementation_stret(_class, _cmd);
1913 imp = class_getMethodImplementation(_class, _cmd);
1915 objc_super super = {self, _class};
1916 imp = objc_msg_lookup_super(&super, _cmd);
1920 void (*function)() = reinterpret_cast<void (*)()>(imp);
1921 return CYCallFunction(pool, context, 2, setup, count, arguments, initialize, exception, &signature, &cif, function);
1924 static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1926 throw CYJSError(context, "too few arguments to objc_msgSend");
1936 if (JSValueIsObjectOfClass(context, arguments[0], Super_)) {
1937 cy::Super *internal(reinterpret_cast<cy::Super *>(JSObjectGetPrivate((JSObjectRef) arguments[0])));
1938 self = internal->GetValue();
1939 _class = internal->class_;;
1940 uninitialized = false;
1941 } else if (JSValueIsObjectOfClass(context, arguments[0], Instance_)) {
1942 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) arguments[0])));
1943 self = internal->GetValue();
1945 uninitialized = internal->IsUninitialized();
1947 internal->value_ = nil;
1949 self = CYCastNSObject(pool, context, arguments[0]);
1951 uninitialized = false;
1955 return CYJSNull(context);
1957 _cmd = CYCastSEL(context, arguments[1]);
1959 return CYSendMessage(pool, context, self, _class, _cmd, count - 2, arguments + 2, uninitialized, exception);
1962 /* Hook: objc_registerClassPair {{{ */
1963 #if defined(__APPLE__) && defined(__arm__)
1964 // XXX: replace this with associated objects
1966 MSHook(void, CYDealloc, id self, SEL sel) {
1967 CYInternal *internal;
1968 object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal));
1969 if (internal != NULL)
1971 _CYDealloc(self, sel);
1974 MSHook(void, objc_registerClassPair, Class _class) {
1975 Class super(class_getSuperclass(_class));
1976 if (super == NULL || class_getInstanceVariable(super, "cy$internal_") == NULL) {
1977 class_addIvar(_class, "cy$internal_", sizeof(CYInternal *), log2(sizeof(CYInternal *)), "^{CYInternal}");
1978 MSHookMessage(_class, @selector(dealloc), MSHake(CYDealloc));
1981 _objc_registerClassPair(_class);
1984 static JSValueRef objc_registerClassPair_(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1986 throw CYJSError(context, "incorrect number of arguments to objc_registerClassPair");
1988 NSObject *value(CYCastNSObject(pool, context, arguments[0]));
1989 if (value == NULL || !CYIsClass(value))
1990 throw CYJSError(context, "incorrect number of arguments to objc_registerClassPair");
1991 Class _class((Class) value);
1992 $objc_registerClassPair(_class);
1993 return CYJSUndefined(context);
1998 static JSValueRef Selector_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
1999 JSValueRef setup[count + 2];
2002 memcpy(setup + 2, arguments, sizeof(JSValueRef) * count);
2003 return $objc_msgSend(context, NULL, NULL, count + 2, setup, exception);
2006 static JSValueRef Message_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2008 Message_privateData *internal(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate(object)));
2010 // XXX: handle Instance::Uninitialized?
2011 id self(CYCastNSObject(pool, context, _this));
2015 setup[1] = &internal->sel_;
2017 return CYCallFunction(pool, context, 2, setup, count, arguments, false, exception, &internal->signature_, &internal->cif_, internal->GetValue());
2020 static JSObjectRef Super_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2022 throw CYJSError(context, "incorrect number of arguments to Super constructor");
2024 id self(CYCastNSObject(pool, context, arguments[0]));
2025 Class _class(CYCastClass(pool, context, arguments[1]));
2026 return cy::Super::Make(context, self, _class);
2029 static JSObjectRef Selector_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2031 throw CYJSError(context, "incorrect number of arguments to Selector constructor");
2033 const char *name(CYPoolCString(pool, context, arguments[0]));
2034 return CYMakeSelector(context, sel_registerName(name));
2037 static JSObjectRef Instance_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2039 throw CYJSError(context, "incorrect number of arguments to Instance constructor");
2040 id self(count == 0 ? nil : CYCastPointer<id>(context, arguments[0]));
2041 return Instance::Make(context, self);
2044 static JSValueRef CYValue_getProperty_value(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
2045 CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(object)));
2046 return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_));
2049 static JSValueRef CYValue_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2050 CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this)));
2051 Type_privateData *typical(internal->GetType());
2056 if (typical == NULL) {
2060 type = typical->type_;
2061 ffi = typical->ffi_;
2064 return CYMakePointer(context, &internal->value_, type, ffi, object);
2067 static JSValueRef Instance_getProperty_constructor(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
2068 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
2069 return Instance::Make(context, (id) object_getClass(internal->GetValue()));
2072 static JSValueRef Instance_getProperty_protocol(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2073 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
2074 id self(internal->GetValue());
2075 if (!CYIsClass(self))
2076 return CYJSUndefined(context);
2077 return CYGetClassPrototype(context, self);
2080 static JSValueRef Instance_getProperty_messages(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
2081 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
2082 id self(internal->GetValue());
2083 if (!CYIsClass(self))
2084 return CYJSUndefined(context);
2085 return Messages::Make(context, (Class) self);
2088 static JSValueRef Instance_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2089 if (!JSValueIsObjectOfClass(context, _this, Instance_))
2092 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2093 return CYCastJSValue(context, CYJSString(context, CYCastNSCYON(internal->GetValue())));
2096 static JSValueRef Instance_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2097 if (!JSValueIsObjectOfClass(context, _this, Instance_))
2100 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2107 key = CYCastNSString(NULL, context, CYJSString(context, arguments[0]));
2108 // XXX: check for support of cy$toJSON?
2109 return CYCastJSValue(context, CYJSString(context, [internal->GetValue() cy$toJSON:key]));
2111 } CYCatch return /*XXX*/ NULL; }
2113 static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2114 if (!JSValueIsObjectOfClass(context, _this, Instance_))
2117 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2120 // XXX: this seems like a stupid implementation; what if it crashes? why not use the CYONifier backend?
2121 return CYCastJSValue(context, CYJSString(context, [internal->GetValue() description]));
2123 } CYCatch return /*XXX*/ NULL; }
2125 static JSValueRef Selector_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2126 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
2127 return CYCastJSValue(context, sel_getName(internal->GetValue()));
2130 static JSValueRef Selector_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2131 return Selector_callAsFunction_toString(context, object, _this, count, arguments, exception);
2134 static JSValueRef Selector_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2135 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
2136 const char *name(sel_getName(internal->GetValue()));
2139 NSString *string([NSString stringWithFormat:@"@selector(%s)", name]);
2140 return CYCastJSValue(context, CYJSString(context, string));
2142 } CYCatch return /*XXX*/ NULL; }
2144 static JSValueRef Selector_callAsFunction_type(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2146 throw CYJSError(context, "incorrect number of arguments to Selector.type");
2149 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
2150 SEL sel(internal->GetValue());
2152 objc_method *method;
2153 if (Class _class = CYCastClass(pool, context, arguments[0]))
2154 method = class_getInstanceMethod(_class, sel);
2158 if (const char *type = CYPoolTypeEncoding(pool, context, sel, method))
2159 return CYCastJSValue(context, CYJSString(type));
2161 return CYJSNull(context);
2164 static JSStaticValue Selector_staticValues[2] = {
2165 {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete},
2166 {NULL, NULL, NULL, 0}
2169 static JSStaticValue Instance_staticValues[5] = {
2170 {"constructor", &Instance_getProperty_constructor, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2171 {"messages", &Instance_getProperty_messages, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2172 {"prototype", &Instance_getProperty_protocol, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2173 {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2174 {NULL, NULL, NULL, 0}
2177 static JSStaticFunction Instance_staticFunctions[5] = {
2178 {"$cya", &CYValue_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2179 {"toCYON", &Instance_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2180 {"toJSON", &Instance_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2181 {"toString", &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2185 static JSStaticFunction Internal_staticFunctions[2] = {
2186 {"$cya", &Internal_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2190 static JSStaticFunction Selector_staticFunctions[5] = {
2191 {"toCYON", &Selector_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2192 {"toJSON", &Selector_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2193 {"toString", &Selector_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2194 {"type", &Selector_callAsFunction_type, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2198 void CYObjectiveC_SetupContext(JSContextRef context) {
2199 JSObjectRef global(CYGetGlobalObject(context));
2200 apr_pool_t *pool(CYGetGlobalPool());
2202 Object_type = new(pool) Type_privateData(pool, "@");
2203 Selector_type = new(pool) Type_privateData(pool, ":");
2206 NSCFBoolean_ = objc_getClass("NSCFBoolean");
2207 NSCFType_ = objc_getClass("NSCFType");
2208 NSMessageBuilder_ = objc_getClass("NSMessageBuilder");
2209 NSZombie_ = objc_getClass("_NSZombie_");
2212 NSArray_ = objc_getClass("NSArray");
2213 NSDictionary_ = objc_getClass("NSDictionary");
2214 Object_ = objc_getClass("Object");
2216 JSClassDefinition definition;
2218 definition = kJSClassDefinitionEmpty;
2219 definition.className = "Instance";
2220 definition.staticValues = Instance_staticValues;
2221 definition.staticFunctions = Instance_staticFunctions;
2222 definition.hasProperty = &Instance_hasProperty;
2223 definition.getProperty = &Instance_getProperty;
2224 definition.setProperty = &Instance_setProperty;
2225 definition.deleteProperty = &Instance_deleteProperty;
2226 definition.getPropertyNames = &Instance_getPropertyNames;
2227 definition.callAsConstructor = &Instance_callAsConstructor;
2228 definition.hasInstance = &Instance_hasInstance;
2229 definition.finalize = &CYFinalize;
2230 Instance_ = JSClassCreate(&definition);
2232 definition = kJSClassDefinitionEmpty;
2233 definition.className = "Internal";
2234 definition.staticFunctions = Internal_staticFunctions;
2235 definition.hasProperty = &Internal_hasProperty;
2236 definition.getProperty = &Internal_getProperty;
2237 definition.setProperty = &Internal_setProperty;
2238 definition.getPropertyNames = &Internal_getPropertyNames;
2239 definition.finalize = &CYFinalize;
2240 Internal_ = JSClassCreate(&definition);
2242 definition = kJSClassDefinitionEmpty;
2243 definition.className = "Message";
2244 definition.staticFunctions = cy::Functor::StaticFunctions;
2245 definition.callAsFunction = &Message_callAsFunction;
2246 definition.finalize = &CYFinalize;
2247 Message_ = JSClassCreate(&definition);
2249 definition = kJSClassDefinitionEmpty;
2250 definition.className = "Messages";
2251 definition.hasProperty = &Messages_hasProperty;
2252 definition.getProperty = &Messages_getProperty;
2253 definition.setProperty = &Messages_setProperty;
2254 #if 0 && OBJC_API_VERSION < 2
2255 definition.deleteProperty = &Messages_deleteProperty;
2257 definition.getPropertyNames = &Messages_getPropertyNames;
2258 definition.finalize = &CYFinalize;
2259 Messages_ = JSClassCreate(&definition);
2261 definition = kJSClassDefinitionEmpty;
2262 definition.className = "Selector";
2263 definition.staticValues = Selector_staticValues;
2264 definition.staticFunctions = Selector_staticFunctions;
2265 definition.callAsFunction = &Selector_callAsFunction;
2266 definition.finalize = &CYFinalize;
2267 Selector_ = JSClassCreate(&definition);
2269 definition = kJSClassDefinitionEmpty;
2270 definition.className = "Super";
2271 definition.staticFunctions = Internal_staticFunctions;
2272 definition.finalize = &CYFinalize;
2273 Super_ = JSClassCreate(&definition);
2275 definition = kJSClassDefinitionEmpty;
2276 definition.className = "ObjectiveC::Classes";
2277 definition.getProperty = &ObjectiveC_Classes_getProperty;
2278 definition.getPropertyNames = &ObjectiveC_Classes_getPropertyNames;
2279 ObjectiveC_Classes_ = JSClassCreate(&definition);
2281 definition = kJSClassDefinitionEmpty;
2282 definition.className = "ObjectiveC::Protocols";
2283 definition.getProperty = &ObjectiveC_Protocols_getProperty;
2284 definition.getPropertyNames = &ObjectiveC_Protocols_getPropertyNames;
2285 ObjectiveC_Protocols_ = JSClassCreate(&definition);
2287 JSObjectRef ObjectiveC(JSObjectMake(context, NULL, NULL));
2288 CYSetProperty(context, global, CYJSString("ObjectiveC"), ObjectiveC);
2290 CYSetProperty(context, ObjectiveC, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Classes_, NULL));
2291 CYSetProperty(context, ObjectiveC, CYJSString("protocols"), JSObjectMake(context, ObjectiveC_Protocols_, NULL));
2293 #if OBJC_API_VERSION >= 2
2294 definition = kJSClassDefinitionEmpty;
2295 definition.className = "ObjectiveC::Images";
2296 definition.getProperty = &ObjectiveC_Images_getProperty;
2297 definition.getPropertyNames = &ObjectiveC_Images_getPropertyNames;
2298 ObjectiveC_Images_ = JSClassCreate(&definition);
2300 definition = kJSClassDefinitionEmpty;
2301 definition.className = "ObjectiveC::Image::Classes";
2302 definition.getProperty = &ObjectiveC_Image_Classes_getProperty;
2303 definition.getPropertyNames = &ObjectiveC_Image_Classes_getPropertyNames;
2304 ObjectiveC_Image_Classes_ = JSClassCreate(&definition);
2306 CYSetProperty(context, ObjectiveC, CYJSString("images"), JSObjectMake(context, ObjectiveC_Images_, NULL));
2309 JSObjectRef Instance(JSObjectMakeConstructor(context, Instance_, &Instance_new));
2310 JSObjectRef Message(JSObjectMakeConstructor(context, Message_, NULL));
2311 JSObjectRef Selector(JSObjectMakeConstructor(context, Selector_, &Selector_new));
2312 JSObjectRef Super(JSObjectMakeConstructor(context, Super_, &Super_new));
2314 Instance_prototype_ = (JSObjectRef) CYGetProperty(context, Instance, prototype_);
2315 JSValueProtect(context, Instance_prototype_);
2317 CYSetProperty(context, global, CYJSString("Instance"), Instance);
2318 CYSetProperty(context, global, CYJSString("Selector"), Selector);
2319 CYSetProperty(context, global, CYJSString("Super"), Super);
2321 #if defined(__APPLE__) && defined(__arm__)
2322 CYSetProperty(context, global, CYJSString("objc_registerClassPair"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_registerClassPair"), &objc_registerClassPair_));
2323 MSHookFunction(&objc_registerClassPair, MSHake(objc_registerClassPair));
2326 CYSetProperty(context, global, CYJSString("objc_msgSend"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_msgSend"), &$objc_msgSend));
2328 JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Message, prototype_), Function_prototype_);
2329 JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Selector, prototype_), Function_prototype_);
2332 class_addMethod(NSCFType_, @selector(cy$toJSON:), reinterpret_cast<IMP>(&NSCFType$cy$toJSON), "@12@0:4@8");
2336 static CYHooks CYObjectiveCHooks = {
2337 &CYObjectiveC_ExecuteStart,
2338 &CYObjectiveC_ExecuteEnd,
2339 &CYObjectiveC_RuntimeProperty,
2340 &CYObjectiveC_CallFunction,
2341 &CYObjectiveC_SetupContext,
2342 &CYObjectiveC_PoolFFI,
2343 &CYObjectiveC_FromFFI,
2346 struct CYObjectiveC {
2348 hooks_ = &CYObjectiveCHooks;