5 #include "ObjectiveC/Internal.hpp"
11 #include <Foundation/Foundation.h>
13 #include <objc/Protocol.h>
15 #include "cycript.hpp"
17 #include "ObjectiveC/Internal.hpp"
20 #include <CoreFoundation/CoreFoundation.h>
21 #include <CoreFoundation/CFLogUtilities.h>
22 #include <JavaScriptCore/JSStringRefCF.h>
23 #include <WebKit/WebScriptObject.h>
27 #include "JavaScript.hpp"
33 #define CYObjectiveTry_(context) { \
34 JSContextRef context_(context); \
36 #define CYObjectiveTry { \
38 #define CYObjectiveCatch \
39 catch (const CYException &error) { \
40 @throw CYCastNSObject(NULL, context_, error.CastJSValue(context_)); \
46 NSAutoreleasePool *_pool([[NSAutoreleasePool alloc] init]); \
48 #define CYPoolCatch(value) \
49 @catch (NSException *error) { \
50 _saved = [error retain]; \
51 throw CYJSError(context, CYCastJSValue(context, error)); \
56 [_saved autorelease]; \
61 #define class_getSuperclass GSObjCSuper
62 #define class_getInstanceVariable GSCGetInstanceVariableDefinition
63 #define class_getName GSNameFromClass
65 #define class_removeMethods(cls, list) GSRemoveMethodList(cls, list, YES)
67 #define ivar_getName(ivar) ((ivar)->ivar_name)
68 #define ivar_getOffset(ivar) ((ivar)->ivar_offset)
69 #define ivar_getTypeEncoding(ivar) ((ivar)->ivar_type)
71 #define method_getName(method) ((method)->method_name)
72 #define method_getImplementation(method) ((method)->method_imp)
73 #define method_getTypeEncoding(method) ((method)->method_types)
74 #define method_setImplementation(method, imp) ((void) ((method)->method_imp = (imp)))
76 #define objc_getProtocol GSProtocolFromName
78 #define object_getClass GSObjCClass
80 #define object_getInstanceVariable(object, name, value) ({ \
81 objc_ivar *ivar(class_getInstanceVariable(object_getClass(object), name)); \
82 GSObjCGetVariable(object, ivar_getOffset(ivar), sizeof(void *), value); \
86 #define object_setIvar(object, ivar, value) ({ \
87 void *data = (value); \
88 GSObjCSetVariable(object, ivar_getOffset(ivar), sizeof(void *), &data); \
91 #define protocol_getName(protocol) [(protocol) name]
94 JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, Class super, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception);
96 extern sqlite3 *Bridge_;
98 /* Objective-C Pool Release {{{ */
99 apr_status_t CYPoolRelease_(void *data) {
100 id object(reinterpret_cast<id>(data));
105 id CYPoolRelease_(apr_pool_t *pool, id object) {
108 else if (pool == NULL)
109 return [object autorelease];
111 apr_pool_cleanup_register(pool, object, &CYPoolRelease_, &apr_pool_cleanup_null);
116 template <typename Type_>
117 Type_ CYPoolRelease(apr_pool_t *pool, Type_ object) {
118 return (Type_) CYPoolRelease_(pool, (id) object);
121 /* Objective-C Strings {{{ */
122 const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, NSString *value) {
124 return [value UTF8String];
126 size_t size([value maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1);
127 char *string(new(pool) char[size]);
128 if (![value getCString:string maxLength:size encoding:NSUTF8StringEncoding])
129 throw CYJSError(context, "[NSString getCString:maxLength:encoding:] == NO");
134 JSStringRef CYCopyJSString(JSContextRef context, NSString *value) {
136 return JSStringCreateWithCFString(reinterpret_cast<CFStringRef>(value));
139 return CYCopyJSString(CYPoolCString(pool, context, value));
143 JSStringRef CYCopyJSString(NSObject *value) {
146 // XXX: this definition scares me; is anyone using this?!
147 NSString *string([value description]);
148 return CYCopyJSString(string);
151 NSString *CYCopyNSString(const CYUTF8String &value) {
153 return (NSString *) CFStringCreateWithBytes(kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(value.data), value.size, kCFStringEncodingUTF8, true);
155 return [[NSString alloc] initWithBytes:value.data length:value.size encoding:NSUTF8StringEncoding];
159 NSString *CYCopyNSString(JSContextRef context, JSStringRef value) {
161 return (NSString *) JSStringCopyCFString(kCFAllocatorDefault, value);
164 return CYCopyNSString(CYPoolUTF8String(pool, context, value));
168 NSString *CYCopyNSString(JSContextRef context, JSValueRef value) {
169 return CYCopyNSString(context, CYJSString(context, value));
172 NSString *CYCastNSString(apr_pool_t *pool, const CYUTF8String &value) {
173 return CYPoolRelease(pool, CYCopyNSString(value));
176 NSString *CYCastNSString(apr_pool_t *pool, SEL sel) {
177 const char *name(sel_getName(sel));
178 return CYPoolRelease(pool, CYCopyNSString(CYUTF8String(name, strlen(name))));
181 NSString *CYCastNSString(apr_pool_t *pool, JSContextRef context, JSStringRef value) {
182 return CYPoolRelease(pool, CYCopyNSString(context, value));
185 CYUTF8String CYCastUTF8String(NSString *value) {
186 NSData *data([value dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO]);
187 return CYUTF8String(reinterpret_cast<const char *>([data bytes]), [data length]);
191 void CYThrow(JSContextRef context, NSException *error, JSValueRef *exception) {
192 if (exception == NULL)
194 *exception = CYCastJSValue(context, error);
197 size_t CYGetIndex(NSString *value) {
198 return CYGetIndex(CYCastUTF8String(value));
201 bool CYGetOffset(apr_pool_t *pool, JSContextRef context, NSString *value, ssize_t &index) {
202 return CYGetOffset(CYPoolCString(pool, context, value), index);
205 static JSClassRef Instance_;
206 static JSClassRef Internal_;
207 static JSClassRef Message_;
208 static JSClassRef Messages_;
209 static JSClassRef Selector_;
210 static JSClassRef Super_;
212 static JSClassRef ObjectiveC_Classes_;
213 static JSClassRef ObjectiveC_Protocols_;
216 static JSClassRef ObjectiveC_Image_Classes_;
217 static JSClassRef ObjectiveC_Images_;
220 static JSObjectRef Instance_prototype_;
223 static Class NSCFBoolean_;
224 static Class NSCFType_;
225 static Class NSMessageBuilder_;
226 static Class NSZombie_;
229 static Class NSArray_;
230 static Class NSDictionary_;
231 static Class Object_;
233 static Type_privateData *Object_type;
234 static Type_privateData *Selector_type;
236 Type_privateData *Instance::GetType() const {
240 Type_privateData *Selector_privateData::GetType() const {
241 return Selector_type;
244 // XXX: trick this out with associated objects!
245 JSValueRef CYGetClassPrototype(JSContextRef context, id self) {
247 return Instance_prototype_;
249 // XXX: I need to think through multi-context
250 typedef std::map<id, JSValueRef> CacheMap;
251 static CacheMap cache_;
253 JSValueRef &value(cache_[self]);
257 JSClassRef _class(NULL);
258 JSValueRef prototype;
260 if (self == NSArray_)
261 prototype = Array_prototype_;
262 else if (self == NSDictionary_)
263 prototype = Object_prototype_;
265 prototype = CYGetClassPrototype(context, class_getSuperclass(self));
267 JSObjectRef object(JSObjectMake(context, _class, NULL));
268 JSObjectSetPrototype(context, object, prototype);
270 JSValueProtect(context, object);
275 JSObjectRef Messages::Make(JSContextRef context, Class _class, bool array) {
276 JSObjectRef value(JSObjectMake(context, Messages_, new Messages(_class)));
277 if (_class == NSArray_)
279 if (Class super = class_getSuperclass(_class))
280 JSObjectSetPrototype(context, value, Messages::Make(context, super, array));
282 JSObjectSetPrototype(context, value, Array_prototype_);*/
286 JSObjectRef Internal::Make(JSContextRef context, id object, JSObjectRef owner) {
287 return JSObjectMake(context, Internal_, new Internal(object, context, owner));
291 JSObjectRef Super::Make(JSContextRef context, id object, Class _class) {
292 JSObjectRef value(JSObjectMake(context, Super_, new Super(object, _class)));
296 JSObjectRef Instance::Make(JSContextRef context, id object, Flags flags) {
297 JSObjectRef value(JSObjectMake(context, Instance_, new Instance(object, flags)));
298 JSObjectSetPrototype(context, value, CYGetClassPrototype(context, object_getClass(object)));
302 Instance::~Instance() {
303 if ((flags_ & Transient) == 0)
304 // XXX: does this handle background threads correctly?
305 // XXX: this simply does not work on the console because I'm stupid
306 [GetValue() performSelector:@selector(release) withObject:nil afterDelay:0];
309 struct Message_privateData :
314 Message_privateData(SEL sel, const char *type, IMP value = NULL) :
315 cy::Functor(type, reinterpret_cast<void (*)()>(value)),
321 JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient) {
322 Instance::Flags flags;
325 flags = Instance::Transient;
327 flags = Instance::None;
328 object = [object retain];
331 return Instance::Make(context, object, flags);
334 @interface NSMethodSignature (Cycript)
335 - (NSString *) _typeString;
338 @interface NSObject (Cycript)
340 - (JSValueRef) cy$JSValueInContext:(JSContextRef)context;
341 - (JSType) cy$JSType;
343 - (NSObject *) cy$toJSON:(NSString *)key;
344 - (NSString *) cy$toCYON;
345 - (NSString *) cy$toKey;
347 - (bool) cy$hasProperty:(NSString *)name;
348 - (NSObject *) cy$getProperty:(NSString *)name;
349 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value;
350 - (bool) cy$deleteProperty:(NSString *)name;
351 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names;
356 - (JSValueRef) cy$JSValueInContext:(JSContextRef)context;
359 NSString *CYCastNSCYON(id value) {
365 Class _class(object_getClass(value));
366 SEL sel(@selector(cy$toCYON));
368 if (objc_method *toCYON = class_getInstanceMethod(_class, sel))
369 string = reinterpret_cast<NSString *(*)(id, SEL)>(method_getImplementation(toCYON))(value, sel);
370 else if (objc_method *methodSignatureForSelector = class_getInstanceMethod(_class, @selector(methodSignatureForSelector:))) {
371 if (reinterpret_cast<NSMethodSignature *(*)(id, SEL, SEL)>(method_getImplementation(methodSignatureForSelector))(value, @selector(methodSignatureForSelector:), sel) != nil)
372 string = [value cy$toCYON];
377 else if (value == NSZombie_)
378 string = @"_NSZombie_";
379 else if (_class == NSZombie_)
380 string = [NSString stringWithFormat:@"<_NSZombie_: %p>", value];
381 // XXX: frowny /in/ the pants
382 else if (value == NSMessageBuilder_ || value == Object_)
386 string = [NSString stringWithFormat:@"%@", value];
391 string = @"undefined";
398 struct PropertyAttributes {
403 const char *variable;
416 PropertyAttributes(objc_property_t property) :
428 name = property_getName(property);
429 const char *attributes(property_getAttributes(property));
431 for (char *state, *token(apr_strtok(apr_pstrdup(pool_, attributes), ",", &state)); token != NULL; token = apr_strtok(NULL, ",", &state)) {
433 case 'R': readonly = true; break;
434 case 'C': copy = true; break;
435 case '&': retain = true; break;
436 case 'N': nonatomic = true; break;
437 case 'G': getter_ = token + 1; break;
438 case 'S': setter_ = token + 1; break;
439 case 'V': variable = token + 1; break;
443 /*if (variable == NULL) {
444 variable = property_getName(property);
445 size_t size(strlen(variable));
446 char *name(new(pool_) char[size + 2]);
448 memcpy(name + 1, variable, size);
449 name[size + 1] = '\0';
454 const char *Getter() {
456 getter_ = apr_pstrdup(pool_, name);
460 const char *Setter() {
461 if (setter_ == NULL && !readonly) {
462 size_t length(strlen(name));
464 char *temp(new(pool_) char[length + 5]);
470 temp[3] = toupper(name[0]);
471 memcpy(temp + 4, name + 1, length - 1);
474 temp[length + 3] = ':';
475 temp[length + 4] = '\0';
486 NSObject *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) {
487 return [(NSString *) CFCopyDescription((CFTypeRef) self) autorelease];
492 @interface CYWebUndefined : NSObject {
495 + (CYWebUndefined *) undefined;
499 @implementation CYWebUndefined
501 + (CYWebUndefined *) undefined {
502 static CYWebUndefined *instance_([[CYWebUndefined alloc] init]);
508 #define WebUndefined CYWebUndefined
511 /* Bridge: CYJSObject {{{ */
512 @interface CYJSObject : NSMutableDictionary {
514 JSContextRef context_;
517 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context;
519 - (NSObject *) cy$toJSON:(NSString *)key;
521 - (NSUInteger) count;
522 - (id) objectForKey:(id)key;
523 - (NSEnumerator *) keyEnumerator;
524 - (void) setObject:(id)object forKey:(id)key;
525 - (void) removeObjectForKey:(id)key;
529 /* Bridge: CYJSArray {{{ */
530 @interface CYJSArray : NSMutableArray {
532 JSContextRef context_;
535 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context;
537 - (NSUInteger) count;
538 - (id) objectAtIndex:(NSUInteger)index;
540 - (void) addObject:(id)anObject;
541 - (void) insertObject:(id)anObject atIndex:(NSUInteger)index;
542 - (void) removeLastObject;
543 - (void) removeObjectAtIndex:(NSUInteger)index;
544 - (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
549 NSObject *CYCastNSObject_(apr_pool_t *pool, JSContextRef context, JSObjectRef object) {
550 JSValueRef exception(NULL);
551 bool array(JSValueIsInstanceOfConstructor(context, object, Array_, &exception));
552 CYThrow(context, exception);
553 id value(array ? [CYJSArray alloc] : [CYJSObject alloc]);
554 return CYPoolRelease(pool, [value initWithJSObject:object inContext:context]);
557 NSObject *CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSObjectRef object) {
558 if (!JSValueIsObjectOfClass(context, object, Instance_))
559 return CYCastNSObject_(pool, context, object);
561 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
562 return internal->GetValue();
566 NSNumber *CYCopyNSNumber(JSContextRef context, JSValueRef value) {
567 return [[NSNumber alloc] initWithDouble:CYCastDouble(context, value)];
570 id CYNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value, bool cast) {
574 switch (JSType type = JSValueGetType(context, value)) {
575 case kJSTypeUndefined:
576 object = [WebUndefined undefined];
586 object = (id) (CYCastBool(context, value) ? kCFBooleanTrue : kCFBooleanFalse);
589 object = [[NSNumber alloc] initWithBool:CYCastBool(context, value)];
595 object = CYCopyNSNumber(context, value);
600 object = CYCopyNSString(context, value);
605 // XXX: this might could be more efficient
606 object = CYCastNSObject(pool, context, (JSObjectRef) value);
611 throw CYJSError(context, "JSValueGetType() == 0x%x", type);
618 return CYPoolRelease(pool, object);
620 return [object retain];
623 NSObject *CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
624 return CYNSObject(pool, context, value, true);
627 NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
628 return CYNSObject(pool, context, value, false);
631 /* Bridge: NSArray {{{ */
632 @implementation NSArray (Cycript)
634 - (NSString *) cy$toCYON {
635 NSMutableString *json([[[NSMutableString alloc] init] autorelease]);
636 [json appendString:@"["];
640 for (id object in self) {
642 for (size_t index(0), count([self count]); index != count; ++index) {
643 id object([self objectAtIndex:index]);
646 [json appendString:@","];
649 if (object == nil || [object cy$JSType] != kJSTypeUndefined)
650 [json appendString:CYCastNSCYON(object)];
652 [json appendString:@","];
657 [json appendString:@"]"];
661 - (bool) cy$hasProperty:(NSString *)name {
662 if ([name isEqualToString:@"length"])
665 size_t index(CYGetIndex(name));
666 if (index == _not(size_t) || index >= [self count])
667 return [super cy$hasProperty:name];
672 - (NSObject *) cy$getProperty:(NSString *)name {
673 if ([name isEqualToString:@"length"]) {
674 NSUInteger count([self count]);
676 return [NSNumber numberWithUnsignedInteger:count];
678 return [NSNumber numberWithUnsignedInt:count];
682 size_t index(CYGetIndex(name));
683 if (index == _not(size_t) || index >= [self count])
684 return [super cy$getProperty:name];
686 return [self objectAtIndex:index];
689 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names {
690 [super cy$getPropertyNames:names];
692 for (size_t index(0), count([self count]); index != count; ++index) {
693 id object([self objectAtIndex:index]);
694 if (object == nil || [object cy$JSType] != kJSTypeUndefined) {
696 sprintf(name, "%zu", index);
697 JSPropertyNameAccumulatorAddName(names, CYJSString(name));
704 /* Bridge: NSDictionary {{{ */
705 @implementation NSDictionary (Cycript)
707 - (NSString *) cy$toCYON {
708 NSMutableString *json([[[NSMutableString alloc] init] autorelease]);
709 [json appendString:@"{"];
713 for (id key in self) {
715 NSEnumerator *keys([self keyEnumerator]);
716 while (id key = [keys nextObject]) {
719 [json appendString:@","];
722 [json appendString:[key cy$toKey]];
723 [json appendString:@":"];
724 NSObject *object([self objectForKey:key]);
725 [json appendString:CYCastNSCYON(object)];
728 [json appendString:@"}"];
732 - (bool) cy$hasProperty:(NSString *)name {
733 return [self objectForKey:name] != nil;
736 - (NSObject *) cy$getProperty:(NSString *)name {
737 return [self objectForKey:name];
740 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names {
741 [super cy$getPropertyNames:names];
744 for (NSString *key in self) {
746 NSEnumerator *keys([self keyEnumerator]);
747 while (NSString *key = [keys nextObject]) {
749 JSPropertyNameAccumulatorAddName(names, CYJSString(key));
755 /* Bridge: NSMutableArray {{{ */
756 @implementation NSMutableArray (Cycript)
758 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
759 if ([name isEqualToString:@"length"]) {
760 // XXX: is this not intelligent?
761 NSNumber *number(reinterpret_cast<NSNumber *>(value));
763 NSUInteger size([number unsignedIntegerValue]);
765 NSUInteger size([number unsignedIntValue]);
767 NSUInteger count([self count]);
769 [self removeObjectsInRange:NSMakeRange(size, count - size)];
770 else if (size != count) {
771 WebUndefined *undefined([WebUndefined undefined]);
772 for (size_t i(count); i != size; ++i)
773 [self addObject:undefined];
778 size_t index(CYGetIndex(name));
779 if (index == _not(size_t))
780 return [super cy$setProperty:name to:value];
782 id object(value ?: [NSNull null]);
784 size_t count([self count]);
786 [self replaceObjectAtIndex:index withObject:object];
788 if (index != count) {
789 WebUndefined *undefined([WebUndefined undefined]);
790 for (size_t i(count); i != index; ++i)
791 [self addObject:undefined];
794 [self addObject:object];
800 - (bool) cy$deleteProperty:(NSString *)name {
801 size_t index(CYGetIndex(name));
802 if (index == _not(size_t) || index >= [self count])
803 return [super cy$deleteProperty:name];
804 [self replaceObjectAtIndex:index withObject:[WebUndefined undefined]];
810 /* Bridge: NSMutableDictionary {{{ */
811 @implementation NSMutableDictionary (Cycript)
813 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
814 [self setObject:(value ?: [NSNull null]) forKey:name];
818 - (bool) cy$deleteProperty:(NSString *)name {
819 if ([self objectForKey:name] == nil)
822 [self removeObjectForKey:name];
829 /* Bridge: NSNumber {{{ */
830 @implementation NSNumber (Cycript)
832 - (JSType) cy$JSType {
834 // XXX: this just seems stupid
835 if ([self class] == NSCFBoolean_)
836 return kJSTypeBoolean;
838 return kJSTypeNumber;
841 - (NSObject *) cy$toJSON:(NSString *)key {
845 - (NSString *) cy$toCYON {
846 return [self cy$JSType] != kJSTypeBoolean ? [self stringValue] : [self boolValue] ? @"true" : @"false";
849 - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) {
850 return [self cy$JSType] != kJSTypeBoolean ? CYCastJSValue(context, [self doubleValue]) : CYCastJSValue(context, [self boolValue]);
855 /* Bridge: NSNull {{{ */
856 @implementation NSNull (Cycript)
858 - (JSType) cy$JSType {
862 - (NSObject *) cy$toJSON:(NSString *)key {
866 - (NSString *) cy$toCYON {
872 /* Bridge: NSObject {{{ */
873 @implementation NSObject (Cycript)
875 - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) {
876 return CYMakeInstance(context, self, false);
879 - (JSType) cy$JSType {
880 return kJSTypeObject;
883 - (NSObject *) cy$toJSON:(NSString *)key {
884 return [self description];
887 - (NSString *) cy$toCYON {
888 return [[self cy$toJSON:@""] cy$toCYON];
891 - (NSString *) cy$toKey {
892 return [self cy$toCYON];
895 - (bool) cy$hasProperty:(NSString *)name {
899 - (NSObject *) cy$getProperty:(NSString *)name {
903 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
907 - (bool) cy$deleteProperty:(NSString *)name {
911 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names {
916 /* Bridge: NSProxy {{{ */
917 @implementation NSProxy (Cycript)
919 - (NSObject *) cy$toJSON:(NSString *)key {
920 return [self description];
923 - (NSString *) cy$toCYON {
924 return [[self cy$toJSON:@""] cy$toCYON];
929 /* Bridge: NSString {{{ */
930 @implementation NSString (Cycript)
932 - (JSType) cy$JSType {
933 return kJSTypeString;
936 - (NSObject *) cy$toJSON:(NSString *)key {
940 - (NSString *) cy$toCYON {
941 std::ostringstream str;
942 CYUTF8String string(CYCastUTF8String(self));
943 CYStringify(str, string.data, string.size);
944 std::string value(str.str());
945 return CYCastNSString(NULL, CYUTF8String(value.c_str(), value.size()));
948 - (NSString *) cy$toKey {
949 if (CYIsKey(CYCastUTF8String(self)))
951 return [self cy$toCYON];
956 /* Bridge: WebUndefined {{{ */
957 @implementation WebUndefined (Cycript)
959 - (JSType) cy$JSType {
960 return kJSTypeUndefined;
963 - (NSObject *) cy$toJSON:(NSString *)key {
967 - (NSString *) cy$toCYON {
971 - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) {
972 return CYJSUndefined(context);
978 static bool CYIsClass(id self) {
980 // XXX: this is a lame object_isClass
981 return class_getInstanceMethod(object_getClass(self), @selector(alloc)) != NULL;
983 return GSObjCIsClass(self);
987 Class CYCastClass(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
988 id self(CYCastNSObject(pool, context, value));
991 throw CYJSError(context, "got something that is not a Class");
995 NSArray *CYCastNSArray(JSContextRef context, JSPropertyNameArrayRef names) {
997 size_t size(JSPropertyNameArrayGetCount(names));
998 NSMutableArray *array([NSMutableArray arrayWithCapacity:size]);
999 for (size_t index(0); index != size; ++index)
1000 [array addObject:CYCastNSString(pool, context, JSPropertyNameArrayGetNameAtIndex(names, index))];
1004 JSValueRef CYCastJSValue(JSContextRef context, id value) { CYPoolTry {
1006 return CYJSNull(context);
1007 else if ([value respondsToSelector:@selector(cy$JSValueInContext:)])
1008 return [value cy$JSValueInContext:context];
1010 return CYMakeInstance(context, value, false);
1011 } CYPoolCatch(NULL) }
1013 @implementation CYJSObject
1015 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { CYObjectiveTry {
1016 if ((self = [super init]) != nil) {
1019 JSValueProtect(context_, object_);
1021 } CYObjectiveCatch }
1023 - (void) dealloc { CYObjectiveTry {
1024 JSValueUnprotect(context_, object_);
1026 } CYObjectiveCatch }
1028 - (NSObject *) cy$toJSON:(NSString *)key { CYObjectiveTry {
1029 JSValueRef toJSON(CYGetProperty(context_, object_, toJSON_));
1030 if (!CYIsCallable(context_, toJSON))
1031 return [super cy$toJSON:key];
1033 JSValueRef arguments[1] = {CYCastJSValue(context_, key)};
1034 JSValueRef value(CYCallAsFunction(context_, (JSObjectRef) toJSON, object_, 1, arguments));
1035 // XXX: do I really want an NSNull here?!
1036 return CYCastNSObject(NULL, context_, value) ?: [NSNull null];
1038 } CYObjectiveCatch }
1040 - (NSString *) cy$toCYON { CYObjectiveTry {
1042 JSValueRef exception(NULL);
1043 const char *cyon(CYPoolCCYON(pool, context_, object_));
1044 CYThrow(context_, exception);
1046 return [super cy$toCYON];
1048 return [NSString stringWithUTF8String:cyon];
1049 } CYObjectiveCatch }
1051 - (NSUInteger) count { CYObjectiveTry {
1052 JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_));
1053 size_t size(JSPropertyNameArrayGetCount(names));
1054 JSPropertyNameArrayRelease(names);
1056 } CYObjectiveCatch }
1058 - (id) objectForKey:(id)key { CYObjectiveTry {
1059 // XXX: are NSDictionary keys always NSString *?
1060 JSValueRef value(CYGetProperty(context_, object_, CYJSString((NSString *) key)));
1061 if (JSValueIsUndefined(context_, value))
1063 return CYCastNSObject(NULL, context_, value) ?: [NSNull null];
1064 } CYObjectiveCatch }
1066 - (NSEnumerator *) keyEnumerator { CYObjectiveTry {
1067 JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_));
1068 NSEnumerator *enumerator([CYCastNSArray(context_, names) objectEnumerator]);
1069 JSPropertyNameArrayRelease(names);
1071 } CYObjectiveCatch }
1073 - (void) setObject:(id)object forKey:(id)key { CYObjectiveTry {
1074 // XXX: are NSDictionary keys always NSString *?
1075 CYSetProperty(context_, object_, CYJSString((NSString *) key), CYCastJSValue(context_, object));
1076 } CYObjectiveCatch }
1078 - (void) removeObjectForKey:(id)key { CYObjectiveTry {
1079 JSValueRef exception(NULL);
1080 // XXX: are NSDictionary keys always NSString *?
1081 (void) JSObjectDeleteProperty(context_, object_, CYJSString((NSString *) key), &exception);
1082 CYThrow(context_, exception);
1083 } CYObjectiveCatch }
1087 @implementation CYJSArray
1089 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { CYObjectiveTry {
1090 if ((self = [super init]) != nil) {
1093 JSValueProtect(context_, object_);
1095 } CYObjectiveCatch }
1097 - (void) dealloc { CYObjectiveTry {
1098 JSValueUnprotect(context_, object_);
1100 } CYObjectiveCatch }
1102 - (NSUInteger) count { CYObjectiveTry {
1103 return CYCastDouble(context_, CYGetProperty(context_, object_, length_));
1104 } CYObjectiveCatch }
1106 - (id) objectAtIndex:(NSUInteger)index { CYObjectiveTry {
1107 size_t bounds([self count]);
1108 if (index >= bounds)
1109 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray objectAtIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
1110 JSValueRef exception(NULL);
1111 JSValueRef value(JSObjectGetPropertyAtIndex(context_, object_, index, &exception));
1112 CYThrow(context_, exception);
1113 return CYCastNSObject(NULL, context_, value) ?: [NSNull null];
1114 } CYObjectiveCatch }
1116 - (void) addObject:(id)object { CYObjectiveTry {
1117 JSValueRef exception(NULL);
1118 JSValueRef arguments[1];
1119 arguments[0] = CYCastJSValue(context_, object);
1120 JSObjectCallAsFunction(context_, Array_push_, object_, 1, arguments, &exception);
1121 CYThrow(context_, exception);
1122 } CYObjectiveCatch }
1124 - (void) insertObject:(id)object atIndex:(NSUInteger)index { CYObjectiveTry {
1125 size_t bounds([self count] + 1);
1126 if (index >= bounds)
1127 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray insertObject:atIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
1128 JSValueRef exception(NULL);
1129 JSValueRef arguments[3];
1130 arguments[0] = CYCastJSValue(context_, index);
1131 arguments[1] = CYCastJSValue(context_, 0);
1132 arguments[2] = CYCastJSValue(context_, object);
1133 JSObjectCallAsFunction(context_, Array_splice_, object_, 3, arguments, &exception);
1134 CYThrow(context_, exception);
1135 } CYObjectiveCatch }
1137 - (void) removeLastObject { CYObjectiveTry {
1138 JSValueRef exception(NULL);
1139 JSObjectCallAsFunction(context_, Array_pop_, object_, 0, NULL, &exception);
1140 CYThrow(context_, exception);
1141 } CYObjectiveCatch }
1143 - (void) removeObjectAtIndex:(NSUInteger)index { CYObjectiveTry {
1144 size_t bounds([self count]);
1145 if (index >= bounds)
1146 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray removeObjectAtIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
1147 JSValueRef exception(NULL);
1148 JSValueRef arguments[2];
1149 arguments[0] = CYCastJSValue(context_, index);
1150 arguments[1] = CYCastJSValue(context_, 1);
1151 JSObjectCallAsFunction(context_, Array_splice_, object_, 2, arguments, &exception);
1152 CYThrow(context_, exception);
1153 } CYObjectiveCatch }
1155 - (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)object { CYObjectiveTry {
1156 size_t bounds([self count]);
1157 if (index >= bounds)
1158 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray replaceObjectAtIndex:withObject:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
1159 CYSetProperty(context_, object_, index, CYCastJSValue(context_, object));
1160 } CYObjectiveCatch }
1164 // XXX: use objc_getAssociatedObject and objc_setAssociatedObject on 10.6
1168 JSObjectRef object_;
1176 // XXX: delete object_? ;(
1179 static CYInternal *Get(id self) {
1180 CYInternal *internal(NULL);
1181 if (object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal)) == NULL) {
1182 // XXX: do something epic? ;P
1188 static CYInternal *Set(id self) {
1189 CYInternal *internal(NULL);
1190 if (objc_ivar *ivar = object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal))) {
1191 if (internal == NULL) {
1192 internal = new CYInternal();
1193 object_setIvar(self, ivar, reinterpret_cast<id>(internal));
1196 // XXX: do something epic? ;P
1202 bool HasProperty(JSContextRef context, JSStringRef name) {
1203 if (object_ == NULL)
1205 return JSObjectHasProperty(context, object_, name);
1208 JSValueRef GetProperty(JSContextRef context, JSStringRef name) {
1209 if (object_ == NULL)
1211 return CYGetProperty(context, object_, name);
1214 void SetProperty(JSContextRef context, JSStringRef name, JSValueRef value) {
1215 if (object_ == NULL)
1216 object_ = JSObjectMake(context, NULL, NULL);
1217 CYSetProperty(context, object_, name, value);
1221 static JSObjectRef CYMakeSelector(JSContextRef context, SEL sel) {
1222 Selector_privateData *internal(new Selector_privateData(sel));
1223 return JSObjectMake(context, Selector_, internal);
1226 static SEL CYCastSEL(JSContextRef context, JSValueRef value) {
1227 if (JSValueIsObjectOfClass(context, value, Selector_)) {
1228 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate((JSObjectRef) value)));
1229 return reinterpret_cast<SEL>(internal->value_);
1231 return CYCastPointer<SEL>(context, value);
1234 void *CYObjectiveC_ExecuteStart(JSContextRef context) {
1235 // XXX: deal with exceptions!
1236 return (void *) [[NSAutoreleasePool alloc] init];
1239 void CYObjectiveC_ExecuteEnd(JSContextRef context, void *handle) {
1240 // XXX: deal with exceptions!
1241 return [(NSAutoreleasePool *) handle release];
1244 JSValueRef CYObjectiveC_RuntimeProperty(JSContextRef context, CYUTF8String name) { CYObjectiveTry_(context) {
1246 return Instance::Make(context, nil);
1247 if (Class _class = objc_getClass(name.data))
1248 return CYMakeInstance(context, _class, true);
1250 } CYObjectiveCatch }
1252 static void CYObjectiveC_CallFunction(JSContextRef context, ffi_cif *cif, void (*function)(), uint8_t *value, void **values) { CYObjectiveTry_(context) {
1253 ffi_call(cif, function, value, values);
1254 } CYObjectiveCatch }
1256 static bool CYObjectiveC_PoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { CYObjectiveTry_(context) {
1257 switch (type->primitive) {
1259 case sig::typename_P:
1260 *reinterpret_cast<id *>(data) = CYCastNSObject(pool, context, value);
1263 case sig::selector_P:
1264 *reinterpret_cast<SEL *>(data) = CYCastSEL(context, value);
1272 } CYObjectiveCatch }
1274 static JSValueRef CYObjectiveC_FromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) { CYObjectiveTry_(context) {
1275 switch (type->primitive) {
1277 if (id object = *reinterpret_cast<id *>(data)) {
1278 JSValueRef value(CYCastJSValue(context, object));
1284 case sig::typename_P:
1285 return CYMakeInstance(context, *reinterpret_cast<Class *>(data), true);
1287 case sig::selector_P:
1288 if (SEL sel = *reinterpret_cast<SEL *>(data))
1289 return CYMakeSelector(context, sel);
1293 return CYJSNull(context);
1297 } CYObjectiveCatch }
1299 static bool CYImplements(id object, Class _class, SEL selector, bool devoid) {
1300 if (objc_method *method = class_getInstanceMethod(_class, selector)) {
1305 method_getReturnType(method, type, sizeof(type));
1307 const char *type(method_getTypeEncoding(method));
1313 // XXX: possibly use a more "awesome" check?
1317 static const char *CYPoolTypeEncoding(apr_pool_t *pool, JSContextRef context, SEL sel, objc_method *method) {
1319 return method_getTypeEncoding(method);
1321 const char *name(sel_getName(sel));
1323 sqlite3_stmt *statement;
1325 _sqlcall(sqlite3_prepare(Bridge_,
1327 "\"bridge\".\"value\" "
1330 " \"bridge\".\"mode\" = -1 and"
1331 " \"bridge\".\"name\" = ?"
1333 , -1, &statement, NULL));
1336 _sqlcall(sqlite3_bind_text(statement, 1, name, -1, SQLITE_STATIC));
1339 if (_sqlcall(sqlite3_step(statement)) == SQLITE_DONE) {
1344 value = sqlite3_column_pooled(pool, statement, 0);
1347 _sqlcall(sqlite3_finalize(statement));
1355 static void MessageClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) {
1356 Closure_privateData *internal(reinterpret_cast<Closure_privateData *>(arg));
1358 JSContextRef context(internal->context_);
1360 size_t count(internal->cif_.nargs);
1361 JSValueRef values[count];
1363 for (size_t index(0); index != count; ++index)
1364 values[index] = CYFromFFI(context, internal->signature_.elements[1 + index].type, internal->cif_.arg_types[index], arguments[index]);
1366 JSObjectRef _this(CYCastJSObject(context, values[0]));
1368 JSValueRef value(CYCallAsFunction(context, internal->function_, _this, count - 2, values + 2));
1369 CYPoolFFI(NULL, context, internal->signature_.elements[0].type, internal->cif_.rtype, result, value);
1372 static JSObjectRef CYMakeMessage(JSContextRef context, SEL sel, IMP imp, const char *type) {
1373 Message_privateData *internal(new Message_privateData(sel, type, imp));
1374 return JSObjectMake(context, Message_, internal);
1377 static IMP CYMakeMessage(JSContextRef context, JSValueRef value, const char *type) {
1378 JSObjectRef function(CYCastJSObject(context, value));
1379 Closure_privateData *internal(CYMakeFunctor_(context, function, type, &MessageClosure_));
1380 return reinterpret_cast<IMP>(internal->GetValue());
1383 static bool Messages_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
1384 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1385 Class _class(internal->GetValue());
1388 const char *name(CYPoolCString(pool, context, property));
1390 if (SEL sel = sel_getUid(name))
1391 if (class_getInstanceMethod(_class, sel) != NULL)
1397 static JSValueRef Messages_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
1398 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1399 Class _class(internal->GetValue());
1402 const char *name(CYPoolCString(pool, context, property));
1404 if (SEL sel = sel_getUid(name))
1405 if (objc_method *method = class_getInstanceMethod(_class, sel))
1406 return CYMakeMessage(context, sel, method_getImplementation(method), method_getTypeEncoding(method));
1411 static bool Messages_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
1412 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1413 Class _class(internal->GetValue());
1416 const char *name(CYPoolCString(pool, context, property));
1418 SEL sel(sel_registerName(name));
1420 objc_method *method(class_getInstanceMethod(_class, sel));
1425 if (JSValueIsObjectOfClass(context, value, Message_)) {
1426 Message_privateData *message(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate((JSObjectRef) value)));
1427 type = sig::Unparse(pool, &message->signature_);
1428 imp = reinterpret_cast<IMP>(message->GetValue());
1430 type = CYPoolTypeEncoding(pool, context, sel, method);
1431 imp = CYMakeMessage(context, value, type);
1435 method_setImplementation(method, imp);
1438 GSMethodList list(GSAllocMethodList(1));
1439 GSAppendMethodToList(list, sel, type, imp, YES);
1440 GSAddMethodList(_class, list, YES);
1441 GSFlushMethodCacheForClass(_class);
1443 class_addMethod(_class, sel, imp, type);
1451 static bool Messages_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
1452 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1453 Class _class(internal->GetValue());
1456 const char *name(CYPoolCString(pool, context, property));
1458 if (SEL sel = sel_getUid(name))
1459 if (objc_method *method = class_getInstanceMethod(_class, sel)) {
1460 objc_method_list list = {NULL, 1, {method}};
1461 class_removeMethods(_class, &list);
1469 static void Messages_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1470 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1471 Class _class(internal->GetValue());
1475 objc_method **data(class_copyMethodList(_class, &size));
1476 for (size_t i(0); i != size; ++i)
1477 JSPropertyNameAccumulatorAddName(names, CYJSString(sel_getName(method_getName(data[i]))));
1480 for (objc_method_list *methods(_class->methods); methods != NULL; methods = methods->method_next)
1481 for (int i(0); i != methods->method_count; ++i)
1482 JSPropertyNameAccumulatorAddName(names, CYJSString(sel_getName(method_getName(&methods->method_list[i]))));
1486 static bool Instance_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
1487 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1488 id self(internal->GetValue());
1490 if (JSStringIsEqualToUTF8CString(property, "$cyi"))
1494 NSString *name(CYCastNSString(pool, context, property));
1496 if (CYInternal *internal = CYInternal::Get(self))
1497 if (internal->HasProperty(context, property))
1500 Class _class(object_getClass(self));
1503 // XXX: this is an evil hack to deal with NSProxy; fix elsewhere
1504 if (CYImplements(self, _class, @selector(cy$hasProperty:), false))
1505 if ([self cy$hasProperty:name])
1507 } CYPoolCatch(false)
1509 const char *string(CYPoolCString(pool, context, name));
1512 if (class_getProperty(_class, string) != NULL)
1516 if (SEL sel = sel_getUid(string))
1517 if (CYImplements(self, _class, sel, true))
1523 static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1524 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1525 id self(internal->GetValue());
1527 if (JSStringIsEqualToUTF8CString(property, "$cyi"))
1528 return Internal::Make(context, self, object);
1531 NSString *name(CYCastNSString(pool, context, property));
1533 if (CYInternal *internal = CYInternal::Get(self))
1534 if (JSValueRef value = internal->GetProperty(context, property))
1538 if (NSObject *data = [self cy$getProperty:name])
1539 return CYCastJSValue(context, data);
1542 const char *string(CYPoolCString(pool, context, name));
1543 Class _class(object_getClass(self));
1546 if (objc_property_t property = class_getProperty(_class, string)) {
1547 PropertyAttributes attributes(property);
1548 SEL sel(sel_registerName(attributes.Getter()));
1549 return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception);
1553 if (SEL sel = sel_getUid(string))
1554 if (CYImplements(self, _class, sel, true))
1555 return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception);
1560 static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
1561 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1562 id self(internal->GetValue());
1566 NSString *name(CYCastNSString(pool, context, property));
1567 NSObject *data(CYCastNSObject(pool, context, value));
1570 if ([self cy$setProperty:name to:data])
1574 const char *string(CYPoolCString(pool, context, name));
1575 Class _class(object_getClass(self));
1578 if (objc_property_t property = class_getProperty(_class, string)) {
1579 PropertyAttributes attributes(property);
1580 if (const char *setter = attributes.Setter()) {
1581 SEL sel(sel_registerName(setter));
1582 JSValueRef arguments[1] = {value};
1583 CYSendMessage(pool, context, self, NULL, sel, 1, arguments, false, exception);
1589 size_t length(strlen(string));
1591 char set[length + 5];
1597 if (string[0] != '\0') {
1598 set[3] = toupper(string[0]);
1599 memcpy(set + 4, string + 1, length - 1);
1602 set[length + 3] = ':';
1603 set[length + 4] = '\0';
1605 if (SEL sel = sel_getUid(set))
1606 if (CYImplements(self, _class, sel, false)) {
1607 JSValueRef arguments[1] = {value};
1608 CYSendMessage(pool, context, self, NULL, sel, 1, arguments, false, exception);
1611 if (CYInternal *internal = CYInternal::Set(self)) {
1612 internal->SetProperty(context, property, value);
1619 static bool Instance_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1620 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1621 id self(internal->GetValue());
1624 NSString *name(CYCastNSString(NULL, context, property));
1625 return [self cy$deleteProperty:name];
1629 static void Instance_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1630 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1631 id self(internal->GetValue());
1634 Class _class(object_getClass(self));
1639 objc_property_t *data(class_copyPropertyList(_class, &size));
1640 for (size_t i(0); i != size; ++i)
1641 JSPropertyNameAccumulatorAddName(names, CYJSString(property_getName(data[i])));
1647 // XXX: this is an evil hack to deal with NSProxy; fix elsewhere
1648 if (CYImplements(self, _class, @selector(cy$getPropertyNames:), false))
1649 [self cy$getPropertyNames:names];
1653 static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1654 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1655 JSObjectRef value(Instance::Make(context, [internal->GetValue() alloc], Instance::Uninitialized));
1659 static bool Instance_hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef instance, JSValueRef *exception) { CYTry {
1660 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) constructor)));
1661 Class _class(internal->GetValue());
1662 if (!CYIsClass(_class))
1665 if (JSValueIsObjectOfClass(context, instance, Instance_)) {
1666 Instance *linternal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) instance)));
1667 // XXX: this isn't always safe
1668 return [linternal->GetValue() isKindOfClass:_class];
1674 static bool Internal_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
1675 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1678 id self(internal->GetValue());
1679 const char *name(CYPoolCString(pool, context, property));
1681 if (object_getInstanceVariable(self, name, NULL) != NULL)
1687 static JSValueRef Internal_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1688 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1691 id self(internal->GetValue());
1692 const char *name(CYPoolCString(pool, context, property));
1694 if (objc_ivar *ivar = object_getInstanceVariable(self, name, NULL)) {
1695 Type_privateData type(pool, ivar_getTypeEncoding(ivar));
1696 return CYFromFFI(context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar));
1702 static bool Internal_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
1703 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1706 id self(internal->GetValue());
1707 const char *name(CYPoolCString(pool, context, property));
1709 if (objc_ivar *ivar = object_getInstanceVariable(self, name, NULL)) {
1710 Type_privateData type(pool, ivar_getTypeEncoding(ivar));
1711 CYPoolFFI(pool, context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar), value);
1718 static void Internal_getPropertyNames_(Class _class, JSPropertyNameAccumulatorRef names) {
1719 if (Class super = class_getSuperclass(_class))
1720 Internal_getPropertyNames_(super, names);
1724 objc_ivar **data(class_copyIvarList(_class, &size));
1725 for (size_t i(0); i != size; ++i)
1726 JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(data[i])));
1729 if (objc_ivar_list *ivars = _class->ivars)
1730 for (int i(0); i != ivars->ivar_count; ++i)
1731 JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(&ivars->ivar_list[i])));
1735 static void Internal_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1736 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1739 id self(internal->GetValue());
1740 Class _class(object_getClass(self));
1742 Internal_getPropertyNames_(_class, names);
1745 static JSValueRef Internal_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
1746 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1747 return internal->GetOwner();
1750 static JSValueRef ObjectiveC_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1752 NSString *name(CYCastNSString(pool, context, property));
1753 if (Class _class = NSClassFromString(name))
1754 return CYMakeInstance(context, _class, true);
1758 static void ObjectiveC_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1760 size_t size(objc_getClassList(NULL, 0));
1761 Class *data(reinterpret_cast<Class *>(malloc(sizeof(Class) * size)));
1764 size_t writ(objc_getClassList(data, size));
1767 if (Class *copy = reinterpret_cast<Class *>(realloc(data, sizeof(Class) * writ))) {
1773 for (size_t i(0); i != writ; ++i)
1774 JSPropertyNameAccumulatorAddName(names, CYJSString(class_getName(data[i])));
1780 while (Class _class = objc_next_class(&state))
1781 JSPropertyNameAccumulatorAddName(names, CYJSString(class_getName(_class)));
1786 static JSValueRef ObjectiveC_Image_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1787 const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object)));
1790 const char *name(CYPoolCString(pool, context, property));
1792 const char **data(objc_copyClassNamesForImage(internal, &size));
1794 for (size_t i(0); i != size; ++i)
1795 if (strcmp(name, data[i]) == 0) {
1796 if (Class _class = objc_getClass(name)) {
1797 value = CYMakeInstance(context, _class, true);
1808 static void ObjectiveC_Image_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1809 const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object)));
1811 const char **data(objc_copyClassNamesForImage(internal, &size));
1812 for (size_t i(0); i != size; ++i)
1813 JSPropertyNameAccumulatorAddName(names, CYJSString(data[i]));
1817 static JSValueRef ObjectiveC_Images_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1819 const char *name(CYPoolCString(pool, context, property));
1821 const char **data(objc_copyImageNames(&size));
1822 for (size_t i(0); i != size; ++i)
1823 if (strcmp(name, data[i]) == 0) {
1832 JSObjectRef value(JSObjectMake(context, NULL, NULL));
1833 CYSetProperty(context, value, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Image_Classes_, const_cast<char *>(name)));
1837 static void ObjectiveC_Images_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1839 const char **data(objc_copyImageNames(&size));
1840 for (size_t i(0); i != size; ++i)
1841 JSPropertyNameAccumulatorAddName(names, CYJSString(data[i]));
1846 static JSValueRef ObjectiveC_Protocols_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1848 const char *name(CYPoolCString(pool, context, property));
1849 if (Protocol *protocol = objc_getProtocol(name))
1850 return CYMakeInstance(context, protocol, true);
1854 static void ObjectiveC_Protocols_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1857 Protocol **data(objc_copyProtocolList(&size));
1858 for (size_t i(0); i != size; ++i)
1859 JSPropertyNameAccumulatorAddName(names, CYJSString(protocol_getName(data[i])));
1867 static bool stret(ffi_type *ffi_type) {
1868 return ffi_type->type == FFI_TYPE_STRUCT && (
1869 ffi_type->size > OBJC_MAX_STRUCT_BY_VALUE ||
1870 struct_forward_array[ffi_type->size] != 0
1875 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 {
1879 _class = object_getClass(self);
1883 if (objc_method *method = class_getInstanceMethod(_class, _cmd)) {
1884 imp = method_getImplementation(method);
1885 type = method_getTypeEncoding(method);
1890 NSMethodSignature *method([self methodSignatureForSelector:_cmd]);
1892 throw CYJSError(context, "unrecognized selector %s sent to object %p", sel_getName(_cmd), self);
1893 type = CYPoolCString(pool, context, [method _typeString]);
1897 objc_super super = {self, _class};
1898 void *arg0 = &super;
1904 sig::Signature signature;
1905 sig::Parse(pool, &signature, type, &Structor_);
1908 sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif);
1912 if (stret(cif.rtype))
1913 imp = class_getMethodImplementation_stret(_class, _cmd);
1915 imp = class_getMethodImplementation(_class, _cmd);
1917 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 {{{ */
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(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([internal->GetValue() cy$toJSON:key]));
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([internal->GetValue() description]));
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 return CYCastJSValue(context, CYJSString((NSString *) [NSString stringWithFormat:@"@selector(%s)", name]));
2143 static JSValueRef Selector_callAsFunction_type(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2145 throw CYJSError(context, "incorrect number of arguments to Selector.type");
2148 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
2149 SEL sel(internal->GetValue());
2151 objc_method *method;
2152 if (Class _class = CYCastClass(pool, context, arguments[0]))
2153 method = class_getInstanceMethod(_class, sel);
2157 if (const char *type = CYPoolTypeEncoding(pool, context, sel, method))
2158 return CYCastJSValue(context, CYJSString(type));
2160 return CYJSNull(context);
2163 static JSStaticValue Selector_staticValues[2] = {
2164 {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete},
2165 {NULL, NULL, NULL, 0}
2168 static JSStaticValue Instance_staticValues[5] = {
2169 {"constructor", &Instance_getProperty_constructor, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2170 {"messages", &Instance_getProperty_messages, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2171 {"prototype", &Instance_getProperty_protocol, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2172 {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2173 {NULL, NULL, NULL, 0}
2176 static JSStaticFunction Instance_staticFunctions[5] = {
2177 {"$cya", &CYValue_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2178 {"toCYON", &Instance_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2179 {"toJSON", &Instance_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2180 {"toString", &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2184 static JSStaticFunction Internal_staticFunctions[2] = {
2185 {"$cya", &Internal_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2189 static JSStaticFunction Selector_staticFunctions[5] = {
2190 {"toCYON", &Selector_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2191 {"toJSON", &Selector_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2192 {"toString", &Selector_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2193 {"type", &Selector_callAsFunction_type, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2197 void CYObjectiveC_SetupContext(JSContextRef context) {
2198 JSObjectRef global(CYGetGlobalObject(context));
2199 apr_pool_t *pool(CYGetGlobalPool());
2201 Object_type = new(pool) Type_privateData(pool, "@");
2202 Selector_type = new(pool) Type_privateData(pool, ":");
2205 NSCFBoolean_ = objc_getClass("NSCFBoolean");
2206 NSCFType_ = objc_getClass("NSCFType");
2207 NSMessageBuilder_ = objc_getClass("NSMessageBuilder");
2208 NSZombie_ = objc_getClass("_NSZombie_");
2211 NSArray_ = objc_getClass("NSArray");
2212 NSDictionary_ = objc_getClass("NSDictionary");
2213 Object_ = objc_getClass("Object");
2215 JSClassDefinition definition;
2217 definition = kJSClassDefinitionEmpty;
2218 definition.className = "Instance";
2219 definition.staticValues = Instance_staticValues;
2220 definition.staticFunctions = Instance_staticFunctions;
2221 definition.hasProperty = &Instance_hasProperty;
2222 definition.getProperty = &Instance_getProperty;
2223 definition.setProperty = &Instance_setProperty;
2224 definition.deleteProperty = &Instance_deleteProperty;
2225 definition.getPropertyNames = &Instance_getPropertyNames;
2226 definition.callAsConstructor = &Instance_callAsConstructor;
2227 definition.hasInstance = &Instance_hasInstance;
2228 definition.finalize = &CYFinalize;
2229 Instance_ = JSClassCreate(&definition);
2231 definition = kJSClassDefinitionEmpty;
2232 definition.className = "Internal";
2233 definition.staticFunctions = Internal_staticFunctions;
2234 definition.hasProperty = &Internal_hasProperty;
2235 definition.getProperty = &Internal_getProperty;
2236 definition.setProperty = &Internal_setProperty;
2237 definition.getPropertyNames = &Internal_getPropertyNames;
2238 definition.finalize = &CYFinalize;
2239 Internal_ = JSClassCreate(&definition);
2241 definition = kJSClassDefinitionEmpty;
2242 definition.className = "Message";
2243 definition.staticFunctions = cy::Functor::StaticFunctions;
2244 definition.callAsFunction = &Message_callAsFunction;
2245 definition.finalize = &CYFinalize;
2246 Message_ = JSClassCreate(&definition);
2248 definition = kJSClassDefinitionEmpty;
2249 definition.className = "Messages";
2250 definition.hasProperty = &Messages_hasProperty;
2251 definition.getProperty = &Messages_getProperty;
2252 definition.setProperty = &Messages_setProperty;
2254 definition.deleteProperty = &Messages_deleteProperty;
2256 definition.getPropertyNames = &Messages_getPropertyNames;
2257 definition.finalize = &CYFinalize;
2258 Messages_ = JSClassCreate(&definition);
2260 definition = kJSClassDefinitionEmpty;
2261 definition.className = "Selector";
2262 definition.staticValues = Selector_staticValues;
2263 definition.staticFunctions = Selector_staticFunctions;
2264 definition.callAsFunction = &Selector_callAsFunction;
2265 definition.finalize = &CYFinalize;
2266 Selector_ = JSClassCreate(&definition);
2268 definition = kJSClassDefinitionEmpty;
2269 definition.className = "Super";
2270 definition.staticFunctions = Internal_staticFunctions;
2271 definition.finalize = &CYFinalize;
2272 Super_ = JSClassCreate(&definition);
2274 definition = kJSClassDefinitionEmpty;
2275 definition.className = "ObjectiveC::Classes";
2276 definition.getProperty = &ObjectiveC_Classes_getProperty;
2277 definition.getPropertyNames = &ObjectiveC_Classes_getPropertyNames;
2278 ObjectiveC_Classes_ = JSClassCreate(&definition);
2280 definition = kJSClassDefinitionEmpty;
2281 definition.className = "ObjectiveC::Protocols";
2282 definition.getProperty = &ObjectiveC_Protocols_getProperty;
2283 definition.getPropertyNames = &ObjectiveC_Protocols_getPropertyNames;
2284 ObjectiveC_Protocols_ = JSClassCreate(&definition);
2286 JSObjectRef ObjectiveC(JSObjectMake(context, NULL, NULL));
2287 CYSetProperty(context, global, CYJSString("ObjectiveC"), ObjectiveC);
2289 CYSetProperty(context, ObjectiveC, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Classes_, NULL));
2290 CYSetProperty(context, ObjectiveC, CYJSString("protocols"), JSObjectMake(context, ObjectiveC_Protocols_, NULL));
2293 definition = kJSClassDefinitionEmpty;
2294 definition.className = "ObjectiveC::Images";
2295 definition.getProperty = &ObjectiveC_Images_getProperty;
2296 definition.getPropertyNames = &ObjectiveC_Images_getPropertyNames;
2297 ObjectiveC_Images_ = JSClassCreate(&definition);
2299 definition = kJSClassDefinitionEmpty;
2300 definition.className = "ObjectiveC::Image::Classes";
2301 definition.getProperty = &ObjectiveC_Image_Classes_getProperty;
2302 definition.getPropertyNames = &ObjectiveC_Image_Classes_getPropertyNames;
2303 ObjectiveC_Image_Classes_ = JSClassCreate(&definition);
2305 CYSetProperty(context, ObjectiveC, CYJSString("images"), JSObjectMake(context, ObjectiveC_Images_, NULL));
2308 JSObjectRef Instance(JSObjectMakeConstructor(context, Instance_, &Instance_new));
2309 JSObjectRef Message(JSObjectMakeConstructor(context, Message_, NULL));
2310 JSObjectRef Selector(JSObjectMakeConstructor(context, Selector_, &Selector_new));
2311 JSObjectRef Super(JSObjectMakeConstructor(context, Super_, &Super_new));
2313 Instance_prototype_ = (JSObjectRef) CYGetProperty(context, Instance, prototype_);
2314 JSValueProtect(context, Instance_prototype_);
2316 CYSetProperty(context, global, CYJSString("Instance"), Instance);
2317 CYSetProperty(context, global, CYJSString("Selector"), Selector);
2318 CYSetProperty(context, global, CYJSString("Super"), Super);
2321 CYSetProperty(context, global, CYJSString("objc_registerClassPair"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_registerClassPair"), &objc_registerClassPair_));
2322 MSHookFunction(&objc_registerClassPair, MSHake(objc_registerClassPair));
2325 CYSetProperty(context, global, CYJSString("objc_msgSend"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_msgSend"), &$objc_msgSend));
2327 JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Message, prototype_), Function_prototype_);
2328 JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Selector, prototype_), Function_prototype_);
2331 class_addMethod(NSCFType_, @selector(cy$toJSON:), reinterpret_cast<IMP>(&NSCFType$cy$toJSON), "@12@0:4@8");
2335 static CYHooks CYObjectiveCHooks = {
2336 &CYObjectiveC_ExecuteStart,
2337 &CYObjectiveC_ExecuteEnd,
2338 &CYObjectiveC_RuntimeProperty,
2339 &CYObjectiveC_CallFunction,
2340 &CYObjectiveC_SetupContext,
2341 &CYObjectiveC_PoolFFI,
2342 &CYObjectiveC_FromFFI,
2345 struct CYObjectiveC {
2347 hooks_ = &CYObjectiveCHooks;