1 #if defined(__APPLE__) && defined(__arm__)
4 #include <objc/objc-api.h>
11 #include <Foundation/Foundation.h>
13 #include "ObjectiveC/Internal.hpp"
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_;
228 static Class NSCFBoolean_;
229 static Class NSCFType_;
230 static Class NSMessageBuilder_;
231 static Class NSZombie_;
233 static Class NSBoolNumber_;
236 static Class NSArray_;
237 static Class NSDictionary_;
238 static Class Object_;
240 static Type_privateData *Object_type;
241 static Type_privateData *Selector_type;
243 Type_privateData *Instance::GetType() const {
247 Type_privateData *Selector_privateData::GetType() const {
248 return Selector_type;
251 // XXX: trick this out with associated objects!
252 JSValueRef CYGetClassPrototype(JSContextRef context, id self) {
254 return CYGetCachedObject(context, CYJSString("Instance_prototype"));
256 // XXX: I need to think through multi-context
257 typedef std::map<id, JSValueRef> CacheMap;
258 static CacheMap cache_;
260 JSValueRef &value(cache_[self]);
264 JSClassRef _class(NULL);
265 JSValueRef prototype;
267 if (self == NSArray_)
268 prototype = CYGetCachedObject(context, CYJSString("Array_prototype"));
269 else if (self == NSDictionary_)
270 prototype = CYGetCachedObject(context, CYJSString("Object_prototype"));
272 prototype = CYGetClassPrototype(context, class_getSuperclass(self));
274 JSObjectRef object(JSObjectMake(context, _class, NULL));
275 JSObjectSetPrototype(context, object, prototype);
277 JSValueProtect(context, object);
282 JSObjectRef Messages::Make(JSContextRef context, Class _class, bool array) {
283 JSObjectRef value(JSObjectMake(context, Messages_, new Messages(_class)));
284 if (_class == NSArray_)
286 if (Class super = class_getSuperclass(_class))
287 JSObjectSetPrototype(context, value, Messages::Make(context, super, array));
289 JSObjectSetPrototype(context, value, Array_prototype_);*/
293 JSObjectRef Internal::Make(JSContextRef context, id object, JSObjectRef owner) {
294 return JSObjectMake(context, Internal_, new Internal(object, context, owner));
298 JSObjectRef Super::Make(JSContextRef context, id object, Class _class) {
299 JSObjectRef value(JSObjectMake(context, Super_, new Super(object, _class)));
303 JSObjectRef Instance::Make(JSContextRef context, id object, Flags flags) {
304 JSObjectRef value(JSObjectMake(context, Instance_, new Instance(object, flags)));
305 JSObjectSetPrototype(context, value, CYGetClassPrototype(context, object_getClass(object)));
309 Instance::~Instance() {
310 if ((flags_ & Transient) == 0)
311 // XXX: does this handle background threads correctly?
312 // XXX: this simply does not work on the console because I'm stupid
313 [GetValue() performSelector:@selector(release) withObject:nil afterDelay:0];
316 struct Message_privateData :
321 Message_privateData(SEL sel, const char *type, IMP value = NULL) :
322 cy::Functor(type, reinterpret_cast<void (*)()>(value)),
328 JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient) {
329 Instance::Flags flags;
332 flags = Instance::Transient;
334 flags = Instance::None;
335 object = [object retain];
338 return Instance::Make(context, object, flags);
341 @interface NSMethodSignature (Cycript)
342 - (NSString *) _typeString;
345 @interface NSObject (Cycript)
347 - (JSValueRef) cy$JSValueInContext:(JSContextRef)context;
348 - (JSType) cy$JSType;
350 - (NSObject *) cy$toJSON:(NSString *)key;
351 - (NSString *) cy$toCYON;
352 - (NSString *) cy$toKey;
354 - (bool) cy$hasProperty:(NSString *)name;
355 - (NSObject *) cy$getProperty:(NSString *)name;
356 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value;
357 - (bool) cy$deleteProperty:(NSString *)name;
358 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context;
363 - (JSValueRef) cy$JSValueInContext:(JSContextRef)context;
366 NSString *CYCastNSCYON(id value) {
372 Class _class(object_getClass(value));
373 SEL sel(@selector(cy$toCYON));
375 if (objc_method *toCYON = class_getInstanceMethod(_class, sel))
376 string = reinterpret_cast<NSString *(*)(id, SEL)>(method_getImplementation(toCYON))(value, sel);
377 else if (objc_method *methodSignatureForSelector = class_getInstanceMethod(_class, @selector(methodSignatureForSelector:))) {
378 if (reinterpret_cast<NSMethodSignature *(*)(id, SEL, SEL)>(method_getImplementation(methodSignatureForSelector))(value, @selector(methodSignatureForSelector:), sel) != nil)
379 string = [value cy$toCYON];
384 else if (value == NSZombie_)
385 string = @"_NSZombie_";
386 else if (_class == NSZombie_)
387 string = [NSString stringWithFormat:@"<_NSZombie_: %p>", value];
388 // XXX: frowny /in/ the pants
389 else if (value == NSMessageBuilder_ || value == Object_)
393 string = [NSString stringWithFormat:@"%@", value];
398 string = @"undefined";
405 struct PropertyAttributes {
410 const char *variable;
423 PropertyAttributes(objc_property_t property) :
435 name = property_getName(property);
436 const char *attributes(property_getAttributes(property));
438 for (char *state, *token(apr_strtok(apr_pstrdup(pool_, attributes), ",", &state)); token != NULL; token = apr_strtok(NULL, ",", &state)) {
440 case 'R': readonly = true; break;
441 case 'C': copy = true; break;
442 case '&': retain = true; break;
443 case 'N': nonatomic = true; break;
444 case 'G': getter_ = token + 1; break;
445 case 'S': setter_ = token + 1; break;
446 case 'V': variable = token + 1; break;
450 /*if (variable == NULL) {
451 variable = property_getName(property);
452 size_t size(strlen(variable));
453 char *name(new(pool_) char[size + 2]);
455 memcpy(name + 1, variable, size);
456 name[size + 1] = '\0';
461 const char *Getter() {
463 getter_ = apr_pstrdup(pool_, name);
467 const char *Setter() {
468 if (setter_ == NULL && !readonly) {
469 size_t length(strlen(name));
471 char *temp(new(pool_) char[length + 5]);
477 temp[3] = toupper(name[0]);
478 memcpy(temp + 4, name + 1, length - 1);
481 temp[length + 3] = ':';
482 temp[length + 4] = '\0';
493 NSObject *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) {
494 return [(NSString *) CFCopyDescription((CFTypeRef) self) autorelease];
499 @interface CYWebUndefined : NSObject {
502 + (CYWebUndefined *) undefined;
506 @implementation CYWebUndefined
508 + (CYWebUndefined *) undefined {
509 static CYWebUndefined *instance_([[CYWebUndefined alloc] init]);
515 #define WebUndefined CYWebUndefined
518 /* Bridge: CYJSObject {{{ */
519 @interface CYJSObject : NSMutableDictionary {
521 JSContextRef context_;
524 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context;
526 - (NSObject *) cy$toJSON:(NSString *)key;
528 - (NSUInteger) count;
529 - (id) objectForKey:(id)key;
530 - (NSEnumerator *) keyEnumerator;
531 - (void) setObject:(id)object forKey:(id)key;
532 - (void) removeObjectForKey:(id)key;
536 /* Bridge: CYJSArray {{{ */
537 @interface CYJSArray : NSMutableArray {
539 JSContextRef context_;
542 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context;
544 - (NSUInteger) count;
545 - (id) objectAtIndex:(NSUInteger)index;
547 - (void) addObject:(id)anObject;
548 - (void) insertObject:(id)anObject atIndex:(NSUInteger)index;
549 - (void) removeLastObject;
550 - (void) removeObjectAtIndex:(NSUInteger)index;
551 - (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
556 NSObject *CYCastNSObject_(apr_pool_t *pool, JSContextRef context, JSObjectRef object) {
557 JSObjectRef Array(CYGetCachedObject(context, Array_s));
558 JSValueRef exception(NULL);
559 bool array(JSValueIsInstanceOfConstructor(context, object, Array, &exception));
560 CYThrow(context, exception);
561 id value(array ? [CYJSArray alloc] : [CYJSObject alloc]);
562 return CYPoolRelease(pool, [value initWithJSObject:object inContext:context]);
565 NSObject *CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSObjectRef object) {
566 if (!JSValueIsObjectOfClass(context, object, Instance_))
567 return CYCastNSObject_(pool, context, object);
569 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
570 return internal->GetValue();
574 NSNumber *CYCopyNSNumber(JSContextRef context, JSValueRef value) {
575 return [[NSNumber alloc] initWithDouble:CYCastDouble(context, value)];
579 @interface NSBoolNumber : NSNumber {
584 id CYNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value, bool cast) {
588 switch (JSType type = JSValueGetType(context, value)) {
589 case kJSTypeUndefined:
590 object = [WebUndefined undefined];
600 object = (id) (CYCastBool(context, value) ? kCFBooleanTrue : kCFBooleanFalse);
603 object = [[NSBoolNumber alloc] initWithBool:CYCastBool(context, value)];
609 object = CYCopyNSNumber(context, value);
614 object = CYCopyNSString(context, value);
619 // XXX: this might could be more efficient
620 object = CYCastNSObject(pool, context, (JSObjectRef) value);
625 throw CYJSError(context, "JSValueGetType() == 0x%x", type);
632 return CYPoolRelease(pool, object);
634 return [object retain];
637 NSObject *CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
638 return CYNSObject(pool, context, value, true);
641 NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
642 return CYNSObject(pool, context, value, false);
645 /* Bridge: NSArray {{{ */
646 @implementation NSArray (Cycript)
648 - (NSString *) cy$toCYON {
649 NSMutableString *json([[[NSMutableString alloc] init] autorelease]);
650 [json appendString:@"["];
654 for (id object in self) {
656 for (size_t index(0), count([self count]); index != count; ++index) {
657 id object([self objectAtIndex:index]);
660 [json appendString:@","];
663 if (object == nil || [object cy$JSType] != kJSTypeUndefined)
664 [json appendString:CYCastNSCYON(object)];
666 [json appendString:@","];
671 [json appendString:@"]"];
675 - (bool) cy$hasProperty:(NSString *)name {
676 if ([name isEqualToString:@"length"])
679 size_t index(CYGetIndex(name));
680 if (index == _not(size_t) || index >= [self count])
681 return [super cy$hasProperty:name];
686 - (NSObject *) cy$getProperty:(NSString *)name {
687 if ([name isEqualToString:@"length"]) {
688 NSUInteger count([self count]);
690 return [NSNumber numberWithUnsignedInteger:count];
692 return [NSNumber numberWithUnsignedInt:count];
696 size_t index(CYGetIndex(name));
697 if (index == _not(size_t) || index >= [self count])
698 return [super cy$getProperty:name];
700 return [self objectAtIndex:index];
703 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context {
704 [super cy$getPropertyNames:names inContext:context];
706 for (size_t index(0), count([self count]); index != count; ++index) {
707 id object([self objectAtIndex:index]);
708 if (object == nil || [object cy$JSType] != kJSTypeUndefined) {
710 sprintf(name, "%zu", index);
711 JSPropertyNameAccumulatorAddName(names, CYJSString(name));
718 /* Bridge: NSBoolNumber {{{ */
720 @implementation NSBoolNumber (Cycript)
722 - (JSType) cy$JSType {
723 return kJSTypeBoolean;
726 - (NSObject *) cy$toJSON:(NSString *)key {
730 - (NSString *) cy$toCYON {
731 return [self boolValue] ? @"true" : @"false";
734 - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) {
735 return CYCastJSValue(context, (bool) [self boolValue]);
741 /* Bridge: NSDictionary {{{ */
742 @implementation NSDictionary (Cycript)
744 - (NSString *) cy$toCYON {
745 NSMutableString *json([[[NSMutableString alloc] init] autorelease]);
746 [json appendString:@"{"];
750 for (NSObject *key in self) {
752 NSEnumerator *keys([self keyEnumerator]);
753 while (NSObject *key = [keys nextObject]) {
756 [json appendString:@","];
759 [json appendString:[key cy$toKey]];
760 [json appendString:@":"];
761 NSObject *object([self objectForKey:key]);
762 [json appendString:CYCastNSCYON(object)];
765 [json appendString:@"}"];
769 - (bool) cy$hasProperty:(NSString *)name {
770 return [self objectForKey:name] != nil;
773 - (NSObject *) cy$getProperty:(NSString *)name {
774 return [self objectForKey:name];
777 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context {
778 [super cy$getPropertyNames:names inContext:context];
781 for (NSObject *key in self) {
783 NSEnumerator *keys([self keyEnumerator]);
784 while (NSObject *key = [keys nextObject]) {
786 JSPropertyNameAccumulatorAddName(names, CYJSString(context, key));
792 /* Bridge: NSMutableArray {{{ */
793 @implementation NSMutableArray (Cycript)
795 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
796 if ([name isEqualToString:@"length"]) {
797 // XXX: is this not intelligent?
798 NSNumber *number(reinterpret_cast<NSNumber *>(value));
800 NSUInteger size([number unsignedIntegerValue]);
802 NSUInteger size([number unsignedIntValue]);
804 NSUInteger count([self count]);
806 [self removeObjectsInRange:NSMakeRange(size, count - size)];
807 else if (size != count) {
808 WebUndefined *undefined([WebUndefined undefined]);
809 for (size_t i(count); i != size; ++i)
810 [self addObject:undefined];
815 size_t index(CYGetIndex(name));
816 if (index == _not(size_t))
817 return [super cy$setProperty:name to:value];
819 id object(value ?: [NSNull null]);
821 size_t count([self count]);
823 [self replaceObjectAtIndex:index withObject:object];
825 if (index != count) {
826 WebUndefined *undefined([WebUndefined undefined]);
827 for (size_t i(count); i != index; ++i)
828 [self addObject:undefined];
831 [self addObject:object];
837 - (bool) cy$deleteProperty:(NSString *)name {
838 size_t index(CYGetIndex(name));
839 if (index == _not(size_t) || index >= [self count])
840 return [super cy$deleteProperty:name];
841 [self replaceObjectAtIndex:index withObject:[WebUndefined undefined]];
847 /* Bridge: NSMutableDictionary {{{ */
848 @implementation NSMutableDictionary (Cycript)
850 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
851 [self setObject:(value ?: [NSNull null]) forKey:name];
855 - (bool) cy$deleteProperty:(NSString *)name {
856 if ([self objectForKey:name] == nil)
859 [self removeObjectForKey:name];
866 /* Bridge: NSNumber {{{ */
867 @implementation NSNumber (Cycript)
869 - (JSType) cy$JSType {
871 // XXX: this just seems stupid
872 if ([self class] == NSCFBoolean_)
873 return kJSTypeBoolean;
875 return kJSTypeNumber;
878 - (NSObject *) cy$toJSON:(NSString *)key {
882 - (NSString *) cy$toCYON {
883 return [self cy$JSType] != kJSTypeBoolean ? [self stringValue] : [self boolValue] ? @"true" : @"false";
886 - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) {
887 return [self cy$JSType] != kJSTypeBoolean ? CYCastJSValue(context, [self doubleValue]) : CYCastJSValue(context, [self boolValue]);
892 /* Bridge: NSNull {{{ */
893 @implementation NSNull (Cycript)
895 - (JSType) cy$JSType {
899 - (NSObject *) cy$toJSON:(NSString *)key {
903 - (NSString *) cy$toCYON {
909 /* Bridge: NSObject {{{ */
910 @implementation NSObject (Cycript)
912 - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) {
913 return CYMakeInstance(context, self, false);
916 - (JSType) cy$JSType {
917 return kJSTypeObject;
920 - (NSObject *) cy$toJSON:(NSString *)key {
921 return [self description];
924 - (NSString *) cy$toCYON {
925 return [[self cy$toJSON:@""] cy$toCYON];
928 - (NSString *) cy$toKey {
929 return [self cy$toCYON];
932 - (bool) cy$hasProperty:(NSString *)name {
936 - (NSObject *) cy$getProperty:(NSString *)name {
940 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
944 - (bool) cy$deleteProperty:(NSString *)name {
948 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context {
953 /* Bridge: NSProxy {{{ */
954 @implementation NSProxy (Cycript)
956 - (NSObject *) cy$toJSON:(NSString *)key {
957 return [self description];
960 - (NSString *) cy$toCYON {
961 return [[self cy$toJSON:@""] cy$toCYON];
966 /* Bridge: NSString {{{ */
967 @implementation NSString (Cycript)
969 - (JSType) cy$JSType {
970 return kJSTypeString;
973 - (NSObject *) cy$toJSON:(NSString *)key {
977 - (NSString *) cy$toCYON {
978 std::ostringstream str;
979 CYUTF8String string(CYCastUTF8String(self));
980 CYStringify(str, string.data, string.size);
981 std::string value(str.str());
982 return CYCastNSString(NULL, CYUTF8String(value.c_str(), value.size()));
985 - (NSString *) cy$toKey {
986 if (CYIsKey(CYCastUTF8String(self)))
988 return [self cy$toCYON];
993 /* Bridge: WebUndefined {{{ */
994 @implementation WebUndefined (Cycript)
996 - (JSType) cy$JSType {
997 return kJSTypeUndefined;
1000 - (NSObject *) cy$toJSON:(NSString *)key {
1004 - (NSString *) cy$toCYON {
1005 return @"undefined";
1008 - (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) {
1009 return CYJSUndefined(context);
1010 } CYObjectiveCatch }
1015 static bool CYIsClass(id self) {
1017 // XXX: this is a lame object_isClass
1018 return class_getInstanceMethod(object_getClass(self), @selector(alloc)) != NULL;
1020 return GSObjCIsClass(self);
1024 Class CYCastClass(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
1025 id self(CYCastNSObject(pool, context, value));
1026 if (CYIsClass(self))
1027 return (Class) self;
1028 throw CYJSError(context, "got something that is not a Class");
1032 NSArray *CYCastNSArray(JSContextRef context, JSPropertyNameArrayRef names) {
1034 size_t size(JSPropertyNameArrayGetCount(names));
1035 NSMutableArray *array([NSMutableArray arrayWithCapacity:size]);
1036 for (size_t index(0); index != size; ++index)
1037 [array addObject:CYCastNSString(pool, context, JSPropertyNameArrayGetNameAtIndex(names, index))];
1041 JSValueRef CYCastJSValue(JSContextRef context, NSObject *value) { CYPoolTry {
1043 return CYJSNull(context);
1044 else if ([value respondsToSelector:@selector(cy$JSValueInContext:)])
1045 return [value cy$JSValueInContext:context];
1047 return CYMakeInstance(context, value, false);
1048 } CYPoolCatch(NULL) return /*XXX*/ NULL; }
1050 @implementation CYJSObject
1052 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { CYObjectiveTry {
1053 if ((self = [super init]) != nil) {
1055 context_ = CYGetJSContext(context);
1056 //XXX:JSGlobalContextRetain(context_);
1057 JSValueProtect(context_, object_);
1059 } CYObjectiveCatch }
1061 - (void) dealloc { CYObjectiveTry {
1062 JSValueUnprotect(context_, object_);
1063 //XXX:JSGlobalContextRelease(context_);
1065 } CYObjectiveCatch }
1067 - (NSObject *) cy$toJSON:(NSString *)key { CYObjectiveTry {
1068 JSValueRef toJSON(CYGetProperty(context_, object_, toJSON_s));
1069 if (!CYIsCallable(context_, toJSON))
1070 return [super cy$toJSON:key];
1072 JSValueRef arguments[1] = {CYCastJSValue(context_, key)};
1073 JSValueRef value(CYCallAsFunction(context_, (JSObjectRef) toJSON, object_, 1, arguments));
1074 // XXX: do I really want an NSNull here?!
1075 return CYCastNSObject(NULL, context_, value) ?: [NSNull null];
1077 } CYObjectiveCatch }
1079 - (NSString *) cy$toCYON { CYObjectiveTry {
1081 JSValueRef exception(NULL);
1082 const char *cyon(CYPoolCCYON(pool, context_, object_));
1083 CYThrow(context_, exception);
1085 return [super cy$toCYON];
1087 return [NSString stringWithUTF8String:cyon];
1088 } CYObjectiveCatch }
1090 - (NSUInteger) count { CYObjectiveTry {
1091 JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_));
1092 size_t size(JSPropertyNameArrayGetCount(names));
1093 JSPropertyNameArrayRelease(names);
1095 } CYObjectiveCatch }
1097 - (id) objectForKey:(id)key { CYObjectiveTry {
1098 JSValueRef value(CYGetProperty(context_, object_, CYJSString(context_, (NSObject *) 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 CYSetProperty(context_, object_, CYJSString(context_, (NSObject *) key), CYCastJSValue(context_, (NSString *) object));
1113 } CYObjectiveCatch }
1115 - (void) removeObjectForKey:(id)key { CYObjectiveTry {
1116 JSValueRef exception(NULL);
1117 (void) JSObjectDeleteProperty(context_, object_, CYJSString(context_, (NSObject *) key), &exception);
1118 CYThrow(context_, exception);
1119 } CYObjectiveCatch }
1123 @implementation CYJSArray
1125 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { CYObjectiveTry {
1126 if ((self = [super init]) != nil) {
1128 context_ = CYGetJSContext(context);
1129 //XXX:JSGlobalContextRetain(context_);
1130 JSValueProtect(context_, object_);
1132 } CYObjectiveCatch }
1134 - (void) dealloc { CYObjectiveTry {
1135 JSValueUnprotect(context_, object_);
1136 //XXX:JSGlobalContextRelease(context_);
1138 } CYObjectiveCatch }
1140 - (NSUInteger) count { CYObjectiveTry {
1141 return CYCastDouble(context_, CYGetProperty(context_, object_, length_s));
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 JSObjectRef Array(CYGetCachedObject(context_, Array_s));
1159 JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array, push_s)), object_, 1, arguments, &exception);
1160 CYThrow(context_, exception);
1161 } CYObjectiveCatch }
1163 - (void) insertObject:(id)object atIndex:(NSUInteger)index { CYObjectiveTry {
1164 size_t bounds([self count] + 1);
1165 if (index >= bounds)
1166 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray insertObject:atIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
1167 JSValueRef exception(NULL);
1168 JSValueRef arguments[3];
1169 arguments[0] = CYCastJSValue(context_, index);
1170 arguments[1] = CYCastJSValue(context_, 0);
1171 arguments[2] = CYCastJSValue(context_, (NSObject *) object);
1172 JSObjectRef Array(CYGetCachedObject(context_, Array_s));
1173 JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array, splice_s)), object_, 3, arguments, &exception);
1174 CYThrow(context_, exception);
1175 } CYObjectiveCatch }
1177 - (void) removeLastObject { CYObjectiveTry {
1178 JSValueRef exception(NULL);
1179 JSObjectRef Array(CYGetCachedObject(context_, Array_s));
1180 JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array, pop_s)), object_, 0, NULL, &exception);
1181 CYThrow(context_, exception);
1182 } CYObjectiveCatch }
1184 - (void) removeObjectAtIndex:(NSUInteger)index { CYObjectiveTry {
1185 size_t bounds([self count]);
1186 if (index >= bounds)
1187 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray removeObjectAtIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
1188 JSValueRef exception(NULL);
1189 JSValueRef arguments[2];
1190 arguments[0] = CYCastJSValue(context_, index);
1191 arguments[1] = CYCastJSValue(context_, 1);
1192 JSObjectRef Array(CYGetCachedObject(context_, Array_s));
1193 JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array, splice_s)), object_, 2, arguments, &exception);
1194 CYThrow(context_, exception);
1195 } CYObjectiveCatch }
1197 - (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)object { CYObjectiveTry {
1198 size_t bounds([self count]);
1199 if (index >= bounds)
1200 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray replaceObjectAtIndex:withObject:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
1201 CYSetProperty(context_, object_, index, CYCastJSValue(context_, (NSObject *) object));
1202 } CYObjectiveCatch }
1206 // XXX: use objc_getAssociatedObject and objc_setAssociatedObject on 10.6
1210 JSObjectRef object_;
1218 // XXX: delete object_? ;(
1221 static CYInternal *Get(id self) {
1222 CYInternal *internal(NULL);
1223 if (object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal)) == NULL) {
1224 // XXX: do something epic? ;P
1230 static CYInternal *Set(id self) {
1231 CYInternal *internal(NULL);
1232 if (objc_ivar *ivar = object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal))) {
1233 if (internal == NULL) {
1234 internal = new CYInternal();
1235 object_setIvar(self, ivar, reinterpret_cast<id>(internal));
1238 // XXX: do something epic? ;P
1244 bool HasProperty(JSContextRef context, JSStringRef name) {
1245 if (object_ == NULL)
1247 return JSObjectHasProperty(context, object_, name);
1250 JSValueRef GetProperty(JSContextRef context, JSStringRef name) {
1251 if (object_ == NULL)
1253 return CYGetProperty(context, object_, name);
1256 void SetProperty(JSContextRef context, JSStringRef name, JSValueRef value) {
1257 if (object_ == NULL)
1258 object_ = JSObjectMake(context, NULL, NULL);
1259 CYSetProperty(context, object_, name, value);
1263 static JSObjectRef CYMakeSelector(JSContextRef context, SEL sel) {
1264 Selector_privateData *internal(new Selector_privateData(sel));
1265 return JSObjectMake(context, Selector_, internal);
1268 static SEL CYCastSEL(JSContextRef context, JSValueRef value) {
1269 if (JSValueIsObjectOfClass(context, value, Selector_)) {
1270 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate((JSObjectRef) value)));
1271 return reinterpret_cast<SEL>(internal->value_);
1273 return CYCastPointer<SEL>(context, value);
1276 void *CYObjectiveC_ExecuteStart(JSContextRef context) {
1277 // XXX: deal with exceptions!
1278 return (void *) [[NSAutoreleasePool alloc] init];
1281 void CYObjectiveC_ExecuteEnd(JSContextRef context, void *handle) {
1282 // XXX: deal with exceptions!
1283 return [(NSAutoreleasePool *) handle release];
1286 JSValueRef CYObjectiveC_RuntimeProperty(JSContextRef context, CYUTF8String name) { CYPoolTry {
1288 return Instance::Make(context, nil);
1289 if (Class _class = objc_getClass(name.data))
1290 return CYMakeInstance(context, _class, true);
1292 } CYPoolCatch(NULL) return /*XXX*/ NULL; }
1294 static void CYObjectiveC_CallFunction(JSContextRef context, ffi_cif *cif, void (*function)(), uint8_t *value, void **values) { @try {
1295 ffi_call(cif, function, value, values);
1296 } @catch (NSException *error ) {
1297 throw CYJSError(context, CYCastJSValue(context, error));
1300 static bool CYObjectiveC_PoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { @try {
1301 switch (type->primitive) {
1303 case sig::typename_P:
1304 *reinterpret_cast<id *>(data) = CYCastNSObject(pool, context, value);
1307 case sig::selector_P:
1308 *reinterpret_cast<SEL *>(data) = CYCastSEL(context, value);
1316 } @catch (NSException *error ) {
1317 throw CYJSError(context, CYCastJSValue(context, error));
1320 static JSValueRef CYObjectiveC_FromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) { CYPoolTry {
1321 switch (type->primitive) {
1323 if (NSObject *object = *reinterpret_cast<NSObject **>(data)) {
1324 JSValueRef value(CYCastJSValue(context, object));
1330 case sig::typename_P:
1331 return CYMakeInstance(context, *reinterpret_cast<Class *>(data), true);
1333 case sig::selector_P:
1334 if (SEL sel = *reinterpret_cast<SEL *>(data))
1335 return CYMakeSelector(context, sel);
1339 return CYJSNull(context);
1343 } CYPoolCatch(NULL) return /*XXX*/ NULL; }
1345 static bool CYImplements(id object, Class _class, SEL selector, bool devoid) {
1346 if (objc_method *method = class_getInstanceMethod(_class, selector)) {
1349 #if OBJC_API_VERSION >= 2
1351 method_getReturnType(method, type, sizeof(type));
1353 const char *type(method_getTypeEncoding(method));
1359 // XXX: possibly use a more "awesome" check?
1363 static const char *CYPoolTypeEncoding(apr_pool_t *pool, JSContextRef context, SEL sel, objc_method *method) {
1365 return method_getTypeEncoding(method);
1367 const char *name(sel_getName(sel));
1369 sqlite3_stmt *statement;
1371 _sqlcall(sqlite3_prepare(Bridge_,
1373 "\"bridge\".\"value\" "
1376 " \"bridge\".\"mode\" = -1 and"
1377 " \"bridge\".\"name\" = ?"
1379 , -1, &statement, NULL));
1382 _sqlcall(sqlite3_bind_text(statement, 1, name, -1, SQLITE_STATIC));
1385 if (_sqlcall(sqlite3_step(statement)) == SQLITE_DONE) {
1390 value = sqlite3_column_pooled(pool, statement, 0);
1393 _sqlcall(sqlite3_finalize(statement));
1401 static void MessageClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) {
1402 Closure_privateData *internal(reinterpret_cast<Closure_privateData *>(arg));
1404 JSContextRef context(internal->context_);
1406 size_t count(internal->cif_.nargs);
1407 JSValueRef values[count];
1409 for (size_t index(0); index != count; ++index)
1410 values[index] = CYFromFFI(context, internal->signature_.elements[1 + index].type, internal->cif_.arg_types[index], arguments[index]);
1412 JSObjectRef _this(CYCastJSObject(context, values[0]));
1414 JSValueRef value(CYCallAsFunction(context, internal->function_, _this, count - 2, values + 2));
1415 CYPoolFFI(NULL, context, internal->signature_.elements[0].type, internal->cif_.rtype, result, value);
1418 static JSObjectRef CYMakeMessage(JSContextRef context, SEL sel, IMP imp, const char *type) {
1419 Message_privateData *internal(new Message_privateData(sel, type, imp));
1420 return JSObjectMake(context, Message_, internal);
1423 static IMP CYMakeMessage(JSContextRef context, JSValueRef value, const char *type) {
1424 JSObjectRef function(CYCastJSObject(context, value));
1425 Closure_privateData *internal(CYMakeFunctor_(context, function, type, &MessageClosure_));
1426 // XXX: see notes in Library.cpp about needing to leak
1427 return reinterpret_cast<IMP>(internal->GetValue());
1430 static bool Messages_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
1431 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1432 Class _class(internal->GetValue());
1435 const char *name(CYPoolCString(pool, context, property));
1437 if (SEL sel = sel_getUid(name))
1438 if (class_getInstanceMethod(_class, sel) != NULL)
1444 static JSValueRef Messages_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
1445 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1446 Class _class(internal->GetValue());
1449 const char *name(CYPoolCString(pool, context, property));
1451 if (SEL sel = sel_getUid(name))
1452 if (objc_method *method = class_getInstanceMethod(_class, sel))
1453 return CYMakeMessage(context, sel, method_getImplementation(method), method_getTypeEncoding(method));
1458 static bool Messages_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
1459 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1460 Class _class(internal->GetValue());
1463 const char *name(CYPoolCString(pool, context, property));
1465 SEL sel(sel_registerName(name));
1467 objc_method *method(class_getInstanceMethod(_class, sel));
1472 if (JSValueIsObjectOfClass(context, value, Message_)) {
1473 Message_privateData *message(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate((JSObjectRef) value)));
1474 type = sig::Unparse(pool, &message->signature_);
1475 imp = reinterpret_cast<IMP>(message->GetValue());
1477 type = CYPoolTypeEncoding(pool, context, sel, method);
1478 imp = CYMakeMessage(context, value, type);
1482 method_setImplementation(method, imp);
1485 GSMethodList list(GSAllocMethodList(1));
1486 GSAppendMethodToList(list, sel, type, imp, YES);
1487 GSAddMethodList(_class, list, YES);
1488 GSFlushMethodCacheForClass(_class);
1490 class_addMethod(_class, sel, imp, type);
1497 #if 0 && OBJC_API_VERSION < 2
1498 static bool Messages_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
1499 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1500 Class _class(internal->GetValue());
1503 const char *name(CYPoolCString(pool, context, property));
1505 if (SEL sel = sel_getUid(name))
1506 if (objc_method *method = class_getInstanceMethod(_class, sel)) {
1507 objc_method_list list = {NULL, 1, {method}};
1508 class_removeMethods(_class, &list);
1516 static void Messages_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1517 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1518 Class _class(internal->GetValue());
1520 #if OBJC_API_VERSION >= 2
1522 objc_method **data(class_copyMethodList(_class, &size));
1523 for (size_t i(0); i != size; ++i)
1524 JSPropertyNameAccumulatorAddName(names, CYJSString(sel_getName(method_getName(data[i]))));
1527 for (objc_method_list *methods(_class->methods); methods != NULL; methods = methods->method_next)
1528 for (int i(0); i != methods->method_count; ++i)
1529 JSPropertyNameAccumulatorAddName(names, CYJSString(sel_getName(method_getName(&methods->method_list[i]))));
1533 static bool Instance_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
1534 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1535 id self(internal->GetValue());
1537 if (JSStringIsEqualToUTF8CString(property, "$cyi"))
1541 NSString *name(CYCastNSString(pool, context, property));
1543 if (CYInternal *internal = CYInternal::Get(self))
1544 if (internal->HasProperty(context, property))
1547 Class _class(object_getClass(self));
1550 // XXX: this is an evil hack to deal with NSProxy; fix elsewhere
1551 if (CYImplements(self, _class, @selector(cy$hasProperty:), false))
1552 if ([self cy$hasProperty:name])
1554 } CYPoolCatch(false)
1556 const char *string(CYPoolCString(pool, context, name));
1559 if (class_getProperty(_class, string) != NULL)
1563 if (SEL sel = sel_getUid(string))
1564 if (CYImplements(self, _class, sel, true))
1570 static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1571 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1572 id self(internal->GetValue());
1574 if (JSStringIsEqualToUTF8CString(property, "$cyi"))
1575 return Internal::Make(context, self, object);
1578 NSString *name(CYCastNSString(pool, context, property));
1580 if (CYInternal *internal = CYInternal::Get(self))
1581 if (JSValueRef value = internal->GetProperty(context, property))
1585 if (NSObject *data = [self cy$getProperty:name])
1586 return CYCastJSValue(context, data);
1589 const char *string(CYPoolCString(pool, context, name));
1590 Class _class(object_getClass(self));
1593 if (objc_property_t property = class_getProperty(_class, string)) {
1594 PropertyAttributes attributes(property);
1595 SEL sel(sel_registerName(attributes.Getter()));
1596 return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception);
1600 if (SEL sel = sel_getUid(string))
1601 if (CYImplements(self, _class, sel, true))
1602 return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception);
1607 static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
1608 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1609 id self(internal->GetValue());
1613 NSString *name(CYCastNSString(pool, context, property));
1614 NSObject *data(CYCastNSObject(pool, context, value));
1617 if ([self cy$setProperty:name to:data])
1621 const char *string(CYPoolCString(pool, context, name));
1622 Class _class(object_getClass(self));
1625 if (objc_property_t property = class_getProperty(_class, string)) {
1626 PropertyAttributes attributes(property);
1627 if (const char *setter = attributes.Setter()) {
1628 SEL sel(sel_registerName(setter));
1629 JSValueRef arguments[1] = {value};
1630 CYSendMessage(pool, context, self, NULL, sel, 1, arguments, false, exception);
1636 size_t length(strlen(string));
1638 char set[length + 5];
1644 if (string[0] != '\0') {
1645 set[3] = toupper(string[0]);
1646 memcpy(set + 4, string + 1, length - 1);
1649 set[length + 3] = ':';
1650 set[length + 4] = '\0';
1652 if (SEL sel = sel_getUid(set))
1653 if (CYImplements(self, _class, sel, false)) {
1654 JSValueRef arguments[1] = {value};
1655 CYSendMessage(pool, context, self, NULL, sel, 1, arguments, false, exception);
1658 if (CYInternal *internal = CYInternal::Set(self)) {
1659 internal->SetProperty(context, property, value);
1666 static bool Instance_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1667 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1668 id self(internal->GetValue());
1671 NSString *name(CYCastNSString(NULL, context, property));
1672 return [self cy$deleteProperty:name];
1674 } CYCatch return /*XXX*/ NULL; }
1676 static void Instance_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1677 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1678 id self(internal->GetValue());
1681 Class _class(object_getClass(self));
1686 objc_property_t *data(class_copyPropertyList(_class, &size));
1687 for (size_t i(0); i != size; ++i)
1688 JSPropertyNameAccumulatorAddName(names, CYJSString(property_getName(data[i])));
1694 // XXX: this is an evil hack to deal with NSProxy; fix elsewhere
1695 if (CYImplements(self, _class, @selector(cy$getPropertyNames:inContext:), false))
1696 [self cy$getPropertyNames:names inContext:context];
1700 static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1701 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1702 JSObjectRef value(Instance::Make(context, [internal->GetValue() alloc], Instance::Uninitialized));
1706 static bool Instance_hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef instance, JSValueRef *exception) { CYTry {
1707 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) constructor)));
1708 Class _class(internal->GetValue());
1709 if (!CYIsClass(_class))
1712 if (JSValueIsObjectOfClass(context, instance, Instance_)) {
1713 Instance *linternal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) instance)));
1714 // XXX: this isn't always safe
1715 return [linternal->GetValue() isKindOfClass:_class];
1721 static bool Internal_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
1722 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1725 id self(internal->GetValue());
1726 const char *name(CYPoolCString(pool, context, property));
1728 if (object_getInstanceVariable(self, name, NULL) != NULL)
1734 static JSValueRef Internal_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1735 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1738 id self(internal->GetValue());
1739 const char *name(CYPoolCString(pool, context, property));
1741 if (objc_ivar *ivar = object_getInstanceVariable(self, name, NULL)) {
1742 Type_privateData type(pool, ivar_getTypeEncoding(ivar));
1743 // XXX: if this fails and throws an exception the person we are throwing it to gets the wrong exception
1744 return CYFromFFI(context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar));
1750 static bool Internal_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
1751 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1754 id self(internal->GetValue());
1755 const char *name(CYPoolCString(pool, context, property));
1757 if (objc_ivar *ivar = object_getInstanceVariable(self, name, NULL)) {
1758 Type_privateData type(pool, ivar_getTypeEncoding(ivar));
1759 CYPoolFFI(pool, context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar), value);
1766 static void Internal_getPropertyNames_(Class _class, JSPropertyNameAccumulatorRef names) {
1767 if (Class super = class_getSuperclass(_class))
1768 Internal_getPropertyNames_(super, names);
1770 #if OBJC_API_VERSION >= 2
1772 objc_ivar **data(class_copyIvarList(_class, &size));
1773 for (size_t i(0); i != size; ++i)
1774 JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(data[i])));
1777 if (objc_ivar_list *ivars = _class->ivars)
1778 for (int i(0); i != ivars->ivar_count; ++i)
1779 JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(&ivars->ivar_list[i])));
1783 static void Internal_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1784 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1787 id self(internal->GetValue());
1788 Class _class(object_getClass(self));
1790 Internal_getPropertyNames_(_class, names);
1793 static JSValueRef Internal_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
1794 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1795 return internal->GetOwner();
1798 static JSValueRef ObjectiveC_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1800 NSString *name(CYCastNSString(pool, context, property));
1801 if (Class _class = NSClassFromString(name))
1802 return CYMakeInstance(context, _class, true);
1806 static void ObjectiveC_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1808 size_t size(objc_getClassList(NULL, 0));
1809 Class *data(reinterpret_cast<Class *>(malloc(sizeof(Class) * size)));
1812 size_t writ(objc_getClassList(data, size));
1815 if (Class *copy = reinterpret_cast<Class *>(realloc(data, sizeof(Class) * writ))) {
1821 for (size_t i(0); i != writ; ++i)
1822 JSPropertyNameAccumulatorAddName(names, CYJSString(class_getName(data[i])));
1828 while (Class _class = objc_next_class(&state))
1829 JSPropertyNameAccumulatorAddName(names, CYJSString(class_getName(_class)));
1833 #if OBJC_API_VERSION >= 2
1834 static JSValueRef ObjectiveC_Image_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1835 const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object)));
1838 const char *name(CYPoolCString(pool, context, property));
1840 const char **data(objc_copyClassNamesForImage(internal, &size));
1842 for (size_t i(0); i != size; ++i)
1843 if (strcmp(name, data[i]) == 0) {
1844 if (Class _class = objc_getClass(name)) {
1845 value = CYMakeInstance(context, _class, true);
1856 static void ObjectiveC_Image_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1857 const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object)));
1859 const char **data(objc_copyClassNamesForImage(internal, &size));
1860 for (size_t i(0); i != size; ++i)
1861 JSPropertyNameAccumulatorAddName(names, CYJSString(data[i]));
1865 static JSValueRef ObjectiveC_Images_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1867 const char *name(CYPoolCString(pool, context, property));
1869 const char **data(objc_copyImageNames(&size));
1870 for (size_t i(0); i != size; ++i)
1871 if (strcmp(name, data[i]) == 0) {
1880 JSObjectRef value(JSObjectMake(context, NULL, NULL));
1881 CYSetProperty(context, value, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Image_Classes_, const_cast<char *>(name)));
1885 static void ObjectiveC_Images_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1887 const char **data(objc_copyImageNames(&size));
1888 for (size_t i(0); i != size; ++i)
1889 JSPropertyNameAccumulatorAddName(names, CYJSString(data[i]));
1894 static JSValueRef ObjectiveC_Protocols_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1896 const char *name(CYPoolCString(pool, context, property));
1897 if (Protocol *protocol = objc_getProtocol(name))
1898 return CYMakeInstance(context, protocol, true);
1902 static void ObjectiveC_Protocols_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1903 #if OBJC_API_VERSION >= 2
1905 Protocol **data(objc_copyProtocolList(&size));
1906 for (size_t i(0); i != size; ++i)
1907 JSPropertyNameAccumulatorAddName(names, CYJSString(protocol_getName(data[i])));
1915 static bool stret(ffi_type *ffi_type) {
1916 return ffi_type->type == FFI_TYPE_STRUCT && (
1917 ffi_type->size > OBJC_MAX_STRUCT_BY_VALUE ||
1918 struct_forward_array[ffi_type->size] != 0
1923 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 {
1927 _class = object_getClass(self);
1931 if (objc_method *method = class_getInstanceMethod(_class, _cmd)) {
1932 imp = method_getImplementation(method);
1933 type = method_getTypeEncoding(method);
1938 NSMethodSignature *method([self methodSignatureForSelector:_cmd]);
1940 throw CYJSError(context, "unrecognized selector %s sent to object %p", sel_getName(_cmd), self);
1941 type = CYPoolCString(pool, context, [method _typeString]);
1949 sig::Signature signature;
1950 sig::Parse(pool, &signature, type, &Structor_);
1953 sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif);
1957 if (stret(cif.rtype))
1958 imp = class_getMethodImplementation_stret(_class, _cmd);
1960 imp = class_getMethodImplementation(_class, _cmd);
1962 objc_super super = {self, _class};
1963 imp = objc_msg_lookup_super(&super, _cmd);
1967 void (*function)() = reinterpret_cast<void (*)()>(imp);
1968 return CYCallFunction(pool, context, 2, setup, count, arguments, initialize, exception, &signature, &cif, function);
1971 static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1973 throw CYJSError(context, "too few arguments to objc_msgSend");
1983 if (JSValueIsObjectOfClass(context, arguments[0], Super_)) {
1984 cy::Super *internal(reinterpret_cast<cy::Super *>(JSObjectGetPrivate((JSObjectRef) arguments[0])));
1985 self = internal->GetValue();
1986 _class = internal->class_;;
1987 uninitialized = false;
1988 } else if (JSValueIsObjectOfClass(context, arguments[0], Instance_)) {
1989 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) arguments[0])));
1990 self = internal->GetValue();
1992 uninitialized = internal->IsUninitialized();
1994 internal->value_ = nil;
1996 self = CYCastNSObject(pool, context, arguments[0]);
1998 uninitialized = false;
2002 return CYJSNull(context);
2004 _cmd = CYCastSEL(context, arguments[1]);
2006 return CYSendMessage(pool, context, self, _class, _cmd, count - 2, arguments + 2, uninitialized, exception);
2009 /* Hook: objc_registerClassPair {{{ */
2010 #if defined(__APPLE__) && defined(__arm__)
2011 // XXX: replace this with associated objects
2013 MSHook(void, CYDealloc, id self, SEL sel) {
2014 CYInternal *internal;
2015 object_getInstanceVariable(self, "cy$internal_", reinterpret_cast<void **>(&internal));
2016 if (internal != NULL)
2018 _CYDealloc(self, sel);
2021 MSHook(void, objc_registerClassPair, Class _class) {
2022 Class super(class_getSuperclass(_class));
2023 if (super == NULL || class_getInstanceVariable(super, "cy$internal_") == NULL) {
2024 class_addIvar(_class, "cy$internal_", sizeof(CYInternal *), log2(sizeof(CYInternal *)), "^{CYInternal}");
2025 MSHookMessage(_class, @selector(dealloc), MSHake(CYDealloc));
2028 _objc_registerClassPair(_class);
2031 static JSValueRef objc_registerClassPair_(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2033 throw CYJSError(context, "incorrect number of arguments to objc_registerClassPair");
2035 NSObject *value(CYCastNSObject(pool, context, arguments[0]));
2036 if (value == NULL || !CYIsClass(value))
2037 throw CYJSError(context, "incorrect number of arguments to objc_registerClassPair");
2038 Class _class((Class) value);
2039 $objc_registerClassPair(_class);
2040 return CYJSUndefined(context);
2045 static JSValueRef Selector_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2046 JSValueRef setup[count + 2];
2049 memcpy(setup + 2, arguments, sizeof(JSValueRef) * count);
2050 return $objc_msgSend(context, NULL, NULL, count + 2, setup, exception);
2053 static JSValueRef Message_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2055 Message_privateData *internal(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate(object)));
2057 // XXX: handle Instance::Uninitialized?
2058 id self(CYCastNSObject(pool, context, _this));
2062 setup[1] = &internal->sel_;
2064 return CYCallFunction(pool, context, 2, setup, count, arguments, false, exception, &internal->signature_, &internal->cif_, internal->GetValue());
2067 static JSObjectRef Super_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2069 throw CYJSError(context, "incorrect number of arguments to Super constructor");
2071 id self(CYCastNSObject(pool, context, arguments[0]));
2072 Class _class(CYCastClass(pool, context, arguments[1]));
2073 return cy::Super::Make(context, self, _class);
2076 static JSObjectRef Selector_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2078 throw CYJSError(context, "incorrect number of arguments to Selector constructor");
2080 const char *name(CYPoolCString(pool, context, arguments[0]));
2081 return CYMakeSelector(context, sel_registerName(name));
2084 static JSObjectRef Instance_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2086 throw CYJSError(context, "incorrect number of arguments to Instance constructor");
2087 id self(count == 0 ? nil : CYCastPointer<id>(context, arguments[0]));
2088 return Instance::Make(context, self);
2091 static JSValueRef CYValue_getProperty_value(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
2092 CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(object)));
2093 return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_));
2096 static JSValueRef CYValue_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2097 CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this)));
2098 Type_privateData *typical(internal->GetType());
2103 if (typical == NULL) {
2107 type = typical->type_;
2108 ffi = typical->ffi_;
2111 return CYMakePointer(context, &internal->value_, _not(size_t), type, ffi, object);
2114 static JSValueRef Instance_getProperty_constructor(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
2115 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
2116 return Instance::Make(context, (id) object_getClass(internal->GetValue()));
2119 static JSValueRef Instance_getProperty_protocol(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2120 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
2121 id self(internal->GetValue());
2122 if (!CYIsClass(self))
2123 return CYJSUndefined(context);
2124 return CYGetClassPrototype(context, self);
2127 static JSValueRef Instance_getProperty_messages(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
2128 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
2129 id self(internal->GetValue());
2130 if (!CYIsClass(self))
2131 return CYJSUndefined(context);
2132 return Messages::Make(context, (Class) self);
2135 static JSValueRef Instance_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2136 if (!JSValueIsObjectOfClass(context, _this, Instance_))
2139 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2140 return CYCastJSValue(context, CYJSString(context, CYCastNSCYON(internal->GetValue())));
2143 static JSValueRef Instance_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2144 if (!JSValueIsObjectOfClass(context, _this, Instance_))
2147 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2154 key = CYCastNSString(NULL, context, CYJSString(context, arguments[0]));
2155 // XXX: check for support of cy$toJSON?
2156 return CYCastJSValue(context, CYJSString(context, [internal->GetValue() cy$toJSON:key]));
2158 } CYCatch return /*XXX*/ NULL; }
2160 static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2161 if (!JSValueIsObjectOfClass(context, _this, Instance_))
2164 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2167 // XXX: this seems like a stupid implementation; what if it crashes? why not use the CYONifier backend?
2168 return CYCastJSValue(context, CYJSString(context, [internal->GetValue() description]));
2170 } CYCatch return /*XXX*/ NULL; }
2172 static JSValueRef Selector_callAsFunction_toString(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 return CYCastJSValue(context, sel_getName(internal->GetValue()));
2177 static JSValueRef Selector_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2178 return Selector_callAsFunction_toString(context, object, _this, count, arguments, exception);
2181 static JSValueRef Selector_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2182 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
2183 const char *name(sel_getName(internal->GetValue()));
2186 NSString *string([NSString stringWithFormat:@"@selector(%s)", name]);
2187 return CYCastJSValue(context, CYJSString(context, string));
2189 } CYCatch return /*XXX*/ NULL; }
2191 static JSValueRef Selector_callAsFunction_type(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2193 throw CYJSError(context, "incorrect number of arguments to Selector.type");
2196 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
2197 SEL sel(internal->GetValue());
2199 objc_method *method;
2200 if (Class _class = CYCastClass(pool, context, arguments[0]))
2201 method = class_getInstanceMethod(_class, sel);
2205 if (const char *type = CYPoolTypeEncoding(pool, context, sel, method))
2206 return CYCastJSValue(context, CYJSString(type));
2208 return CYJSNull(context);
2211 static JSStaticValue Selector_staticValues[2] = {
2212 {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete},
2213 {NULL, NULL, NULL, 0}
2216 static JSStaticValue Instance_staticValues[5] = {
2217 {"constructor", &Instance_getProperty_constructor, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2218 {"messages", &Instance_getProperty_messages, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2219 {"prototype", &Instance_getProperty_protocol, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2220 {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2221 {NULL, NULL, NULL, 0}
2224 static JSStaticFunction Instance_staticFunctions[5] = {
2225 {"$cya", &CYValue_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2226 {"toCYON", &Instance_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2227 {"toJSON", &Instance_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2228 {"toString", &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2232 static JSStaticFunction Internal_staticFunctions[2] = {
2233 {"$cya", &Internal_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2237 static JSStaticFunction Selector_staticFunctions[5] = {
2238 {"toCYON", &Selector_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2239 {"toJSON", &Selector_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2240 {"toString", &Selector_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2241 {"type", &Selector_callAsFunction_type, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2245 void CYObjectiveC_Initialize() { /*XXX*/ JSContextRef context(NULL); CYPoolTry {
2246 apr_pool_t *pool(CYGetGlobalPool());
2248 Object_type = new(pool) Type_privateData("@");
2249 Selector_type = new(pool) Type_privateData(":");
2252 NSCFBoolean_ = objc_getClass("NSCFBoolean");
2253 NSCFType_ = objc_getClass("NSCFType");
2254 NSMessageBuilder_ = objc_getClass("NSMessageBuilder");
2255 NSZombie_ = objc_getClass("_NSZombie_");
2257 NSBoolNumber_ = objc_getClass("NSBoolNumber");
2260 NSArray_ = objc_getClass("NSArray");
2261 NSDictionary_ = objc_getClass("NSDictionary");
2262 Object_ = objc_getClass("Object");
2264 JSClassDefinition definition;
2266 definition = kJSClassDefinitionEmpty;
2267 definition.className = "Instance";
2268 definition.staticValues = Instance_staticValues;
2269 definition.staticFunctions = Instance_staticFunctions;
2270 definition.hasProperty = &Instance_hasProperty;
2271 definition.getProperty = &Instance_getProperty;
2272 definition.setProperty = &Instance_setProperty;
2273 definition.deleteProperty = &Instance_deleteProperty;
2274 definition.getPropertyNames = &Instance_getPropertyNames;
2275 definition.callAsConstructor = &Instance_callAsConstructor;
2276 definition.hasInstance = &Instance_hasInstance;
2277 definition.finalize = &CYFinalize;
2278 Instance_ = JSClassCreate(&definition);
2280 definition = kJSClassDefinitionEmpty;
2281 definition.className = "Internal";
2282 definition.staticFunctions = Internal_staticFunctions;
2283 definition.hasProperty = &Internal_hasProperty;
2284 definition.getProperty = &Internal_getProperty;
2285 definition.setProperty = &Internal_setProperty;
2286 definition.getPropertyNames = &Internal_getPropertyNames;
2287 definition.finalize = &CYFinalize;
2288 Internal_ = JSClassCreate(&definition);
2290 definition = kJSClassDefinitionEmpty;
2291 definition.className = "Message";
2292 definition.staticFunctions = cy::Functor::StaticFunctions;
2293 definition.callAsFunction = &Message_callAsFunction;
2294 definition.finalize = &CYFinalize;
2295 Message_ = JSClassCreate(&definition);
2297 definition = kJSClassDefinitionEmpty;
2298 definition.className = "Messages";
2299 definition.hasProperty = &Messages_hasProperty;
2300 definition.getProperty = &Messages_getProperty;
2301 definition.setProperty = &Messages_setProperty;
2302 #if 0 && OBJC_API_VERSION < 2
2303 definition.deleteProperty = &Messages_deleteProperty;
2305 definition.getPropertyNames = &Messages_getPropertyNames;
2306 definition.finalize = &CYFinalize;
2307 Messages_ = JSClassCreate(&definition);
2309 definition = kJSClassDefinitionEmpty;
2310 definition.className = "Selector";
2311 definition.staticValues = Selector_staticValues;
2312 definition.staticFunctions = Selector_staticFunctions;
2313 definition.callAsFunction = &Selector_callAsFunction;
2314 definition.finalize = &CYFinalize;
2315 Selector_ = JSClassCreate(&definition);
2317 definition = kJSClassDefinitionEmpty;
2318 definition.className = "Super";
2319 definition.staticFunctions = Internal_staticFunctions;
2320 definition.finalize = &CYFinalize;
2321 Super_ = JSClassCreate(&definition);
2323 definition = kJSClassDefinitionEmpty;
2324 definition.className = "ObjectiveC::Classes";
2325 definition.getProperty = &ObjectiveC_Classes_getProperty;
2326 definition.getPropertyNames = &ObjectiveC_Classes_getPropertyNames;
2327 ObjectiveC_Classes_ = JSClassCreate(&definition);
2329 #if OBJC_API_VERSION >= 2
2330 definition = kJSClassDefinitionEmpty;
2331 definition.className = "ObjectiveC::Images";
2332 definition.getProperty = &ObjectiveC_Images_getProperty;
2333 definition.getPropertyNames = &ObjectiveC_Images_getPropertyNames;
2334 ObjectiveC_Images_ = JSClassCreate(&definition);
2336 definition = kJSClassDefinitionEmpty;
2337 definition.className = "ObjectiveC::Image::Classes";
2338 definition.getProperty = &ObjectiveC_Image_Classes_getProperty;
2339 definition.getPropertyNames = &ObjectiveC_Image_Classes_getPropertyNames;
2340 ObjectiveC_Image_Classes_ = JSClassCreate(&definition);
2343 definition = kJSClassDefinitionEmpty;
2344 definition.className = "ObjectiveC::Protocols";
2345 definition.getProperty = &ObjectiveC_Protocols_getProperty;
2346 definition.getPropertyNames = &ObjectiveC_Protocols_getPropertyNames;
2347 ObjectiveC_Protocols_ = JSClassCreate(&definition);
2349 #if defined(__APPLE__) && defined(__arm__)
2350 MSHookFunction(&objc_registerClassPair, MSHake(objc_registerClassPair));
2354 class_addMethod(NSCFType_, @selector(cy$toJSON:), reinterpret_cast<IMP>(&NSCFType$cy$toJSON), "@12@0:4@8");
2358 void CYObjectiveC_SetupContext(JSContextRef context) { CYPoolTry {
2359 JSObjectRef global(CYGetGlobalObject(context));
2360 JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s)));
2362 JSObjectRef ObjectiveC(JSObjectMake(context, NULL, NULL));
2363 CYSetProperty(context, global, CYJSString("ObjectiveC"), ObjectiveC);
2365 CYSetProperty(context, ObjectiveC, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Classes_, NULL));
2366 CYSetProperty(context, ObjectiveC, CYJSString("protocols"), JSObjectMake(context, ObjectiveC_Protocols_, NULL));
2368 #if OBJC_API_VERSION >= 2
2369 CYSetProperty(context, ObjectiveC, CYJSString("images"), JSObjectMake(context, ObjectiveC_Images_, NULL));
2372 JSObjectRef Instance(JSObjectMakeConstructor(context, Instance_, &Instance_new));
2373 JSObjectRef Message(JSObjectMakeConstructor(context, Message_, NULL));
2374 JSObjectRef Selector(JSObjectMakeConstructor(context, Selector_, &Selector_new));
2375 JSObjectRef Super(JSObjectMakeConstructor(context, Super_, &Super_new));
2377 JSObjectRef Instance_prototype(CYCastJSObject(context, CYGetProperty(context, Instance, prototype_s)));
2378 CYSetProperty(context, cy, CYJSString("Instance_prototype"), Instance_prototype);
2380 CYSetProperty(context, global, CYJSString("Instance"), Instance);
2381 CYSetProperty(context, global, CYJSString("Selector"), Selector);
2382 CYSetProperty(context, global, CYJSString("Super"), Super);
2384 #if defined(__APPLE__) && defined(__arm__)
2385 CYSetProperty(context, global, CYJSString("objc_registerClassPair"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_registerClassPair"), &objc_registerClassPair_));
2388 CYSetProperty(context, global, CYJSString("objc_msgSend"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_msgSend"), &$objc_msgSend));
2390 JSObjectRef Function_prototype(CYGetCachedObject(context, CYJSString("Function_prototype")));
2391 JSObjectSetPrototype(context, CYCastJSObject(context, CYGetProperty(context, Message, prototype_s)), Function_prototype);
2392 JSObjectSetPrototype(context, CYCastJSObject(context, CYGetProperty(context, Selector, prototype_s)), Function_prototype);
2395 static CYHooks CYObjectiveCHooks = {
2396 &CYObjectiveC_ExecuteStart,
2397 &CYObjectiveC_ExecuteEnd,
2398 &CYObjectiveC_RuntimeProperty,
2399 &CYObjectiveC_CallFunction,
2400 &CYObjectiveC_Initialize,
2401 &CYObjectiveC_SetupContext,
2402 &CYObjectiveC_PoolFFI,
2403 &CYObjectiveC_FromFFI,
2406 struct CYObjectiveC {
2408 hooks_ = &CYObjectiveCHooks;