1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2013 Jay Freeman (saurik)
5 /* GNU General Public License, Version 3 {{{ */
7 * Cycript is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
12 * Cycript is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
22 #include <Foundation/Foundation.h>
24 #include "ObjectiveC/Internal.hpp"
26 #include <objc/objc-api.h>
28 #include "cycript.hpp"
30 #include "ObjectiveC/Internal.hpp"
33 #include <CoreFoundation/CoreFoundation.h>
34 #include <JavaScriptCore/JSStringRefCF.h>
35 #include <WebKit/WebScriptObject.h>
36 #include <objc/runtime.h>
40 #include "JavaScript.hpp"
42 #include "Execute.hpp"
49 #define CYObjectiveTry_(context) { \
50 JSContextRef context_(context); \
52 #define CYObjectiveTry { \
54 #define CYObjectiveCatch \
55 catch (const CYException &error) { \
56 @throw CYCastNSObject(NULL, context_, error.CastJSValue(context_)); \
62 NSAutoreleasePool *_pool([[NSAutoreleasePool alloc] init]); \
64 #define CYPoolCatch(value) \
65 @catch (NSException *error) { \
66 _saved = [error retain]; \
67 throw CYJSError(context, CYCastJSValue(context, error)); \
72 [_saved autorelease]; \
78 #define CYSadCatch(value) \
79 @catch (NSException *error ) { \
80 throw CYJSError(context, CYCastJSValue(context, error)); \
85 #define class_getSuperclass GSObjCSuper
86 #define class_getInstanceVariable GSCGetInstanceVariableDefinition
87 #define class_getName GSNameFromClass
89 #define class_removeMethods(cls, list) GSRemoveMethodList(cls, list, YES)
91 #define ivar_getName(ivar) ((ivar)->ivar_name)
92 #define ivar_getOffset(ivar) ((ivar)->ivar_offset)
93 #define ivar_getTypeEncoding(ivar) ((ivar)->ivar_type)
95 #define method_getName(method) ((method)->method_name)
96 #define method_getImplementation(method) ((method)->method_imp)
97 #define method_getTypeEncoding(method) ((method)->method_types)
98 #define method_setImplementation(method, imp) ((void) ((method)->method_imp = (imp)))
101 #define objc_getClass GSClassFromName
103 #define objc_getProtocol GSProtocolFromName
105 #define object_getClass GSObjCClass
107 #define object_getInstanceVariable(object, name, value) ({ \
108 objc_ivar *ivar(class_getInstanceVariable(object_getClass(object), name)); \
109 _assert(value != NULL); \
111 GSObjCGetVariable(object, ivar_getOffset(ivar), sizeof(void *), value); \
115 #define object_setIvar(object, ivar, value) ({ \
116 void *data = (value); \
117 GSObjCSetVariable(object, ivar_getOffset(ivar), sizeof(void *), &data); \
120 #define protocol_getName(protocol) [(protocol) name]
123 static void (*$objc_setAssociatedObject)(id object, void *key, id value, objc_AssociationPolicy policy);
124 static id (*$objc_getAssociatedObject)(id object, void *key);
125 static void (*$objc_removeAssociatedObjects)(id object);
127 struct BlockLiteral {
131 void (*invoke)(void *, ...);
135 struct BlockDescriptor1 {
136 unsigned long int reserved;
137 unsigned long int size;
140 struct BlockDescriptor2 {
141 void (*copy_helper)(BlockLiteral *dst, BlockLiteral *src);
142 void (*dispose_helper)(BlockLiteral *src);
145 struct BlockDescriptor3 {
146 const char *signature;
151 BLOCK_DEALLOCATING = 0x0001,
152 BLOCK_REFCOUNT_MASK = 0xfffe,
153 BLOCK_NEEDS_FREE = 1 << 24,
154 BLOCK_HAS_COPY_DISPOSE = 1 << 25,
155 BLOCK_HAS_CTOR = 1 << 26,
156 BLOCK_IS_GC = 1 << 27,
157 BLOCK_IS_GLOBAL = 1 << 28,
158 BLOCK_HAS_STRET = 1 << 29,
159 BLOCK_HAS_SIGNATURE = 1 << 30,
162 JSValueRef CYSendMessage(CYPool &pool, JSContextRef context, id self, Class super, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception);
164 /* Objective-C Pool Release {{{ */
165 void CYPoolRelease_(void *data) {
166 id object(reinterpret_cast<id>(data));
170 id CYPoolRelease_(CYPool *pool, id object) {
173 else if (pool == NULL)
174 return [object autorelease];
176 pool->atexit(CYPoolRelease_);
181 template <typename Type_>
182 Type_ CYPoolRelease(CYPool *pool, Type_ object) {
183 return (Type_) CYPoolRelease_(pool, (id) object);
186 /* Objective-C Strings {{{ */
187 const char *CYPoolCString(CYPool &pool, JSContextRef context, NSString *value) {
189 return [value UTF8String];
191 size_t size([value maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1);
192 char *string(new(pool) char[size]);
193 if (![value getCString:string maxLength:size encoding:NSUTF8StringEncoding])
194 throw CYJSError(context, "[NSString getCString:maxLength:encoding:] == NO");
199 JSStringRef CYCopyJSString(JSContextRef context, NSString *value) {
201 return JSStringCreateWithCFString(reinterpret_cast<CFStringRef>(value));
204 return CYCopyJSString(CYPoolCString(pool, context, value));
208 JSStringRef CYCopyJSString(JSContextRef context, NSObject *value) {
211 // XXX: this definition scares me; is anyone using this?!
212 NSString *string([value description]);
213 return CYCopyJSString(context, string);
216 NSString *CYCopyNSString(const CYUTF8String &value) {
218 return (NSString *) CFStringCreateWithBytes(kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(value.data), value.size, kCFStringEncodingUTF8, true);
220 return [[NSString alloc] initWithBytes:value.data length:value.size encoding:NSUTF8StringEncoding];
224 NSString *CYCopyNSString(JSContextRef context, JSStringRef value) {
226 return (NSString *) JSStringCopyCFString(kCFAllocatorDefault, value);
229 return CYCopyNSString(CYPoolUTF8String(pool, context, value));
233 NSString *CYCopyNSString(JSContextRef context, JSValueRef value) {
234 return CYCopyNSString(context, CYJSString(context, value));
237 NSString *CYCastNSString(CYPool *pool, const CYUTF8String &value) {
238 return CYPoolRelease(pool, CYCopyNSString(value));
241 NSString *CYCastNSString(CYPool *pool, SEL sel) {
242 const char *name(sel_getName(sel));
243 return CYPoolRelease(pool, CYCopyNSString(CYUTF8String(name, strlen(name))));
246 NSString *CYCastNSString(CYPool *pool, JSContextRef context, JSStringRef value) {
247 return CYPoolRelease(pool, CYCopyNSString(context, value));
250 CYUTF8String CYCastUTF8String(NSString *value) {
251 NSData *data([value dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO]);
252 return CYUTF8String(reinterpret_cast<const char *>([data bytes]), [data length]);
256 JSValueRef CYCastJSValue(JSContextRef context, NSObject *value);
258 void CYThrow(JSContextRef context, NSException *error, JSValueRef *exception) {
259 if (exception == NULL)
261 *exception = CYCastJSValue(context, error);
264 size_t CYGetIndex(NSString *value) {
265 return CYGetIndex(CYCastUTF8String(value));
268 bool CYGetOffset(CYPool &pool, JSContextRef context, NSString *value, ssize_t &index) {
269 return CYGetOffset(CYPoolCString(pool, context, value), index);
272 static JSClassRef Instance_;
274 static JSClassRef ArrayInstance_;
275 static JSClassRef FunctionInstance_;
276 static JSClassRef ObjectInstance_;
277 static JSClassRef StringInstance_;
279 static JSClassRef Class_;
280 static JSClassRef Internal_;
281 static JSClassRef Message_;
282 static JSClassRef Messages_;
283 static JSClassRef Selector_;
284 static JSClassRef Super_;
286 static JSClassRef ObjectiveC_Classes_;
287 static JSClassRef ObjectiveC_Constants_;
288 static JSClassRef ObjectiveC_Protocols_;
291 static JSClassRef ObjectiveC_Image_Classes_;
292 static JSClassRef ObjectiveC_Images_;
296 static Class NSCFBoolean_;
297 static Class NSCFType_;
298 static Class NSGenericDeallocHandler_;
299 static Class NSMessageBuilder_;
300 static Class NSZombie_;
302 static Class NSBoolNumber_;
305 static Class NSArray_;
306 static Class NSBlock_;
307 static Class NSDictionary_;
308 static Class NSString_;
309 static Class Object_;
311 static Type_privateData *Object_type;
312 static Type_privateData *Selector_type;
314 Type_privateData *Instance::GetType() const {
318 Type_privateData *Selector_privateData::GetType() const {
319 return Selector_type;
322 static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception);
324 JSValueRef CYGetClassPrototype(JSContextRef context, Class self, bool meta) {
326 return CYGetCachedObject(context, CYJSString("Instance_prototype"));
327 else if (meta && !class_isMetaClass(self))
328 return CYGetCachedObject(context, CYJSString("Class_prototype"));
330 JSObjectRef global(CYGetGlobalObject(context));
331 JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s)));
334 sprintf(label, "i%p", self);
335 CYJSString name(label);
337 JSValueRef value(CYGetProperty(context, cy, name));
338 if (!JSValueIsUndefined(context, value))
341 JSClassRef _class(NULL);
342 JSValueRef prototype;
344 if (self == NSArray_)
345 prototype = CYGetCachedObject(context, CYJSString("ArrayInstance_prototype"));
346 else if (self == NSBlock_)
347 prototype = CYGetCachedObject(context, CYJSString("FunctionInstance_prototype"));
348 else if (self == NSDictionary_)
349 prototype = CYGetCachedObject(context, CYJSString("ObjectInstance_prototype"));
350 else if (self == NSString_)
351 prototype = CYGetCachedObject(context, CYJSString("StringInstance_prototype"));
353 prototype = CYGetClassPrototype(context, class_getSuperclass(self), meta);
355 JSObjectRef object(JSObjectMake(context, _class, NULL));
356 JSObjectSetPrototype(context, object, prototype);
357 CYSetProperty(context, cy, name, object);
362 _finline JSValueRef CYGetClassPrototype(JSContextRef context, Class self) {
363 return CYGetClassPrototype(context, self, class_isMetaClass(self));
366 JSObjectRef Messages::Make(JSContextRef context, Class _class) {
367 JSObjectRef value(JSObjectMake(context, Messages_, new Messages(_class)));
368 if (Class super = class_getSuperclass(_class))
369 JSObjectSetPrototype(context, value, Messages::Make(context, super));
373 JSObjectRef Internal::Make(JSContextRef context, id object, JSObjectRef owner) {
374 return JSObjectMake(context, Internal_, new Internal(object, context, owner));
378 JSObjectRef Super::Make(JSContextRef context, id object, Class _class) {
379 JSObjectRef value(JSObjectMake(context, Super_, new Super(object, _class)));
383 JSObjectRef Instance::Make(JSContextRef context, id object, Flags flags) {
384 JSObjectRef value(JSObjectMake(context, Instance_, new Instance(object, flags)));
385 JSObjectSetPrototype(context, value, CYGetClassPrototype(context, object_getClass(object)));
389 Instance::~Instance() {
390 if ((flags_ & Transient) == 0)
391 // XXX: does this handle background threads correctly?
392 // XXX: this simply does not work on the console because I'm stupid
393 [GetValue() performSelector:@selector(release) withObject:nil afterDelay:0];
396 struct Message_privateData :
401 Message_privateData(SEL sel, const char *type, IMP value = NULL) :
402 cy::Functor(type, reinterpret_cast<void (*)()>(value)),
408 JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient) {
409 Instance::Flags flags;
412 flags = Instance::Transient;
414 flags = Instance::None;
415 object = [object retain];
418 return Instance::Make(context, object, flags);
421 @interface NSMethodSignature (Cycript)
422 - (NSString *) _typeString;
425 @interface NSObject (Cycript)
427 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context;
428 - (JSType) cy$JSType;
430 - (JSValueRef) cy$toJSON:(NSString *)key inContext:(JSContextRef)context;
431 - (NSString *) cy$toCYON:(bool)objective;
433 - (bool) cy$hasProperty:(NSString *)name;
434 - (NSObject *) cy$getProperty:(NSString *)name;
435 - (JSValueRef) cy$getProperty:(NSString *)name inContext:(JSContextRef)context;
436 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value;
437 - (bool) cy$deleteProperty:(NSString *)name;
438 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context;
440 + (bool) cy$hasImplicitProperties;
446 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context;
449 NSString *CYCastNSCYON(id value, bool objective) {
455 Class _class(object_getClass(value));
456 SEL sel(@selector(cy$toCYON:));
458 if (objc_method *toCYON = class_getInstanceMethod(_class, sel))
459 string = reinterpret_cast<NSString *(*)(id, SEL, bool)>(method_getImplementation(toCYON))(value, sel, objective);
460 else if (objc_method *methodSignatureForSelector = class_getInstanceMethod(_class, @selector(methodSignatureForSelector:))) {
461 if (reinterpret_cast<NSMethodSignature *(*)(id, SEL, SEL)>(method_getImplementation(methodSignatureForSelector))(value, @selector(methodSignatureForSelector:), sel) != nil)
462 string = [value cy$toCYON:objective];
467 else if (value == NSZombie_)
468 string = @"_NSZombie_";
469 else if (_class == NSZombie_)
470 string = [NSString stringWithFormat:@"<_NSZombie_: %p>", value];
471 // XXX: frowny /in/ the pants
472 else if (value == NSGenericDeallocHandler_ || value == NSMessageBuilder_ || value == Object_)
476 string = [NSString stringWithFormat:@"%@", value];
481 string = @"undefined";
488 struct PropertyAttributes {
493 const char *variable;
506 PropertyAttributes(objc_property_t property) :
518 name = property_getName(property);
519 const char *attributes(property_getAttributes(property));
521 for (char *token(pool_.strdup(attributes)), *next; token != NULL; token = next) {
522 if ((next = strchr(token, ',')) != NULL)
525 case 'R': readonly = true; break;
526 case 'C': copy = true; break;
527 case '&': retain = true; break;
528 case 'N': nonatomic = true; break;
529 case 'G': getter_ = token + 1; break;
530 case 'S': setter_ = token + 1; break;
531 case 'V': variable = token + 1; break;
535 /*if (variable == NULL) {
536 variable = property_getName(property);
537 size_t size(strlen(variable));
538 char *name(new(pool_) char[size + 2]);
540 memcpy(name + 1, variable, size);
541 name[size + 1] = '\0';
546 const char *Getter() {
548 getter_ = pool_.strdup(name);
552 const char *Setter() {
553 if (setter_ == NULL && !readonly) {
554 size_t length(strlen(name));
556 char *temp(new(pool_) char[length + 5]);
562 temp[3] = toupper(name[0]);
563 memcpy(temp + 4, name + 1, length - 1);
566 temp[length + 3] = ':';
567 temp[length + 4] = '\0';
578 @interface CYWebUndefined : NSObject {
581 + (CYWebUndefined *) undefined;
585 @implementation CYWebUndefined
587 + (CYWebUndefined *) undefined {
588 static CYWebUndefined *instance_([[CYWebUndefined alloc] init]);
594 #define WebUndefined CYWebUndefined
597 /* Bridge: CYJSObject {{{ */
598 @interface CYJSObject : NSMutableDictionary {
600 JSGlobalContextRef context_;
603 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context;
605 - (NSUInteger) count;
606 - (id) objectForKey:(id)key;
607 - (NSEnumerator *) keyEnumerator;
608 - (void) setObject:(id)object forKey:(id)key;
609 - (void) removeObjectForKey:(id)key;
613 /* Bridge: CYJSArray {{{ */
614 @interface CYJSArray : NSMutableArray {
616 JSGlobalContextRef context_;
619 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context;
621 - (NSUInteger) count;
622 - (id) objectAtIndex:(NSUInteger)index;
624 - (void) addObject:(id)anObject;
625 - (void) insertObject:(id)anObject atIndex:(NSUInteger)index;
626 - (void) removeLastObject;
627 - (void) removeObjectAtIndex:(NSUInteger)index;
628 - (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
633 _finline bool CYJSValueIsNSObject(JSContextRef context, JSValueRef value) {
634 return JSValueIsObjectOfClass(context, value, Instance_);
637 _finline bool CYJSValueIsInstanceOfCachedConstructor(JSContextRef context, JSValueRef value, JSStringRef cache) {
638 JSValueRef exception(NULL);
639 JSObjectRef constructor(CYGetCachedObject(context, cache));
640 bool is(JSValueIsInstanceOfConstructor(context, value, constructor, &exception));
641 CYThrow(context, exception);
645 NSObject *CYMakeBlock(void (*invoke)(), sig::Signature &signature) {
646 BlockLiteral *literal(reinterpret_cast<BlockLiteral *>(malloc(sizeof(BlockLiteral))));
650 BlockDescriptor1 one_;
651 BlockDescriptor2 two_;
652 BlockDescriptor3 three_;
658 Descriptor *descriptor(new Descriptor);
659 memset(&descriptor->d_, 0, sizeof(descriptor->d_));
661 literal->isa = objc_getClass("__NSGlobalBlock__");
662 literal->flags = BLOCK_HAS_SIGNATURE | BLOCK_HAS_COPY_DISPOSE | BLOCK_IS_GLOBAL;
663 literal->reserved = 0;
664 literal->invoke = reinterpret_cast<void (*)(void *, ...)>(invoke);
665 literal->descriptor = descriptor;
667 descriptor->d_.one_.size = sizeof(descriptor->d_);
668 descriptor->d_.three_.signature = sig::Unparse(descriptor->pool_, &signature);
670 return reinterpret_cast<NSObject *>(literal);
673 NSObject *CYCastNSObject(CYPool *pool, JSContextRef context, JSObjectRef object) {
674 if (CYJSValueIsNSObject(context, object)) {
675 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
676 return internal->GetValue();
679 if (JSValueIsObjectOfClass(context, object, Functor_)) {
680 cy::Functor *internal(reinterpret_cast<cy::Functor *>(JSObjectGetPrivate(object)));
681 return CYMakeBlock(internal->GetValue(), internal->signature_);
684 bool array(CYJSValueIsInstanceOfCachedConstructor(context, object, Array_s));
685 id value(array ? [CYJSArray alloc] : [CYJSObject alloc]);
686 return CYPoolRelease(pool, [value initWithJSObject:object inContext:context]);
689 NSNumber *CYCopyNSNumber(JSContextRef context, JSValueRef value) {
690 return [[NSNumber alloc] initWithDouble:CYCastDouble(context, value)];
694 @interface NSBoolNumber : NSNumber {
699 id CYNSObject(CYPool *pool, JSContextRef context, JSValueRef value, bool cast) {
703 switch (JSType type = JSValueGetType(context, value)) {
704 case kJSTypeUndefined:
705 object = [WebUndefined undefined];
715 object = (id) (CYCastBool(context, value) ? kCFBooleanTrue : kCFBooleanFalse);
718 object = [[NSBoolNumber alloc] initWithBool:CYCastBool(context, value)];
724 object = CYCopyNSNumber(context, value);
729 object = CYCopyNSString(context, value);
734 // XXX: this might could be more efficient
735 object = CYCastNSObject(pool, context, (JSObjectRef) value);
740 throw CYJSError(context, "JSValueGetType() == 0x%x", type);
747 return CYPoolRelease(pool, object);
749 return [object retain];
752 NSObject *CYCastNSObject(CYPool *pool, JSContextRef context, JSValueRef value) {
753 return CYNSObject(pool, context, value, true);
756 NSObject *CYCopyNSObject(CYPool &pool, JSContextRef context, JSValueRef value) {
757 return CYNSObject(&pool, context, value, false);
760 /* Bridge: NSArray {{{ */
761 @implementation NSArray (Cycript)
764 return [[self mutableCopy] autorelease];
767 - (NSString *) cy$toCYON:(bool)objective {
768 NSMutableString *json([[[NSMutableString alloc] init] autorelease]);
769 [json appendString:@"@["];
773 for (id object in self) {
775 for (size_t index(0), count([self count]); index != count; ++index) {
776 id object([self objectAtIndex:index]);
779 [json appendString:@","];
782 if (object == nil || [object cy$JSType] != kJSTypeUndefined)
783 [json appendString:CYCastNSCYON(object, true)];
785 [json appendString:@","];
790 [json appendString:@"]"];
794 - (bool) cy$hasProperty:(NSString *)name {
795 if ([name isEqualToString:@"length"])
798 size_t index(CYGetIndex(name));
799 if (index == _not(size_t) || index >= [self count])
800 return [super cy$hasProperty:name];
805 - (NSObject *) cy$getProperty:(NSString *)name {
806 size_t index(CYGetIndex(name));
807 if (index == _not(size_t) || index >= [self count])
808 return [super cy$getProperty:name];
810 return [self objectAtIndex:index];
813 - (JSValueRef) cy$getProperty:(NSString *)name inContext:(JSContextRef)context {
814 CYObjectiveTry_(context) {
815 if ([name isEqualToString:@"length"])
816 return CYCastJSValue(context, [self count]);
819 return [super cy$getProperty:name inContext:context];
822 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context {
823 [super cy$getPropertyNames:names inContext:context];
825 for (size_t index(0), count([self count]); index != count; ++index) {
826 id object([self objectAtIndex:index]);
827 if (object == nil || [object cy$JSType] != kJSTypeUndefined) {
829 sprintf(name, "%zu", index);
830 JSPropertyNameAccumulatorAddName(names, CYJSString(name));
835 + (bool) cy$hasImplicitProperties {
841 /* Bridge: NSBlock {{{ */
848 /* Bridge: NSBoolNumber {{{ */
850 @implementation NSBoolNumber (Cycript)
852 - (JSType) cy$JSType {
853 return kJSTypeBoolean;
856 - (NSString *) cy$toCYON:(bool)objective {
857 NSString *value([self boolValue] ? @"true" : @"false");
858 return objective ? value : [NSString stringWithFormat:@"@%@", value];
861 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
862 return CYCastJSValue(context, (bool) [self boolValue]);
868 /* Bridge: NSDictionary {{{ */
869 @implementation NSDictionary (Cycript)
872 return [[self mutableCopy] autorelease];
875 - (NSString *) cy$toCYON:(bool)objective {
876 NSMutableString *json([[[NSMutableString alloc] init] autorelease]);
877 [json appendString:@"@{"];
881 for (NSObject *key in self) {
883 NSEnumerator *keys([self keyEnumerator]);
884 while (NSObject *key = [keys nextObject]) {
887 [json appendString:@","];
890 [json appendString:CYCastNSCYON(key, true)];
891 [json appendString:@":"];
892 NSObject *object([self objectForKey:key]);
893 [json appendString:CYCastNSCYON(object, true)];
896 [json appendString:@"}"];
900 - (bool) cy$hasProperty:(NSString *)name {
901 return [self objectForKey:name] != nil;
904 - (NSObject *) cy$getProperty:(NSString *)name {
905 return [self objectForKey:name];
908 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context {
909 [super cy$getPropertyNames:names inContext:context];
912 for (NSObject *key in self) {
914 NSEnumerator *keys([self keyEnumerator]);
915 while (NSObject *key = [keys nextObject]) {
917 JSPropertyNameAccumulatorAddName(names, CYJSString(context, key));
921 + (bool) cy$hasImplicitProperties {
927 /* Bridge: NSMutableArray {{{ */
928 @implementation NSMutableArray (Cycript)
930 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
931 if ([name isEqualToString:@"length"]) {
932 // XXX: is this not intelligent?
933 NSNumber *number(reinterpret_cast<NSNumber *>(value));
935 NSUInteger size([number unsignedIntegerValue]);
937 NSUInteger size([number unsignedIntValue]);
939 NSUInteger count([self count]);
941 [self removeObjectsInRange:NSMakeRange(size, count - size)];
942 else if (size != count) {
943 WebUndefined *undefined([WebUndefined undefined]);
944 for (size_t i(count); i != size; ++i)
945 [self addObject:undefined];
950 size_t index(CYGetIndex(name));
951 if (index == _not(size_t))
952 return [super cy$setProperty:name to:value];
954 id object(value ?: [NSNull null]);
956 size_t count([self count]);
958 [self replaceObjectAtIndex:index withObject:object];
960 if (index != count) {
961 WebUndefined *undefined([WebUndefined undefined]);
962 for (size_t i(count); i != index; ++i)
963 [self addObject:undefined];
966 [self addObject:object];
972 - (bool) cy$deleteProperty:(NSString *)name {
973 size_t index(CYGetIndex(name));
974 if (index == _not(size_t) || index >= [self count])
975 return [super cy$deleteProperty:name];
976 [self replaceObjectAtIndex:index withObject:[WebUndefined undefined]];
982 /* Bridge: NSMutableDictionary {{{ */
983 @implementation NSMutableDictionary (Cycript)
985 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
986 [self setObject:(value ?: [NSNull null]) forKey:name];
990 - (bool) cy$deleteProperty:(NSString *)name {
991 if ([self objectForKey:name] == nil)
994 [self removeObjectForKey:name];
1001 /* Bridge: NSNumber {{{ */
1002 @implementation NSNumber (Cycript)
1004 - (JSType) cy$JSType {
1006 // XXX: this just seems stupid
1007 if ([self class] == NSCFBoolean_)
1008 return kJSTypeBoolean;
1010 return kJSTypeNumber;
1013 - (NSString *) cy$toCYON:(bool)objective {
1014 NSString *value([self cy$JSType] != kJSTypeBoolean ? [self stringValue] : [self boolValue] ? @"true" : @"false");
1015 return objective ? value : [NSString stringWithFormat:@"@%@", value];
1018 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
1019 return [self cy$JSType] != kJSTypeBoolean ? CYCastJSValue(context, [self doubleValue]) : CYCastJSValue(context, static_cast<bool>([self boolValue]));
1020 } CYObjectiveCatch }
1024 /* Bridge: NSNull {{{ */
1025 @implementation NSNull (Cycript)
1027 - (JSType) cy$JSType {
1031 - (NSString *) cy$toCYON:(bool)objective {
1032 NSString *value(@"null");
1033 return objective ? value : [NSString stringWithFormat:@"@%@", value];
1036 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
1037 return CYJSNull(context);
1038 } CYObjectiveCatch }
1042 /* Bridge: NSObject {{{ */
1043 @implementation NSObject (Cycript)
1049 - (JSValueRef) cy$toJSON:(NSString *)key inContext:(JSContextRef)context {
1050 return [self cy$valueOfInContext:context];
1053 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
1055 } CYObjectiveCatch }
1057 - (JSType) cy$JSType {
1058 return kJSTypeObject;
1061 - (NSString *) cy$toCYON:(bool)objective {
1062 return [[self description] cy$toCYON:objective];
1065 - (bool) cy$hasProperty:(NSString *)name {
1069 - (NSObject *) cy$getProperty:(NSString *)name {
1073 - (JSValueRef) cy$getProperty:(NSString *)name inContext:(JSContextRef)context { CYObjectiveTry_(context) {
1074 if (NSObject *value = [self cy$getProperty:name])
1075 return CYCastJSValue(context, value);
1077 } CYObjectiveCatch }
1079 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
1083 - (bool) cy$deleteProperty:(NSString *)name {
1087 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context {
1090 + (bool) cy$hasImplicitProperties {
1096 /* Bridge: NSProxy {{{ */
1097 @implementation NSProxy (Cycript)
1099 - (NSString *) cy$toCYON:(bool)objective {
1100 return [[self description] cy$toCYON:objective];
1105 /* Bridge: NSString {{{ */
1106 @implementation NSString (Cycript)
1109 return [[self copy] autorelease];
1112 - (JSType) cy$JSType {
1113 return kJSTypeString;
1116 - (NSString *) cy$toCYON:(bool)objective {
1117 std::ostringstream str;
1120 CYUTF8String string(CYCastUTF8String(self));
1121 CYStringify(str, string.data, string.size);
1122 std::string value(str.str());
1123 return CYCastNSString(NULL, CYUTF8String(value.c_str(), value.size()));
1126 - (bool) cy$hasProperty:(NSString *)name {
1127 size_t index(CYGetIndex(name));
1128 if (index == _not(size_t) || index >= [self length])
1129 return [super cy$hasProperty:name];
1134 - (NSObject *) cy$getProperty:(NSString *)name {
1135 size_t index(CYGetIndex(name));
1136 if (index == _not(size_t) || index >= [self length])
1137 return [super cy$getProperty:name];
1139 return [self substringWithRange:NSMakeRange(index, 1)];
1142 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context {
1143 [super cy$getPropertyNames:names inContext:context];
1145 for (size_t index(0), length([self length]); index != length; ++index) {
1147 sprintf(name, "%zu", index);
1148 JSPropertyNameAccumulatorAddName(names, CYJSString(name));
1152 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
1153 return CYCastJSValue(context, CYJSString(context, self));
1154 } CYObjectiveCatch }
1158 /* Bridge: WebUndefined {{{ */
1159 @implementation WebUndefined (Cycript)
1161 - (JSType) cy$JSType {
1162 return kJSTypeUndefined;
1165 - (NSString *) cy$toCYON:(bool)objective {
1166 NSString *value(@"undefined");
1167 return value; // XXX: maybe use the below code, adding @undefined?
1168 //return objective ? value : [NSString stringWithFormat:@"@%@", value];
1171 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
1172 return CYJSUndefined(context);
1173 } CYObjectiveCatch }
1178 static bool CYIsClass(id self) {
1180 return class_isMetaClass(object_getClass(self));
1182 return GSObjCIsClass(self);
1186 Class CYCastClass(CYPool &pool, JSContextRef context, JSValueRef value) {
1187 id self(CYCastNSObject(&pool, context, value));
1188 if (CYIsClass(self))
1189 return (Class) self;
1190 throw CYJSError(context, "got something that is not a Class");
1194 NSArray *CYCastNSArray(JSContextRef context, JSPropertyNameArrayRef names) {
1196 size_t size(JSPropertyNameArrayGetCount(names));
1197 NSMutableArray *array([NSMutableArray arrayWithCapacity:size]);
1198 for (size_t index(0); index != size; ++index)
1199 [array addObject:CYCastNSString(&pool, context, JSPropertyNameArrayGetNameAtIndex(names, index))];
1203 JSValueRef CYCastJSValue(JSContextRef context, NSObject *value) { CYPoolTry {
1205 return CYJSNull(context);
1207 return CYMakeInstance(context, value, false);
1208 } CYPoolCatch(NULL) return /*XXX*/ NULL; }
1210 @implementation CYJSObject
1212 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { CYObjectiveTry {
1213 if ((self = [super init]) != nil) {
1215 context_ = CYGetJSContext(context);
1216 JSGlobalContextRetain(context_);
1217 JSValueProtect(context_, object_);
1219 } CYObjectiveCatch }
1221 - (void) dealloc { CYObjectiveTry {
1222 JSValueUnprotect(context_, object_);
1223 JSGlobalContextRelease(context_);
1225 } CYObjectiveCatch }
1227 - (NSString *) cy$toCYON:(bool)objective { CYObjectiveTry {
1229 JSValueRef exception(NULL);
1230 const char *cyon(CYPoolCCYON(pool, context_, object_));
1231 CYThrow(context_, exception);
1233 return [super cy$toCYON:objective];
1235 return [NSString stringWithUTF8String:cyon];
1236 } CYObjectiveCatch }
1238 - (NSUInteger) count { CYObjectiveTry {
1239 JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_));
1240 size_t size(JSPropertyNameArrayGetCount(names));
1241 JSPropertyNameArrayRelease(names);
1243 } CYObjectiveCatch }
1245 - (id) objectForKey:(id)key { CYObjectiveTry {
1246 JSValueRef value(CYGetProperty(context_, object_, CYJSString(context_, (NSObject *) key)));
1247 if (JSValueIsUndefined(context_, value))
1249 return CYCastNSObject(NULL, context_, value) ?: [NSNull null];
1250 } CYObjectiveCatch }
1252 - (NSEnumerator *) keyEnumerator { CYObjectiveTry {
1253 JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_));
1254 NSEnumerator *enumerator([CYCastNSArray(context_, names) objectEnumerator]);
1255 JSPropertyNameArrayRelease(names);
1257 } CYObjectiveCatch }
1259 - (void) setObject:(id)object forKey:(id)key { CYObjectiveTry {
1260 CYSetProperty(context_, object_, CYJSString(context_, (NSObject *) key), CYCastJSValue(context_, (NSString *) object));
1261 } CYObjectiveCatch }
1263 - (void) removeObjectForKey:(id)key { CYObjectiveTry {
1264 JSValueRef exception(NULL);
1265 (void) JSObjectDeleteProperty(context_, object_, CYJSString(context_, (NSObject *) key), &exception);
1266 CYThrow(context_, exception);
1267 } CYObjectiveCatch }
1271 @implementation CYJSArray
1273 - (NSString *) cy$toCYON:(bool)objective {
1275 return [NSString stringWithUTF8String:CYPoolCCYON(pool, context_, object_)];
1278 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { CYObjectiveTry {
1279 if ((self = [super init]) != nil) {
1281 context_ = CYGetJSContext(context);
1282 JSGlobalContextRetain(context_);
1283 JSValueProtect(context_, object_);
1285 } CYObjectiveCatch }
1287 - (void) dealloc { CYObjectiveTry {
1288 JSValueUnprotect(context_, object_);
1289 JSGlobalContextRelease(context_);
1291 } CYObjectiveCatch }
1293 - (NSUInteger) count { CYObjectiveTry {
1294 return CYArrayLength(context_, object_);
1295 } CYObjectiveCatch }
1297 - (id) objectAtIndex:(NSUInteger)index { CYObjectiveTry {
1298 size_t bounds([self count]);
1299 if (index >= bounds)
1300 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray objectAtIndex:]: index (%zu) beyond bounds (%zu)", static_cast<size_t>(index), bounds] userInfo:nil];
1301 JSValueRef exception(NULL);
1302 JSValueRef value(JSObjectGetPropertyAtIndex(context_, object_, index, &exception));
1303 CYThrow(context_, exception);
1304 return CYCastNSObject(NULL, context_, value) ?: [NSNull null];
1305 } CYObjectiveCatch }
1307 - (void) addObject:(id)object { CYObjectiveTry {
1308 CYArrayPush(context_, object_, CYCastJSValue(context_, (NSObject *) object));
1309 } CYObjectiveCatch }
1311 - (void) insertObject:(id)object atIndex:(NSUInteger)index { CYObjectiveTry {
1312 size_t bounds([self count] + 1);
1313 if (index >= bounds)
1314 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray insertObject:atIndex:]: index (%zu) beyond bounds (%zu)", static_cast<size_t>(index), bounds] userInfo:nil];
1315 JSValueRef exception(NULL);
1316 JSValueRef arguments[3];
1317 arguments[0] = CYCastJSValue(context_, index);
1318 arguments[1] = CYCastJSValue(context_, 0);
1319 arguments[2] = CYCastJSValue(context_, (NSObject *) object);
1320 JSObjectRef Array(CYGetCachedObject(context_, CYJSString("Array_prototype")));
1321 JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array, splice_s)), object_, 3, arguments, &exception);
1322 CYThrow(context_, exception);
1323 } CYObjectiveCatch }
1325 - (void) removeLastObject { CYObjectiveTry {
1326 JSValueRef exception(NULL);
1327 JSObjectRef Array(CYGetCachedObject(context_, CYJSString("Array_prototype")));
1328 JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array, pop_s)), object_, 0, NULL, &exception);
1329 CYThrow(context_, exception);
1330 } CYObjectiveCatch }
1332 - (void) removeObjectAtIndex:(NSUInteger)index { CYObjectiveTry {
1333 size_t bounds([self count]);
1334 if (index >= bounds)
1335 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray removeObjectAtIndex:]: index (%zu) beyond bounds (%zu)", static_cast<size_t>(index), bounds] userInfo:nil];
1336 JSValueRef exception(NULL);
1337 JSValueRef arguments[2];
1338 arguments[0] = CYCastJSValue(context_, index);
1339 arguments[1] = CYCastJSValue(context_, 1);
1340 JSObjectRef Array(CYGetCachedObject(context_, CYJSString("Array_prototype")));
1341 JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array, splice_s)), object_, 2, arguments, &exception);
1342 CYThrow(context_, exception);
1343 } CYObjectiveCatch }
1345 - (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)object { CYObjectiveTry {
1346 size_t bounds([self count]);
1347 if (index >= bounds)
1348 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray replaceObjectAtIndex:withObject:]: index (%zu) beyond bounds (%zu)", static_cast<size_t>(index), bounds] userInfo:nil];
1349 CYSetProperty(context_, object_, index, CYCastJSValue(context_, (NSObject *) object));
1350 } CYObjectiveCatch }
1354 // XXX: inherit from or replace with CYJSObject
1355 @interface CYInternal : NSObject {
1356 JSGlobalContextRef context_;
1357 JSObjectRef object_;
1362 @implementation CYInternal
1365 JSValueUnprotect(context_, object_);
1366 JSGlobalContextRelease(context_);
1370 - (id) initInContext:(JSContextRef)context {
1371 if ((self = [super init]) != nil) {
1372 context_ = CYGetJSContext(context);
1373 JSGlobalContextRetain(context_);
1377 - (bool) hasProperty:(JSStringRef)name inContext:(JSContextRef)context {
1378 if (object_ == NULL)
1381 return JSObjectHasProperty(context, object_, name);
1384 - (JSValueRef) getProperty:(JSStringRef)name inContext:(JSContextRef)context {
1385 if (object_ == NULL)
1388 return CYGetProperty(context, object_, name);
1391 - (void) setProperty:(JSStringRef)name toValue:(JSValueRef)value inContext:(JSContextRef)context {
1392 @synchronized (self) {
1393 if (object_ == NULL) {
1394 object_ = JSObjectMake(context, NULL, NULL);
1395 JSValueProtect(context, object_);
1399 CYSetProperty(context, object_, name, value);
1402 + (CYInternal *) get:(id)object {
1403 if ($objc_getAssociatedObject == NULL)
1406 @synchronized (object) {
1407 if (CYInternal *internal = $objc_getAssociatedObject(object, @selector(cy$internal)))
1414 + (CYInternal *) set:(id)object inContext:(JSContextRef)context {
1415 if ($objc_getAssociatedObject == NULL)
1418 @synchronized (object) {
1419 if (CYInternal *internal = $objc_getAssociatedObject(object, @selector(cy$internal)))
1422 if ($objc_setAssociatedObject == NULL)
1425 CYInternal *internal([[[CYInternal alloc] initInContext:context] autorelease]);
1426 objc_setAssociatedObject(object, @selector(cy$internal), internal, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
1435 static JSObjectRef CYMakeSelector(JSContextRef context, SEL sel) {
1436 Selector_privateData *internal(new Selector_privateData(sel));
1437 return JSObjectMake(context, Selector_, internal);
1440 static SEL CYCastSEL(JSContextRef context, JSValueRef value) {
1441 if (JSValueIsObjectOfClass(context, value, Selector_)) {
1442 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate((JSObjectRef) value)));
1443 return reinterpret_cast<SEL>(internal->value_);
1445 return CYCastPointer<SEL>(context, value);
1448 void *CYObjectiveC_ExecuteStart(JSContextRef context) { CYSadTry {
1449 return (void *) [[NSAutoreleasePool alloc] init];
1450 } CYSadCatch(NULL) }
1452 void CYObjectiveC_ExecuteEnd(JSContextRef context, void *handle) { CYSadTry {
1453 return [(NSAutoreleasePool *) handle release];
1456 JSValueRef CYObjectiveC_RuntimeProperty(JSContextRef context, CYUTF8String name) { CYPoolTry {
1458 return Instance::Make(context, nil);
1459 if (Class _class = objc_getClass(name.data))
1460 return CYMakeInstance(context, _class, true);
1461 if (Protocol *protocol = objc_getProtocol(name.data))
1462 return CYMakeInstance(context, protocol, true);
1464 } CYPoolCatch(NULL) return /*XXX*/ NULL; }
1466 static void CYObjectiveC_CallFunction(JSContextRef context, ffi_cif *cif, void (*function)(), uint8_t *value, void **values) { CYSadTry {
1467 ffi_call(cif, function, value, values);
1470 static bool CYObjectiveC_PoolFFI(CYPool *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { CYSadTry {
1471 switch (type->primitive) {
1472 // XXX: do something epic about blocks
1475 case sig::typename_P:
1476 // XXX: this works for return values, but not for properties and fields
1477 *reinterpret_cast<id *>(data) = CYCastNSObject(pool, context, value);
1480 case sig::selector_P:
1481 *reinterpret_cast<SEL *>(data) = CYCastSEL(context, value);
1489 } CYSadCatch(false) }
1491 static JSValueRef CYObjectiveC_FromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) { CYPoolTry {
1492 switch (type->primitive) {
1493 // XXX: do something epic about blocks
1496 if (NSObject *object = *reinterpret_cast<NSObject **>(data)) {
1497 JSValueRef value(CYCastJSValue(context, object));
1503 case sig::typename_P:
1504 return CYMakeInstance(context, *reinterpret_cast<Class *>(data), true);
1506 case sig::selector_P:
1507 if (SEL sel = *reinterpret_cast<SEL *>(data))
1508 return CYMakeSelector(context, sel);
1512 return CYJSNull(context);
1516 } CYPoolCatch(NULL) return /*XXX*/ NULL; }
1518 static bool CYImplements(id object, Class _class, SEL selector, bool devoid = false) {
1519 if (objc_method *method = class_getInstanceMethod(_class, selector)) {
1522 #if OBJC_API_VERSION >= 2
1524 method_getReturnType(method, type, sizeof(type));
1526 const char *type(method_getTypeEncoding(method));
1532 // XXX: possibly use a more "awesome" check?
1536 static const char *CYPoolTypeEncoding(CYPool &pool, JSContextRef context, SEL sel, objc_method *method) {
1538 return method_getTypeEncoding(method);
1540 const char *name(sel_getName(sel));
1541 size_t length(strlen(name));
1543 char keyed[length + 2];
1545 keyed[length + 1] = '\0';
1546 memcpy(keyed + 1, name, length);
1548 if (CYBridgeEntry *entry = CYBridgeHash(keyed, length + 1))
1549 return entry->value_;
1554 static JSValueRef MessageAdapter_(JSContextRef context, size_t count, JSValueRef values[], JSObjectRef function) {
1555 JSObjectRef _this(CYCastJSObject(context, values[0]));
1556 return CYCallAsFunction(context, function, _this, count - 2, values + 2);
1559 static void MessageClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) {
1560 CYExecuteClosure(cif, result, arguments, arg, &MessageAdapter_);
1563 static JSObjectRef CYMakeMessage(JSContextRef context, SEL sel, IMP imp, const char *type) {
1564 Message_privateData *internal(new Message_privateData(sel, type, imp));
1565 return JSObjectMake(context, Message_, internal);
1568 static IMP CYMakeMessage(JSContextRef context, JSValueRef value, const char *type) {
1569 JSObjectRef function(CYCastJSObject(context, value));
1570 Closure_privateData *internal(CYMakeFunctor_(context, function, type, &MessageClosure_));
1571 // XXX: see notes in Library.cpp about needing to leak
1572 return reinterpret_cast<IMP>(internal->GetValue());
1575 static bool Messages_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
1576 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1577 Class _class(internal->GetValue());
1580 const char *name(CYPoolCString(pool, context, property));
1582 if (SEL sel = sel_getUid(name))
1583 if (class_getInstanceMethod(_class, sel) != NULL)
1589 static JSValueRef Messages_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
1590 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1591 Class _class(internal->GetValue());
1594 const char *name(CYPoolCString(pool, context, property));
1596 if (SEL sel = sel_getUid(name))
1597 if (objc_method *method = class_getInstanceMethod(_class, sel))
1598 return CYMakeMessage(context, sel, method_getImplementation(method), method_getTypeEncoding(method));
1603 static bool Messages_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
1604 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1605 Class _class(internal->GetValue());
1608 const char *name(CYPoolCString(pool, context, property));
1610 SEL sel(sel_registerName(name));
1612 objc_method *method(class_getInstanceMethod(_class, sel));
1617 if (JSValueIsObjectOfClass(context, value, Message_)) {
1618 Message_privateData *message(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate((JSObjectRef) value)));
1619 type = sig::Unparse(pool, &message->signature_);
1620 imp = reinterpret_cast<IMP>(message->GetValue());
1622 type = CYPoolTypeEncoding(pool, context, sel, method);
1623 imp = CYMakeMessage(context, value, type);
1627 method_setImplementation(method, imp);
1630 GSMethodList list(GSAllocMethodList(1));
1631 GSAppendMethodToList(list, sel, type, imp, YES);
1632 GSAddMethodList(_class, list, YES);
1633 GSFlushMethodCacheForClass(_class);
1635 class_addMethod(_class, sel, imp, type);
1642 #if 0 && OBJC_API_VERSION < 2
1643 static bool Messages_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
1644 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1645 Class _class(internal->GetValue());
1648 const char *name(CYPoolCString(pool, context, property));
1650 if (SEL sel = sel_getUid(name))
1651 if (objc_method *method = class_getInstanceMethod(_class, sel)) {
1652 objc_method_list list = {NULL, 1, {method}};
1653 class_removeMethods(_class, &list);
1661 static void Messages_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1662 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1663 Class _class(internal->GetValue());
1665 #if OBJC_API_VERSION >= 2
1667 objc_method **data(class_copyMethodList(_class, &size));
1668 for (size_t i(0); i != size; ++i)
1669 JSPropertyNameAccumulatorAddName(names, CYJSString(sel_getName(method_getName(data[i]))));
1672 for (objc_method_list *methods(_class->methods); methods != NULL; methods = methods->method_next)
1673 for (int i(0); i != methods->method_count; ++i)
1674 JSPropertyNameAccumulatorAddName(names, CYJSString(sel_getName(method_getName(&methods->method_list[i]))));
1678 static bool CYHasImplicitProperties(Class _class) {
1679 // XXX: this is an evil hack to deal with NSProxy; fix elsewhere
1680 if (!CYImplements(_class, object_getClass(_class), @selector(cy$hasImplicitProperties)))
1682 return [_class cy$hasImplicitProperties];
1685 static bool Instance_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
1686 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1687 id self(internal->GetValue());
1689 if (JSStringIsEqualToUTF8CString(property, "$cyi"))
1693 NSString *name(CYCastNSString(&pool, context, property));
1695 if (CYInternal *internal = [CYInternal get:self])
1696 if ([internal hasProperty:property inContext:context])
1699 Class _class(object_getClass(self));
1702 // XXX: this is an evil hack to deal with NSProxy; fix elsewhere
1703 if (CYImplements(self, _class, @selector(cy$hasProperty:)))
1704 if ([self cy$hasProperty:name])
1706 } CYPoolCatch(false)
1708 const char *string(CYPoolCString(pool, context, name));
1711 if (class_getProperty(_class, string) != NULL)
1715 if (CYHasImplicitProperties(_class))
1716 if (SEL sel = sel_getUid(string))
1717 if (CYImplements(self, _class, sel, true))
1723 static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1724 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1725 id self(internal->GetValue());
1727 if (JSStringIsEqualToUTF8CString(property, "$cyi"))
1728 return Internal::Make(context, self, object);
1731 NSString *name(CYCastNSString(&pool, context, property));
1733 if (CYInternal *internal = [CYInternal get:self])
1734 if (JSValueRef value = [internal getProperty:property inContext:context])
1738 if (JSValueRef value = [self cy$getProperty:name inContext:context])
1742 const char *string(CYPoolCString(pool, context, name));
1743 Class _class(object_getClass(self));
1746 if (objc_property_t property = class_getProperty(_class, string)) {
1747 PropertyAttributes attributes(property);
1748 SEL sel(sel_registerName(attributes.Getter()));
1749 return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception);
1753 if (CYHasImplicitProperties(_class))
1754 if (SEL sel = sel_getUid(string))
1755 if (CYImplements(self, _class, sel, true))
1756 return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception);
1761 static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
1762 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1763 id self(internal->GetValue());
1767 NSString *name(CYCastNSString(&pool, context, property));
1768 NSObject *data(CYCastNSObject(&pool, context, value));
1771 if ([self cy$setProperty:name to:data])
1773 } CYPoolCatch(false)
1775 const char *string(CYPoolCString(pool, context, name));
1776 Class _class(object_getClass(self));
1779 if (objc_property_t property = class_getProperty(_class, string)) {
1780 PropertyAttributes attributes(property);
1781 if (const char *setter = attributes.Setter()) {
1782 SEL sel(sel_registerName(setter));
1783 JSValueRef arguments[1] = {value};
1784 CYSendMessage(pool, context, self, NULL, sel, 1, arguments, false, exception);
1790 size_t length(strlen(string));
1792 char set[length + 5];
1798 if (string[0] != '\0') {
1799 set[3] = toupper(string[0]);
1800 memcpy(set + 4, string + 1, length - 1);
1803 set[length + 3] = ':';
1804 set[length + 4] = '\0';
1806 if (SEL sel = sel_getUid(set))
1807 if (CYImplements(self, _class, sel)) {
1808 JSValueRef arguments[1] = {value};
1809 CYSendMessage(pool, context, self, NULL, sel, 1, arguments, false, exception);
1813 if (CYInternal *internal = [CYInternal set:self inContext:context]) {
1814 [internal setProperty:property toValue:value inContext:context];
1821 static bool Instance_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1822 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1823 id self(internal->GetValue());
1826 NSString *name(CYCastNSString(NULL, context, property));
1827 return [self cy$deleteProperty:name];
1828 } CYPoolCatch(false)
1829 } CYCatch(false) return /*XXX*/ false; }
1831 static void Instance_getPropertyNames_message(JSPropertyNameAccumulatorRef names, objc_method *method) {
1832 const char *name(sel_getName(method_getName(method)));
1833 if (strchr(name, ':') != NULL)
1836 const char *type(method_getTypeEncoding(method));
1837 if (type == NULL || *type == '\0' || *type == 'v')
1840 JSPropertyNameAccumulatorAddName(names, CYJSString(name));
1843 static void Instance_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1844 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1845 id self(internal->GetValue());
1848 Class _class(object_getClass(self));
1853 objc_property_t *data(class_copyPropertyList(_class, &size));
1854 for (size_t i(0); i != size; ++i)
1855 JSPropertyNameAccumulatorAddName(names, CYJSString(property_getName(data[i])));
1860 if (CYHasImplicitProperties(_class))
1861 for (Class current(_class); current != nil; current = class_getSuperclass(current)) {
1862 #if OBJC_API_VERSION >= 2
1864 objc_method **data(class_copyMethodList(current, &size));
1865 for (size_t i(0); i != size; ++i)
1866 Instance_getPropertyNames_message(names, data[i]);
1869 for (objc_method_list *methods(current->methods); methods != NULL; methods = methods->method_next)
1870 for (int i(0); i != methods->method_count; ++i)
1871 Instance_getPropertyNames_message(names, &methods->method_list[i]);
1876 // XXX: this is an evil hack to deal with NSProxy; fix elsewhere
1877 if (CYImplements(self, _class, @selector(cy$getPropertyNames:inContext:)))
1878 [self cy$getPropertyNames:names inContext:context];
1882 static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1883 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1884 JSObjectRef value(Instance::Make(context, [internal->GetValue() alloc], Instance::Uninitialized));
1888 static JSValueRef Instance_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1889 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1890 id self(internal->GetValue());
1892 if (![self isKindOfClass:NSBlock_])
1893 CYThrow("non-NSBlock object is not a function");
1894 // XXX: replace above logic with the following assertion
1895 //_assert([self isKindOfClass:NSBlock_]);
1896 // to do this, make it so FunctionInstance_ is the class of blocks
1897 // to do /that/, generalize the various "is exactly Instance_" checks
1898 // then, move Instance_callAsFunction to only be on FunctionInstance
1900 BlockLiteral *literal(reinterpret_cast<BlockLiteral *>(self));
1902 if ((literal->flags & BLOCK_HAS_SIGNATURE) != 0) {
1903 uint8_t *descriptor(reinterpret_cast<uint8_t *>(literal->descriptor));
1904 descriptor += sizeof(BlockDescriptor1);
1905 if ((literal->flags & BLOCK_HAS_COPY_DISPOSE) != 0)
1906 descriptor += sizeof(BlockDescriptor2);
1907 BlockDescriptor3 *descriptor3(reinterpret_cast<BlockDescriptor3 *>(descriptor));
1909 if (const char *type = descriptor3->signature) {
1915 sig::Signature signature;
1916 sig::Parse(pool, &signature, type, &Structor_);
1919 sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif);
1921 void (*function)() = reinterpret_cast<void (*)()>(literal->invoke);
1922 return CYCallFunction(pool, context, 1, setup, count, arguments, false, exception, &signature, &cif, function);
1927 CYThrow("NSBlock without signature field passed arguments");
1931 } CYPoolCatch(NULL);
1936 static bool Instance_hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef instance, JSValueRef *exception) { CYTry {
1937 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) constructor)));
1938 Class _class(internal->GetValue());
1939 if (!CYIsClass(_class))
1942 if (CYJSValueIsNSObject(context, instance)) {
1943 Instance *linternal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) instance)));
1944 // XXX: this isn't always safe
1945 return [linternal->GetValue() isKindOfClass:_class];
1951 static JSValueRef Instance_box_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1953 throw CYJSError(context, "incorrect number of arguments to Instance");
1955 id value(CYCastNSObject(&pool, context, arguments[0]));
1957 value = [NSNull null];
1958 return CYCastJSValue(context, [value cy$box]);
1961 static bool Internal_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
1962 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1965 id self(internal->GetValue());
1966 const char *name(CYPoolCString(pool, context, property));
1968 if (object_getInstanceVariable(self, name, NULL) != NULL)
1974 static JSValueRef Internal_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1975 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1978 id self(internal->GetValue());
1979 const char *name(CYPoolCString(pool, context, property));
1981 if (objc_ivar *ivar = object_getInstanceVariable(self, name, NULL)) {
1982 Type_privateData type(pool, ivar_getTypeEncoding(ivar));
1983 // XXX: if this fails and throws an exception the person we are throwing it to gets the wrong exception
1984 return CYFromFFI(context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar));
1990 static bool Internal_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
1991 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1994 id self(internal->GetValue());
1995 const char *name(CYPoolCString(pool, context, property));
1997 if (objc_ivar *ivar = object_getInstanceVariable(self, name, NULL)) {
1998 Type_privateData type(pool, ivar_getTypeEncoding(ivar));
1999 CYPoolFFI(&pool, context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar), value);
2006 static void Internal_getPropertyNames_(Class _class, JSPropertyNameAccumulatorRef names) {
2007 if (Class super = class_getSuperclass(_class))
2008 Internal_getPropertyNames_(super, names);
2010 #if OBJC_API_VERSION >= 2
2012 objc_ivar **data(class_copyIvarList(_class, &size));
2013 for (size_t i(0); i != size; ++i)
2014 JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(data[i])));
2017 if (objc_ivar_list *ivars = _class->ivars)
2018 for (int i(0); i != ivars->ivar_count; ++i)
2019 JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(&ivars->ivar_list[i])));
2023 static void Internal_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
2024 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
2027 id self(internal->GetValue());
2028 Class _class(object_getClass(self));
2030 Internal_getPropertyNames_(_class, names);
2033 static JSValueRef Internal_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2034 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
2035 return internal->GetOwner();
2038 static JSValueRef ObjectiveC_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2040 NSString *name(CYCastNSString(&pool, context, property));
2041 if (Class _class = NSClassFromString(name))
2042 return CYMakeInstance(context, _class, true);
2046 static void ObjectiveC_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
2048 size_t size(objc_getClassList(NULL, 0));
2049 Class *data(reinterpret_cast<Class *>(malloc(sizeof(Class) * size)));
2052 size_t writ(objc_getClassList(data, size));
2055 if (Class *copy = reinterpret_cast<Class *>(realloc(data, sizeof(Class) * writ))) {
2061 for (size_t i(0); i != writ; ++i)
2062 JSPropertyNameAccumulatorAddName(names, CYJSString(class_getName(data[i])));
2068 while (Class _class = objc_next_class(&state))
2069 JSPropertyNameAccumulatorAddName(names, CYJSString(class_getName(_class)));
2073 #if OBJC_API_VERSION >= 2
2074 static JSValueRef ObjectiveC_Image_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2075 const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object)));
2078 const char *name(CYPoolCString(pool, context, property));
2080 const char **data(objc_copyClassNamesForImage(internal, &size));
2082 for (size_t i(0); i != size; ++i)
2083 if (strcmp(name, data[i]) == 0) {
2084 if (Class _class = objc_getClass(name)) {
2085 value = CYMakeInstance(context, _class, true);
2096 static void ObjectiveC_Image_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
2097 const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object)));
2099 const char **data(objc_copyClassNamesForImage(internal, &size));
2100 for (size_t i(0); i != size; ++i)
2101 JSPropertyNameAccumulatorAddName(names, CYJSString(data[i]));
2105 static JSValueRef ObjectiveC_Images_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2107 const char *name(CYPoolCString(pool, context, property));
2109 const char **data(objc_copyImageNames(&size));
2110 for (size_t i(0); i != size; ++i)
2111 if (strcmp(name, data[i]) == 0) {
2120 JSObjectRef value(JSObjectMake(context, NULL, NULL));
2121 CYSetProperty(context, value, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Image_Classes_, const_cast<char *>(name)));
2125 static void ObjectiveC_Images_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
2127 const char **data(objc_copyImageNames(&size));
2128 for (size_t i(0); i != size; ++i)
2129 JSPropertyNameAccumulatorAddName(names, CYJSString(data[i]));
2134 static JSValueRef ObjectiveC_Protocols_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2136 const char *name(CYPoolCString(pool, context, property));
2137 if (Protocol *protocol = objc_getProtocol(name))
2138 return CYMakeInstance(context, protocol, true);
2142 static void ObjectiveC_Protocols_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
2143 #if OBJC_API_VERSION >= 2
2145 Protocol **data(objc_copyProtocolList(&size));
2146 for (size_t i(0); i != size; ++i)
2147 JSPropertyNameAccumulatorAddName(names, CYJSString(protocol_getName(data[i])));
2154 static JSValueRef ObjectiveC_Constants_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2156 CYUTF8String name(CYPoolUTF8String(pool, context, property));
2158 return Instance::Make(context, nil);
2162 static void ObjectiveC_Constants_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
2163 JSPropertyNameAccumulatorAddName(names, CYJSString("nil"));
2167 #if defined(__i386__) || defined(__x86_64__)
2168 #define OBJC_MAX_STRUCT_BY_VALUE 8
2169 static int struct_forward_array[] = {
2170 0, 0, 0, 1, 0, 1, 1, 1, 0 };
2171 #elif defined(__arm__)
2172 #define OBJC_MAX_STRUCT_BY_VALUE 1
2173 static int struct_forward_array[] = {
2176 #error missing objc-runtime-info
2179 static bool stret(ffi_type *ffi_type) {
2180 return ffi_type->type == FFI_TYPE_STRUCT && (
2181 ffi_type->size > OBJC_MAX_STRUCT_BY_VALUE ||
2182 struct_forward_array[ffi_type->size] != 0
2187 JSValueRef CYSendMessage(CYPool &pool, JSContextRef context, id self, Class _class, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception) { CYTry {
2191 _class = object_getClass(self);
2195 if (objc_method *method = class_getInstanceMethod(_class, _cmd)) {
2196 imp = method_getImplementation(method);
2197 type = method_getTypeEncoding(method);
2202 NSMethodSignature *method([self methodSignatureForSelector:_cmd]);
2204 throw CYJSError(context, "unrecognized selector %s sent to object %p", sel_getName(_cmd), self);
2205 type = CYPoolCString(pool, context, [method _typeString]);
2213 sig::Signature signature;
2214 sig::Parse(pool, &signature, type, &Structor_);
2216 size_t used(count + 3);
2217 if (used > signature.count) {
2218 sig::Element *elements(new (pool) sig::Element[used]);
2219 memcpy(elements, signature.elements, used * sizeof(sig::Element));
2221 for (size_t index(signature.count); index != used; ++index) {
2222 sig::Element *element(&elements[index]);
2223 element->name = NULL;
2224 element->offset = _not(size_t);
2226 sig::Type *type(new (pool) sig::Type);
2227 memset(type, 0, sizeof(*type));
2228 type->primitive = sig::object_P;
2229 element->type = type;
2232 signature.elements = elements;
2233 signature.count = used;
2237 sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif);
2241 if (stret(cif.rtype))
2242 imp = class_getMethodImplementation_stret(_class, _cmd);
2244 imp = class_getMethodImplementation(_class, _cmd);
2246 objc_super super = {self, _class};
2247 imp = objc_msg_lookup_super(&super, _cmd);
2251 void (*function)() = reinterpret_cast<void (*)()>(imp);
2252 return CYCallFunction(pool, context, 2, setup, count, arguments, initialize, exception, &signature, &cif, function);
2255 static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2257 throw CYJSError(context, "too few arguments to objc_msgSend");
2267 if (JSValueIsObjectOfClass(context, arguments[0], Super_)) {
2268 cy::Super *internal(reinterpret_cast<cy::Super *>(JSObjectGetPrivate((JSObjectRef) arguments[0])));
2269 self = internal->GetValue();
2270 _class = internal->class_;;
2271 uninitialized = false;
2272 } else if (CYJSValueIsNSObject(context, arguments[0])) {
2273 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) arguments[0])));
2274 self = internal->GetValue();
2276 uninitialized = internal->IsUninitialized();
2278 internal->value_ = nil;
2280 self = CYCastNSObject(&pool, context, arguments[0]);
2282 uninitialized = false;
2286 return CYJSNull(context);
2288 _cmd = CYCastSEL(context, arguments[1]);
2290 return CYSendMessage(pool, context, self, _class, _cmd, count - 2, arguments + 2, uninitialized, exception);
2293 static JSValueRef Selector_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2294 JSValueRef setup[count + 2];
2297 memcpy(setup + 2, arguments, sizeof(JSValueRef) * count);
2298 return $objc_msgSend(context, NULL, NULL, count + 2, setup, exception);
2301 static JSValueRef Message_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2303 Message_privateData *internal(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate(object)));
2305 // XXX: handle Instance::Uninitialized?
2306 id self(CYCastNSObject(&pool, context, _this));
2310 setup[1] = &internal->sel_;
2312 return CYCallFunction(pool, context, 2, setup, count, arguments, false, exception, &internal->signature_, &internal->cif_, internal->GetValue());
2315 static JSObjectRef Super_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2317 throw CYJSError(context, "incorrect number of arguments to Super constructor");
2319 id self(CYCastNSObject(&pool, context, arguments[0]));
2320 Class _class(CYCastClass(pool, context, arguments[1]));
2321 return cy::Super::Make(context, self, _class);
2324 static JSObjectRef Selector_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2326 throw CYJSError(context, "incorrect number of arguments to Selector constructor");
2328 const char *name(CYPoolCString(pool, context, arguments[0]));
2329 return CYMakeSelector(context, sel_registerName(name));
2332 static JSObjectRef Instance_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2334 throw CYJSError(context, "incorrect number of arguments to Instance constructor");
2335 id self(count == 0 ? nil : CYCastPointer<id>(context, arguments[0]));
2336 return CYMakeInstance(context, self, false);
2339 static JSValueRef CYValue_getProperty_value(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
2340 CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(object)));
2341 return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_));
2344 static JSValueRef CYValue_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2345 CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this)));
2346 Type_privateData *typical(internal->GetType());
2351 if (typical == NULL) {
2355 type = typical->type_;
2356 ffi = typical->ffi_;
2359 return CYMakePointer(context, &internal->value_, _not(size_t), type, ffi, object);
2362 static JSValueRef Instance_getProperty_constructor(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
2363 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
2364 return Instance::Make(context, (id) object_getClass(internal->GetValue()));
2367 static JSValueRef Instance_getProperty_prototype(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2368 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
2369 id self(internal->GetValue());
2370 if (!CYIsClass(self))
2371 return CYJSUndefined(context);
2372 return CYGetClassPrototype(context, self);
2375 static JSValueRef Instance_getProperty_messages(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
2376 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
2377 id self(internal->GetValue());
2378 if (!CYIsClass(self))
2379 return CYJSUndefined(context);
2380 return Messages::Make(context, (Class) self);
2383 static JSValueRef Instance_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2384 if (!CYJSValueIsNSObject(context, _this))
2387 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2388 return CYCastJSValue(context, CYJSString(context, CYCastNSCYON(internal->GetValue(), false)));
2391 static JSValueRef Instance_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2392 if (!CYJSValueIsNSObject(context, _this))
2395 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2396 id value(internal->GetValue());
2403 key = CYCastNSString(NULL, context, CYJSString(context, arguments[0]));
2405 if (!CYImplements(value, object_getClass(value), @selector(cy$toJSON:inContext:)))
2406 return CYJSUndefined(context);
2407 else if (JSValueRef json = [value cy$toJSON:key inContext:context])
2410 return CYCastJSValue(context, CYJSString(context, [value description]));
2412 } CYCatch(NULL) return /*XXX*/ NULL; }
2414 static JSValueRef Instance_callAsFunction_valueOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2415 if (!CYJSValueIsNSObject(context, _this))
2418 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2419 id value(internal->GetValue());
2421 if (![value respondsToSelector:@selector(cy$valueOfInContext:)])
2424 if (JSValueRef result = [value cy$valueOfInContext:context])
2428 } CYCatch(NULL) return /*XXX*/ NULL; }
2430 static JSValueRef Instance_callAsFunction_toPointer(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2431 if (!CYJSValueIsNSObject(context, _this))
2434 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2435 // XXX: but... but... THIS ISN'T A POINTER! :(
2436 return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->GetValue()));
2437 } CYCatch(NULL) return /*XXX*/ NULL; }
2439 static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2440 if (!CYJSValueIsNSObject(context, _this))
2443 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2444 id value(internal->GetValue());
2447 // XXX: this seems like a stupid implementation; what if it crashes? why not use the CYONifier backend?
2448 return CYCastJSValue(context, CYJSString(context, [value description]));
2450 } CYCatch(NULL) return /*XXX*/ NULL; }
2452 static JSValueRef Class_callAsFunction_pointerTo(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2453 if (!CYJSValueIsNSObject(context, _this))
2456 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2457 id value(internal->GetValue());
2459 if (!CYIsClass(value))
2460 CYThrow("non-Class object cannot be used as Type");
2462 // XXX: this is a very silly implementation
2464 std::ostringstream type;
2466 type << class_getName(value);
2470 return CYMakeType(context, type.str().c_str());
2472 } CYCatch(NULL) return /*XXX*/ NULL; }
2474 static JSValueRef Selector_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2475 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
2476 return CYCastJSValue(context, sel_getName(internal->GetValue()));
2479 static JSValueRef Selector_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2480 return Selector_callAsFunction_toString(context, object, _this, count, arguments, exception);
2483 static JSValueRef Selector_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2484 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
2485 const char *name(sel_getName(internal->GetValue()));
2488 NSString *string([NSString stringWithFormat:@"@selector(%s)", name]);
2489 return CYCastJSValue(context, CYJSString(context, string));
2491 } CYCatch(NULL) return /*XXX*/ NULL; }
2493 static JSValueRef Selector_callAsFunction_type(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2495 throw CYJSError(context, "incorrect number of arguments to Selector.type");
2498 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
2499 SEL sel(internal->GetValue());
2501 objc_method *method;
2502 if (Class _class = CYCastClass(pool, context, arguments[0]))
2503 method = class_getInstanceMethod(_class, sel);
2507 if (const char *type = CYPoolTypeEncoding(pool, context, sel, method))
2508 return CYCastJSValue(context, CYJSString(type));
2510 return CYJSNull(context);
2513 static JSStaticValue Selector_staticValues[2] = {
2514 {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete},
2515 {NULL, NULL, NULL, 0}
2518 static JSStaticValue Instance_staticValues[5] = {
2519 {"constructor", &Instance_getProperty_constructor, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2520 {"messages", &Instance_getProperty_messages, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2521 {"prototype", &Instance_getProperty_prototype, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2522 {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2523 {NULL, NULL, NULL, 0}
2526 static JSStaticFunction Instance_staticFunctions[7] = {
2527 {"$cya", &CYValue_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2528 {"toCYON", &Instance_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2529 {"toJSON", &Instance_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2530 {"valueOf", &Instance_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2531 {"toPointer", &Instance_callAsFunction_toPointer, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2532 {"toString", &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2536 static JSStaticFunction Class_staticFunctions[2] = {
2537 {"pointerTo", &Class_callAsFunction_pointerTo, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2541 static JSStaticFunction Internal_staticFunctions[2] = {
2542 {"$cya", &Internal_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2546 static JSStaticFunction Selector_staticFunctions[5] = {
2547 {"toCYON", &Selector_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2548 {"toJSON", &Selector_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2549 {"toString", &Selector_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2550 {"type", &Selector_callAsFunction_type, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2555 JSValueRef NSCFType$cy$toJSON$inContext$(id self, SEL sel, JSValueRef key, JSContextRef context) { CYObjectiveTry_(context) {
2556 return CYCastJSValue(context, [(NSString *) CFCopyDescription((CFTypeRef) self) autorelease]);
2557 } CYObjectiveCatch }
2560 void CYObjectiveC_Initialize() { /*XXX*/ JSContextRef context(NULL); CYPoolTry {
2561 $objc_setAssociatedObject = reinterpret_cast<void (*)(id, void *, id value, objc_AssociationPolicy)>(dlsym(RTLD_DEFAULT, "objc_setAssociatedObject"));
2562 $objc_getAssociatedObject = reinterpret_cast<id (*)(id, void *)>(dlsym(RTLD_DEFAULT, "objc_getAssociatedObject"));
2563 $objc_removeAssociatedObjects = reinterpret_cast<void (*)(id)>(dlsym(RTLD_DEFAULT, "objc_removeAssociatedObjects"));
2565 CYPool &pool(CYGetGlobalPool());
2567 Object_type = new(pool) Type_privateData("@");
2568 Selector_type = new(pool) Type_privateData(":");
2571 // XXX: apparently, iOS now has both of these
2572 NSCFBoolean_ = objc_getClass("__NSCFBoolean");
2573 if (NSCFBoolean_ == nil)
2574 NSCFBoolean_ = objc_getClass("NSCFBoolean");
2576 NSCFType_ = objc_getClass("NSCFType");
2577 NSGenericDeallocHandler_ = objc_getClass("__NSGenericDeallocHandler");
2578 NSMessageBuilder_ = objc_getClass("NSMessageBuilder");
2579 NSZombie_ = objc_getClass("_NSZombie_");
2581 NSBoolNumber_ = objc_getClass("NSBoolNumber");
2584 NSArray_ = objc_getClass("NSArray");
2585 NSBlock_ = objc_getClass("NSBlock");
2586 NSDictionary_ = objc_getClass("NSDictionary");
2587 NSString_ = objc_getClass("NSString");
2588 Object_ = objc_getClass("Object");
2590 JSClassDefinition definition;
2592 definition = kJSClassDefinitionEmpty;
2593 definition.className = "Instance";
2594 definition.staticValues = Instance_staticValues;
2595 definition.staticFunctions = Instance_staticFunctions;
2596 definition.hasProperty = &Instance_hasProperty;
2597 definition.getProperty = &Instance_getProperty;
2598 definition.setProperty = &Instance_setProperty;
2599 definition.deleteProperty = &Instance_deleteProperty;
2600 definition.getPropertyNames = &Instance_getPropertyNames;
2601 definition.callAsConstructor = &Instance_callAsConstructor;
2602 definition.callAsFunction = &Instance_callAsFunction;
2603 definition.hasInstance = &Instance_hasInstance;
2604 definition.finalize = &CYFinalize;
2605 Instance_ = JSClassCreate(&definition);
2607 definition.className = "ArrayInstance";
2608 ArrayInstance_ = JSClassCreate(&definition);
2610 definition.className = "FunctionInstance";
2611 FunctionInstance_ = JSClassCreate(&definition);
2613 definition.className = "ObjectInstance";
2614 ObjectInstance_ = JSClassCreate(&definition);
2616 definition.className = "StringInstance";
2617 StringInstance_ = JSClassCreate(&definition);
2619 definition = kJSClassDefinitionEmpty;
2620 definition.className = "Class";
2621 definition.staticFunctions = Class_staticFunctions;
2622 Class_ = JSClassCreate(&definition);
2624 definition = kJSClassDefinitionEmpty;
2625 definition.className = "Internal";
2626 definition.staticFunctions = Internal_staticFunctions;
2627 definition.hasProperty = &Internal_hasProperty;
2628 definition.getProperty = &Internal_getProperty;
2629 definition.setProperty = &Internal_setProperty;
2630 definition.getPropertyNames = &Internal_getPropertyNames;
2631 definition.finalize = &CYFinalize;
2632 Internal_ = JSClassCreate(&definition);
2634 definition = kJSClassDefinitionEmpty;
2635 definition.className = "Message";
2636 definition.staticFunctions = cy::Functor::StaticFunctions;
2637 definition.callAsFunction = &Message_callAsFunction;
2638 definition.finalize = &CYFinalize;
2639 Message_ = JSClassCreate(&definition);
2641 definition = kJSClassDefinitionEmpty;
2642 definition.className = "Messages";
2643 definition.hasProperty = &Messages_hasProperty;
2644 definition.getProperty = &Messages_getProperty;
2645 definition.setProperty = &Messages_setProperty;
2646 #if 0 && OBJC_API_VERSION < 2
2647 definition.deleteProperty = &Messages_deleteProperty;
2649 definition.getPropertyNames = &Messages_getPropertyNames;
2650 definition.finalize = &CYFinalize;
2651 Messages_ = JSClassCreate(&definition);
2653 definition = kJSClassDefinitionEmpty;
2654 definition.className = "Selector";
2655 definition.staticValues = Selector_staticValues;
2656 definition.staticFunctions = Selector_staticFunctions;
2657 definition.callAsFunction = &Selector_callAsFunction;
2658 definition.finalize = &CYFinalize;
2659 Selector_ = JSClassCreate(&definition);
2661 definition = kJSClassDefinitionEmpty;
2662 definition.className = "Super";
2663 definition.staticFunctions = Internal_staticFunctions;
2664 definition.finalize = &CYFinalize;
2665 Super_ = JSClassCreate(&definition);
2667 definition = kJSClassDefinitionEmpty;
2668 definition.className = "ObjectiveC::Classes";
2669 definition.getProperty = &ObjectiveC_Classes_getProperty;
2670 definition.getPropertyNames = &ObjectiveC_Classes_getPropertyNames;
2671 ObjectiveC_Classes_ = JSClassCreate(&definition);
2673 definition = kJSClassDefinitionEmpty;
2674 definition.className = "ObjectiveC::Constants";
2675 definition.getProperty = &ObjectiveC_Constants_getProperty;
2676 definition.getPropertyNames = &ObjectiveC_Constants_getPropertyNames;
2677 ObjectiveC_Constants_ = JSClassCreate(&definition);
2679 #if OBJC_API_VERSION >= 2
2680 definition = kJSClassDefinitionEmpty;
2681 definition.className = "ObjectiveC::Images";
2682 definition.getProperty = &ObjectiveC_Images_getProperty;
2683 definition.getPropertyNames = &ObjectiveC_Images_getPropertyNames;
2684 ObjectiveC_Images_ = JSClassCreate(&definition);
2686 definition = kJSClassDefinitionEmpty;
2687 definition.className = "ObjectiveC::Image::Classes";
2688 definition.getProperty = &ObjectiveC_Image_Classes_getProperty;
2689 definition.getPropertyNames = &ObjectiveC_Image_Classes_getPropertyNames;
2690 ObjectiveC_Image_Classes_ = JSClassCreate(&definition);
2693 definition = kJSClassDefinitionEmpty;
2694 definition.className = "ObjectiveC::Protocols";
2695 definition.getProperty = &ObjectiveC_Protocols_getProperty;
2696 definition.getPropertyNames = &ObjectiveC_Protocols_getPropertyNames;
2697 ObjectiveC_Protocols_ = JSClassCreate(&definition);
2700 class_addMethod(NSCFType_, @selector(cy$toJSON:inContext:), reinterpret_cast<IMP>(&NSCFType$cy$toJSON$inContext$), "^{OpaqueJSValue=}16@0:4@8^{OpaqueJSContext=}12");
2704 void CYObjectiveC_SetupContext(JSContextRef context) { CYPoolTry {
2705 JSObjectRef global(CYGetGlobalObject(context));
2706 JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s)));
2707 JSObjectRef cycript(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Cycript"))));
2708 JSObjectRef all(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("all"))));
2709 JSObjectRef alls(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("alls"))));
2711 JSObjectRef ObjectiveC(JSObjectMake(context, NULL, NULL));
2712 CYSetProperty(context, cycript, CYJSString("ObjectiveC"), ObjectiveC);
2714 JSObjectRef protocols(JSObjectMake(context, ObjectiveC_Protocols_, NULL));
2715 CYSetProperty(context, ObjectiveC, CYJSString("protocols"), protocols);
2716 CYArrayPush(context, alls, protocols);
2718 JSObjectRef classes(JSObjectMake(context, ObjectiveC_Classes_, NULL));
2719 CYSetProperty(context, ObjectiveC, CYJSString("classes"), classes);
2720 CYArrayPush(context, alls, classes);
2722 JSObjectRef constants(JSObjectMake(context, ObjectiveC_Constants_, NULL));
2723 CYSetProperty(context, ObjectiveC, CYJSString("constants"), constants);
2724 CYArrayPush(context, alls, constants);
2726 #if OBJC_API_VERSION >= 2
2727 CYSetProperty(context, ObjectiveC, CYJSString("images"), JSObjectMake(context, ObjectiveC_Images_, NULL));
2730 JSObjectRef Class(JSObjectMakeConstructor(context, Class_, NULL));
2731 JSObjectRef Instance(JSObjectMakeConstructor(context, Instance_, &Instance_new));
2732 JSObjectRef Message(JSObjectMakeConstructor(context, Message_, NULL));
2733 JSObjectRef Selector(JSObjectMakeConstructor(context, Selector_, &Selector_new));
2734 JSObjectRef Super(JSObjectMakeConstructor(context, Super_, &Super_new));
2736 JSObjectRef Instance_prototype(CYCastJSObject(context, CYGetProperty(context, Instance, prototype_s)));
2737 CYSetProperty(context, cy, CYJSString("Instance_prototype"), Instance_prototype);
2739 JSObjectRef ArrayInstance(JSObjectMakeConstructor(context, ArrayInstance_, NULL));
2740 JSObjectRef ArrayInstance_prototype(CYCastJSObject(context, CYGetProperty(context, ArrayInstance, prototype_s)));
2741 CYSetProperty(context, cy, CYJSString("ArrayInstance_prototype"), ArrayInstance_prototype);
2742 JSObjectRef Array_prototype(CYGetCachedObject(context, CYJSString("Array_prototype")));
2743 JSObjectSetPrototype(context, ArrayInstance_prototype, Array_prototype);
2745 JSObjectRef FunctionInstance(JSObjectMakeConstructor(context, FunctionInstance_, NULL));
2746 JSObjectRef FunctionInstance_prototype(CYCastJSObject(context, CYGetProperty(context, FunctionInstance, prototype_s)));
2747 CYSetProperty(context, cy, CYJSString("FunctionInstance_prototype"), FunctionInstance_prototype);
2748 JSObjectRef Function_prototype(CYGetCachedObject(context, CYJSString("Function_prototype")));
2749 JSObjectSetPrototype(context, FunctionInstance_prototype, Function_prototype);
2751 JSObjectRef ObjectInstance(JSObjectMakeConstructor(context, ObjectInstance_, NULL));
2752 JSObjectRef ObjectInstance_prototype(CYCastJSObject(context, CYGetProperty(context, ObjectInstance, prototype_s)));
2753 CYSetProperty(context, cy, CYJSString("ObjectInstance_prototype"), ObjectInstance_prototype);
2754 JSObjectRef Object_prototype(CYGetCachedObject(context, CYJSString("Object_prototype")));
2755 JSObjectSetPrototype(context, ObjectInstance_prototype, Object_prototype);
2757 JSObjectRef StringInstance(JSObjectMakeConstructor(context, StringInstance_, NULL));
2758 JSObjectRef StringInstance_prototype(CYCastJSObject(context, CYGetProperty(context, StringInstance, prototype_s)));
2759 CYSetProperty(context, cy, CYJSString("StringInstance_prototype"), StringInstance_prototype);
2760 JSObjectRef String_prototype(CYGetCachedObject(context, CYJSString("String_prototype")));
2761 JSObjectSetPrototype(context, StringInstance_prototype, String_prototype);
2763 JSObjectRef Class_prototype(CYCastJSObject(context, CYGetProperty(context, Class, prototype_s)));
2764 CYSetProperty(context, cy, CYJSString("Class_prototype"), Class_prototype);
2765 JSObjectSetPrototype(context, Class_prototype, Instance_prototype);
2767 CYSetProperty(context, cycript, CYJSString("Instance"), Instance);
2768 CYSetProperty(context, cycript, CYJSString("Selector"), Selector);
2769 CYSetProperty(context, cycript, CYJSString("Super"), Super);
2771 JSObjectRef box(JSObjectMakeFunctionWithCallback(context, CYJSString("box"), &Instance_box_callAsFunction));
2772 CYSetProperty(context, Instance, CYJSString("box"), box);
2774 #if defined(__APPLE__) && defined(__arm__) && 0
2775 CYSetProperty(context, all, CYJSString("objc_registerClassPair"), &objc_registerClassPair_, kJSPropertyAttributeDontEnum);
2778 CYSetProperty(context, all, CYJSString("objc_msgSend"), &$objc_msgSend, kJSPropertyAttributeDontEnum);
2780 JSObjectSetPrototype(context, CYCastJSObject(context, CYGetProperty(context, Message, prototype_s)), Function_prototype);
2781 JSObjectSetPrototype(context, CYCastJSObject(context, CYGetProperty(context, Selector, prototype_s)), Function_prototype);
2784 static CYHooks CYObjectiveCHooks = {
2785 &CYObjectiveC_ExecuteStart,
2786 &CYObjectiveC_ExecuteEnd,
2787 &CYObjectiveC_CallFunction,
2788 &CYObjectiveC_Initialize,
2789 &CYObjectiveC_SetupContext,
2790 &CYObjectiveC_PoolFFI,
2791 &CYObjectiveC_FromFFI,
2794 struct CYObjectiveC {
2796 hooks_ = &CYObjectiveCHooks;
2797 // XXX: evil magic juju to make this actually take effect on a Mac when compiled with autoconf/libtool doom!
2798 _assert(hooks_ != NULL);