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)))
78 #define objc_getClass GSClassFromName
80 #define objc_getProtocol GSProtocolFromName
82 #define object_getClass GSObjCClass
84 #define object_getInstanceVariable(object, name, value) ({ \
85 objc_ivar *ivar(class_getInstanceVariable(object_getClass(object), name)); \
87 GSObjCGetVariable(object, ivar_getOffset(ivar), sizeof(void *), value); \
91 #define object_setIvar(object, ivar, value) ({ \
92 void *data = (value); \
93 GSObjCSetVariable(object, ivar_getOffset(ivar), sizeof(void *), &data); \
96 #define protocol_getName(protocol) [(protocol) name]
99 JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, Class super, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception);
101 extern sqlite3 *Bridge_;
103 /* Objective-C Pool Release {{{ */
104 apr_status_t CYPoolRelease_(void *data) {
105 id object(reinterpret_cast<id>(data));
110 id CYPoolRelease_(apr_pool_t *pool, id object) {
113 else if (pool == NULL)
114 return [object autorelease];
116 apr_pool_cleanup_register(pool, object, &CYPoolRelease_, &apr_pool_cleanup_null);
121 template <typename Type_>
122 Type_ CYPoolRelease(apr_pool_t *pool, Type_ object) {
123 return (Type_) CYPoolRelease_(pool, (id) object);
126 /* Objective-C Strings {{{ */
127 const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, NSString *value) {
129 return [value UTF8String];
131 size_t size([value maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1);
132 char *string(new(pool) char[size]);
133 if (![value getCString:string maxLength:size encoding:NSUTF8StringEncoding])
134 throw CYJSError(context, "[NSString getCString:maxLength:encoding:] == NO");
139 JSStringRef CYCopyJSString(JSContextRef context, NSString *value) {
141 return JSStringCreateWithCFString(reinterpret_cast<CFStringRef>(value));
144 return CYCopyJSString(CYPoolCString(pool, context, value));
148 JSStringRef CYCopyJSString(JSContextRef context, NSObject *value) {
151 // XXX: this definition scares me; is anyone using this?!
152 NSString *string([value description]);
153 return CYCopyJSString(context, string);
156 NSString *CYCopyNSString(const CYUTF8String &value) {
158 return (NSString *) CFStringCreateWithBytes(kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(value.data), value.size, kCFStringEncodingUTF8, true);
160 return [[NSString alloc] initWithBytes:value.data length:value.size encoding:NSUTF8StringEncoding];
164 NSString *CYCopyNSString(JSContextRef context, JSStringRef value) {
166 return (NSString *) JSStringCopyCFString(kCFAllocatorDefault, value);
169 return CYCopyNSString(CYPoolUTF8String(pool, context, value));
173 NSString *CYCopyNSString(JSContextRef context, JSValueRef value) {
174 return CYCopyNSString(context, CYJSString(context, value));
177 NSString *CYCastNSString(apr_pool_t *pool, const CYUTF8String &value) {
178 return CYPoolRelease(pool, CYCopyNSString(value));
181 NSString *CYCastNSString(apr_pool_t *pool, SEL sel) {
182 const char *name(sel_getName(sel));
183 return CYPoolRelease(pool, CYCopyNSString(CYUTF8String(name, strlen(name))));
186 NSString *CYCastNSString(apr_pool_t *pool, JSContextRef context, JSStringRef value) {
187 return CYPoolRelease(pool, CYCopyNSString(context, value));
190 CYUTF8String CYCastUTF8String(NSString *value) {
191 NSData *data([value dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO]);
192 return CYUTF8String(reinterpret_cast<const char *>([data bytes]), [data length]);
196 JSValueRef CYCastJSValue(JSContextRef context, NSObject *value);
198 void CYThrow(JSContextRef context, NSException *error, JSValueRef *exception) {
199 if (exception == NULL)
201 *exception = CYCastJSValue(context, error);
204 size_t CYGetIndex(NSString *value) {
205 return CYGetIndex(CYCastUTF8String(value));
208 bool CYGetOffset(apr_pool_t *pool, JSContextRef context, NSString *value, ssize_t &index) {
209 return CYGetOffset(CYPoolCString(pool, context, value), index);
212 static JSClassRef Instance_;
213 static JSClassRef Internal_;
214 static JSClassRef Message_;
215 static JSClassRef Messages_;
216 static JSClassRef Selector_;
217 static JSClassRef Super_;
219 static JSClassRef ObjectiveC_Classes_;
220 static JSClassRef ObjectiveC_Protocols_;
223 static JSClassRef ObjectiveC_Image_Classes_;
224 static JSClassRef ObjectiveC_Images_;
227 static JSObjectRef Instance_prototype_;
230 static Class NSCFBoolean_;
231 static Class NSCFType_;
232 static Class NSMessageBuilder_;
233 static Class NSZombie_;
235 static Class NSBoolNumber_;
238 static Class NSArray_;
239 static Class NSDictionary_;
240 static Class Object_;
242 static Type_privateData *Object_type;
243 static Type_privateData *Selector_type;
245 Type_privateData *Instance::GetType() const {
249 Type_privateData *Selector_privateData::GetType() const {
250 return Selector_type;
253 // XXX: trick this out with associated objects!
254 JSValueRef CYGetClassPrototype(JSContextRef context, id self) {
256 return Instance_prototype_;
258 // XXX: I need to think through multi-context
259 typedef std::map<id, JSValueRef> CacheMap;
260 static CacheMap cache_;
262 JSValueRef &value(cache_[self]);
266 JSClassRef _class(NULL);
267 JSValueRef prototype;
269 if (self == NSArray_)
270 prototype = Array_prototype_;
271 else if (self == NSDictionary_)
272 prototype = Object_prototype_;
274 prototype = CYGetClassPrototype(context, class_getSuperclass(self));
276 JSObjectRef object(JSObjectMake(context, _class, NULL));
277 JSObjectSetPrototype(context, object, prototype);
279 JSValueProtect(context, object);
284 JSObjectRef Messages::Make(JSContextRef context, Class _class, bool array) {
285 JSObjectRef value(JSObjectMake(context, Messages_, new Messages(_class)));
286 if (_class == NSArray_)
288 if (Class super = class_getSuperclass(_class))
289 JSObjectSetPrototype(context, value, Messages::Make(context, super, array));
291 JSObjectSetPrototype(context, value, Array_prototype_);*/
295 JSObjectRef Internal::Make(JSContextRef context, id object, JSObjectRef owner) {
296 return JSObjectMake(context, Internal_, new Internal(object, context, owner));
300 JSObjectRef Super::Make(JSContextRef context, id object, Class _class) {
301 JSObjectRef value(JSObjectMake(context, Super_, new Super(object, _class)));
305 JSObjectRef Instance::Make(JSContextRef context, id object, Flags flags) {
306 JSObjectRef value(JSObjectMake(context, Instance_, new Instance(object, flags)));
307 JSObjectSetPrototype(context, value, CYGetClassPrototype(context, object_getClass(object)));
311 Instance::~Instance() {
312 if ((flags_ & Transient) == 0)
313 // XXX: does this handle background threads correctly?
314 // XXX: this simply does not work on the console because I'm stupid
315 [GetValue() performSelector:@selector(release) withObject:nil afterDelay:0];
318 struct Message_privateData :
323 Message_privateData(SEL sel, const char *type, IMP value = NULL) :
324 cy::Functor(type, reinterpret_cast<void (*)()>(value)),
330 JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient) {
331 Instance::Flags flags;
334 flags = Instance::Transient;
336 flags = Instance::None;
337 object = [object retain];
340 return Instance::Make(context, object, flags);
343 @interface NSMethodSignature (Cycript)
344 - (NSString *) _typeString;
347 @interface NSObject (Cycript)
349 - (JSValueRef) cy$JSValueInContext:(JSContextRef)context;
350 - (JSType) cy$JSType;
352 - (NSObject *) cy$toJSON:(NSString *)key;
353 - (NSString *) cy$toCYON;
354 - (NSString *) cy$toKey;
356 - (bool) cy$hasProperty:(NSString *)name;
357 - (NSObject *) cy$getProperty:(NSString *)name;
358 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value;
359 - (bool) cy$deleteProperty:(NSString *)name;
360 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context;
365 - (JSValueRef) cy$JSValueInContext:(JSContextRef)context;
368 NSString *CYCastNSCYON(id value) {
374 Class _class(object_getClass(value));
375 SEL sel(@selector(cy$toCYON));
377 if (objc_method *toCYON = class_getInstanceMethod(_class, sel))
378 string = reinterpret_cast<NSString *(*)(id, SEL)>(method_getImplementation(toCYON))(value, sel);
379 else if (objc_method *methodSignatureForSelector = class_getInstanceMethod(_class, @selector(methodSignatureForSelector:))) {
380 if (reinterpret_cast<NSMethodSignature *(*)(id, SEL, SEL)>(method_getImplementation(methodSignatureForSelector))(value, @selector(methodSignatureForSelector:), sel) != nil)
381 string = [value cy$toCYON];
386 else if (value == NSZombie_)
387 string = @"_NSZombie_";
388 else if (_class == NSZombie_)
389 string = [NSString stringWithFormat:@"<_NSZombie_: %p>", value];
390 // XXX: frowny /in/ the pants
391 else if (value == NSMessageBuilder_ || value == Object_)
395 string = [NSString stringWithFormat:@"%@", value];
400 string = @"undefined";
407 struct PropertyAttributes {
412 const char *variable;
425 PropertyAttributes(objc_property_t property) :
437 name = property_getName(property);
438 const char *attributes(property_getAttributes(property));
440 for (char *state, *token(apr_strtok(apr_pstrdup(pool_, attributes), ",", &state)); token != NULL; token = apr_strtok(NULL, ",", &state)) {
442 case 'R': readonly = true; break;
443 case 'C': copy = true; break;
444 case '&': retain = true; break;
445 case 'N': nonatomic = true; break;
446 case 'G': getter_ = token + 1; break;
447 case 'S': setter_ = token + 1; break;
448 case 'V': variable = token + 1; break;
452 /*if (variable == NULL) {
453 variable = property_getName(property);
454 size_t size(strlen(variable));
455 char *name(new(pool_) char[size + 2]);
457 memcpy(name + 1, variable, size);
458 name[size + 1] = '\0';
463 const char *Getter() {
465 getter_ = apr_pstrdup(pool_, name);
469 const char *Setter() {
470 if (setter_ == NULL && !readonly) {
471 size_t length(strlen(name));
473 char *temp(new(pool_) char[length + 5]);
479 temp[3] = toupper(name[0]);
480 memcpy(temp + 4, name + 1, length - 1);
483 temp[length + 3] = ':';
484 temp[length + 4] = '\0';
495 NSObject *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) {
496 return [(NSString *) CFCopyDescription((CFTypeRef) self) autorelease];
501 @interface CYWebUndefined : NSObject {
504 + (CYWebUndefined *) undefined;
508 @implementation CYWebUndefined
510 + (CYWebUndefined *) undefined {
511 static CYWebUndefined *instance_([[CYWebUndefined alloc] init]);
517 #define WebUndefined CYWebUndefined
520 /* Bridge: CYJSObject {{{ */
521 @interface CYJSObject : NSMutableDictionary {
523 JSContextRef context_;
526 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context;
528 - (NSObject *) cy$toJSON:(NSString *)key;
530 - (NSUInteger) count;
531 - (id) objectForKey:(id)key;
532 - (NSEnumerator *) keyEnumerator;
533 - (void) setObject:(id)object forKey:(id)key;
534 - (void) removeObjectForKey:(id)key;
538 /* Bridge: CYJSArray {{{ */
539 @interface CYJSArray : NSMutableArray {
541 JSContextRef context_;
544 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context;
546 - (NSUInteger) count;
547 - (id) objectAtIndex:(NSUInteger)index;
549 - (void) addObject:(id)anObject;
550 - (void) insertObject:(id)anObject atIndex:(NSUInteger)index;
551 - (void) removeLastObject;
552 - (void) removeObjectAtIndex:(NSUInteger)index;
553 - (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
558 NSObject *CYCastNSObject_(apr_pool_t *pool, JSContextRef context, JSObjectRef object) {
559 JSValueRef exception(NULL);
560 bool array(JSValueIsInstanceOfConstructor(context, object, Array_, &exception));
561 CYThrow(context, exception);
562 id value(array ? [CYJSArray alloc] : [CYJSObject alloc]);
563 return CYPoolRelease(pool, [value initWithJSObject:object inContext:context]);
566 NSObject *CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSObjectRef object) {
567 if (!JSValueIsObjectOfClass(context, object, Instance_))
568 return CYCastNSObject_(pool, context, object);
570 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
571 return internal->GetValue();
575 NSNumber *CYCopyNSNumber(JSContextRef context, JSValueRef value) {
576 return [[NSNumber alloc] initWithDouble:CYCastDouble(context, value)];
580 @interface NSBoolNumber : NSNumber {
585 id CYNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value, bool cast) {
589 switch (JSType type = JSValueGetType(context, value)) {
590 case kJSTypeUndefined:
591 object = [WebUndefined undefined];
601 object = (id) (CYCastBool(context, value) ? kCFBooleanTrue : kCFBooleanFalse);
604 object = [[NSBoolNumber alloc] initWithBool:CYCastBool(context, value)];
610 object = CYCopyNSNumber(context, value);
615 object = CYCopyNSString(context, value);
620 // XXX: this might could be more efficient
621 object = CYCastNSObject(pool, context, (JSObjectRef) value);
626 throw CYJSError(context, "JSValueGetType() == 0x%x", type);
633 return CYPoolRelease(pool, object);
635 return [object retain];
638 NSObject *CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
639 return CYNSObject(pool, context, value, true);
642 NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
643 return CYNSObject(pool, context, value, false);
646 /* Bridge: NSArray {{{ */
647 @implementation NSArray (Cycript)
649 - (NSString *) cy$toCYON {
650 NSMutableString *json([[[NSMutableString alloc] init] autorelease]);
651 [json appendString:@"["];
655 for (id object in self) {
657 for (size_t index(0), count([self count]); index != count; ++index) {
658 id object([self objectAtIndex:index]);
661 [json appendString:@","];
664 if (object == nil || [object cy$JSType] != kJSTypeUndefined)
665 [json appendString:CYCastNSCYON(object)];
667 [json appendString:@","];
672 [json appendString:@"]"];
676 - (bool) cy$hasProperty:(NSString *)name {
677 if ([name isEqualToString:@"length"])
680 size_t index(CYGetIndex(name));
681 if (index == _not(size_t) || index >= [self count])
682 return [super cy$hasProperty:name];
687 - (NSObject *) cy$getProperty:(NSString *)name {
688 if ([name isEqualToString:@"length"]) {
689 NSUInteger count([self count]);
691 return [NSNumber numberWithUnsignedInteger:count];
693 return [NSNumber numberWithUnsignedInt:count];
697 size_t index(CYGetIndex(name));
698 if (index == _not(size_t) || index >= [self count])
699 return [super cy$getProperty:name];
701 return [self objectAtIndex:index];
704 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context {
705 [super cy$getPropertyNames:names inContext:context];
707 for (size_t index(0), count([self count]); index != count; ++index) {
708 id object([self objectAtIndex:index]);
709 if (object == nil || [object cy$JSType] != kJSTypeUndefined) {
711 sprintf(name, "%zu", index);
712 JSPropertyNameAccumulatorAddName(names, CYJSString(name));
719 /* Bridge: NSBoolNumber {{{ */
721 @implementation NSBoolNumber (Cycript)
723 - (JSType) cy$JSType {
724 return kJSTypeBoolean;
727 - (NSObject *) cy$toJSON:(NSString *)key {
731 - (NSString *) cy$toCYON {
732 return [self boolValue] ? @"true" : @"false";
735 - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) {
736 return CYCastJSValue(context, (bool) [self boolValue]);
742 /* Bridge: NSDictionary {{{ */
743 @implementation NSDictionary (Cycript)
745 - (NSString *) cy$toCYON {
746 NSMutableString *json([[[NSMutableString alloc] init] autorelease]);
747 [json appendString:@"{"];
751 for (id key in self) {
753 NSEnumerator *keys([self keyEnumerator]);
754 while (id key = [keys nextObject]) {
757 [json appendString:@","];
760 [json appendString:[key cy$toKey]];
761 [json appendString:@":"];
762 NSObject *object([self objectForKey:key]);
763 [json appendString:CYCastNSCYON(object)];
766 [json appendString:@"}"];
770 - (bool) cy$hasProperty:(NSString *)name {
771 return [self objectForKey:name] != nil;
774 - (NSObject *) cy$getProperty:(NSString *)name {
775 return [self objectForKey:name];
778 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context {
779 [super cy$getPropertyNames:names inContext:context];
782 for (NSString *key in self) {
784 NSEnumerator *keys([self keyEnumerator]);
785 while (NSString *key = [keys nextObject]) {
787 JSPropertyNameAccumulatorAddName(names, CYJSString(context, key));
793 /* Bridge: NSMutableArray {{{ */
794 @implementation NSMutableArray (Cycript)
796 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
797 if ([name isEqualToString:@"length"]) {
798 // XXX: is this not intelligent?
799 NSNumber *number(reinterpret_cast<NSNumber *>(value));
801 NSUInteger size([number unsignedIntegerValue]);
803 NSUInteger size([number unsignedIntValue]);
805 NSUInteger count([self count]);
807 [self removeObjectsInRange:NSMakeRange(size, count - size)];
808 else if (size != count) {
809 WebUndefined *undefined([WebUndefined undefined]);
810 for (size_t i(count); i != size; ++i)
811 [self addObject:undefined];
816 size_t index(CYGetIndex(name));
817 if (index == _not(size_t))
818 return [super cy$setProperty:name to:value];
820 id object(value ?: [NSNull null]);
822 size_t count([self count]);
824 [self replaceObjectAtIndex:index withObject:object];
826 if (index != count) {
827 WebUndefined *undefined([WebUndefined undefined]);
828 for (size_t i(count); i != index; ++i)
829 [self addObject:undefined];
832 [self addObject:object];
838 - (bool) cy$deleteProperty:(NSString *)name {
839 size_t index(CYGetIndex(name));
840 if (index == _not(size_t) || index >= [self count])
841 return [super cy$deleteProperty:name];
842 [self replaceObjectAtIndex:index withObject:[WebUndefined undefined]];
848 /* Bridge: NSMutableDictionary {{{ */
849 @implementation NSMutableDictionary (Cycript)
851 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
852 [self setObject:(value ?: [NSNull null]) forKey:name];
856 - (bool) cy$deleteProperty:(NSString *)name {
857 if ([self objectForKey:name] == nil)
860 [self removeObjectForKey:name];
867 /* Bridge: NSNumber {{{ */
868 @implementation NSNumber (Cycript)
870 - (JSType) cy$JSType {
872 // XXX: this just seems stupid
873 if ([self class] == NSCFBoolean_)
874 return kJSTypeBoolean;
876 return kJSTypeNumber;
879 - (NSObject *) cy$toJSON:(NSString *)key {
883 - (NSString *) cy$toCYON {
884 return [self cy$JSType] != kJSTypeBoolean ? [self stringValue] : [self boolValue] ? @"true" : @"false";
887 - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) {
888 return [self cy$JSType] != kJSTypeBoolean ? CYCastJSValue(context, [self doubleValue]) : CYCastJSValue(context, [self boolValue]);
893 /* Bridge: NSNull {{{ */
894 @implementation NSNull (Cycript)
896 - (JSType) cy$JSType {
900 - (NSObject *) cy$toJSON:(NSString *)key {
904 - (NSString *) cy$toCYON {
910 /* Bridge: NSObject {{{ */
911 @implementation NSObject (Cycript)
913 - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) {
914 return CYMakeInstance(context, self, false);
917 - (JSType) cy$JSType {
918 return kJSTypeObject;
921 - (NSObject *) cy$toJSON:(NSString *)key {
922 return [self description];
925 - (NSString *) cy$toCYON {
926 return [[self cy$toJSON:@""] cy$toCYON];
929 - (NSString *) cy$toKey {
930 return [self cy$toCYON];
933 - (bool) cy$hasProperty:(NSString *)name {
937 - (NSObject *) cy$getProperty:(NSString *)name {
941 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
945 - (bool) cy$deleteProperty:(NSString *)name {
949 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context {
954 /* Bridge: NSProxy {{{ */
955 @implementation NSProxy (Cycript)
957 - (NSObject *) cy$toJSON:(NSString *)key {
958 return [self description];
961 - (NSString *) cy$toCYON {
962 return [[self cy$toJSON:@""] cy$toCYON];
967 /* Bridge: NSString {{{ */
968 @implementation NSString (Cycript)
970 - (JSType) cy$JSType {
971 return kJSTypeString;
974 - (NSObject *) cy$toJSON:(NSString *)key {
978 - (NSString *) cy$toCYON {
979 std::ostringstream str;
980 CYUTF8String string(CYCastUTF8String(self));
981 CYStringify(str, string.data, string.size);
982 std::string value(str.str());
983 return CYCastNSString(NULL, CYUTF8String(value.c_str(), value.size()));
986 - (NSString *) cy$toKey {
987 if (CYIsKey(CYCastUTF8String(self)))
989 return [self cy$toCYON];
994 /* Bridge: WebUndefined {{{ */
995 @implementation WebUndefined (Cycript)
997 - (JSType) cy$JSType {
998 return kJSTypeUndefined;
1001 - (NSObject *) cy$toJSON:(NSString *)key {
1005 - (NSString *) cy$toCYON {
1006 return @"undefined";
1009 - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) {
1010 return CYJSUndefined(context);
1011 } CYObjectiveCatch }
1016 static bool CYIsClass(id self) {
1018 // XXX: this is a lame object_isClass
1019 return class_getInstanceMethod(object_getClass(self), @selector(alloc)) != NULL;
1021 return GSObjCIsClass(self);
1025 Class CYCastClass(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
1026 id self(CYCastNSObject(pool, context, value));
1027 if (CYIsClass(self))
1028 return (Class) self;
1029 throw CYJSError(context, "got something that is not a Class");
1033 NSArray *CYCastNSArray(JSContextRef context, JSPropertyNameArrayRef names) {
1035 size_t size(JSPropertyNameArrayGetCount(names));
1036 NSMutableArray *array([NSMutableArray arrayWithCapacity:size]);
1037 for (size_t index(0); index != size; ++index)
1038 [array addObject:CYCastNSString(pool, context, JSPropertyNameArrayGetNameAtIndex(names, index))];
1042 JSValueRef CYCastJSValue(JSContextRef context, NSObject *value) { CYPoolTry {
1044 return CYJSNull(context);
1045 else if ([value respondsToSelector:@selector(cy$JSValueInContext:)])
1046 return [value cy$JSValueInContext:context];
1048 return CYMakeInstance(context, value, false);
1049 } CYPoolCatch(NULL) return /*XXX*/ NULL; }
1051 @implementation CYJSObject
1053 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { CYObjectiveTry {
1054 if ((self = [super init]) != nil) {
1057 JSValueProtect(context_, object_);
1059 } CYObjectiveCatch }
1061 - (void) dealloc { CYObjectiveTry {
1062 JSValueUnprotect(context_, object_);
1064 } CYObjectiveCatch }
1066 - (NSObject *) cy$toJSON:(NSString *)key { CYObjectiveTry {
1067 JSValueRef toJSON(CYGetProperty(context_, object_, toJSON_));
1068 if (!CYIsCallable(context_, toJSON))
1069 return [super cy$toJSON:key];
1071 JSValueRef arguments[1] = {CYCastJSValue(context_, key)};
1072 JSValueRef value(CYCallAsFunction(context_, (JSObjectRef) toJSON, object_, 1, arguments));
1073 // XXX: do I really want an NSNull here?!
1074 return CYCastNSObject(NULL, context_, value) ?: [NSNull null];
1076 } CYObjectiveCatch }
1078 - (NSString *) cy$toCYON { CYObjectiveTry {
1080 JSValueRef exception(NULL);
1081 const char *cyon(CYPoolCCYON(pool, context_, object_));
1082 CYThrow(context_, exception);
1084 return [super cy$toCYON];
1086 return [NSString stringWithUTF8String:cyon];
1087 } CYObjectiveCatch }
1089 - (NSUInteger) count { CYObjectiveTry {
1090 JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_));
1091 size_t size(JSPropertyNameArrayGetCount(names));
1092 JSPropertyNameArrayRelease(names);
1094 } CYObjectiveCatch }
1096 - (id) objectForKey:(id)key { CYObjectiveTry {
1097 // XXX: are NSDictionary keys always NSString *?
1098 JSValueRef value(CYGetProperty(context_, object_, CYJSString(context_, (NSString *) key)));
1099 if (JSValueIsUndefined(context_, value))
1101 return CYCastNSObject(NULL, context_, value) ?: [NSNull null];
1102 } CYObjectiveCatch }
1104 - (NSEnumerator *) keyEnumerator { CYObjectiveTry {
1105 JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_));
1106 NSEnumerator *enumerator([CYCastNSArray(context_, names) objectEnumerator]);
1107 JSPropertyNameArrayRelease(names);
1109 } CYObjectiveCatch }
1111 - (void) setObject:(id)object forKey:(id)key { CYObjectiveTry {
1112 // XXX: are NSDictionary keys always NSString *?
1113 CYSetProperty(context_, object_, CYJSString(context_, (NSString *) key), CYCastJSValue(context_, (NSString *) object));
1114 } CYObjectiveCatch }
1116 - (void) removeObjectForKey:(id)key { CYObjectiveTry {
1117 JSValueRef exception(NULL);
1118 // XXX: are NSDictionary keys always NSString *?
1119 (void) JSObjectDeleteProperty(context_, object_, CYJSString(context_, (NSString *) key), &exception);
1120 CYThrow(context_, exception);
1121 } CYObjectiveCatch }
1125 @implementation CYJSArray
1127 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { CYObjectiveTry {
1128 if ((self = [super init]) != nil) {
1131 JSValueProtect(context_, object_);
1133 } CYObjectiveCatch }
1135 - (void) dealloc { CYObjectiveTry {
1136 JSValueUnprotect(context_, object_);
1138 } CYObjectiveCatch }
1140 - (NSUInteger) count { CYObjectiveTry {
1141 return CYCastDouble(context_, CYGetProperty(context_, object_, length_));
1142 } CYObjectiveCatch }
1144 - (id) objectAtIndex:(NSUInteger)index { CYObjectiveTry {
1145 size_t bounds([self count]);
1146 if (index >= bounds)
1147 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray objectAtIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
1148 JSValueRef exception(NULL);
1149 JSValueRef value(JSObjectGetPropertyAtIndex(context_, object_, index, &exception));
1150 CYThrow(context_, exception);
1151 return CYCastNSObject(NULL, context_, value) ?: [NSNull null];
1152 } CYObjectiveCatch }
1154 - (void) addObject:(id)object { CYObjectiveTry {
1155 JSValueRef exception(NULL);
1156 JSValueRef arguments[1];
1157 arguments[0] = CYCastJSValue(context_, (NSObject *) object);
1158 JSObjectCallAsFunction(context_, Array_push_, object_, 1, arguments, &exception);
1159 CYThrow(context_, exception);
1160 } CYObjectiveCatch }
1162 - (void) insertObject:(id)object atIndex:(NSUInteger)index { CYObjectiveTry {
1163 size_t bounds([self count] + 1);
1164 if (index >= bounds)
1165 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray insertObject:atIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
1166 JSValueRef exception(NULL);
1167 JSValueRef arguments[3];
1168 arguments[0] = CYCastJSValue(context_, index);
1169 arguments[1] = CYCastJSValue(context_, 0);
1170 arguments[2] = CYCastJSValue(context_, (NSObject *) object);
1171 JSObjectCallAsFunction(context_, Array_splice_, object_, 3, arguments, &exception);
1172 CYThrow(context_, exception);
1173 } CYObjectiveCatch }
1175 - (void) removeLastObject { CYObjectiveTry {
1176 JSValueRef exception(NULL);
1177 JSObjectCallAsFunction(context_, Array_pop_, object_, 0, NULL, &exception);
1178 CYThrow(context_, exception);
1179 } CYObjectiveCatch }
1181 - (void) removeObjectAtIndex:(NSUInteger)index { CYObjectiveTry {
1182 size_t bounds([self count]);
1183 if (index >= bounds)
1184 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray removeObjectAtIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
1185 JSValueRef exception(NULL);
1186 JSValueRef arguments[2];
1187 arguments[0] = CYCastJSValue(context_, index);
1188 arguments[1] = CYCastJSValue(context_, 1);
1189 JSObjectCallAsFunction(context_, Array_splice_, object_, 2, arguments, &exception);
1190 CYThrow(context_, exception);
1191 } CYObjectiveCatch }
1193 - (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)object { CYObjectiveTry {
1194 size_t bounds([self count]);
1195 if (index >= bounds)
1196 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray replaceObjectAtIndex:withObject:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
1197 CYSetProperty(context_, object_, index, CYCastJSValue(context_, (NSObject *) object));
1198 } CYObjectiveCatch }
1202 // XXX: use objc_getAssociatedObject and objc_setAssociatedObject on 10.6
1206 JSObjectRef object_;
1214 // XXX: delete object_? ;(
1217 static CYInternal *Get(id self) {
1218 CYInternal *internal(NULL);
1219 if (object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal)) == NULL) {
1220 // XXX: do something epic? ;P
1226 static CYInternal *Set(id self) {
1227 CYInternal *internal(NULL);
1228 if (objc_ivar *ivar = object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal))) {
1229 if (internal == NULL) {
1230 internal = new CYInternal();
1231 object_setIvar(self, ivar, reinterpret_cast<id>(internal));
1234 // XXX: do something epic? ;P
1240 bool HasProperty(JSContextRef context, JSStringRef name) {
1241 if (object_ == NULL)
1243 return JSObjectHasProperty(context, object_, name);
1246 JSValueRef GetProperty(JSContextRef context, JSStringRef name) {
1247 if (object_ == NULL)
1249 return CYGetProperty(context, object_, name);
1252 void SetProperty(JSContextRef context, JSStringRef name, JSValueRef value) {
1253 if (object_ == NULL)
1254 object_ = JSObjectMake(context, NULL, NULL);
1255 CYSetProperty(context, object_, name, value);
1259 static JSObjectRef CYMakeSelector(JSContextRef context, SEL sel) {
1260 Selector_privateData *internal(new Selector_privateData(sel));
1261 return JSObjectMake(context, Selector_, internal);
1264 static SEL CYCastSEL(JSContextRef context, JSValueRef value) {
1265 if (JSValueIsObjectOfClass(context, value, Selector_)) {
1266 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate((JSObjectRef) value)));
1267 return reinterpret_cast<SEL>(internal->value_);
1269 return CYCastPointer<SEL>(context, value);
1272 void *CYObjectiveC_ExecuteStart(JSContextRef context) {
1273 // XXX: deal with exceptions!
1274 return (void *) [[NSAutoreleasePool alloc] init];
1277 void CYObjectiveC_ExecuteEnd(JSContextRef context, void *handle) {
1278 // XXX: deal with exceptions!
1279 return [(NSAutoreleasePool *) handle release];
1282 JSValueRef CYObjectiveC_RuntimeProperty(JSContextRef context, CYUTF8String name) { CYPoolTry {
1284 return Instance::Make(context, nil);
1285 if (Class _class = objc_getClass(name.data))
1286 return CYMakeInstance(context, _class, true);
1288 } CYPoolCatch(NULL) return /*XXX*/ NULL; }
1290 static void CYObjectiveC_CallFunction(JSContextRef context, ffi_cif *cif, void (*function)(), uint8_t *value, void **values) { CYPoolTry {
1291 ffi_call(cif, function, value, values);
1294 static bool CYObjectiveC_PoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { CYPoolTry {
1295 switch (type->primitive) {
1297 case sig::typename_P:
1298 *reinterpret_cast<id *>(data) = CYCastNSObject(pool, context, value);
1301 case sig::selector_P:
1302 *reinterpret_cast<SEL *>(data) = CYCastSEL(context, value);
1310 } CYPoolCatch(false) return /*XXX*/ NULL; }
1312 static JSValueRef CYObjectiveC_FromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) { CYPoolTry {
1313 switch (type->primitive) {
1315 if (NSObject *object = *reinterpret_cast<NSObject **>(data)) {
1316 JSValueRef value(CYCastJSValue(context, object));
1322 case sig::typename_P:
1323 return CYMakeInstance(context, *reinterpret_cast<Class *>(data), true);
1325 case sig::selector_P:
1326 if (SEL sel = *reinterpret_cast<SEL *>(data))
1327 return CYMakeSelector(context, sel);
1331 return CYJSNull(context);
1335 } CYPoolCatch(NULL) return /*XXX*/ NULL; }
1337 static bool CYImplements(id object, Class _class, SEL selector, bool devoid) {
1338 if (objc_method *method = class_getInstanceMethod(_class, selector)) {
1341 #if OBJC_API_VERSION >= 2
1343 method_getReturnType(method, type, sizeof(type));
1345 const char *type(method_getTypeEncoding(method));
1351 // XXX: possibly use a more "awesome" check?
1355 static const char *CYPoolTypeEncoding(apr_pool_t *pool, JSContextRef context, SEL sel, objc_method *method) {
1357 return method_getTypeEncoding(method);
1359 const char *name(sel_getName(sel));
1361 sqlite3_stmt *statement;
1363 _sqlcall(sqlite3_prepare(Bridge_,
1365 "\"bridge\".\"value\" "
1368 " \"bridge\".\"mode\" = -1 and"
1369 " \"bridge\".\"name\" = ?"
1371 , -1, &statement, NULL));
1374 _sqlcall(sqlite3_bind_text(statement, 1, name, -1, SQLITE_STATIC));
1377 if (_sqlcall(sqlite3_step(statement)) == SQLITE_DONE) {
1382 value = sqlite3_column_pooled(pool, statement, 0);
1385 _sqlcall(sqlite3_finalize(statement));
1393 static void MessageClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) {
1394 Closure_privateData *internal(reinterpret_cast<Closure_privateData *>(arg));
1396 JSContextRef context(internal->context_);
1398 size_t count(internal->cif_.nargs);
1399 JSValueRef values[count];
1401 for (size_t index(0); index != count; ++index)
1402 values[index] = CYFromFFI(context, internal->signature_.elements[1 + index].type, internal->cif_.arg_types[index], arguments[index]);
1404 JSObjectRef _this(CYCastJSObject(context, values[0]));
1406 JSValueRef value(CYCallAsFunction(context, internal->function_, _this, count - 2, values + 2));
1407 CYPoolFFI(NULL, context, internal->signature_.elements[0].type, internal->cif_.rtype, result, value);
1410 static JSObjectRef CYMakeMessage(JSContextRef context, SEL sel, IMP imp, const char *type) {
1411 Message_privateData *internal(new Message_privateData(sel, type, imp));
1412 return JSObjectMake(context, Message_, internal);
1415 static IMP CYMakeMessage(JSContextRef context, JSValueRef value, const char *type) {
1416 JSObjectRef function(CYCastJSObject(context, value));
1417 Closure_privateData *internal(CYMakeFunctor_(context, function, type, &MessageClosure_));
1418 return reinterpret_cast<IMP>(internal->GetValue());
1421 static bool Messages_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
1422 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1423 Class _class(internal->GetValue());
1426 const char *name(CYPoolCString(pool, context, property));
1428 if (SEL sel = sel_getUid(name))
1429 if (class_getInstanceMethod(_class, sel) != NULL)
1435 static JSValueRef Messages_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
1436 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1437 Class _class(internal->GetValue());
1440 const char *name(CYPoolCString(pool, context, property));
1442 if (SEL sel = sel_getUid(name))
1443 if (objc_method *method = class_getInstanceMethod(_class, sel))
1444 return CYMakeMessage(context, sel, method_getImplementation(method), method_getTypeEncoding(method));
1449 static bool Messages_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
1450 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1451 Class _class(internal->GetValue());
1454 const char *name(CYPoolCString(pool, context, property));
1456 SEL sel(sel_registerName(name));
1458 objc_method *method(class_getInstanceMethod(_class, sel));
1463 if (JSValueIsObjectOfClass(context, value, Message_)) {
1464 Message_privateData *message(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate((JSObjectRef) value)));
1465 type = sig::Unparse(pool, &message->signature_);
1466 imp = reinterpret_cast<IMP>(message->GetValue());
1468 type = CYPoolTypeEncoding(pool, context, sel, method);
1469 imp = CYMakeMessage(context, value, type);
1473 method_setImplementation(method, imp);
1476 GSMethodList list(GSAllocMethodList(1));
1477 GSAppendMethodToList(list, sel, type, imp, YES);
1478 GSAddMethodList(_class, list, YES);
1479 GSFlushMethodCacheForClass(_class);
1481 class_addMethod(_class, sel, imp, type);
1488 #if 0 && OBJC_API_VERSION < 2
1489 static bool Messages_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
1490 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1491 Class _class(internal->GetValue());
1494 const char *name(CYPoolCString(pool, context, property));
1496 if (SEL sel = sel_getUid(name))
1497 if (objc_method *method = class_getInstanceMethod(_class, sel)) {
1498 objc_method_list list = {NULL, 1, {method}};
1499 class_removeMethods(_class, &list);
1507 static void Messages_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1508 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1509 Class _class(internal->GetValue());
1511 #if OBJC_API_VERSION >= 2
1513 objc_method **data(class_copyMethodList(_class, &size));
1514 for (size_t i(0); i != size; ++i)
1515 JSPropertyNameAccumulatorAddName(names, CYJSString(sel_getName(method_getName(data[i]))));
1518 for (objc_method_list *methods(_class->methods); methods != NULL; methods = methods->method_next)
1519 for (int i(0); i != methods->method_count; ++i)
1520 JSPropertyNameAccumulatorAddName(names, CYJSString(sel_getName(method_getName(&methods->method_list[i]))));
1524 static bool Instance_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
1525 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1526 id self(internal->GetValue());
1528 if (JSStringIsEqualToUTF8CString(property, "$cyi"))
1532 NSString *name(CYCastNSString(pool, context, property));
1534 if (CYInternal *internal = CYInternal::Get(self))
1535 if (internal->HasProperty(context, property))
1538 Class _class(object_getClass(self));
1541 // XXX: this is an evil hack to deal with NSProxy; fix elsewhere
1542 if (CYImplements(self, _class, @selector(cy$hasProperty:), false))
1543 if ([self cy$hasProperty:name])
1545 } CYPoolCatch(false)
1547 const char *string(CYPoolCString(pool, context, name));
1550 if (class_getProperty(_class, string) != NULL)
1554 if (SEL sel = sel_getUid(string))
1555 if (CYImplements(self, _class, sel, true))
1561 static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1562 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1563 id self(internal->GetValue());
1565 if (JSStringIsEqualToUTF8CString(property, "$cyi"))
1566 return Internal::Make(context, self, object);
1569 NSString *name(CYCastNSString(pool, context, property));
1571 if (CYInternal *internal = CYInternal::Get(self))
1572 if (JSValueRef value = internal->GetProperty(context, property))
1576 if (NSObject *data = [self cy$getProperty:name])
1577 return CYCastJSValue(context, data);
1580 const char *string(CYPoolCString(pool, context, name));
1581 Class _class(object_getClass(self));
1584 if (objc_property_t property = class_getProperty(_class, string)) {
1585 PropertyAttributes attributes(property);
1586 SEL sel(sel_registerName(attributes.Getter()));
1587 return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception);
1591 if (SEL sel = sel_getUid(string))
1592 if (CYImplements(self, _class, sel, true))
1593 return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception);
1598 static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
1599 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1600 id self(internal->GetValue());
1604 NSString *name(CYCastNSString(pool, context, property));
1605 NSObject *data(CYCastNSObject(pool, context, value));
1608 if ([self cy$setProperty:name to:data])
1612 const char *string(CYPoolCString(pool, context, name));
1613 Class _class(object_getClass(self));
1616 if (objc_property_t property = class_getProperty(_class, string)) {
1617 PropertyAttributes attributes(property);
1618 if (const char *setter = attributes.Setter()) {
1619 SEL sel(sel_registerName(setter));
1620 JSValueRef arguments[1] = {value};
1621 CYSendMessage(pool, context, self, NULL, sel, 1, arguments, false, exception);
1627 size_t length(strlen(string));
1629 char set[length + 5];
1635 if (string[0] != '\0') {
1636 set[3] = toupper(string[0]);
1637 memcpy(set + 4, string + 1, length - 1);
1640 set[length + 3] = ':';
1641 set[length + 4] = '\0';
1643 if (SEL sel = sel_getUid(set))
1644 if (CYImplements(self, _class, sel, false)) {
1645 JSValueRef arguments[1] = {value};
1646 CYSendMessage(pool, context, self, NULL, sel, 1, arguments, false, exception);
1649 if (CYInternal *internal = CYInternal::Set(self)) {
1650 internal->SetProperty(context, property, value);
1657 static bool Instance_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1658 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1659 id self(internal->GetValue());
1662 NSString *name(CYCastNSString(NULL, context, property));
1663 return [self cy$deleteProperty:name];
1665 } CYCatch return /*XXX*/ NULL; }
1667 static void Instance_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1668 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1669 id self(internal->GetValue());
1672 Class _class(object_getClass(self));
1677 objc_property_t *data(class_copyPropertyList(_class, &size));
1678 for (size_t i(0); i != size; ++i)
1679 JSPropertyNameAccumulatorAddName(names, CYJSString(property_getName(data[i])));
1685 // XXX: this is an evil hack to deal with NSProxy; fix elsewhere
1686 if (CYImplements(self, _class, @selector(cy$getPropertyNames:inContext:), false))
1687 [self cy$getPropertyNames:names inContext:context];
1691 static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1692 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1693 JSObjectRef value(Instance::Make(context, [internal->GetValue() alloc], Instance::Uninitialized));
1697 static bool Instance_hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef instance, JSValueRef *exception) { CYTry {
1698 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) constructor)));
1699 Class _class(internal->GetValue());
1700 if (!CYIsClass(_class))
1703 if (JSValueIsObjectOfClass(context, instance, Instance_)) {
1704 Instance *linternal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) instance)));
1705 // XXX: this isn't always safe
1706 return [linternal->GetValue() isKindOfClass:_class];
1712 static bool Internal_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
1713 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1716 id self(internal->GetValue());
1717 const char *name(CYPoolCString(pool, context, property));
1719 if (object_getInstanceVariable(self, name, NULL) != NULL)
1725 static JSValueRef Internal_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1726 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1729 id self(internal->GetValue());
1730 const char *name(CYPoolCString(pool, context, property));
1732 if (objc_ivar *ivar = object_getInstanceVariable(self, name, NULL)) {
1733 Type_privateData type(pool, ivar_getTypeEncoding(ivar));
1734 // XXX: if this fails and throws an exception the person we are throwing it to gets the wrong exception
1735 return CYFromFFI(context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar));
1741 static bool Internal_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
1742 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1745 id self(internal->GetValue());
1746 const char *name(CYPoolCString(pool, context, property));
1748 if (objc_ivar *ivar = object_getInstanceVariable(self, name, NULL)) {
1749 Type_privateData type(pool, ivar_getTypeEncoding(ivar));
1750 CYPoolFFI(pool, context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar), value);
1757 static void Internal_getPropertyNames_(Class _class, JSPropertyNameAccumulatorRef names) {
1758 if (Class super = class_getSuperclass(_class))
1759 Internal_getPropertyNames_(super, names);
1761 #if OBJC_API_VERSION >= 2
1763 objc_ivar **data(class_copyIvarList(_class, &size));
1764 for (size_t i(0); i != size; ++i)
1765 JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(data[i])));
1768 if (objc_ivar_list *ivars = _class->ivars)
1769 for (int i(0); i != ivars->ivar_count; ++i)
1770 JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(&ivars->ivar_list[i])));
1774 static void Internal_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1775 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1778 id self(internal->GetValue());
1779 Class _class(object_getClass(self));
1781 Internal_getPropertyNames_(_class, names);
1784 static JSValueRef Internal_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
1785 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1786 return internal->GetOwner();
1789 static JSValueRef ObjectiveC_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1791 NSString *name(CYCastNSString(pool, context, property));
1792 if (Class _class = NSClassFromString(name))
1793 return CYMakeInstance(context, _class, true);
1797 static void ObjectiveC_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1799 size_t size(objc_getClassList(NULL, 0));
1800 Class *data(reinterpret_cast<Class *>(malloc(sizeof(Class) * size)));
1803 size_t writ(objc_getClassList(data, size));
1806 if (Class *copy = reinterpret_cast<Class *>(realloc(data, sizeof(Class) * writ))) {
1812 for (size_t i(0); i != writ; ++i)
1813 JSPropertyNameAccumulatorAddName(names, CYJSString(class_getName(data[i])));
1819 while (Class _class = objc_next_class(&state))
1820 JSPropertyNameAccumulatorAddName(names, CYJSString(class_getName(_class)));
1824 #if OBJC_API_VERSION >= 2
1825 static JSValueRef ObjectiveC_Image_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1826 const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object)));
1829 const char *name(CYPoolCString(pool, context, property));
1831 const char **data(objc_copyClassNamesForImage(internal, &size));
1833 for (size_t i(0); i != size; ++i)
1834 if (strcmp(name, data[i]) == 0) {
1835 if (Class _class = objc_getClass(name)) {
1836 value = CYMakeInstance(context, _class, true);
1847 static void ObjectiveC_Image_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1848 const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object)));
1850 const char **data(objc_copyClassNamesForImage(internal, &size));
1851 for (size_t i(0); i != size; ++i)
1852 JSPropertyNameAccumulatorAddName(names, CYJSString(data[i]));
1856 static JSValueRef ObjectiveC_Images_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1858 const char *name(CYPoolCString(pool, context, property));
1860 const char **data(objc_copyImageNames(&size));
1861 for (size_t i(0); i != size; ++i)
1862 if (strcmp(name, data[i]) == 0) {
1871 JSObjectRef value(JSObjectMake(context, NULL, NULL));
1872 CYSetProperty(context, value, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Image_Classes_, const_cast<char *>(name)));
1876 static void ObjectiveC_Images_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1878 const char **data(objc_copyImageNames(&size));
1879 for (size_t i(0); i != size; ++i)
1880 JSPropertyNameAccumulatorAddName(names, CYJSString(data[i]));
1885 static JSValueRef ObjectiveC_Protocols_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1887 const char *name(CYPoolCString(pool, context, property));
1888 if (Protocol *protocol = objc_getProtocol(name))
1889 return CYMakeInstance(context, protocol, true);
1893 static void ObjectiveC_Protocols_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1894 #if OBJC_API_VERSION >= 2
1896 Protocol **data(objc_copyProtocolList(&size));
1897 for (size_t i(0); i != size; ++i)
1898 JSPropertyNameAccumulatorAddName(names, CYJSString(protocol_getName(data[i])));
1906 static bool stret(ffi_type *ffi_type) {
1907 return ffi_type->type == FFI_TYPE_STRUCT && (
1908 ffi_type->size > OBJC_MAX_STRUCT_BY_VALUE ||
1909 struct_forward_array[ffi_type->size] != 0
1914 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 {
1918 _class = object_getClass(self);
1922 if (objc_method *method = class_getInstanceMethod(_class, _cmd)) {
1923 imp = method_getImplementation(method);
1924 type = method_getTypeEncoding(method);
1929 NSMethodSignature *method([self methodSignatureForSelector:_cmd]);
1931 throw CYJSError(context, "unrecognized selector %s sent to object %p", sel_getName(_cmd), self);
1932 type = CYPoolCString(pool, context, [method _typeString]);
1940 sig::Signature signature;
1941 sig::Parse(pool, &signature, type, &Structor_);
1944 sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif);
1948 if (stret(cif.rtype))
1949 imp = class_getMethodImplementation_stret(_class, _cmd);
1951 imp = class_getMethodImplementation(_class, _cmd);
1953 objc_super super = {self, _class};
1954 imp = objc_msg_lookup_super(&super, _cmd);
1958 void (*function)() = reinterpret_cast<void (*)()>(imp);
1959 return CYCallFunction(pool, context, 2, setup, count, arguments, initialize, exception, &signature, &cif, function);
1962 static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1964 throw CYJSError(context, "too few arguments to objc_msgSend");
1974 if (JSValueIsObjectOfClass(context, arguments[0], Super_)) {
1975 cy::Super *internal(reinterpret_cast<cy::Super *>(JSObjectGetPrivate((JSObjectRef) arguments[0])));
1976 self = internal->GetValue();
1977 _class = internal->class_;;
1978 uninitialized = false;
1979 } else if (JSValueIsObjectOfClass(context, arguments[0], Instance_)) {
1980 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) arguments[0])));
1981 self = internal->GetValue();
1983 uninitialized = internal->IsUninitialized();
1985 internal->value_ = nil;
1987 self = CYCastNSObject(pool, context, arguments[0]);
1989 uninitialized = false;
1993 return CYJSNull(context);
1995 _cmd = CYCastSEL(context, arguments[1]);
1997 return CYSendMessage(pool, context, self, _class, _cmd, count - 2, arguments + 2, uninitialized, exception);
2000 /* Hook: objc_registerClassPair {{{ */
2001 #if defined(__APPLE__) && defined(__arm__)
2002 // XXX: replace this with associated objects
2004 MSHook(void, CYDealloc, id self, SEL sel) {
2005 CYInternal *internal;
2006 object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal));
2007 if (internal != NULL)
2009 _CYDealloc(self, sel);
2012 MSHook(void, objc_registerClassPair, Class _class) {
2013 Class super(class_getSuperclass(_class));
2014 if (super == NULL || class_getInstanceVariable(super, "cy$internal_") == NULL) {
2015 class_addIvar(_class, "cy$internal_", sizeof(CYInternal *), log2(sizeof(CYInternal *)), "^{CYInternal}");
2016 MSHookMessage(_class, @selector(dealloc), MSHake(CYDealloc));
2019 _objc_registerClassPair(_class);
2022 static JSValueRef objc_registerClassPair_(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2024 throw CYJSError(context, "incorrect number of arguments to objc_registerClassPair");
2026 NSObject *value(CYCastNSObject(pool, context, arguments[0]));
2027 if (value == NULL || !CYIsClass(value))
2028 throw CYJSError(context, "incorrect number of arguments to objc_registerClassPair");
2029 Class _class((Class) value);
2030 $objc_registerClassPair(_class);
2031 return CYJSUndefined(context);
2036 static JSValueRef Selector_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2037 JSValueRef setup[count + 2];
2040 memcpy(setup + 2, arguments, sizeof(JSValueRef) * count);
2041 return $objc_msgSend(context, NULL, NULL, count + 2, setup, exception);
2044 static JSValueRef Message_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2046 Message_privateData *internal(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate(object)));
2048 // XXX: handle Instance::Uninitialized?
2049 id self(CYCastNSObject(pool, context, _this));
2053 setup[1] = &internal->sel_;
2055 return CYCallFunction(pool, context, 2, setup, count, arguments, false, exception, &internal->signature_, &internal->cif_, internal->GetValue());
2058 static JSObjectRef Super_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2060 throw CYJSError(context, "incorrect number of arguments to Super constructor");
2062 id self(CYCastNSObject(pool, context, arguments[0]));
2063 Class _class(CYCastClass(pool, context, arguments[1]));
2064 return cy::Super::Make(context, self, _class);
2067 static JSObjectRef Selector_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2069 throw CYJSError(context, "incorrect number of arguments to Selector constructor");
2071 const char *name(CYPoolCString(pool, context, arguments[0]));
2072 return CYMakeSelector(context, sel_registerName(name));
2075 static JSObjectRef Instance_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2077 throw CYJSError(context, "incorrect number of arguments to Instance constructor");
2078 id self(count == 0 ? nil : CYCastPointer<id>(context, arguments[0]));
2079 return Instance::Make(context, self);
2082 static JSValueRef CYValue_getProperty_value(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
2083 CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(object)));
2084 return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_));
2087 static JSValueRef CYValue_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2088 CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this)));
2089 Type_privateData *typical(internal->GetType());
2094 if (typical == NULL) {
2098 type = typical->type_;
2099 ffi = typical->ffi_;
2102 return CYMakePointer(context, &internal->value_, type, ffi, object);
2105 static JSValueRef Instance_getProperty_constructor(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
2106 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
2107 return Instance::Make(context, (id) object_getClass(internal->GetValue()));
2110 static JSValueRef Instance_getProperty_protocol(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2111 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
2112 id self(internal->GetValue());
2113 if (!CYIsClass(self))
2114 return CYJSUndefined(context);
2115 return CYGetClassPrototype(context, self);
2118 static JSValueRef Instance_getProperty_messages(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
2119 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
2120 id self(internal->GetValue());
2121 if (!CYIsClass(self))
2122 return CYJSUndefined(context);
2123 return Messages::Make(context, (Class) self);
2126 static JSValueRef Instance_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2127 if (!JSValueIsObjectOfClass(context, _this, Instance_))
2130 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2131 return CYCastJSValue(context, CYJSString(context, CYCastNSCYON(internal->GetValue())));
2134 static JSValueRef Instance_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2135 if (!JSValueIsObjectOfClass(context, _this, Instance_))
2138 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2145 key = CYCastNSString(NULL, context, CYJSString(context, arguments[0]));
2146 // XXX: check for support of cy$toJSON?
2147 return CYCastJSValue(context, CYJSString(context, [internal->GetValue() cy$toJSON:key]));
2149 } CYCatch return /*XXX*/ NULL; }
2151 static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2152 if (!JSValueIsObjectOfClass(context, _this, Instance_))
2155 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2158 // XXX: this seems like a stupid implementation; what if it crashes? why not use the CYONifier backend?
2159 return CYCastJSValue(context, CYJSString(context, [internal->GetValue() description]));
2161 } CYCatch return /*XXX*/ NULL; }
2163 static JSValueRef Selector_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2164 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
2165 return CYCastJSValue(context, sel_getName(internal->GetValue()));
2168 static JSValueRef Selector_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2169 return Selector_callAsFunction_toString(context, object, _this, count, arguments, exception);
2172 static JSValueRef Selector_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2173 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
2174 const char *name(sel_getName(internal->GetValue()));
2177 NSString *string([NSString stringWithFormat:@"@selector(%s)", name]);
2178 return CYCastJSValue(context, CYJSString(context, string));
2180 } CYCatch return /*XXX*/ NULL; }
2182 static JSValueRef Selector_callAsFunction_type(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2184 throw CYJSError(context, "incorrect number of arguments to Selector.type");
2187 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
2188 SEL sel(internal->GetValue());
2190 objc_method *method;
2191 if (Class _class = CYCastClass(pool, context, arguments[0]))
2192 method = class_getInstanceMethod(_class, sel);
2196 if (const char *type = CYPoolTypeEncoding(pool, context, sel, method))
2197 return CYCastJSValue(context, CYJSString(type));
2199 return CYJSNull(context);
2202 static JSStaticValue Selector_staticValues[2] = {
2203 {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete},
2204 {NULL, NULL, NULL, 0}
2207 static JSStaticValue Instance_staticValues[5] = {
2208 {"constructor", &Instance_getProperty_constructor, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2209 {"messages", &Instance_getProperty_messages, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2210 {"prototype", &Instance_getProperty_protocol, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2211 {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2212 {NULL, NULL, NULL, 0}
2215 static JSStaticFunction Instance_staticFunctions[5] = {
2216 {"$cya", &CYValue_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2217 {"toCYON", &Instance_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2218 {"toJSON", &Instance_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2219 {"toString", &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2223 static JSStaticFunction Internal_staticFunctions[2] = {
2224 {"$cya", &Internal_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2228 static JSStaticFunction Selector_staticFunctions[5] = {
2229 {"toCYON", &Selector_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2230 {"toJSON", &Selector_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2231 {"toString", &Selector_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2232 {"type", &Selector_callAsFunction_type, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2236 void CYObjectiveC_SetupContext(JSContextRef context) {
2237 JSObjectRef global(CYGetGlobalObject(context));
2238 apr_pool_t *pool(CYGetGlobalPool());
2240 Object_type = new(pool) Type_privateData(pool, "@");
2241 Selector_type = new(pool) Type_privateData(pool, ":");
2244 NSCFBoolean_ = objc_getClass("NSCFBoolean");
2245 NSCFType_ = objc_getClass("NSCFType");
2246 NSMessageBuilder_ = objc_getClass("NSMessageBuilder");
2247 NSZombie_ = objc_getClass("_NSZombie_");
2249 NSBoolNumber_ = objc_getClass("NSBoolNumber");
2252 NSArray_ = objc_getClass("NSArray");
2253 NSDictionary_ = objc_getClass("NSDictionary");
2254 Object_ = objc_getClass("Object");
2256 JSClassDefinition definition;
2258 definition = kJSClassDefinitionEmpty;
2259 definition.className = "Instance";
2260 definition.staticValues = Instance_staticValues;
2261 definition.staticFunctions = Instance_staticFunctions;
2262 definition.hasProperty = &Instance_hasProperty;
2263 definition.getProperty = &Instance_getProperty;
2264 definition.setProperty = &Instance_setProperty;
2265 definition.deleteProperty = &Instance_deleteProperty;
2266 definition.getPropertyNames = &Instance_getPropertyNames;
2267 definition.callAsConstructor = &Instance_callAsConstructor;
2268 definition.hasInstance = &Instance_hasInstance;
2269 definition.finalize = &CYFinalize;
2270 Instance_ = JSClassCreate(&definition);
2272 definition = kJSClassDefinitionEmpty;
2273 definition.className = "Internal";
2274 definition.staticFunctions = Internal_staticFunctions;
2275 definition.hasProperty = &Internal_hasProperty;
2276 definition.getProperty = &Internal_getProperty;
2277 definition.setProperty = &Internal_setProperty;
2278 definition.getPropertyNames = &Internal_getPropertyNames;
2279 definition.finalize = &CYFinalize;
2280 Internal_ = JSClassCreate(&definition);
2282 definition = kJSClassDefinitionEmpty;
2283 definition.className = "Message";
2284 definition.staticFunctions = cy::Functor::StaticFunctions;
2285 definition.callAsFunction = &Message_callAsFunction;
2286 definition.finalize = &CYFinalize;
2287 Message_ = JSClassCreate(&definition);
2289 definition = kJSClassDefinitionEmpty;
2290 definition.className = "Messages";
2291 definition.hasProperty = &Messages_hasProperty;
2292 definition.getProperty = &Messages_getProperty;
2293 definition.setProperty = &Messages_setProperty;
2294 #if 0 && OBJC_API_VERSION < 2
2295 definition.deleteProperty = &Messages_deleteProperty;
2297 definition.getPropertyNames = &Messages_getPropertyNames;
2298 definition.finalize = &CYFinalize;
2299 Messages_ = JSClassCreate(&definition);
2301 definition = kJSClassDefinitionEmpty;
2302 definition.className = "Selector";
2303 definition.staticValues = Selector_staticValues;
2304 definition.staticFunctions = Selector_staticFunctions;
2305 definition.callAsFunction = &Selector_callAsFunction;
2306 definition.finalize = &CYFinalize;
2307 Selector_ = JSClassCreate(&definition);
2309 definition = kJSClassDefinitionEmpty;
2310 definition.className = "Super";
2311 definition.staticFunctions = Internal_staticFunctions;
2312 definition.finalize = &CYFinalize;
2313 Super_ = JSClassCreate(&definition);
2315 definition = kJSClassDefinitionEmpty;
2316 definition.className = "ObjectiveC::Classes";
2317 definition.getProperty = &ObjectiveC_Classes_getProperty;
2318 definition.getPropertyNames = &ObjectiveC_Classes_getPropertyNames;
2319 ObjectiveC_Classes_ = JSClassCreate(&definition);
2321 definition = kJSClassDefinitionEmpty;
2322 definition.className = "ObjectiveC::Protocols";
2323 definition.getProperty = &ObjectiveC_Protocols_getProperty;
2324 definition.getPropertyNames = &ObjectiveC_Protocols_getPropertyNames;
2325 ObjectiveC_Protocols_ = JSClassCreate(&definition);
2327 JSObjectRef ObjectiveC(JSObjectMake(context, NULL, NULL));
2328 CYSetProperty(context, global, CYJSString("ObjectiveC"), ObjectiveC);
2330 CYSetProperty(context, ObjectiveC, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Classes_, NULL));
2331 CYSetProperty(context, ObjectiveC, CYJSString("protocols"), JSObjectMake(context, ObjectiveC_Protocols_, NULL));
2333 #if OBJC_API_VERSION >= 2
2334 definition = kJSClassDefinitionEmpty;
2335 definition.className = "ObjectiveC::Images";
2336 definition.getProperty = &ObjectiveC_Images_getProperty;
2337 definition.getPropertyNames = &ObjectiveC_Images_getPropertyNames;
2338 ObjectiveC_Images_ = JSClassCreate(&definition);
2340 definition = kJSClassDefinitionEmpty;
2341 definition.className = "ObjectiveC::Image::Classes";
2342 definition.getProperty = &ObjectiveC_Image_Classes_getProperty;
2343 definition.getPropertyNames = &ObjectiveC_Image_Classes_getPropertyNames;
2344 ObjectiveC_Image_Classes_ = JSClassCreate(&definition);
2346 CYSetProperty(context, ObjectiveC, CYJSString("images"), JSObjectMake(context, ObjectiveC_Images_, NULL));
2349 JSObjectRef Instance(JSObjectMakeConstructor(context, Instance_, &Instance_new));
2350 JSObjectRef Message(JSObjectMakeConstructor(context, Message_, NULL));
2351 JSObjectRef Selector(JSObjectMakeConstructor(context, Selector_, &Selector_new));
2352 JSObjectRef Super(JSObjectMakeConstructor(context, Super_, &Super_new));
2354 Instance_prototype_ = (JSObjectRef) CYGetProperty(context, Instance, prototype_);
2355 JSValueProtect(context, Instance_prototype_);
2357 CYSetProperty(context, global, CYJSString("Instance"), Instance);
2358 CYSetProperty(context, global, CYJSString("Selector"), Selector);
2359 CYSetProperty(context, global, CYJSString("Super"), Super);
2361 #if defined(__APPLE__) && defined(__arm__)
2362 CYSetProperty(context, global, CYJSString("objc_registerClassPair"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_registerClassPair"), &objc_registerClassPair_));
2363 MSHookFunction(&objc_registerClassPair, MSHake(objc_registerClassPair));
2366 CYSetProperty(context, global, CYJSString("objc_msgSend"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_msgSend"), &$objc_msgSend));
2368 JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Message, prototype_), Function_prototype_);
2369 JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Selector, prototype_), Function_prototype_);
2372 class_addMethod(NSCFType_, @selector(cy$toJSON:), reinterpret_cast<IMP>(&NSCFType$cy$toJSON), "@12@0:4@8");
2376 static CYHooks CYObjectiveCHooks = {
2377 &CYObjectiveC_ExecuteStart,
2378 &CYObjectiveC_ExecuteEnd,
2379 &CYObjectiveC_RuntimeProperty,
2380 &CYObjectiveC_CallFunction,
2381 &CYObjectiveC_SetupContext,
2382 &CYObjectiveC_PoolFFI,
2383 &CYObjectiveC_FromFFI,
2386 struct CYObjectiveC {
2388 hooks_ = &CYObjectiveCHooks;