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 <objc/runtime.h>
39 #include "JavaScript.hpp"
41 #include "Execute.hpp"
48 #define CYObjectiveTry_(context) { \
49 JSContextRef context_(context); \
51 #define CYObjectiveTry { \
53 #define CYObjectiveCatch \
54 catch (const CYException &error) { \
55 @throw CYCastNSObject(NULL, context_, error.CastJSValue(context_)); \
61 NSAutoreleasePool *_pool([[NSAutoreleasePool alloc] init]); \
63 #define CYPoolCatch(value) \
64 @catch (NSException *error) { \
65 _saved = [error retain]; \
66 throw CYJSError(context, CYCastJSValue(context, error)); \
71 [_saved autorelease]; \
77 #define CYSadCatch(value) \
78 @catch (NSException *error ) { \
79 throw CYJSError(context, CYCastJSValue(context, error)); \
84 #define class_getSuperclass GSObjCSuper
85 #define class_getInstanceVariable GSCGetInstanceVariableDefinition
86 #define class_getName GSNameFromClass
88 #define class_removeMethods(cls, list) GSRemoveMethodList(cls, list, YES)
90 #define ivar_getName(ivar) ((ivar)->ivar_name)
91 #define ivar_getOffset(ivar) ((ivar)->ivar_offset)
92 #define ivar_getTypeEncoding(ivar) ((ivar)->ivar_type)
94 #define method_getName(method) ((method)->method_name)
95 #define method_getImplementation(method) ((method)->method_imp)
96 #define method_getTypeEncoding(method) ((method)->method_types)
97 #define method_setImplementation(method, imp) ((void) ((method)->method_imp = (imp)))
100 #define objc_getClass GSClassFromName
102 #define objc_getProtocol GSProtocolFromName
104 #define object_getClass GSObjCClass
106 #define object_getInstanceVariable(object, name, value) ({ \
107 objc_ivar *ivar(class_getInstanceVariable(object_getClass(object), name)); \
108 _assert(value != NULL); \
110 GSObjCGetVariable(object, ivar_getOffset(ivar), sizeof(void *), value); \
114 #define object_setIvar(object, ivar, value) ({ \
115 void *data = (value); \
116 GSObjCSetVariable(object, ivar_getOffset(ivar), sizeof(void *), &data); \
119 #define protocol_getName(protocol) [(protocol) name]
122 static void (*$objc_setAssociatedObject)(id object, void *key, id value, objc_AssociationPolicy policy);
123 static id (*$objc_getAssociatedObject)(id object, void *key);
124 static void (*$objc_removeAssociatedObjects)(id object);
126 struct BlockLiteral {
130 void (*invoke)(void *, ...);
134 struct BlockDescriptor1 {
135 unsigned long int reserved;
136 unsigned long int size;
139 struct BlockDescriptor2 {
140 void (*copy_helper)(BlockLiteral *dst, BlockLiteral *src);
141 void (*dispose_helper)(BlockLiteral *src);
144 struct BlockDescriptor3 {
145 const char *signature;
150 BLOCK_DEALLOCATING = 0x0001,
151 BLOCK_REFCOUNT_MASK = 0xfffe,
152 BLOCK_NEEDS_FREE = 1 << 24,
153 BLOCK_HAS_COPY_DISPOSE = 1 << 25,
154 BLOCK_HAS_CTOR = 1 << 26,
155 BLOCK_IS_GC = 1 << 27,
156 BLOCK_IS_GLOBAL = 1 << 28,
157 BLOCK_HAS_STRET = 1 << 29,
158 BLOCK_HAS_SIGNATURE = 1 << 30,
161 JSValueRef CYSendMessage(CYPool &pool, JSContextRef context, id self, Class super, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception);
163 /* Objective-C Pool Release {{{ */
164 void CYPoolRelease_(void *data) {
165 id object(reinterpret_cast<id>(data));
169 id CYPoolRelease_(CYPool *pool, id object) {
172 else if (pool == NULL)
173 return [object autorelease];
175 pool->atexit(CYPoolRelease_);
180 template <typename Type_>
181 Type_ CYPoolRelease(CYPool *pool, Type_ object) {
182 return (Type_) CYPoolRelease_(pool, (id) object);
185 /* Objective-C Strings {{{ */
186 const char *CYPoolCString(CYPool &pool, JSContextRef context, NSString *value) {
187 size_t size([value maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1);
188 char *string(new(pool) char[size]);
189 if (![value getCString:string maxLength:size encoding:NSUTF8StringEncoding])
190 throw CYJSError(context, "[NSString getCString:maxLength:encoding:] == NO");
194 JSStringRef CYCopyJSString(JSContextRef context, NSString *value) {
196 return JSStringCreateWithCFString(reinterpret_cast<CFStringRef>(value));
199 return CYCopyJSString(CYPoolCString(pool, context, value));
203 JSStringRef CYCopyJSString(JSContextRef context, NSObject *value) {
206 // XXX: this definition scares me; is anyone using this?!
207 NSString *string([value description]);
208 return CYCopyJSString(context, string);
211 NSString *CYCopyNSString(const CYUTF8String &value) {
213 return (NSString *) CFStringCreateWithBytes(kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(value.data), value.size, kCFStringEncodingUTF8, true);
215 return [[NSString alloc] initWithBytes:value.data length:value.size encoding:NSUTF8StringEncoding];
219 NSString *CYCopyNSString(JSContextRef context, JSStringRef value) {
221 return (NSString *) JSStringCopyCFString(kCFAllocatorDefault, value);
224 return CYCopyNSString(CYPoolUTF8String(pool, context, value));
228 NSString *CYCopyNSString(JSContextRef context, JSValueRef value) {
229 return CYCopyNSString(context, CYJSString(context, value));
232 NSString *CYCastNSString(CYPool *pool, const CYUTF8String &value) {
233 return CYPoolRelease(pool, CYCopyNSString(value));
236 NSString *CYCastNSString(CYPool *pool, SEL sel) {
237 const char *name(sel_getName(sel));
238 return CYPoolRelease(pool, CYCopyNSString(CYUTF8String(name, strlen(name))));
241 NSString *CYCastNSString(CYPool *pool, JSContextRef context, JSStringRef value) {
242 return CYPoolRelease(pool, CYCopyNSString(context, value));
245 CYUTF8String CYCastUTF8String(NSString *value) {
246 NSData *data([value dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO]);
247 return CYUTF8String(reinterpret_cast<const char *>([data bytes]), [data length]);
251 JSValueRef CYCastJSValue(JSContextRef context, NSObject *value);
253 void CYThrow(JSContextRef context, NSException *error, JSValueRef *exception) {
254 if (exception == NULL)
256 *exception = CYCastJSValue(context, error);
259 size_t CYGetIndex(NSString *value) {
260 return CYGetIndex(CYCastUTF8String(value));
263 bool CYGetOffset(CYPool &pool, JSContextRef context, NSString *value, ssize_t &index) {
264 return CYGetOffset(CYPoolCString(pool, context, value), index);
267 static JSClassRef Instance_;
269 static JSClassRef ArrayInstance_;
270 static JSClassRef FunctionInstance_;
271 static JSClassRef ObjectInstance_;
272 static JSClassRef StringInstance_;
274 static JSClassRef Class_;
275 static JSClassRef Internal_;
276 static JSClassRef Message_;
277 static JSClassRef Messages_;
278 static JSClassRef Selector_;
279 static JSClassRef Super_;
281 static JSClassRef ObjectiveC_Classes_;
282 static JSClassRef ObjectiveC_Constants_;
283 static JSClassRef ObjectiveC_Protocols_;
286 static JSClassRef ObjectiveC_Image_Classes_;
287 static JSClassRef ObjectiveC_Images_;
291 static Class NSCFBoolean_;
292 static Class NSCFType_;
293 static Class NSGenericDeallocHandler_;
294 static Class NSMessageBuilder_;
295 static Class NSZombie_;
297 static Class NSBoolNumber_;
300 static Class NSArray_;
301 static Class NSBlock_;
302 static Class NSDictionary_;
303 static Class NSString_;
304 static Class Object_;
306 static Type_privateData *Object_type;
307 static Type_privateData *Selector_type;
309 Type_privateData *Instance::GetType() const {
313 Type_privateData *Selector_privateData::GetType() const {
314 return Selector_type;
317 static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception);
319 JSValueRef CYGetClassPrototype(JSContextRef context, Class self, bool meta) {
321 return CYGetCachedObject(context, CYJSString("Instance_prototype"));
322 else if (meta && !class_isMetaClass(self))
323 return CYGetCachedObject(context, CYJSString("Class_prototype"));
325 JSObjectRef global(CYGetGlobalObject(context));
326 JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s)));
329 sprintf(label, "i%p", self);
330 CYJSString name(label);
332 JSValueRef value(CYGetProperty(context, cy, name));
333 if (!JSValueIsUndefined(context, value))
336 JSClassRef _class(NULL);
337 JSValueRef prototype;
339 if (self == NSArray_)
340 prototype = CYGetCachedObject(context, CYJSString("ArrayInstance_prototype"));
341 else if (self == NSBlock_)
342 prototype = CYGetCachedObject(context, CYJSString("FunctionInstance_prototype"));
343 else if (self == NSDictionary_)
344 prototype = CYGetCachedObject(context, CYJSString("ObjectInstance_prototype"));
345 else if (self == NSString_)
346 prototype = CYGetCachedObject(context, CYJSString("StringInstance_prototype"));
348 prototype = CYGetClassPrototype(context, class_getSuperclass(self), meta);
350 JSObjectRef object(JSObjectMake(context, _class, NULL));
351 JSObjectSetPrototype(context, object, prototype);
352 CYSetProperty(context, cy, name, object);
357 _finline JSValueRef CYGetClassPrototype(JSContextRef context, Class self) {
358 return CYGetClassPrototype(context, self, class_isMetaClass(self));
361 JSObjectRef Messages::Make(JSContextRef context, Class _class) {
362 JSObjectRef value(JSObjectMake(context, Messages_, new Messages(_class)));
363 if (Class super = class_getSuperclass(_class))
364 JSObjectSetPrototype(context, value, Messages::Make(context, super));
368 JSObjectRef Internal::Make(JSContextRef context, id object, JSObjectRef owner) {
369 return JSObjectMake(context, Internal_, new Internal(object, context, owner));
373 JSObjectRef Super::Make(JSContextRef context, id object, Class _class) {
374 JSObjectRef value(JSObjectMake(context, Super_, new Super(object, _class)));
378 JSObjectRef Instance::Make(JSContextRef context, id object, Flags flags) {
379 JSObjectRef value(JSObjectMake(context, Instance_, new Instance(object, flags)));
380 JSObjectSetPrototype(context, value, CYGetClassPrototype(context, object_getClass(object)));
384 Instance::~Instance() {
385 if ((flags_ & Transient) == 0)
386 // XXX: does this handle background threads correctly?
387 // XXX: this simply does not work on the console because I'm stupid
388 [GetValue() performSelector:@selector(release) withObject:nil afterDelay:0];
391 struct Message_privateData :
396 Message_privateData(SEL sel, const char *type, IMP value = NULL) :
397 cy::Functor(type, reinterpret_cast<void (*)()>(value)),
403 JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient) {
404 Instance::Flags flags;
407 flags = Instance::Transient;
409 flags = Instance::None;
410 object = [object retain];
413 return Instance::Make(context, object, flags);
416 @interface NSMethodSignature (Cycript)
417 - (NSString *) _typeString;
420 @interface NSObject (Cycript)
422 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context;
423 - (JSType) cy$JSType;
425 - (JSValueRef) cy$toJSON:(NSString *)key inContext:(JSContextRef)context;
426 - (NSString *) cy$toCYON:(bool)objective;
428 - (bool) cy$hasProperty:(NSString *)name;
429 - (NSObject *) cy$getProperty:(NSString *)name;
430 - (JSValueRef) cy$getProperty:(NSString *)name inContext:(JSContextRef)context;
431 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value;
432 - (bool) cy$deleteProperty:(NSString *)name;
433 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context;
435 + (bool) cy$hasImplicitProperties;
441 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context;
444 NSString *CYCastNSCYON(id value, bool objective) {
450 Class _class(object_getClass(value));
451 SEL sel(@selector(cy$toCYON:));
453 if (objc_method *toCYON = class_getInstanceMethod(_class, sel))
454 string = reinterpret_cast<NSString *(*)(id, SEL, bool)>(method_getImplementation(toCYON))(value, sel, objective);
455 else if (objc_method *methodSignatureForSelector = class_getInstanceMethod(_class, @selector(methodSignatureForSelector:))) {
456 if (reinterpret_cast<NSMethodSignature *(*)(id, SEL, SEL)>(method_getImplementation(methodSignatureForSelector))(value, @selector(methodSignatureForSelector:), sel) != nil)
457 string = [value cy$toCYON:objective];
462 else if (value == NSZombie_)
463 string = @"_NSZombie_";
464 else if (_class == NSZombie_)
465 string = [NSString stringWithFormat:@"<_NSZombie_: %p>", value];
466 // XXX: frowny /in/ the pants
467 else if (value == NSGenericDeallocHandler_ || value == NSMessageBuilder_ || value == Object_)
471 string = [NSString stringWithFormat:@"%@", value];
476 string = @"undefined";
483 struct PropertyAttributes {
488 const char *variable;
501 PropertyAttributes(objc_property_t property) :
513 name = property_getName(property);
514 const char *attributes(property_getAttributes(property));
516 for (char *token(pool_.strdup(attributes)), *next; token != NULL; token = next) {
517 if ((next = strchr(token, ',')) != NULL)
520 case 'R': readonly = true; break;
521 case 'C': copy = true; break;
522 case '&': retain = true; break;
523 case 'N': nonatomic = true; break;
524 case 'G': getter_ = token + 1; break;
525 case 'S': setter_ = token + 1; break;
526 case 'V': variable = token + 1; break;
530 /*if (variable == NULL) {
531 variable = property_getName(property);
532 size_t size(strlen(variable));
533 char *name(new(pool_) char[size + 2]);
535 memcpy(name + 1, variable, size);
536 name[size + 1] = '\0';
541 const char *Getter() {
543 getter_ = pool_.strdup(name);
547 const char *Setter() {
548 if (setter_ == NULL && !readonly) {
549 size_t length(strlen(name));
551 char *temp(new(pool_) char[length + 5]);
557 temp[3] = toupper(name[0]);
558 memcpy(temp + 4, name + 1, length - 1);
561 temp[length + 3] = ':';
562 temp[length + 4] = '\0';
572 @interface CYWebUndefined : NSObject {
575 + (CYWebUndefined *) undefined;
579 @implementation CYWebUndefined
581 + (CYWebUndefined *) undefined {
582 static CYWebUndefined *instance_([[CYWebUndefined alloc] init]);
588 #define WebUndefined CYWebUndefined
590 /* Bridge: CYJSObject {{{ */
591 @interface CYJSObject : NSMutableDictionary {
593 JSGlobalContextRef context_;
596 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context;
598 - (NSUInteger) count;
599 - (id) objectForKey:(id)key;
600 - (NSEnumerator *) keyEnumerator;
601 - (void) setObject:(id)object forKey:(id)key;
602 - (void) removeObjectForKey:(id)key;
606 /* Bridge: CYJSArray {{{ */
607 @interface CYJSArray : NSMutableArray {
609 JSGlobalContextRef context_;
612 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context;
614 - (NSUInteger) count;
615 - (id) objectAtIndex:(NSUInteger)index;
617 - (void) addObject:(id)anObject;
618 - (void) insertObject:(id)anObject atIndex:(NSUInteger)index;
619 - (void) removeLastObject;
620 - (void) removeObjectAtIndex:(NSUInteger)index;
621 - (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
626 _finline bool CYJSValueIsNSObject(JSContextRef context, JSValueRef value) {
627 return JSValueIsObjectOfClass(context, value, Instance_);
630 _finline bool CYJSValueIsInstanceOfCachedConstructor(JSContextRef context, JSValueRef value, JSStringRef cache) {
631 JSValueRef exception(NULL);
632 JSObjectRef constructor(CYGetCachedObject(context, cache));
633 bool is(JSValueIsInstanceOfConstructor(context, value, constructor, &exception));
634 CYThrow(context, exception);
638 NSObject *CYMakeBlock(void (*invoke)(), sig::Signature &signature) {
639 BlockLiteral *literal(reinterpret_cast<BlockLiteral *>(malloc(sizeof(BlockLiteral))));
643 BlockDescriptor1 one_;
644 BlockDescriptor2 two_;
645 BlockDescriptor3 three_;
651 Descriptor *descriptor(new Descriptor);
652 memset(&descriptor->d_, 0, sizeof(descriptor->d_));
654 literal->isa = objc_getClass("__NSGlobalBlock__");
655 literal->flags = BLOCK_HAS_SIGNATURE | BLOCK_HAS_COPY_DISPOSE | BLOCK_IS_GLOBAL;
656 literal->reserved = 0;
657 literal->invoke = reinterpret_cast<void (*)(void *, ...)>(invoke);
658 literal->descriptor = descriptor;
660 descriptor->d_.one_.size = sizeof(descriptor->d_);
661 descriptor->d_.three_.signature = sig::Unparse(descriptor->pool_, &signature);
663 return reinterpret_cast<NSObject *>(literal);
666 NSObject *CYCastNSObject(CYPool *pool, JSContextRef context, JSObjectRef object) {
667 if (CYJSValueIsNSObject(context, object)) {
668 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
669 return internal->GetValue();
672 if (JSValueIsObjectOfClass(context, object, Functor_)) {
673 cy::Functor *internal(reinterpret_cast<cy::Functor *>(JSObjectGetPrivate(object)));
674 return CYMakeBlock(internal->GetValue(), internal->signature_);
677 bool array(CYJSValueIsInstanceOfCachedConstructor(context, object, Array_s));
678 id value(array ? [CYJSArray alloc] : [CYJSObject alloc]);
679 return CYPoolRelease(pool, [value initWithJSObject:object inContext:context]);
682 NSNumber *CYCopyNSNumber(JSContextRef context, JSValueRef value) {
683 return [[NSNumber alloc] initWithDouble:CYCastDouble(context, value)];
687 @interface NSBoolNumber : NSNumber {
692 id CYNSObject(CYPool *pool, JSContextRef context, JSValueRef value, bool cast) {
696 switch (JSType type = JSValueGetType(context, value)) {
697 case kJSTypeUndefined:
698 object = [WebUndefined undefined];
708 object = (id) (CYCastBool(context, value) ? kCFBooleanTrue : kCFBooleanFalse);
711 object = [[NSBoolNumber alloc] initWithBool:CYCastBool(context, value)];
717 object = CYCopyNSNumber(context, value);
722 object = CYCopyNSString(context, value);
727 // XXX: this might could be more efficient
728 object = CYCastNSObject(pool, context, (JSObjectRef) value);
733 throw CYJSError(context, "JSValueGetType() == 0x%x", type);
740 return CYPoolRelease(pool, object);
742 return [object retain];
745 NSObject *CYCastNSObject(CYPool *pool, JSContextRef context, JSValueRef value) {
746 return CYNSObject(pool, context, value, true);
749 NSObject *CYCopyNSObject(CYPool &pool, JSContextRef context, JSValueRef value) {
750 return CYNSObject(&pool, context, value, false);
753 /* Bridge: NSArray {{{ */
754 @implementation NSArray (Cycript)
757 return [[self mutableCopy] autorelease];
760 - (NSString *) cy$toCYON:(bool)objective {
761 NSMutableString *json([[[NSMutableString alloc] init] autorelease]);
762 [json appendString:@"@["];
766 for (id object in self) {
768 for (size_t index(0), count([self count]); index != count; ++index) {
769 id object([self objectAtIndex:index]);
772 [json appendString:@","];
775 if (object == nil || [object cy$JSType] != kJSTypeUndefined)
776 [json appendString:CYCastNSCYON(object, true)];
778 [json appendString:@","];
783 [json appendString:@"]"];
787 - (bool) cy$hasProperty:(NSString *)name {
788 if ([name isEqualToString:@"length"])
791 size_t index(CYGetIndex(name));
792 if (index == _not(size_t) || index >= [self count])
793 return [super cy$hasProperty:name];
798 - (NSObject *) cy$getProperty:(NSString *)name {
799 size_t index(CYGetIndex(name));
800 if (index == _not(size_t) || index >= [self count])
801 return [super cy$getProperty:name];
803 return [self objectAtIndex:index];
806 - (JSValueRef) cy$getProperty:(NSString *)name inContext:(JSContextRef)context {
807 CYObjectiveTry_(context) {
808 if ([name isEqualToString:@"length"])
809 return CYCastJSValue(context, [self count]);
812 return [super cy$getProperty:name inContext:context];
815 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context {
816 [super cy$getPropertyNames:names inContext:context];
818 for (size_t index(0), count([self count]); index != count; ++index) {
819 id object([self objectAtIndex:index]);
820 if (object == nil || [object cy$JSType] != kJSTypeUndefined) {
822 sprintf(name, "%zu", index);
823 JSPropertyNameAccumulatorAddName(names, CYJSString(name));
828 + (bool) cy$hasImplicitProperties {
834 /* Bridge: NSBlock {{{ */
841 /* Bridge: NSBoolNumber {{{ */
843 @implementation NSBoolNumber (Cycript)
845 - (JSType) cy$JSType {
846 return kJSTypeBoolean;
849 - (NSString *) cy$toCYON:(bool)objective {
850 NSString *value([self boolValue] ? @"true" : @"false");
851 return objective ? value : [NSString stringWithFormat:@"@%@", value];
854 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
855 return CYCastJSValue(context, (bool) [self boolValue]);
861 /* Bridge: NSDictionary {{{ */
862 @implementation NSDictionary (Cycript)
865 return [[self mutableCopy] autorelease];
868 - (NSString *) cy$toCYON:(bool)objective {
869 NSMutableString *json([[[NSMutableString alloc] init] autorelease]);
870 [json appendString:@"@{"];
874 for (NSObject *key in self) {
876 NSEnumerator *keys([self keyEnumerator]);
877 while (NSObject *key = [keys nextObject]) {
880 [json appendString:@","];
883 [json appendString:CYCastNSCYON(key, true)];
884 [json appendString:@":"];
885 NSObject *object([self objectForKey:key]);
886 [json appendString:CYCastNSCYON(object, true)];
889 [json appendString:@"}"];
893 - (bool) cy$hasProperty:(NSString *)name {
894 return [self objectForKey:name] != nil;
897 - (NSObject *) cy$getProperty:(NSString *)name {
898 return [self objectForKey:name];
901 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context {
902 [super cy$getPropertyNames:names inContext:context];
905 for (NSObject *key in self) {
907 NSEnumerator *keys([self keyEnumerator]);
908 while (NSObject *key = [keys nextObject]) {
910 JSPropertyNameAccumulatorAddName(names, CYJSString(context, key));
914 + (bool) cy$hasImplicitProperties {
920 /* Bridge: NSMutableArray {{{ */
921 @implementation NSMutableArray (Cycript)
923 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
924 if ([name isEqualToString:@"length"]) {
925 // XXX: is this not intelligent?
926 NSNumber *number(reinterpret_cast<NSNumber *>(value));
928 NSUInteger size([number unsignedIntegerValue]);
930 NSUInteger size([number unsignedIntValue]);
932 NSUInteger count([self count]);
934 [self removeObjectsInRange:NSMakeRange(size, count - size)];
935 else if (size != count) {
936 WebUndefined *undefined([WebUndefined undefined]);
937 for (size_t i(count); i != size; ++i)
938 [self addObject:undefined];
943 size_t index(CYGetIndex(name));
944 if (index == _not(size_t))
945 return [super cy$setProperty:name to:value];
947 id object(value ?: [NSNull null]);
949 size_t count([self count]);
951 [self replaceObjectAtIndex:index withObject:object];
953 if (index != count) {
954 WebUndefined *undefined([WebUndefined undefined]);
955 for (size_t i(count); i != index; ++i)
956 [self addObject:undefined];
959 [self addObject:object];
965 - (bool) cy$deleteProperty:(NSString *)name {
966 size_t index(CYGetIndex(name));
967 if (index == _not(size_t) || index >= [self count])
968 return [super cy$deleteProperty:name];
969 [self replaceObjectAtIndex:index withObject:[WebUndefined undefined]];
975 /* Bridge: NSMutableDictionary {{{ */
976 @implementation NSMutableDictionary (Cycript)
978 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
979 [self setObject:(value ?: [NSNull null]) forKey:name];
983 - (bool) cy$deleteProperty:(NSString *)name {
984 if ([self objectForKey:name] == nil)
987 [self removeObjectForKey:name];
994 /* Bridge: NSNumber {{{ */
995 @implementation NSNumber (Cycript)
997 - (JSType) cy$JSType {
999 // XXX: this just seems stupid
1000 if ([self class] == NSCFBoolean_)
1001 return kJSTypeBoolean;
1003 return kJSTypeNumber;
1006 - (NSString *) cy$toCYON:(bool)objective {
1007 NSString *value([self cy$JSType] != kJSTypeBoolean ? [self stringValue] : [self boolValue] ? @"true" : @"false");
1008 return objective ? value : [NSString stringWithFormat:@"@%@", value];
1011 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
1012 return [self cy$JSType] != kJSTypeBoolean ? CYCastJSValue(context, [self doubleValue]) : CYCastJSValue(context, static_cast<bool>([self boolValue]));
1013 } CYObjectiveCatch }
1017 /* Bridge: NSNull {{{ */
1018 @implementation NSNull (Cycript)
1020 - (JSType) cy$JSType {
1024 - (NSString *) cy$toCYON:(bool)objective {
1025 NSString *value(@"null");
1026 return objective ? value : [NSString stringWithFormat:@"@%@", value];
1029 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
1030 return CYJSNull(context);
1031 } CYObjectiveCatch }
1035 /* Bridge: NSObject {{{ */
1036 @implementation NSObject (Cycript)
1042 - (JSValueRef) cy$toJSON:(NSString *)key inContext:(JSContextRef)context {
1043 return [self cy$valueOfInContext:context];
1046 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
1048 } CYObjectiveCatch }
1050 - (JSType) cy$JSType {
1051 return kJSTypeObject;
1054 - (NSString *) cy$toCYON:(bool)objective {
1055 return [[self description] cy$toCYON:objective];
1058 - (bool) cy$hasProperty:(NSString *)name {
1062 - (NSObject *) cy$getProperty:(NSString *)name {
1066 - (JSValueRef) cy$getProperty:(NSString *)name inContext:(JSContextRef)context { CYObjectiveTry_(context) {
1067 if (NSObject *value = [self cy$getProperty:name])
1068 return CYCastJSValue(context, value);
1070 } CYObjectiveCatch }
1072 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
1076 - (bool) cy$deleteProperty:(NSString *)name {
1080 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context {
1083 + (bool) cy$hasImplicitProperties {
1089 /* Bridge: NSProxy {{{ */
1090 @implementation NSProxy (Cycript)
1092 - (NSString *) cy$toCYON:(bool)objective {
1093 return [[self description] cy$toCYON:objective];
1098 /* Bridge: NSString {{{ */
1099 @implementation NSString (Cycript)
1102 return [[self copy] autorelease];
1105 - (JSType) cy$JSType {
1106 return kJSTypeString;
1109 - (NSString *) cy$toCYON:(bool)objective {
1110 std::ostringstream str;
1113 CYUTF8String string(CYCastUTF8String(self));
1114 CYStringify(str, string.data, string.size);
1115 std::string value(str.str());
1116 return CYCastNSString(NULL, CYUTF8String(value.c_str(), value.size()));
1119 - (bool) cy$hasProperty:(NSString *)name {
1120 size_t index(CYGetIndex(name));
1121 if (index == _not(size_t) || index >= [self length])
1122 return [super cy$hasProperty:name];
1127 - (NSObject *) cy$getProperty:(NSString *)name {
1128 size_t index(CYGetIndex(name));
1129 if (index == _not(size_t) || index >= [self length])
1130 return [super cy$getProperty:name];
1132 return [self substringWithRange:NSMakeRange(index, 1)];
1135 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context {
1136 [super cy$getPropertyNames:names inContext:context];
1138 for (size_t index(0), length([self length]); index != length; ++index) {
1140 sprintf(name, "%zu", index);
1141 JSPropertyNameAccumulatorAddName(names, CYJSString(name));
1145 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
1146 return CYCastJSValue(context, CYJSString(context, self));
1147 } CYObjectiveCatch }
1151 /* Bridge: WebUndefined {{{ */
1152 @implementation WebUndefined (Cycript)
1154 - (JSType) cy$JSType {
1155 return kJSTypeUndefined;
1158 - (NSString *) cy$toCYON:(bool)objective {
1159 NSString *value(@"undefined");
1160 return value; // XXX: maybe use the below code, adding @undefined?
1161 //return objective ? value : [NSString stringWithFormat:@"@%@", value];
1164 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
1165 return CYJSUndefined(context);
1166 } CYObjectiveCatch }
1171 static bool CYIsClass(id self) {
1173 return class_isMetaClass(object_getClass(self));
1175 return GSObjCIsClass(self);
1179 Class CYCastClass(CYPool &pool, JSContextRef context, JSValueRef value) {
1180 id self(CYCastNSObject(&pool, context, value));
1181 if (CYIsClass(self))
1182 return (Class) self;
1183 throw CYJSError(context, "got something that is not a Class");
1187 NSArray *CYCastNSArray(JSContextRef context, JSPropertyNameArrayRef names) {
1189 size_t size(JSPropertyNameArrayGetCount(names));
1190 NSMutableArray *array([NSMutableArray arrayWithCapacity:size]);
1191 for (size_t index(0); index != size; ++index)
1192 [array addObject:CYCastNSString(&pool, context, JSPropertyNameArrayGetNameAtIndex(names, index))];
1196 JSValueRef CYCastJSValue(JSContextRef context, NSObject *value) { CYPoolTry {
1198 return CYJSNull(context);
1200 return CYMakeInstance(context, value, false);
1201 } CYPoolCatch(NULL) return /*XXX*/ NULL; }
1203 @implementation CYJSObject
1205 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { CYObjectiveTry {
1206 if ((self = [super init]) != nil) {
1208 context_ = CYGetJSContext(context);
1209 JSGlobalContextRetain(context_);
1210 JSValueProtect(context_, object_);
1212 } CYObjectiveCatch }
1214 - (void) dealloc { CYObjectiveTry {
1215 JSValueUnprotect(context_, object_);
1216 JSGlobalContextRelease(context_);
1218 } CYObjectiveCatch }
1220 - (NSString *) cy$toCYON:(bool)objective { CYObjectiveTry {
1222 const char *cyon(CYPoolCCYON(pool, context_, object_));
1224 return [super cy$toCYON:objective];
1226 return [NSString stringWithUTF8String:cyon];
1227 } CYObjectiveCatch }
1229 - (NSUInteger) count { CYObjectiveTry {
1230 JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_));
1231 size_t size(JSPropertyNameArrayGetCount(names));
1232 JSPropertyNameArrayRelease(names);
1234 } CYObjectiveCatch }
1236 - (id) objectForKey:(id)key { CYObjectiveTry {
1237 JSValueRef value(CYGetProperty(context_, object_, CYJSString(context_, (NSObject *) key)));
1238 if (JSValueIsUndefined(context_, value))
1240 return CYCastNSObject(NULL, context_, value) ?: [NSNull null];
1241 } CYObjectiveCatch }
1243 - (NSEnumerator *) keyEnumerator { CYObjectiveTry {
1244 JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_));
1245 NSEnumerator *enumerator([CYCastNSArray(context_, names) objectEnumerator]);
1246 JSPropertyNameArrayRelease(names);
1248 } CYObjectiveCatch }
1250 - (void) setObject:(id)object forKey:(id)key { CYObjectiveTry {
1251 CYSetProperty(context_, object_, CYJSString(context_, (NSObject *) key), CYCastJSValue(context_, (NSString *) object));
1252 } CYObjectiveCatch }
1254 - (void) removeObjectForKey:(id)key { CYObjectiveTry {
1255 JSValueRef exception(NULL);
1256 (void) JSObjectDeleteProperty(context_, object_, CYJSString(context_, (NSObject *) key), &exception);
1257 CYThrow(context_, exception);
1258 } CYObjectiveCatch }
1262 @implementation CYJSArray
1264 - (NSString *) cy$toCYON:(bool)objective {
1266 return [NSString stringWithUTF8String:CYPoolCCYON(pool, context_, object_)];
1269 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { CYObjectiveTry {
1270 if ((self = [super init]) != nil) {
1272 context_ = CYGetJSContext(context);
1273 JSGlobalContextRetain(context_);
1274 JSValueProtect(context_, object_);
1276 } CYObjectiveCatch }
1278 - (void) dealloc { CYObjectiveTry {
1279 JSValueUnprotect(context_, object_);
1280 JSGlobalContextRelease(context_);
1282 } CYObjectiveCatch }
1284 - (NSUInteger) count { CYObjectiveTry {
1285 return CYArrayLength(context_, object_);
1286 } CYObjectiveCatch }
1288 - (id) objectAtIndex:(NSUInteger)index { CYObjectiveTry {
1289 size_t bounds([self count]);
1290 if (index >= bounds)
1291 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray objectAtIndex:]: index (%zu) beyond bounds (%zu)", static_cast<size_t>(index), bounds] userInfo:nil];
1292 JSValueRef exception(NULL);
1293 JSValueRef value(JSObjectGetPropertyAtIndex(context_, object_, index, &exception));
1294 CYThrow(context_, exception);
1295 return CYCastNSObject(NULL, context_, value) ?: [NSNull null];
1296 } CYObjectiveCatch }
1298 - (void) addObject:(id)object { CYObjectiveTry {
1299 CYArrayPush(context_, object_, CYCastJSValue(context_, (NSObject *) object));
1300 } CYObjectiveCatch }
1302 - (void) insertObject:(id)object atIndex:(NSUInteger)index { CYObjectiveTry {
1303 size_t bounds([self count] + 1);
1304 if (index >= bounds)
1305 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray insertObject:atIndex:]: index (%zu) beyond bounds (%zu)", static_cast<size_t>(index), bounds] userInfo:nil];
1306 JSValueRef exception(NULL);
1307 JSValueRef arguments[3];
1308 arguments[0] = CYCastJSValue(context_, index);
1309 arguments[1] = CYCastJSValue(context_, 0);
1310 arguments[2] = CYCastJSValue(context_, (NSObject *) object);
1311 JSObjectRef Array(CYGetCachedObject(context_, CYJSString("Array_prototype")));
1312 JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array, splice_s)), object_, 3, arguments, &exception);
1313 CYThrow(context_, exception);
1314 } CYObjectiveCatch }
1316 - (void) removeLastObject { CYObjectiveTry {
1317 JSValueRef exception(NULL);
1318 JSObjectRef Array(CYGetCachedObject(context_, CYJSString("Array_prototype")));
1319 JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array, pop_s)), object_, 0, NULL, &exception);
1320 CYThrow(context_, exception);
1321 } CYObjectiveCatch }
1323 - (void) removeObjectAtIndex:(NSUInteger)index { CYObjectiveTry {
1324 size_t bounds([self count]);
1325 if (index >= bounds)
1326 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray removeObjectAtIndex:]: index (%zu) beyond bounds (%zu)", static_cast<size_t>(index), bounds] userInfo:nil];
1327 JSValueRef exception(NULL);
1328 JSValueRef arguments[2];
1329 arguments[0] = CYCastJSValue(context_, index);
1330 arguments[1] = CYCastJSValue(context_, 1);
1331 JSObjectRef Array(CYGetCachedObject(context_, CYJSString("Array_prototype")));
1332 JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array, splice_s)), object_, 2, arguments, &exception);
1333 CYThrow(context_, exception);
1334 } CYObjectiveCatch }
1336 - (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)object { CYObjectiveTry {
1337 size_t bounds([self count]);
1338 if (index >= bounds)
1339 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray replaceObjectAtIndex:withObject:]: index (%zu) beyond bounds (%zu)", static_cast<size_t>(index), bounds] userInfo:nil];
1340 CYSetProperty(context_, object_, index, CYCastJSValue(context_, (NSObject *) object));
1341 } CYObjectiveCatch }
1345 // XXX: inherit from or replace with CYJSObject
1346 @interface CYInternal : NSObject {
1347 JSGlobalContextRef context_;
1348 JSObjectRef object_;
1353 @implementation CYInternal
1356 JSValueUnprotect(context_, object_);
1357 JSGlobalContextRelease(context_);
1361 - (id) initInContext:(JSContextRef)context {
1362 if ((self = [super init]) != nil) {
1363 context_ = CYGetJSContext(context);
1364 JSGlobalContextRetain(context_);
1368 - (bool) hasProperty:(JSStringRef)name inContext:(JSContextRef)context {
1369 if (object_ == NULL)
1372 return JSObjectHasProperty(context, object_, name);
1375 - (JSValueRef) getProperty:(JSStringRef)name inContext:(JSContextRef)context {
1376 if (object_ == NULL)
1379 return CYGetProperty(context, object_, name);
1382 - (void) setProperty:(JSStringRef)name toValue:(JSValueRef)value inContext:(JSContextRef)context {
1383 @synchronized (self) {
1384 if (object_ == NULL) {
1385 object_ = JSObjectMake(context, NULL, NULL);
1386 JSValueProtect(context, object_);
1390 CYSetProperty(context, object_, name, value);
1393 + (CYInternal *) get:(id)object {
1394 if ($objc_getAssociatedObject == NULL)
1397 @synchronized (object) {
1398 if (CYInternal *internal = $objc_getAssociatedObject(object, @selector(cy$internal)))
1405 + (CYInternal *) set:(id)object inContext:(JSContextRef)context {
1406 if ($objc_getAssociatedObject == NULL)
1409 @synchronized (object) {
1410 if (CYInternal *internal = $objc_getAssociatedObject(object, @selector(cy$internal)))
1413 if ($objc_setAssociatedObject == NULL)
1416 CYInternal *internal([[[CYInternal alloc] initInContext:context] autorelease]);
1417 objc_setAssociatedObject(object, @selector(cy$internal), internal, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
1426 static JSObjectRef CYMakeSelector(JSContextRef context, SEL sel) {
1427 Selector_privateData *internal(new Selector_privateData(sel));
1428 return JSObjectMake(context, Selector_, internal);
1431 static SEL CYCastSEL(JSContextRef context, JSValueRef value) {
1432 if (JSValueIsObjectOfClass(context, value, Selector_)) {
1433 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate((JSObjectRef) value)));
1434 return reinterpret_cast<SEL>(internal->value_);
1436 return CYCastPointer<SEL>(context, value);
1439 void *CYObjectiveC_ExecuteStart(JSContextRef context) { CYSadTry {
1440 return (void *) [[NSAutoreleasePool alloc] init];
1441 } CYSadCatch(NULL) }
1443 void CYObjectiveC_ExecuteEnd(JSContextRef context, void *handle) { CYSadTry {
1444 return [(NSAutoreleasePool *) handle release];
1447 JSValueRef CYObjectiveC_RuntimeProperty(JSContextRef context, CYUTF8String name) { CYPoolTry {
1449 return Instance::Make(context, nil);
1450 if (Class _class = objc_getClass(name.data))
1451 return CYMakeInstance(context, _class, true);
1452 if (Protocol *protocol = objc_getProtocol(name.data))
1453 return CYMakeInstance(context, protocol, true);
1455 } CYPoolCatch(NULL) return /*XXX*/ NULL; }
1457 static void CYObjectiveC_CallFunction(JSContextRef context, ffi_cif *cif, void (*function)(), uint8_t *value, void **values) { CYSadTry {
1458 ffi_call(cif, function, value, values);
1461 static bool CYObjectiveC_PoolFFI(CYPool *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { CYSadTry {
1462 switch (type->primitive) {
1463 // XXX: do something epic about blocks
1466 case sig::typename_P:
1467 // XXX: this works for return values, but not for properties and fields
1468 *reinterpret_cast<id *>(data) = CYCastNSObject(pool, context, value);
1471 case sig::selector_P:
1472 *reinterpret_cast<SEL *>(data) = CYCastSEL(context, value);
1480 } CYSadCatch(false) }
1482 static JSValueRef CYObjectiveC_FromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) { CYPoolTry {
1483 switch (type->primitive) {
1484 // XXX: do something epic about blocks
1487 if (NSObject *object = *reinterpret_cast<NSObject **>(data)) {
1488 JSValueRef value(CYCastJSValue(context, object));
1494 case sig::typename_P:
1495 return CYMakeInstance(context, *reinterpret_cast<Class *>(data), true);
1497 case sig::selector_P:
1498 if (SEL sel = *reinterpret_cast<SEL *>(data))
1499 return CYMakeSelector(context, sel);
1503 return CYJSNull(context);
1507 } CYPoolCatch(NULL) return /*XXX*/ NULL; }
1509 static bool CYImplements(id object, Class _class, SEL selector, bool devoid = false) {
1510 if (objc_method *method = class_getInstanceMethod(_class, selector)) {
1513 #if OBJC_API_VERSION >= 2
1515 method_getReturnType(method, type, sizeof(type));
1517 const char *type(method_getTypeEncoding(method));
1523 // XXX: possibly use a more "awesome" check?
1527 static const char *CYPoolTypeEncoding(CYPool &pool, JSContextRef context, SEL sel, objc_method *method) {
1529 return method_getTypeEncoding(method);
1531 const char *name(sel_getName(sel));
1532 size_t length(strlen(name));
1534 char keyed[length + 2];
1536 keyed[length + 1] = '\0';
1537 memcpy(keyed + 1, name, length);
1539 if (CYBridgeEntry *entry = CYBridgeHash(keyed, length + 1))
1540 return entry->value_;
1545 static JSValueRef MessageAdapter_(JSContextRef context, size_t count, JSValueRef values[], JSObjectRef function) {
1546 JSObjectRef _this(CYCastJSObject(context, values[0]));
1547 return CYCallAsFunction(context, function, _this, count - 2, values + 2);
1550 static void MessageClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) {
1551 CYExecuteClosure(cif, result, arguments, arg, &MessageAdapter_);
1554 static JSObjectRef CYMakeMessage(JSContextRef context, SEL sel, IMP imp, const char *type) {
1555 Message_privateData *internal(new Message_privateData(sel, type, imp));
1556 return JSObjectMake(context, Message_, internal);
1559 static IMP CYMakeMessage(JSContextRef context, JSValueRef value, const char *type) {
1560 JSObjectRef function(CYCastJSObject(context, value));
1561 Closure_privateData *internal(CYMakeFunctor_(context, function, type, &MessageClosure_));
1562 // XXX: see notes in Library.cpp about needing to leak
1563 return reinterpret_cast<IMP>(internal->GetValue());
1566 static bool Messages_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
1567 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1568 Class _class(internal->GetValue());
1571 const char *name(CYPoolCString(pool, context, property));
1573 if (SEL sel = sel_getUid(name))
1574 if (class_getInstanceMethod(_class, sel) != NULL)
1580 static JSValueRef Messages_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1581 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1582 Class _class(internal->GetValue());
1585 const char *name(CYPoolCString(pool, context, property));
1587 if (SEL sel = sel_getUid(name))
1588 if (objc_method *method = class_getInstanceMethod(_class, sel))
1589 return CYMakeMessage(context, sel, method_getImplementation(method), method_getTypeEncoding(method));
1594 static bool Messages_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
1595 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1596 Class _class(internal->GetValue());
1599 const char *name(CYPoolCString(pool, context, property));
1601 SEL sel(sel_registerName(name));
1603 objc_method *method(class_getInstanceMethod(_class, sel));
1608 if (JSValueIsObjectOfClass(context, value, Message_)) {
1609 Message_privateData *message(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate((JSObjectRef) value)));
1610 type = sig::Unparse(pool, &message->signature_);
1611 imp = reinterpret_cast<IMP>(message->GetValue());
1613 type = CYPoolTypeEncoding(pool, context, sel, method);
1614 imp = CYMakeMessage(context, value, type);
1618 method_setImplementation(method, imp);
1621 GSMethodList list(GSAllocMethodList(1));
1622 GSAppendMethodToList(list, sel, type, imp, YES);
1623 GSAddMethodList(_class, list, YES);
1624 GSFlushMethodCacheForClass(_class);
1626 class_addMethod(_class, sel, imp, type);
1633 #if 0 && OBJC_API_VERSION < 2
1634 static bool Messages_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1635 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1636 Class _class(internal->GetValue());
1639 const char *name(CYPoolCString(pool, context, property));
1641 if (SEL sel = sel_getUid(name))
1642 if (objc_method *method = class_getInstanceMethod(_class, sel)) {
1643 objc_method_list list = {NULL, 1, {method}};
1644 class_removeMethods(_class, &list);
1652 static void Messages_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1653 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1654 Class _class(internal->GetValue());
1656 #if OBJC_API_VERSION >= 2
1658 objc_method **data(class_copyMethodList(_class, &size));
1659 for (size_t i(0); i != size; ++i)
1660 JSPropertyNameAccumulatorAddName(names, CYJSString(sel_getName(method_getName(data[i]))));
1663 for (objc_method_list *methods(_class->methods); methods != NULL; methods = methods->method_next)
1664 for (int i(0); i != methods->method_count; ++i)
1665 JSPropertyNameAccumulatorAddName(names, CYJSString(sel_getName(method_getName(&methods->method_list[i]))));
1669 static bool CYHasImplicitProperties(Class _class) {
1670 // XXX: this is an evil hack to deal with NSProxy; fix elsewhere
1671 if (!CYImplements(_class, object_getClass(_class), @selector(cy$hasImplicitProperties)))
1673 return [_class cy$hasImplicitProperties];
1676 static bool Instance_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
1677 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1678 id self(internal->GetValue());
1680 if (JSStringIsEqualToUTF8CString(property, "$cyi"))
1684 NSString *name(CYCastNSString(&pool, context, property));
1686 if (CYInternal *internal = [CYInternal get:self])
1687 if ([internal hasProperty:property inContext:context])
1690 Class _class(object_getClass(self));
1693 // XXX: this is an evil hack to deal with NSProxy; fix elsewhere
1694 if (CYImplements(self, _class, @selector(cy$hasProperty:)))
1695 if ([self cy$hasProperty:name])
1697 } CYPoolCatch(false)
1699 const char *string(CYPoolCString(pool, context, name));
1702 if (class_getProperty(_class, string) != NULL)
1706 if (CYHasImplicitProperties(_class))
1707 if (SEL sel = sel_getUid(string))
1708 if (CYImplements(self, _class, sel, true))
1714 static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1715 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1716 id self(internal->GetValue());
1718 if (JSStringIsEqualToUTF8CString(property, "$cyi"))
1719 return Internal::Make(context, self, object);
1722 NSString *name(CYCastNSString(&pool, context, property));
1724 if (CYInternal *internal = [CYInternal get:self])
1725 if (JSValueRef value = [internal getProperty:property inContext:context])
1729 if (JSValueRef value = [self cy$getProperty:name inContext:context])
1733 const char *string(CYPoolCString(pool, context, name));
1734 Class _class(object_getClass(self));
1737 if (objc_property_t property = class_getProperty(_class, string)) {
1738 PropertyAttributes attributes(property);
1739 SEL sel(sel_registerName(attributes.Getter()));
1740 return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception);
1744 if (CYHasImplicitProperties(_class))
1745 if (SEL sel = sel_getUid(string))
1746 if (CYImplements(self, _class, sel, true))
1747 return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception);
1752 static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
1753 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1754 id self(internal->GetValue());
1758 NSString *name(CYCastNSString(&pool, context, property));
1759 NSObject *data(CYCastNSObject(&pool, context, value));
1762 if ([self cy$setProperty:name to:data])
1764 } CYPoolCatch(false)
1766 const char *string(CYPoolCString(pool, context, name));
1767 Class _class(object_getClass(self));
1770 if (objc_property_t property = class_getProperty(_class, string)) {
1771 PropertyAttributes attributes(property);
1772 if (const char *setter = attributes.Setter()) {
1773 SEL sel(sel_registerName(setter));
1774 JSValueRef arguments[1] = {value};
1775 CYSendMessage(pool, context, self, NULL, sel, 1, arguments, false, exception);
1781 size_t length(strlen(string));
1783 char set[length + 5];
1789 if (string[0] != '\0') {
1790 set[3] = toupper(string[0]);
1791 memcpy(set + 4, string + 1, length - 1);
1794 set[length + 3] = ':';
1795 set[length + 4] = '\0';
1797 if (SEL sel = sel_getUid(set))
1798 if (CYImplements(self, _class, sel)) {
1799 JSValueRef arguments[1] = {value};
1800 CYSendMessage(pool, context, self, NULL, sel, 1, arguments, false, exception);
1804 if (CYInternal *internal = [CYInternal set:self inContext:context]) {
1805 [internal setProperty:property toValue:value inContext:context];
1812 static bool Instance_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1813 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1814 id self(internal->GetValue());
1817 NSString *name(CYCastNSString(NULL, context, property));
1818 return [self cy$deleteProperty:name];
1819 } CYPoolCatch(false)
1820 } CYCatch(false) return /*XXX*/ false; }
1822 static void Instance_getPropertyNames_message(JSPropertyNameAccumulatorRef names, objc_method *method) {
1823 const char *name(sel_getName(method_getName(method)));
1824 if (strchr(name, ':') != NULL)
1827 const char *type(method_getTypeEncoding(method));
1828 if (type == NULL || *type == '\0' || *type == 'v')
1831 JSPropertyNameAccumulatorAddName(names, CYJSString(name));
1834 static void Instance_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1835 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1836 id self(internal->GetValue());
1839 Class _class(object_getClass(self));
1844 objc_property_t *data(class_copyPropertyList(_class, &size));
1845 for (size_t i(0); i != size; ++i)
1846 JSPropertyNameAccumulatorAddName(names, CYJSString(property_getName(data[i])));
1851 if (CYHasImplicitProperties(_class))
1852 for (Class current(_class); current != nil; current = class_getSuperclass(current)) {
1853 #if OBJC_API_VERSION >= 2
1855 objc_method **data(class_copyMethodList(current, &size));
1856 for (size_t i(0); i != size; ++i)
1857 Instance_getPropertyNames_message(names, data[i]);
1860 for (objc_method_list *methods(current->methods); methods != NULL; methods = methods->method_next)
1861 for (int i(0); i != methods->method_count; ++i)
1862 Instance_getPropertyNames_message(names, &methods->method_list[i]);
1867 // XXX: this is an evil hack to deal with NSProxy; fix elsewhere
1868 if (CYImplements(self, _class, @selector(cy$getPropertyNames:inContext:)))
1869 [self cy$getPropertyNames:names inContext:context];
1873 static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1874 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1875 JSObjectRef value(Instance::Make(context, [internal->GetValue() alloc], Instance::Uninitialized));
1879 static JSValueRef Instance_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1880 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1881 id self(internal->GetValue());
1883 if (![self isKindOfClass:NSBlock_])
1884 CYThrow("non-NSBlock object is not a function");
1885 // XXX: replace above logic with the following assertion
1886 //_assert([self isKindOfClass:NSBlock_]);
1887 // to do this, make it so FunctionInstance_ is the class of blocks
1888 // to do /that/, generalize the various "is exactly Instance_" checks
1889 // then, move Instance_callAsFunction to only be on FunctionInstance
1891 BlockLiteral *literal(reinterpret_cast<BlockLiteral *>(self));
1893 if ((literal->flags & BLOCK_HAS_SIGNATURE) != 0) {
1894 uint8_t *descriptor(reinterpret_cast<uint8_t *>(literal->descriptor));
1895 descriptor += sizeof(BlockDescriptor1);
1896 if ((literal->flags & BLOCK_HAS_COPY_DISPOSE) != 0)
1897 descriptor += sizeof(BlockDescriptor2);
1898 BlockDescriptor3 *descriptor3(reinterpret_cast<BlockDescriptor3 *>(descriptor));
1900 if (const char *type = descriptor3->signature) {
1906 sig::Signature signature;
1907 sig::Parse(pool, &signature, type, &Structor_);
1910 sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif);
1912 void (*function)() = reinterpret_cast<void (*)()>(literal->invoke);
1913 return CYCallFunction(pool, context, 1, setup, count, arguments, false, exception, &signature, &cif, function);
1918 CYThrow("NSBlock without signature field passed arguments");
1922 } CYPoolCatch(NULL);
1927 static bool Instance_hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef instance, JSValueRef *exception) { CYTry {
1928 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) constructor)));
1929 Class _class(internal->GetValue());
1930 if (!CYIsClass(_class))
1933 if (CYJSValueIsNSObject(context, instance)) {
1934 Instance *linternal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) instance)));
1935 // XXX: this isn't always safe
1936 return [linternal->GetValue() isKindOfClass:_class];
1942 static JSValueRef Instance_box_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1944 throw CYJSError(context, "incorrect number of arguments to Instance");
1946 id value(CYCastNSObject(&pool, context, arguments[0]));
1948 value = [NSNull null];
1949 return CYCastJSValue(context, [value cy$box]);
1952 static bool Internal_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
1953 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1956 id self(internal->GetValue());
1957 const char *name(CYPoolCString(pool, context, property));
1959 if (object_getInstanceVariable(self, name, NULL) != NULL)
1965 static JSValueRef Internal_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1966 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1969 id self(internal->GetValue());
1970 const char *name(CYPoolCString(pool, context, property));
1972 if (objc_ivar *ivar = object_getInstanceVariable(self, name, NULL)) {
1973 Type_privateData type(pool, ivar_getTypeEncoding(ivar));
1974 // XXX: if this fails and throws an exception the person we are throwing it to gets the wrong exception
1975 return CYFromFFI(context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar));
1981 static bool Internal_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
1982 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1985 id self(internal->GetValue());
1986 const char *name(CYPoolCString(pool, context, property));
1988 if (objc_ivar *ivar = object_getInstanceVariable(self, name, NULL)) {
1989 Type_privateData type(pool, ivar_getTypeEncoding(ivar));
1990 CYPoolFFI(&pool, context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar), value);
1997 static void Internal_getPropertyNames_(Class _class, JSPropertyNameAccumulatorRef names) {
1998 if (Class super = class_getSuperclass(_class))
1999 Internal_getPropertyNames_(super, names);
2001 #if OBJC_API_VERSION >= 2
2003 objc_ivar **data(class_copyIvarList(_class, &size));
2004 for (size_t i(0); i != size; ++i)
2005 JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(data[i])));
2008 if (objc_ivar_list *ivars = _class->ivars)
2009 for (int i(0); i != ivars->ivar_count; ++i)
2010 JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(&ivars->ivar_list[i])));
2014 static void Internal_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
2015 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
2018 id self(internal->GetValue());
2019 Class _class(object_getClass(self));
2021 Internal_getPropertyNames_(_class, names);
2024 static JSValueRef Internal_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2025 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
2026 return internal->GetOwner();
2029 static JSValueRef ObjectiveC_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2031 NSString *name(CYCastNSString(&pool, context, property));
2032 if (Class _class = NSClassFromString(name))
2033 return CYMakeInstance(context, _class, true);
2037 static void ObjectiveC_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
2039 size_t size(objc_getClassList(NULL, 0));
2040 Class *data(reinterpret_cast<Class *>(malloc(sizeof(Class) * size)));
2043 size_t writ(objc_getClassList(data, size));
2046 if (Class *copy = reinterpret_cast<Class *>(realloc(data, sizeof(Class) * writ))) {
2052 for (size_t i(0); i != writ; ++i)
2053 JSPropertyNameAccumulatorAddName(names, CYJSString(class_getName(data[i])));
2059 while (Class _class = objc_next_class(&state))
2060 JSPropertyNameAccumulatorAddName(names, CYJSString(class_getName(_class)));
2064 #if OBJC_API_VERSION >= 2
2065 static JSValueRef ObjectiveC_Image_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2066 const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object)));
2069 const char *name(CYPoolCString(pool, context, property));
2071 const char **data(objc_copyClassNamesForImage(internal, &size));
2073 for (size_t i(0); i != size; ++i)
2074 if (strcmp(name, data[i]) == 0) {
2075 if (Class _class = objc_getClass(name)) {
2076 value = CYMakeInstance(context, _class, true);
2087 static void ObjectiveC_Image_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
2088 const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object)));
2090 const char **data(objc_copyClassNamesForImage(internal, &size));
2091 for (size_t i(0); i != size; ++i)
2092 JSPropertyNameAccumulatorAddName(names, CYJSString(data[i]));
2096 static JSValueRef ObjectiveC_Images_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2098 const char *name(CYPoolCString(pool, context, property));
2100 const char **data(objc_copyImageNames(&size));
2101 for (size_t i(0); i != size; ++i)
2102 if (strcmp(name, data[i]) == 0) {
2111 JSObjectRef value(JSObjectMake(context, NULL, NULL));
2112 CYSetProperty(context, value, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Image_Classes_, const_cast<char *>(name)));
2116 static void ObjectiveC_Images_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
2118 const char **data(objc_copyImageNames(&size));
2119 for (size_t i(0); i != size; ++i)
2120 JSPropertyNameAccumulatorAddName(names, CYJSString(data[i]));
2125 static JSValueRef ObjectiveC_Protocols_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2127 const char *name(CYPoolCString(pool, context, property));
2128 if (Protocol *protocol = objc_getProtocol(name))
2129 return CYMakeInstance(context, protocol, true);
2133 static void ObjectiveC_Protocols_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
2134 #if OBJC_API_VERSION >= 2
2136 Protocol **data(objc_copyProtocolList(&size));
2137 for (size_t i(0); i != size; ++i)
2138 JSPropertyNameAccumulatorAddName(names, CYJSString(protocol_getName(data[i])));
2145 static JSValueRef ObjectiveC_Constants_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2147 CYUTF8String name(CYPoolUTF8String(pool, context, property));
2149 return Instance::Make(context, nil);
2153 static void ObjectiveC_Constants_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
2154 JSPropertyNameAccumulatorAddName(names, CYJSString("nil"));
2158 #if defined(__i386__) || defined(__x86_64__)
2159 #define OBJC_MAX_STRUCT_BY_VALUE 8
2160 static int struct_forward_array[] = {
2161 0, 0, 0, 1, 0, 1, 1, 1, 0 };
2162 #elif defined(__arm__)
2163 #define OBJC_MAX_STRUCT_BY_VALUE 1
2164 static int struct_forward_array[] = {
2166 #elif defined(__arm64__)
2169 #error missing objc-runtime-info
2173 static bool stret(ffi_type *ffi_type) {
2174 return ffi_type->type == FFI_TYPE_STRUCT && (
2175 ffi_type->size > OBJC_MAX_STRUCT_BY_VALUE ||
2176 struct_forward_array[ffi_type->size] != 0
2182 JSValueRef CYSendMessage(CYPool &pool, JSContextRef context, id self, Class _class, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception) { CYTry {
2186 _class = object_getClass(self);
2190 if (objc_method *method = class_getInstanceMethod(_class, _cmd)) {
2191 imp = method_getImplementation(method);
2192 type = method_getTypeEncoding(method);
2197 NSMethodSignature *method([self methodSignatureForSelector:_cmd]);
2199 throw CYJSError(context, "unrecognized selector %s sent to object %p", sel_getName(_cmd), self);
2200 type = CYPoolCString(pool, context, [method _typeString]);
2208 sig::Signature signature;
2209 sig::Parse(pool, &signature, type, &Structor_);
2211 size_t used(count + 3);
2212 if (used > signature.count) {
2213 sig::Element *elements(new (pool) sig::Element[used]);
2214 memcpy(elements, signature.elements, used * sizeof(sig::Element));
2216 for (size_t index(signature.count); index != used; ++index) {
2217 sig::Element *element(&elements[index]);
2218 element->name = NULL;
2219 element->offset = _not(size_t);
2221 sig::Type *type(new (pool) sig::Type);
2222 memset(type, 0, sizeof(*type));
2223 type->primitive = sig::object_P;
2224 element->type = type;
2227 signature.elements = elements;
2228 signature.count = used;
2232 sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif);
2237 if (stret(cif.rtype))
2238 imp = class_getMethodImplementation_stret(_class, _cmd);
2241 imp = class_getMethodImplementation(_class, _cmd);
2243 objc_super super = {self, _class};
2244 imp = objc_msg_lookup_super(&super, _cmd);
2248 void (*function)() = reinterpret_cast<void (*)()>(imp);
2249 return CYCallFunction(pool, context, 2, setup, count, arguments, initialize, exception, &signature, &cif, function);
2252 static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2254 throw CYJSError(context, "too few arguments to objc_msgSend");
2264 if (JSValueIsObjectOfClass(context, arguments[0], Super_)) {
2265 cy::Super *internal(reinterpret_cast<cy::Super *>(JSObjectGetPrivate((JSObjectRef) arguments[0])));
2266 self = internal->GetValue();
2267 _class = internal->class_;;
2268 uninitialized = false;
2269 } else if (CYJSValueIsNSObject(context, arguments[0])) {
2270 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) arguments[0])));
2271 self = internal->GetValue();
2273 uninitialized = internal->IsUninitialized();
2275 internal->value_ = nil;
2277 self = CYCastNSObject(&pool, context, arguments[0]);
2279 uninitialized = false;
2283 return CYJSNull(context);
2285 _cmd = CYCastSEL(context, arguments[1]);
2287 return CYSendMessage(pool, context, self, _class, _cmd, count - 2, arguments + 2, uninitialized, exception);
2290 static JSValueRef Selector_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2291 JSValueRef setup[count + 2];
2294 memcpy(setup + 2, arguments, sizeof(JSValueRef) * count);
2295 return $objc_msgSend(context, NULL, NULL, count + 2, setup, exception);
2298 static JSValueRef Message_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2300 Message_privateData *internal(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate(object)));
2302 // XXX: handle Instance::Uninitialized?
2303 id self(CYCastNSObject(&pool, context, _this));
2307 setup[1] = &internal->sel_;
2309 return CYCallFunction(pool, context, 2, setup, count, arguments, false, exception, &internal->signature_, &internal->cif_, internal->GetValue());
2312 static JSObjectRef Super_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2314 throw CYJSError(context, "incorrect number of arguments to Super constructor");
2316 id self(CYCastNSObject(&pool, context, arguments[0]));
2317 Class _class(CYCastClass(pool, context, arguments[1]));
2318 return cy::Super::Make(context, self, _class);
2321 static JSObjectRef Selector_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2323 throw CYJSError(context, "incorrect number of arguments to Selector constructor");
2325 const char *name(CYPoolCString(pool, context, arguments[0]));
2326 return CYMakeSelector(context, sel_registerName(name));
2329 static JSObjectRef Instance_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2331 throw CYJSError(context, "incorrect number of arguments to Instance constructor");
2332 id self(count == 0 ? nil : CYCastPointer<id>(context, arguments[0]));
2333 return CYMakeInstance(context, self, false);
2336 static JSValueRef CYValue_getProperty_value(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2337 CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(object)));
2338 return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_));
2341 static JSValueRef CYValue_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2342 CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this)));
2343 Type_privateData *typical(internal->GetType());
2348 if (typical == NULL) {
2352 type = typical->type_;
2353 ffi = typical->ffi_;
2356 return CYMakePointer(context, &internal->value_, _not(size_t), type, ffi, object);
2359 static JSValueRef Instance_getProperty_constructor(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2360 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
2361 return Instance::Make(context, (id) object_getClass(internal->GetValue()));
2364 static JSValueRef Instance_getProperty_prototype(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2365 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
2366 id self(internal->GetValue());
2367 if (!CYIsClass(self))
2368 return CYJSUndefined(context);
2369 return CYGetClassPrototype(context, self);
2372 static JSValueRef Instance_getProperty_messages(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2373 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
2374 id self(internal->GetValue());
2375 if (!CYIsClass(self))
2376 return CYJSUndefined(context);
2377 return Messages::Make(context, (Class) self);
2380 static JSValueRef Instance_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2381 if (!CYJSValueIsNSObject(context, _this))
2384 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2385 return CYCastJSValue(context, CYJSString(context, CYCastNSCYON(internal->GetValue(), false)));
2388 static JSValueRef Instance_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2389 if (!CYJSValueIsNSObject(context, _this))
2392 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2393 id value(internal->GetValue());
2400 key = CYCastNSString(NULL, context, CYJSString(context, arguments[0]));
2402 if (!CYImplements(value, object_getClass(value), @selector(cy$toJSON:inContext:)))
2403 return CYJSUndefined(context);
2404 else if (JSValueRef json = [value cy$toJSON:key inContext:context])
2407 return CYCastJSValue(context, CYJSString(context, [value description]));
2409 } CYCatch(NULL) return /*XXX*/ NULL; }
2411 static JSValueRef Instance_callAsFunction_valueOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2412 if (!CYJSValueIsNSObject(context, _this))
2415 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2416 id value(internal->GetValue());
2418 if (![value respondsToSelector:@selector(cy$valueOfInContext:)])
2421 if (JSValueRef result = [value cy$valueOfInContext:context])
2425 } CYCatch(NULL) return /*XXX*/ NULL; }
2427 static JSValueRef Instance_callAsFunction_toPointer(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2428 if (!CYJSValueIsNSObject(context, _this))
2431 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2432 // XXX: but... but... THIS ISN'T A POINTER! :(
2433 return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->GetValue()));
2434 } CYCatch(NULL) return /*XXX*/ NULL; }
2436 static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2437 if (!CYJSValueIsNSObject(context, _this))
2440 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2441 id value(internal->GetValue());
2444 // XXX: this seems like a stupid implementation; what if it crashes? why not use the CYONifier backend?
2445 return CYCastJSValue(context, CYJSString(context, [value description]));
2447 } CYCatch(NULL) return /*XXX*/ NULL; }
2449 static JSValueRef Class_callAsFunction_pointerTo(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2450 if (!CYJSValueIsNSObject(context, _this))
2453 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2454 id value(internal->GetValue());
2456 if (!CYIsClass(value))
2457 CYThrow("non-Class object cannot be used as Type");
2459 // XXX: this is a very silly implementation
2461 std::ostringstream type;
2463 type << class_getName(value);
2467 return CYMakeType(context, type.str().c_str());
2469 } CYCatch(NULL) return /*XXX*/ NULL; }
2471 static JSValueRef Selector_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2472 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
2473 return CYCastJSValue(context, sel_getName(internal->GetValue()));
2476 static JSValueRef Selector_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2477 return Selector_callAsFunction_toString(context, object, _this, count, arguments, exception);
2480 static JSValueRef Selector_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2481 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
2482 const char *name(sel_getName(internal->GetValue()));
2485 NSString *string([NSString stringWithFormat:@"@selector(%s)", name]);
2486 return CYCastJSValue(context, CYJSString(context, string));
2488 } CYCatch(NULL) return /*XXX*/ NULL; }
2490 static JSValueRef Selector_callAsFunction_type(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2492 throw CYJSError(context, "incorrect number of arguments to Selector.type");
2495 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
2496 SEL sel(internal->GetValue());
2498 objc_method *method;
2499 if (Class _class = CYCastClass(pool, context, arguments[0]))
2500 method = class_getInstanceMethod(_class, sel);
2504 if (const char *type = CYPoolTypeEncoding(pool, context, sel, method))
2505 return CYCastJSValue(context, CYJSString(type));
2507 return CYJSNull(context);
2510 static JSStaticValue Selector_staticValues[2] = {
2511 {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete},
2512 {NULL, NULL, NULL, 0}
2515 static JSStaticValue Instance_staticValues[5] = {
2516 {"constructor", &Instance_getProperty_constructor, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2517 {"messages", &Instance_getProperty_messages, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2518 {"prototype", &Instance_getProperty_prototype, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2519 {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2520 {NULL, NULL, NULL, 0}
2523 static JSStaticFunction Instance_staticFunctions[7] = {
2524 {"$cya", &CYValue_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2525 {"toCYON", &Instance_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2526 {"toJSON", &Instance_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2527 {"valueOf", &Instance_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2528 {"toPointer", &Instance_callAsFunction_toPointer, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2529 {"toString", &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2533 static JSStaticFunction Class_staticFunctions[2] = {
2534 {"pointerTo", &Class_callAsFunction_pointerTo, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2538 static JSStaticFunction Internal_staticFunctions[2] = {
2539 {"$cya", &Internal_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2543 static JSStaticFunction Selector_staticFunctions[5] = {
2544 {"toCYON", &Selector_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2545 {"toJSON", &Selector_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2546 {"toString", &Selector_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2547 {"type", &Selector_callAsFunction_type, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2552 JSValueRef NSCFType$cy$toJSON$inContext$(id self, SEL sel, JSValueRef key, JSContextRef context) { CYObjectiveTry_(context) {
2553 return CYCastJSValue(context, [(NSString *) CFCopyDescription((CFTypeRef) self) autorelease]);
2554 } CYObjectiveCatch }
2557 void CYObjectiveC_Initialize() { /*XXX*/ JSContextRef context(NULL); CYPoolTry {
2558 $objc_setAssociatedObject = reinterpret_cast<void (*)(id, void *, id value, objc_AssociationPolicy)>(dlsym(RTLD_DEFAULT, "objc_setAssociatedObject"));
2559 $objc_getAssociatedObject = reinterpret_cast<id (*)(id, void *)>(dlsym(RTLD_DEFAULT, "objc_getAssociatedObject"));
2560 $objc_removeAssociatedObjects = reinterpret_cast<void (*)(id)>(dlsym(RTLD_DEFAULT, "objc_removeAssociatedObjects"));
2562 CYPool &pool(CYGetGlobalPool());
2564 Object_type = new(pool) Type_privateData("@");
2565 Selector_type = new(pool) Type_privateData(":");
2568 // XXX: apparently, iOS now has both of these
2569 NSCFBoolean_ = objc_getClass("__NSCFBoolean");
2570 if (NSCFBoolean_ == nil)
2571 NSCFBoolean_ = objc_getClass("NSCFBoolean");
2573 NSCFType_ = objc_getClass("NSCFType");
2574 NSGenericDeallocHandler_ = objc_getClass("__NSGenericDeallocHandler");
2575 NSMessageBuilder_ = objc_getClass("NSMessageBuilder");
2576 NSZombie_ = objc_getClass("_NSZombie_");
2578 NSBoolNumber_ = objc_getClass("NSBoolNumber");
2581 NSArray_ = objc_getClass("NSArray");
2582 NSBlock_ = objc_getClass("NSBlock");
2583 NSDictionary_ = objc_getClass("NSDictionary");
2584 NSString_ = objc_getClass("NSString");
2585 Object_ = objc_getClass("Object");
2587 JSClassDefinition definition;
2589 definition = kJSClassDefinitionEmpty;
2590 definition.className = "Instance";
2591 definition.staticValues = Instance_staticValues;
2592 definition.staticFunctions = Instance_staticFunctions;
2593 definition.hasProperty = &Instance_hasProperty;
2594 definition.getProperty = &Instance_getProperty;
2595 definition.setProperty = &Instance_setProperty;
2596 definition.deleteProperty = &Instance_deleteProperty;
2597 definition.getPropertyNames = &Instance_getPropertyNames;
2598 definition.callAsConstructor = &Instance_callAsConstructor;
2599 definition.callAsFunction = &Instance_callAsFunction;
2600 definition.hasInstance = &Instance_hasInstance;
2601 definition.finalize = &CYFinalize;
2602 Instance_ = JSClassCreate(&definition);
2604 definition.className = "ArrayInstance";
2605 ArrayInstance_ = JSClassCreate(&definition);
2607 definition.className = "FunctionInstance";
2608 FunctionInstance_ = JSClassCreate(&definition);
2610 definition.className = "ObjectInstance";
2611 ObjectInstance_ = JSClassCreate(&definition);
2613 definition.className = "StringInstance";
2614 StringInstance_ = JSClassCreate(&definition);
2616 definition = kJSClassDefinitionEmpty;
2617 definition.className = "Class";
2618 definition.staticFunctions = Class_staticFunctions;
2619 Class_ = JSClassCreate(&definition);
2621 definition = kJSClassDefinitionEmpty;
2622 definition.className = "Internal";
2623 definition.staticFunctions = Internal_staticFunctions;
2624 definition.hasProperty = &Internal_hasProperty;
2625 definition.getProperty = &Internal_getProperty;
2626 definition.setProperty = &Internal_setProperty;
2627 definition.getPropertyNames = &Internal_getPropertyNames;
2628 definition.finalize = &CYFinalize;
2629 Internal_ = JSClassCreate(&definition);
2631 definition = kJSClassDefinitionEmpty;
2632 definition.className = "Message";
2633 definition.staticFunctions = cy::Functor::StaticFunctions;
2634 definition.callAsFunction = &Message_callAsFunction;
2635 definition.finalize = &CYFinalize;
2636 Message_ = JSClassCreate(&definition);
2638 definition = kJSClassDefinitionEmpty;
2639 definition.className = "Messages";
2640 definition.hasProperty = &Messages_hasProperty;
2641 definition.getProperty = &Messages_getProperty;
2642 definition.setProperty = &Messages_setProperty;
2643 #if 0 && OBJC_API_VERSION < 2
2644 definition.deleteProperty = &Messages_deleteProperty;
2646 definition.getPropertyNames = &Messages_getPropertyNames;
2647 definition.finalize = &CYFinalize;
2648 Messages_ = JSClassCreate(&definition);
2650 definition = kJSClassDefinitionEmpty;
2651 definition.className = "Selector";
2652 definition.staticValues = Selector_staticValues;
2653 definition.staticFunctions = Selector_staticFunctions;
2654 definition.callAsFunction = &Selector_callAsFunction;
2655 definition.finalize = &CYFinalize;
2656 Selector_ = JSClassCreate(&definition);
2658 definition = kJSClassDefinitionEmpty;
2659 definition.className = "Super";
2660 definition.staticFunctions = Internal_staticFunctions;
2661 definition.finalize = &CYFinalize;
2662 Super_ = JSClassCreate(&definition);
2664 definition = kJSClassDefinitionEmpty;
2665 definition.className = "ObjectiveC::Classes";
2666 definition.getProperty = &ObjectiveC_Classes_getProperty;
2667 definition.getPropertyNames = &ObjectiveC_Classes_getPropertyNames;
2668 ObjectiveC_Classes_ = JSClassCreate(&definition);
2670 definition = kJSClassDefinitionEmpty;
2671 definition.className = "ObjectiveC::Constants";
2672 definition.getProperty = &ObjectiveC_Constants_getProperty;
2673 definition.getPropertyNames = &ObjectiveC_Constants_getPropertyNames;
2674 ObjectiveC_Constants_ = JSClassCreate(&definition);
2676 #if OBJC_API_VERSION >= 2
2677 definition = kJSClassDefinitionEmpty;
2678 definition.className = "ObjectiveC::Images";
2679 definition.getProperty = &ObjectiveC_Images_getProperty;
2680 definition.getPropertyNames = &ObjectiveC_Images_getPropertyNames;
2681 ObjectiveC_Images_ = JSClassCreate(&definition);
2683 definition = kJSClassDefinitionEmpty;
2684 definition.className = "ObjectiveC::Image::Classes";
2685 definition.getProperty = &ObjectiveC_Image_Classes_getProperty;
2686 definition.getPropertyNames = &ObjectiveC_Image_Classes_getPropertyNames;
2687 ObjectiveC_Image_Classes_ = JSClassCreate(&definition);
2690 definition = kJSClassDefinitionEmpty;
2691 definition.className = "ObjectiveC::Protocols";
2692 definition.getProperty = &ObjectiveC_Protocols_getProperty;
2693 definition.getPropertyNames = &ObjectiveC_Protocols_getPropertyNames;
2694 ObjectiveC_Protocols_ = JSClassCreate(&definition);
2697 class_addMethod(NSCFType_, @selector(cy$toJSON:inContext:), reinterpret_cast<IMP>(&NSCFType$cy$toJSON$inContext$), "^{OpaqueJSValue=}16@0:4@8^{OpaqueJSContext=}12");
2701 void CYObjectiveC_SetupContext(JSContextRef context) { CYPoolTry {
2702 JSObjectRef global(CYGetGlobalObject(context));
2703 JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s)));
2704 JSObjectRef cycript(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Cycript"))));
2705 JSObjectRef all(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("all"))));
2706 JSObjectRef alls(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("alls"))));
2708 JSObjectRef ObjectiveC(JSObjectMake(context, NULL, NULL));
2709 CYSetProperty(context, cycript, CYJSString("ObjectiveC"), ObjectiveC);
2711 JSObjectRef protocols(JSObjectMake(context, ObjectiveC_Protocols_, NULL));
2712 CYSetProperty(context, ObjectiveC, CYJSString("protocols"), protocols);
2713 CYArrayPush(context, alls, protocols);
2715 JSObjectRef classes(JSObjectMake(context, ObjectiveC_Classes_, NULL));
2716 CYSetProperty(context, ObjectiveC, CYJSString("classes"), classes);
2717 CYArrayPush(context, alls, classes);
2719 JSObjectRef constants(JSObjectMake(context, ObjectiveC_Constants_, NULL));
2720 CYSetProperty(context, ObjectiveC, CYJSString("constants"), constants);
2721 CYArrayPush(context, alls, constants);
2723 #if OBJC_API_VERSION >= 2
2724 CYSetProperty(context, ObjectiveC, CYJSString("images"), JSObjectMake(context, ObjectiveC_Images_, NULL));
2727 JSObjectRef Class(JSObjectMakeConstructor(context, Class_, NULL));
2728 JSObjectRef Instance(JSObjectMakeConstructor(context, Instance_, &Instance_new));
2729 JSObjectRef Message(JSObjectMakeConstructor(context, Message_, NULL));
2730 JSObjectRef Selector(JSObjectMakeConstructor(context, Selector_, &Selector_new));
2731 JSObjectRef Super(JSObjectMakeConstructor(context, Super_, &Super_new));
2733 JSObjectRef Instance_prototype(CYCastJSObject(context, CYGetProperty(context, Instance, prototype_s)));
2734 CYSetProperty(context, cy, CYJSString("Instance_prototype"), Instance_prototype);
2736 JSObjectRef ArrayInstance(JSObjectMakeConstructor(context, ArrayInstance_, NULL));
2737 JSObjectRef ArrayInstance_prototype(CYCastJSObject(context, CYGetProperty(context, ArrayInstance, prototype_s)));
2738 CYSetProperty(context, cy, CYJSString("ArrayInstance_prototype"), ArrayInstance_prototype);
2739 JSObjectRef Array_prototype(CYGetCachedObject(context, CYJSString("Array_prototype")));
2740 JSObjectSetPrototype(context, ArrayInstance_prototype, Array_prototype);
2742 JSObjectRef FunctionInstance(JSObjectMakeConstructor(context, FunctionInstance_, NULL));
2743 JSObjectRef FunctionInstance_prototype(CYCastJSObject(context, CYGetProperty(context, FunctionInstance, prototype_s)));
2744 CYSetProperty(context, cy, CYJSString("FunctionInstance_prototype"), FunctionInstance_prototype);
2745 JSObjectRef Function_prototype(CYGetCachedObject(context, CYJSString("Function_prototype")));
2746 JSObjectSetPrototype(context, FunctionInstance_prototype, Function_prototype);
2748 JSObjectRef ObjectInstance(JSObjectMakeConstructor(context, ObjectInstance_, NULL));
2749 JSObjectRef ObjectInstance_prototype(CYCastJSObject(context, CYGetProperty(context, ObjectInstance, prototype_s)));
2750 CYSetProperty(context, cy, CYJSString("ObjectInstance_prototype"), ObjectInstance_prototype);
2751 JSObjectRef Object_prototype(CYGetCachedObject(context, CYJSString("Object_prototype")));
2752 JSObjectSetPrototype(context, ObjectInstance_prototype, Object_prototype);
2754 JSObjectRef StringInstance(JSObjectMakeConstructor(context, StringInstance_, NULL));
2755 JSObjectRef StringInstance_prototype(CYCastJSObject(context, CYGetProperty(context, StringInstance, prototype_s)));
2756 CYSetProperty(context, cy, CYJSString("StringInstance_prototype"), StringInstance_prototype);
2757 JSObjectRef String_prototype(CYGetCachedObject(context, CYJSString("String_prototype")));
2758 JSObjectSetPrototype(context, StringInstance_prototype, String_prototype);
2760 JSObjectRef Class_prototype(CYCastJSObject(context, CYGetProperty(context, Class, prototype_s)));
2761 CYSetProperty(context, cy, CYJSString("Class_prototype"), Class_prototype);
2762 JSObjectSetPrototype(context, Class_prototype, Instance_prototype);
2764 CYSetProperty(context, cycript, CYJSString("Instance"), Instance);
2765 CYSetProperty(context, cycript, CYJSString("Selector"), Selector);
2766 CYSetProperty(context, cycript, CYJSString("Super"), Super);
2768 JSObjectRef box(JSObjectMakeFunctionWithCallback(context, CYJSString("box"), &Instance_box_callAsFunction));
2769 CYSetProperty(context, Instance, CYJSString("box"), box);
2771 #if defined(__APPLE__) && defined(__arm__) && 0
2772 CYSetProperty(context, all, CYJSString("objc_registerClassPair"), &objc_registerClassPair_, kJSPropertyAttributeDontEnum);
2775 CYSetProperty(context, all, CYJSString("objc_msgSend"), &$objc_msgSend, kJSPropertyAttributeDontEnum);
2777 JSObjectSetPrototype(context, CYCastJSObject(context, CYGetProperty(context, Message, prototype_s)), Function_prototype);
2778 JSObjectSetPrototype(context, CYCastJSObject(context, CYGetProperty(context, Selector, prototype_s)), Function_prototype);
2781 static CYHooks CYObjectiveCHooks = {
2782 &CYObjectiveC_ExecuteStart,
2783 &CYObjectiveC_ExecuteEnd,
2784 &CYObjectiveC_CallFunction,
2785 &CYObjectiveC_Initialize,
2786 &CYObjectiveC_SetupContext,
2787 &CYObjectiveC_PoolFFI,
2788 &CYObjectiveC_FromFFI,
2791 struct CYObjectiveC {
2793 hooks_ = &CYObjectiveCHooks;
2794 // XXX: evil magic juju to make this actually take effect on a Mac when compiled with autoconf/libtool doom!
2795 _assert(hooks_ != NULL);