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"
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) {
188 size_t size([value maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1);
189 char *string(new(pool) char[size]);
190 if (![value getCString:string maxLength:size encoding:NSUTF8StringEncoding])
191 throw CYJSError(context, "[NSString getCString:maxLength:encoding:] == NO");
195 JSStringRef CYCopyJSString(JSContextRef context, NSString *value) {
197 return JSStringCreateWithCFString(reinterpret_cast<CFStringRef>(value));
200 return CYCopyJSString(CYPoolCString(pool, context, value));
204 JSStringRef CYCopyJSString(JSContextRef context, NSObject *value) {
207 // XXX: this definition scares me; is anyone using this?!
208 NSString *string([value description]);
209 return CYCopyJSString(context, string);
212 NSString *CYCopyNSString(const CYUTF8String &value) {
214 return (NSString *) CFStringCreateWithBytes(kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(value.data), value.size, kCFStringEncodingUTF8, true);
216 return [[NSString alloc] initWithBytes:value.data length:value.size encoding:NSUTF8StringEncoding];
220 NSString *CYCopyNSString(JSContextRef context, JSStringRef value) {
222 return (NSString *) JSStringCopyCFString(kCFAllocatorDefault, value);
225 return CYCopyNSString(CYPoolUTF8String(pool, context, value));
229 NSString *CYCopyNSString(JSContextRef context, JSValueRef value) {
230 return CYCopyNSString(context, CYJSString(context, value));
233 NSString *CYCastNSString(CYPool *pool, const CYUTF8String &value) {
234 return CYPoolRelease(pool, CYCopyNSString(value));
237 NSString *CYCastNSString(CYPool *pool, SEL sel) {
238 const char *name(sel_getName(sel));
239 return CYPoolRelease(pool, CYCopyNSString(CYUTF8String(name, strlen(name))));
242 NSString *CYCastNSString(CYPool *pool, JSContextRef context, JSStringRef value) {
243 return CYPoolRelease(pool, CYCopyNSString(context, value));
246 CYUTF8String CYCastUTF8String(NSString *value) {
247 NSData *data([value dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO]);
248 return CYUTF8String(reinterpret_cast<const char *>([data bytes]), [data length]);
252 JSValueRef CYCastJSValue(JSContextRef context, NSObject *value);
254 void CYThrow(JSContextRef context, NSException *error, JSValueRef *exception) {
255 if (exception == NULL)
257 *exception = CYCastJSValue(context, error);
260 size_t CYGetIndex(NSString *value) {
261 return CYGetIndex(CYCastUTF8String(value));
264 bool CYGetOffset(CYPool &pool, JSContextRef context, NSString *value, ssize_t &index) {
265 return CYGetOffset(CYPoolCString(pool, context, value), index);
268 static JSClassRef Instance_;
270 static JSClassRef ArrayInstance_;
271 static JSClassRef FunctionInstance_;
272 static JSClassRef ObjectInstance_;
273 static JSClassRef StringInstance_;
275 static JSClassRef Class_;
276 static JSClassRef Internal_;
277 static JSClassRef Message_;
278 static JSClassRef Messages_;
279 static JSClassRef Selector_;
280 static JSClassRef Super_;
282 static JSClassRef ObjectiveC_Classes_;
283 static JSClassRef ObjectiveC_Constants_;
284 static JSClassRef ObjectiveC_Protocols_;
287 static JSClassRef ObjectiveC_Image_Classes_;
288 static JSClassRef ObjectiveC_Images_;
292 static Class NSCFBoolean_;
293 static Class NSCFType_;
294 static Class NSGenericDeallocHandler_;
295 static Class NSZombie_;
297 static std::set<Class> banned_;
299 static Class NSBoolNumber_;
302 static Class NSArray_;
303 static Class NSBlock_;
304 static Class NSDictionary_;
305 static Class NSString_;
306 static Class Object_;
308 static Type_privateData *Object_type;
309 static Type_privateData *Selector_type;
311 Type_privateData *Instance::GetType() const {
315 Type_privateData *Selector_privateData::GetType() const {
316 return Selector_type;
319 static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception);
321 JSValueRef CYGetClassPrototype(JSContextRef context, Class self, bool meta) {
323 return CYGetCachedObject(context, CYJSString("Instance_prototype"));
324 else if (meta && !class_isMetaClass(self))
325 return CYGetCachedObject(context, CYJSString("Class_prototype"));
327 JSObjectRef global(CYGetGlobalObject(context));
328 JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s)));
331 sprintf(label, "i%p", self);
332 CYJSString name(label);
334 JSValueRef value(CYGetProperty(context, cy, name));
335 if (!JSValueIsUndefined(context, value))
338 JSClassRef _class(NULL);
339 JSValueRef prototype;
341 if (self == NSArray_)
342 prototype = CYGetCachedObject(context, CYJSString("ArrayInstance_prototype"));
343 else if (self == NSBlock_)
344 prototype = CYGetCachedObject(context, CYJSString("FunctionInstance_prototype"));
345 else if (self == NSDictionary_)
346 prototype = CYGetCachedObject(context, CYJSString("ObjectInstance_prototype"));
347 else if (self == NSString_)
348 prototype = CYGetCachedObject(context, CYJSString("StringInstance_prototype"));
350 prototype = CYGetClassPrototype(context, class_getSuperclass(self), meta);
352 JSObjectRef object(JSObjectMake(context, _class, NULL));
353 JSObjectSetPrototype(context, object, prototype);
354 CYSetProperty(context, cy, name, object);
359 _finline JSValueRef CYGetClassPrototype(JSContextRef context, Class self) {
360 return CYGetClassPrototype(context, self, class_isMetaClass(self));
363 JSObjectRef Messages::Make(JSContextRef context, Class _class) {
364 JSObjectRef value(JSObjectMake(context, Messages_, new Messages(_class)));
365 if (Class super = class_getSuperclass(_class))
366 JSObjectSetPrototype(context, value, Messages::Make(context, super));
370 JSObjectRef Internal::Make(JSContextRef context, id object, JSObjectRef owner) {
371 return JSObjectMake(context, Internal_, new Internal(object, context, owner));
375 JSObjectRef Super::Make(JSContextRef context, id object, Class _class) {
376 JSObjectRef value(JSObjectMake(context, Super_, new Super(object, _class)));
380 JSObjectRef Instance::Make(JSContextRef context, id object, Flags flags) {
381 JSObjectRef value(JSObjectMake(context, Instance_, new Instance(object, flags)));
382 JSObjectSetPrototype(context, value, CYGetClassPrototype(context, object_getClass(object)));
386 Instance::~Instance() {
387 if ((flags_ & Transient) == 0)
388 // XXX: does this handle background threads correctly?
389 // XXX: this simply does not work on the console because I'm stupid
390 [GetValue() performSelector:@selector(release) withObject:nil afterDelay:0];
393 struct Message_privateData :
398 Message_privateData(SEL sel, const char *type, IMP value = NULL) :
399 cy::Functor(type, reinterpret_cast<void (*)()>(value)),
405 JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient) {
406 Instance::Flags flags;
409 flags = Instance::Transient;
411 flags = Instance::None;
412 object = [object retain];
415 return Instance::Make(context, object, flags);
418 @interface NSMethodSignature (Cycript)
419 - (NSString *) _typeString;
422 @interface NSObject (Cycript)
424 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context;
425 - (JSType) cy$JSType;
427 - (JSValueRef) cy$toJSON:(NSString *)key inContext:(JSContextRef)context;
428 - (NSString *) cy$toCYON:(bool)objective;
430 - (bool) cy$hasProperty:(NSString *)name;
431 - (NSObject *) cy$getProperty:(NSString *)name;
432 - (JSValueRef) cy$getProperty:(NSString *)name inContext:(JSContextRef)context;
433 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value;
434 - (bool) cy$deleteProperty:(NSString *)name;
435 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context;
437 + (bool) cy$hasImplicitProperties;
443 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context;
446 NSString *CYCastNSCYON(id value, bool objective) {
452 Class _class(object_getClass(value));
453 SEL sel(@selector(cy$toCYON:));
455 if (objc_method *toCYON = class_getInstanceMethod(_class, sel))
456 string = reinterpret_cast<NSString *(*)(id, SEL, bool)>(method_getImplementation(toCYON))(value, sel, objective);
457 else if (objc_method *methodSignatureForSelector = class_getInstanceMethod(_class, @selector(methodSignatureForSelector:))) {
458 if (reinterpret_cast<NSMethodSignature *(*)(id, SEL, SEL)>(method_getImplementation(methodSignatureForSelector))(value, @selector(methodSignatureForSelector:), sel) != nil)
459 string = [value cy$toCYON:objective];
464 else if (value == NSZombie_)
465 string = @"_NSZombie_";
466 else if (_class == NSZombie_)
467 string = [NSString stringWithFormat:@"<_NSZombie_: %p>", value];
468 // XXX: frowny /in/ the pants
469 else if (banned_.find(value) != banned_.end())
473 string = [NSString stringWithFormat:@"%@", value];
478 string = @"undefined";
485 struct PropertyAttributes {
490 const char *variable;
503 PropertyAttributes(objc_property_t property) :
515 name = property_getName(property);
516 const char *attributes(property_getAttributes(property));
518 for (char *token(pool_.strdup(attributes)), *next; token != NULL; token = next) {
519 if ((next = strchr(token, ',')) != NULL)
522 case 'R': readonly = true; break;
523 case 'C': copy = true; break;
524 case '&': retain = true; break;
525 case 'N': nonatomic = true; break;
526 case 'G': getter_ = token + 1; break;
527 case 'S': setter_ = token + 1; break;
528 case 'V': variable = token + 1; break;
532 /*if (variable == NULL) {
533 variable = property_getName(property);
534 size_t size(strlen(variable));
535 char *name(new(pool_) char[size + 2]);
537 memcpy(name + 1, variable, size);
538 name[size + 1] = '\0';
543 const char *Getter() {
545 getter_ = pool_.strdup(name);
549 const char *Setter() {
550 if (setter_ == NULL && !readonly) {
551 size_t length(strlen(name));
553 char *temp(new(pool_) char[length + 5]);
559 temp[3] = toupper(name[0]);
560 memcpy(temp + 4, name + 1, length - 1);
563 temp[length + 3] = ':';
564 temp[length + 4] = '\0';
574 @interface CYWebUndefined : NSObject {
577 + (CYWebUndefined *) undefined;
581 @implementation CYWebUndefined
583 + (CYWebUndefined *) undefined {
584 static CYWebUndefined *instance_([[CYWebUndefined alloc] init]);
590 #define WebUndefined CYWebUndefined
592 /* Bridge: CYJSObject {{{ */
593 @interface CYJSObject : NSMutableDictionary {
595 JSGlobalContextRef context_;
598 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context;
600 - (NSUInteger) count;
601 - (id) objectForKey:(id)key;
602 - (NSEnumerator *) keyEnumerator;
603 - (void) setObject:(id)object forKey:(id)key;
604 - (void) removeObjectForKey:(id)key;
608 /* Bridge: CYJSArray {{{ */
609 @interface CYJSArray : NSMutableArray {
611 JSGlobalContextRef context_;
614 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context;
616 - (NSUInteger) count;
617 - (id) objectAtIndex:(NSUInteger)index;
619 - (void) addObject:(id)anObject;
620 - (void) insertObject:(id)anObject atIndex:(NSUInteger)index;
621 - (void) removeLastObject;
622 - (void) removeObjectAtIndex:(NSUInteger)index;
623 - (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
628 _finline bool CYJSValueIsNSObject(JSContextRef context, JSValueRef value) {
629 return JSValueIsObjectOfClass(context, value, Instance_);
632 _finline bool CYJSValueIsInstanceOfCachedConstructor(JSContextRef context, JSValueRef value, JSStringRef cache) {
633 JSValueRef exception(NULL);
634 JSObjectRef constructor(CYGetCachedObject(context, cache));
635 bool is(JSValueIsInstanceOfConstructor(context, value, constructor, &exception));
636 CYThrow(context, exception);
640 NSObject *CYMakeBlock(void (*invoke)(), sig::Signature &signature) {
641 BlockLiteral *literal(reinterpret_cast<BlockLiteral *>(malloc(sizeof(BlockLiteral))));
645 BlockDescriptor1 one_;
646 BlockDescriptor2 two_;
647 BlockDescriptor3 three_;
653 Descriptor *descriptor(new Descriptor);
654 memset(&descriptor->d_, 0, sizeof(descriptor->d_));
656 literal->isa = objc_getClass("__NSGlobalBlock__");
657 literal->flags = BLOCK_HAS_SIGNATURE | BLOCK_HAS_COPY_DISPOSE | BLOCK_IS_GLOBAL;
658 literal->reserved = 0;
659 literal->invoke = reinterpret_cast<void (*)(void *, ...)>(invoke);
660 literal->descriptor = descriptor;
662 descriptor->d_.one_.size = sizeof(descriptor->d_);
663 descriptor->d_.three_.signature = sig::Unparse(descriptor->pool_, &signature);
665 return reinterpret_cast<NSObject *>(literal);
668 NSObject *CYCastNSObject(CYPool *pool, JSContextRef context, JSObjectRef object) {
669 if (CYJSValueIsNSObject(context, object)) {
670 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
671 return internal->GetValue();
674 if (JSValueIsObjectOfClass(context, object, Functor_)) {
675 cy::Functor *internal(reinterpret_cast<cy::Functor *>(JSObjectGetPrivate(object)));
676 return CYMakeBlock(internal->GetValue(), internal->signature_);
679 bool array(CYJSValueIsInstanceOfCachedConstructor(context, object, Array_s));
680 id value(array ? [CYJSArray alloc] : [CYJSObject alloc]);
681 return CYPoolRelease(pool, [value initWithJSObject:object inContext:context]);
684 NSNumber *CYCopyNSNumber(JSContextRef context, JSValueRef value) {
685 return [[NSNumber alloc] initWithDouble:CYCastDouble(context, value)];
689 @interface NSBoolNumber : NSNumber {
694 id CYNSObject(CYPool *pool, JSContextRef context, JSValueRef value, bool cast) {
698 switch (JSType type = JSValueGetType(context, value)) {
699 case kJSTypeUndefined:
700 object = [WebUndefined undefined];
710 object = (id) (CYCastBool(context, value) ? kCFBooleanTrue : kCFBooleanFalse);
713 object = [[NSBoolNumber alloc] initWithBool:CYCastBool(context, value)];
719 object = CYCopyNSNumber(context, value);
724 object = CYCopyNSString(context, value);
729 // XXX: this might could be more efficient
730 object = CYCastNSObject(pool, context, (JSObjectRef) value);
735 throw CYJSError(context, "JSValueGetType() == 0x%x", type);
742 return CYPoolRelease(pool, object);
744 return [object retain];
747 NSObject *CYCastNSObject(CYPool *pool, JSContextRef context, JSValueRef value) {
748 return CYNSObject(pool, context, value, true);
751 NSObject *CYCopyNSObject(CYPool &pool, JSContextRef context, JSValueRef value) {
752 return CYNSObject(&pool, context, value, false);
755 /* Bridge: NSArray {{{ */
756 @implementation NSArray (Cycript)
759 return [[self mutableCopy] autorelease];
762 - (NSString *) cy$toCYON:(bool)objective {
763 NSMutableString *json([[[NSMutableString alloc] init] autorelease]);
764 [json appendString:@"@["];
768 for (id object in self) {
770 for (size_t index(0), count([self count]); index != count; ++index) {
771 id object([self objectAtIndex:index]);
774 [json appendString:@","];
777 if (object == nil || [object cy$JSType] != kJSTypeUndefined)
778 [json appendString:CYCastNSCYON(object, true)];
780 [json appendString:@","];
785 [json appendString:@"]"];
789 - (bool) cy$hasProperty:(NSString *)name {
790 if ([name isEqualToString:@"length"])
793 size_t index(CYGetIndex(name));
794 if (index == _not(size_t) || index >= [self count])
795 return [super cy$hasProperty:name];
800 - (NSObject *) cy$getProperty:(NSString *)name {
801 size_t index(CYGetIndex(name));
802 if (index == _not(size_t) || index >= [self count])
803 return [super cy$getProperty:name];
805 return [self objectAtIndex:index];
808 - (JSValueRef) cy$getProperty:(NSString *)name inContext:(JSContextRef)context {
809 CYObjectiveTry_(context) {
810 if ([name isEqualToString:@"length"])
811 return CYCastJSValue(context, [self count]);
814 return [super cy$getProperty:name inContext:context];
817 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context {
818 [super cy$getPropertyNames:names inContext:context];
820 for (size_t index(0), count([self count]); index != count; ++index) {
821 id object([self objectAtIndex:index]);
822 if (object == nil || [object cy$JSType] != kJSTypeUndefined) {
824 sprintf(name, "%zu", index);
825 JSPropertyNameAccumulatorAddName(names, CYJSString(name));
830 + (bool) cy$hasImplicitProperties {
836 /* Bridge: NSBlock {{{ */
843 /* Bridge: NSBoolNumber {{{ */
845 @implementation NSBoolNumber (Cycript)
847 - (JSType) cy$JSType {
848 return kJSTypeBoolean;
851 - (NSString *) cy$toCYON:(bool)objective {
852 NSString *value([self boolValue] ? @"true" : @"false");
853 return objective ? value : [NSString stringWithFormat:@"@%@", value];
856 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
857 return CYCastJSValue(context, (bool) [self boolValue]);
863 /* Bridge: NSDictionary {{{ */
864 @implementation NSDictionary (Cycript)
867 return [[self mutableCopy] autorelease];
870 - (NSString *) cy$toCYON:(bool)objective {
871 NSMutableString *json([[[NSMutableString alloc] init] autorelease]);
872 [json appendString:@"@{"];
876 for (NSObject *key in self) {
878 NSEnumerator *keys([self keyEnumerator]);
879 while (NSObject *key = [keys nextObject]) {
882 [json appendString:@","];
885 [json appendString:CYCastNSCYON(key, true)];
886 [json appendString:@":"];
887 NSObject *object([self objectForKey:key]);
888 [json appendString:CYCastNSCYON(object, true)];
891 [json appendString:@"}"];
895 - (bool) cy$hasProperty:(NSString *)name {
896 return [self objectForKey:name] != nil;
899 - (NSObject *) cy$getProperty:(NSString *)name {
900 return [self objectForKey:name];
903 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context {
904 [super cy$getPropertyNames:names inContext:context];
907 for (NSObject *key in self) {
909 NSEnumerator *keys([self keyEnumerator]);
910 while (NSObject *key = [keys nextObject]) {
912 JSPropertyNameAccumulatorAddName(names, CYJSString(context, key));
916 + (bool) cy$hasImplicitProperties {
922 /* Bridge: NSMutableArray {{{ */
923 @implementation NSMutableArray (Cycript)
925 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
926 if ([name isEqualToString:@"length"]) {
927 // XXX: is this not intelligent?
928 NSNumber *number(reinterpret_cast<NSNumber *>(value));
930 NSUInteger size([number unsignedIntegerValue]);
932 NSUInteger size([number unsignedIntValue]);
934 NSUInteger count([self count]);
936 [self removeObjectsInRange:NSMakeRange(size, count - size)];
937 else if (size != count) {
938 WebUndefined *undefined([WebUndefined undefined]);
939 for (size_t i(count); i != size; ++i)
940 [self addObject:undefined];
945 size_t index(CYGetIndex(name));
946 if (index == _not(size_t))
947 return [super cy$setProperty:name to:value];
949 id object(value ?: [NSNull null]);
951 size_t count([self count]);
953 [self replaceObjectAtIndex:index withObject:object];
955 if (index != count) {
956 WebUndefined *undefined([WebUndefined undefined]);
957 for (size_t i(count); i != index; ++i)
958 [self addObject:undefined];
961 [self addObject:object];
967 - (bool) cy$deleteProperty:(NSString *)name {
968 size_t index(CYGetIndex(name));
969 if (index == _not(size_t) || index >= [self count])
970 return [super cy$deleteProperty:name];
971 [self replaceObjectAtIndex:index withObject:[WebUndefined undefined]];
977 /* Bridge: NSMutableDictionary {{{ */
978 @implementation NSMutableDictionary (Cycript)
980 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
981 [self setObject:(value ?: [NSNull null]) forKey:name];
985 - (bool) cy$deleteProperty:(NSString *)name {
986 if ([self objectForKey:name] == nil)
989 [self removeObjectForKey:name];
996 /* Bridge: NSNumber {{{ */
997 @implementation NSNumber (Cycript)
999 - (JSType) cy$JSType {
1001 // XXX: this just seems stupid
1002 if ([self class] == NSCFBoolean_)
1003 return kJSTypeBoolean;
1005 return kJSTypeNumber;
1008 - (NSString *) cy$toCYON:(bool)objective {
1009 NSString *value([self cy$JSType] != kJSTypeBoolean ? [self stringValue] : [self boolValue] ? @"true" : @"false");
1010 return objective ? value : [NSString stringWithFormat:@"@%@", value];
1013 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
1014 return [self cy$JSType] != kJSTypeBoolean ? CYCastJSValue(context, [self doubleValue]) : CYCastJSValue(context, static_cast<bool>([self boolValue]));
1015 } CYObjectiveCatch }
1019 /* Bridge: NSNull {{{ */
1020 @implementation NSNull (Cycript)
1022 - (JSType) cy$JSType {
1026 - (NSString *) cy$toCYON:(bool)objective {
1027 NSString *value(@"null");
1028 return objective ? value : [NSString stringWithFormat:@"@%@", value];
1031 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
1032 return CYJSNull(context);
1033 } CYObjectiveCatch }
1037 /* Bridge: NSObject {{{ */
1038 @implementation NSObject (Cycript)
1044 - (JSValueRef) cy$toJSON:(NSString *)key inContext:(JSContextRef)context {
1045 return [self cy$valueOfInContext:context];
1048 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
1050 } CYObjectiveCatch }
1052 - (JSType) cy$JSType {
1053 return kJSTypeObject;
1056 - (NSString *) cy$toCYON:(bool)objective {
1057 return [[self description] cy$toCYON:objective];
1060 - (bool) cy$hasProperty:(NSString *)name {
1064 - (NSObject *) cy$getProperty:(NSString *)name {
1068 - (JSValueRef) cy$getProperty:(NSString *)name inContext:(JSContextRef)context { CYObjectiveTry_(context) {
1069 if (NSObject *value = [self cy$getProperty:name])
1070 return CYCastJSValue(context, value);
1072 } CYObjectiveCatch }
1074 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
1078 - (bool) cy$deleteProperty:(NSString *)name {
1082 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context {
1085 + (bool) cy$hasImplicitProperties {
1091 /* Bridge: NSProxy {{{ */
1092 @implementation NSProxy (Cycript)
1094 - (NSString *) cy$toCYON:(bool)objective {
1095 return [[self description] cy$toCYON:objective];
1100 /* Bridge: NSString {{{ */
1101 @implementation NSString (Cycript)
1104 return [[self copy] autorelease];
1107 - (JSType) cy$JSType {
1108 return kJSTypeString;
1111 - (NSString *) cy$toCYON:(bool)objective {
1112 std::ostringstream str;
1115 CYUTF8String string(CYCastUTF8String(self));
1116 CYStringify(str, string.data, string.size);
1117 std::string value(str.str());
1118 return CYCastNSString(NULL, CYUTF8String(value.c_str(), value.size()));
1121 - (bool) cy$hasProperty:(NSString *)name {
1122 size_t index(CYGetIndex(name));
1123 if (index == _not(size_t) || index >= [self length])
1124 return [super cy$hasProperty:name];
1129 - (NSObject *) cy$getProperty:(NSString *)name {
1130 size_t index(CYGetIndex(name));
1131 if (index == _not(size_t) || index >= [self length])
1132 return [super cy$getProperty:name];
1134 return [self substringWithRange:NSMakeRange(index, 1)];
1137 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context {
1138 [super cy$getPropertyNames:names inContext:context];
1140 for (size_t index(0), length([self length]); index != length; ++index) {
1142 sprintf(name, "%zu", index);
1143 JSPropertyNameAccumulatorAddName(names, CYJSString(name));
1147 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
1148 return CYCastJSValue(context, CYJSString(context, self));
1149 } CYObjectiveCatch }
1153 /* Bridge: WebUndefined {{{ */
1154 @implementation WebUndefined (Cycript)
1156 - (JSType) cy$JSType {
1157 return kJSTypeUndefined;
1160 - (NSString *) cy$toCYON:(bool)objective {
1161 NSString *value(@"undefined");
1162 return value; // XXX: maybe use the below code, adding @undefined?
1163 //return objective ? value : [NSString stringWithFormat:@"@%@", value];
1166 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
1167 return CYJSUndefined(context);
1168 } CYObjectiveCatch }
1173 static bool CYIsClass(id self) {
1175 return class_isMetaClass(object_getClass(self));
1177 return GSObjCIsClass(self);
1181 Class CYCastClass(CYPool &pool, JSContextRef context, JSValueRef value) {
1182 id self(CYCastNSObject(&pool, context, value));
1183 if (CYIsClass(self))
1184 return (Class) self;
1185 throw CYJSError(context, "got something that is not a Class");
1189 NSArray *CYCastNSArray(JSContextRef context, JSPropertyNameArrayRef names) {
1191 size_t size(JSPropertyNameArrayGetCount(names));
1192 NSMutableArray *array([NSMutableArray arrayWithCapacity:size]);
1193 for (size_t index(0); index != size; ++index)
1194 [array addObject:CYCastNSString(&pool, context, JSPropertyNameArrayGetNameAtIndex(names, index))];
1198 JSValueRef CYCastJSValue(JSContextRef context, NSObject *value) { CYPoolTry {
1200 return CYJSNull(context);
1202 return CYMakeInstance(context, value, false);
1203 } CYPoolCatch(NULL) return /*XXX*/ NULL; }
1205 @implementation CYJSObject
1207 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { CYObjectiveTry {
1208 if ((self = [super init]) != nil) {
1210 context_ = CYGetJSContext(context);
1211 JSGlobalContextRetain(context_);
1212 JSValueProtect(context_, object_);
1214 } CYObjectiveCatch }
1216 - (void) dealloc { CYObjectiveTry {
1217 JSValueUnprotect(context_, object_);
1218 JSGlobalContextRelease(context_);
1220 } CYObjectiveCatch }
1222 - (NSString *) cy$toCYON:(bool)objective { CYObjectiveTry {
1224 const char *cyon(CYPoolCCYON(pool, context_, object_));
1226 return [super cy$toCYON:objective];
1228 return [NSString stringWithUTF8String:cyon];
1229 } CYObjectiveCatch }
1231 - (NSUInteger) count { CYObjectiveTry {
1232 JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_));
1233 size_t size(JSPropertyNameArrayGetCount(names));
1234 JSPropertyNameArrayRelease(names);
1236 } CYObjectiveCatch }
1238 - (id) objectForKey:(id)key { CYObjectiveTry {
1239 JSValueRef value(CYGetProperty(context_, object_, CYJSString(context_, (NSObject *) key)));
1240 if (JSValueIsUndefined(context_, value))
1242 return CYCastNSObject(NULL, context_, value) ?: [NSNull null];
1243 } CYObjectiveCatch }
1245 - (NSEnumerator *) keyEnumerator { CYObjectiveTry {
1246 JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_));
1247 NSEnumerator *enumerator([CYCastNSArray(context_, names) objectEnumerator]);
1248 JSPropertyNameArrayRelease(names);
1250 } CYObjectiveCatch }
1252 - (void) setObject:(id)object forKey:(id)key { CYObjectiveTry {
1253 CYSetProperty(context_, object_, CYJSString(context_, (NSObject *) key), CYCastJSValue(context_, (NSString *) object));
1254 } CYObjectiveCatch }
1256 - (void) removeObjectForKey:(id)key { CYObjectiveTry {
1257 JSValueRef exception(NULL);
1258 (void) JSObjectDeleteProperty(context_, object_, CYJSString(context_, (NSObject *) key), &exception);
1259 CYThrow(context_, exception);
1260 } CYObjectiveCatch }
1264 @implementation CYJSArray
1266 - (NSString *) cy$toCYON:(bool)objective {
1268 return [NSString stringWithUTF8String:CYPoolCCYON(pool, context_, object_)];
1271 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { CYObjectiveTry {
1272 if ((self = [super init]) != nil) {
1274 context_ = CYGetJSContext(context);
1275 JSGlobalContextRetain(context_);
1276 JSValueProtect(context_, object_);
1278 } CYObjectiveCatch }
1280 - (void) dealloc { CYObjectiveTry {
1281 JSValueUnprotect(context_, object_);
1282 JSGlobalContextRelease(context_);
1284 } CYObjectiveCatch }
1286 - (NSUInteger) count { CYObjectiveTry {
1287 return CYArrayLength(context_, object_);
1288 } CYObjectiveCatch }
1290 - (id) objectAtIndex:(NSUInteger)index { CYObjectiveTry {
1291 size_t bounds([self count]);
1292 if (index >= bounds)
1293 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray objectAtIndex:]: index (%zu) beyond bounds (%zu)", static_cast<size_t>(index), bounds] userInfo:nil];
1294 JSValueRef exception(NULL);
1295 JSValueRef value(JSObjectGetPropertyAtIndex(context_, object_, index, &exception));
1296 CYThrow(context_, exception);
1297 return CYCastNSObject(NULL, context_, value) ?: [NSNull null];
1298 } CYObjectiveCatch }
1300 - (void) addObject:(id)object { CYObjectiveTry {
1301 CYArrayPush(context_, object_, CYCastJSValue(context_, (NSObject *) object));
1302 } CYObjectiveCatch }
1304 - (void) insertObject:(id)object atIndex:(NSUInteger)index { CYObjectiveTry {
1305 size_t bounds([self count] + 1);
1306 if (index >= bounds)
1307 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray insertObject:atIndex:]: index (%zu) beyond bounds (%zu)", static_cast<size_t>(index), bounds] userInfo:nil];
1308 JSValueRef exception(NULL);
1309 JSValueRef arguments[3];
1310 arguments[0] = CYCastJSValue(context_, index);
1311 arguments[1] = CYCastJSValue(context_, 0);
1312 arguments[2] = CYCastJSValue(context_, (NSObject *) object);
1313 JSObjectRef Array(CYGetCachedObject(context_, CYJSString("Array_prototype")));
1314 JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array, splice_s)), object_, 3, arguments, &exception);
1315 CYThrow(context_, exception);
1316 } CYObjectiveCatch }
1318 - (void) removeLastObject { CYObjectiveTry {
1319 JSValueRef exception(NULL);
1320 JSObjectRef Array(CYGetCachedObject(context_, CYJSString("Array_prototype")));
1321 JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array, pop_s)), object_, 0, NULL, &exception);
1322 CYThrow(context_, exception);
1323 } CYObjectiveCatch }
1325 - (void) removeObjectAtIndex:(NSUInteger)index { CYObjectiveTry {
1326 size_t bounds([self count]);
1327 if (index >= bounds)
1328 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray removeObjectAtIndex:]: index (%zu) beyond bounds (%zu)", static_cast<size_t>(index), bounds] userInfo:nil];
1329 JSValueRef exception(NULL);
1330 JSValueRef arguments[2];
1331 arguments[0] = CYCastJSValue(context_, index);
1332 arguments[1] = CYCastJSValue(context_, 1);
1333 JSObjectRef Array(CYGetCachedObject(context_, CYJSString("Array_prototype")));
1334 JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array, splice_s)), object_, 2, arguments, &exception);
1335 CYThrow(context_, exception);
1336 } CYObjectiveCatch }
1338 - (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)object { CYObjectiveTry {
1339 size_t bounds([self count]);
1340 if (index >= bounds)
1341 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray replaceObjectAtIndex:withObject:]: index (%zu) beyond bounds (%zu)", static_cast<size_t>(index), bounds] userInfo:nil];
1342 CYSetProperty(context_, object_, index, CYCastJSValue(context_, (NSObject *) object));
1343 } CYObjectiveCatch }
1347 // XXX: inherit from or replace with CYJSObject
1348 @interface CYInternal : NSObject {
1349 JSGlobalContextRef context_;
1350 JSObjectRef object_;
1355 @implementation CYInternal
1358 JSValueUnprotect(context_, object_);
1359 JSGlobalContextRelease(context_);
1363 - (id) initInContext:(JSContextRef)context {
1364 if ((self = [super init]) != nil) {
1365 context_ = CYGetJSContext(context);
1366 JSGlobalContextRetain(context_);
1370 - (bool) hasProperty:(JSStringRef)name inContext:(JSContextRef)context {
1371 if (object_ == NULL)
1374 return JSObjectHasProperty(context, object_, name);
1377 - (JSValueRef) getProperty:(JSStringRef)name inContext:(JSContextRef)context {
1378 if (object_ == NULL)
1381 return CYGetProperty(context, object_, name);
1384 - (void) setProperty:(JSStringRef)name toValue:(JSValueRef)value inContext:(JSContextRef)context {
1385 @synchronized (self) {
1386 if (object_ == NULL) {
1387 object_ = JSObjectMake(context, NULL, NULL);
1388 JSValueProtect(context, object_);
1392 CYSetProperty(context, object_, name, value);
1395 + (CYInternal *) get:(id)object {
1396 if ($objc_getAssociatedObject == NULL)
1399 @synchronized (object) {
1400 if (CYInternal *internal = $objc_getAssociatedObject(object, @selector(cy$internal)))
1407 + (CYInternal *) set:(id)object inContext:(JSContextRef)context {
1408 if ($objc_getAssociatedObject == NULL)
1411 @synchronized (object) {
1412 if (CYInternal *internal = $objc_getAssociatedObject(object, @selector(cy$internal)))
1415 if ($objc_setAssociatedObject == NULL)
1418 CYInternal *internal([[[CYInternal alloc] initInContext:context] autorelease]);
1419 objc_setAssociatedObject(object, @selector(cy$internal), internal, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
1428 static JSObjectRef CYMakeSelector(JSContextRef context, SEL sel) {
1429 Selector_privateData *internal(new Selector_privateData(sel));
1430 return JSObjectMake(context, Selector_, internal);
1433 static SEL CYCastSEL(JSContextRef context, JSValueRef value) {
1434 if (JSValueIsObjectOfClass(context, value, Selector_)) {
1435 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate((JSObjectRef) value)));
1436 return reinterpret_cast<SEL>(internal->value_);
1438 return CYCastPointer<SEL>(context, value);
1441 void *CYObjectiveC_ExecuteStart(JSContextRef context) { CYSadTry {
1442 return (void *) [[NSAutoreleasePool alloc] init];
1443 } CYSadCatch(NULL) }
1445 void CYObjectiveC_ExecuteEnd(JSContextRef context, void *handle) { CYSadTry {
1446 return [(NSAutoreleasePool *) handle release];
1449 JSValueRef CYObjectiveC_RuntimeProperty(JSContextRef context, CYUTF8String name) { CYPoolTry {
1451 return Instance::Make(context, nil);
1452 if (Class _class = objc_getClass(name.data))
1453 return CYMakeInstance(context, _class, true);
1454 if (Protocol *protocol = objc_getProtocol(name.data))
1455 return CYMakeInstance(context, protocol, true);
1457 } CYPoolCatch(NULL) return /*XXX*/ NULL; }
1459 static void CYObjectiveC_CallFunction(JSContextRef context, ffi_cif *cif, void (*function)(), uint8_t *value, void **values) { CYSadTry {
1460 ffi_call(cif, function, value, values);
1463 static bool CYObjectiveC_PoolFFI(CYPool *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { CYSadTry {
1464 switch (type->primitive) {
1465 // XXX: do something epic about blocks
1468 case sig::typename_P:
1469 // XXX: this works for return values, but not for properties and fields
1470 *reinterpret_cast<id *>(data) = CYCastNSObject(pool, context, value);
1473 case sig::selector_P:
1474 *reinterpret_cast<SEL *>(data) = CYCastSEL(context, value);
1482 } CYSadCatch(false) }
1484 static JSValueRef CYObjectiveC_FromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) { CYPoolTry {
1485 switch (type->primitive) {
1486 // XXX: do something epic about blocks
1489 if (NSObject *object = *reinterpret_cast<NSObject **>(data)) {
1490 JSValueRef value(CYCastJSValue(context, object));
1496 case sig::typename_P:
1497 return CYMakeInstance(context, *reinterpret_cast<Class *>(data), true);
1499 case sig::selector_P:
1500 if (SEL sel = *reinterpret_cast<SEL *>(data))
1501 return CYMakeSelector(context, sel);
1505 return CYJSNull(context);
1509 } CYPoolCatch(NULL) return /*XXX*/ NULL; }
1511 static bool CYImplements(id object, Class _class, SEL selector, bool devoid = false) {
1512 if (objc_method *method = class_getInstanceMethod(_class, selector)) {
1515 #if OBJC_API_VERSION >= 2
1517 method_getReturnType(method, type, sizeof(type));
1519 const char *type(method_getTypeEncoding(method));
1525 // XXX: possibly use a more "awesome" check?
1529 static const char *CYPoolTypeEncoding(CYPool &pool, JSContextRef context, SEL sel, objc_method *method) {
1531 return method_getTypeEncoding(method);
1533 const char *name(sel_getName(sel));
1534 size_t length(strlen(name));
1536 char keyed[length + 2];
1538 keyed[length + 1] = '\0';
1539 memcpy(keyed + 1, name, length);
1541 if (CYBridgeEntry *entry = CYBridgeHash(keyed, length + 1))
1542 return entry->value_;
1547 static JSValueRef MessageAdapter_(JSContextRef context, size_t count, JSValueRef values[], JSObjectRef function) {
1548 JSObjectRef _this(CYCastJSObject(context, values[0]));
1549 return CYCallAsFunction(context, function, _this, count - 2, values + 2);
1552 static void MessageClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) {
1553 CYExecuteClosure(cif, result, arguments, arg, &MessageAdapter_);
1556 static JSObjectRef CYMakeMessage(JSContextRef context, SEL sel, IMP imp, const char *type) {
1557 Message_privateData *internal(new Message_privateData(sel, type, imp));
1558 return JSObjectMake(context, Message_, internal);
1561 static IMP CYMakeMessage(JSContextRef context, JSValueRef value, const char *type) {
1562 JSObjectRef function(CYCastJSObject(context, value));
1563 Closure_privateData *internal(CYMakeFunctor_(context, function, type, &MessageClosure_));
1564 // XXX: see notes in Library.cpp about needing to leak
1565 return reinterpret_cast<IMP>(internal->GetValue());
1568 static bool Messages_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
1569 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1570 Class _class(internal->GetValue());
1573 const char *name(CYPoolCString(pool, context, property));
1575 if (SEL sel = sel_getUid(name))
1576 if (class_getInstanceMethod(_class, sel) != NULL)
1582 static JSValueRef Messages_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1583 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1584 Class _class(internal->GetValue());
1587 const char *name(CYPoolCString(pool, context, property));
1589 if (SEL sel = sel_getUid(name))
1590 if (objc_method *method = class_getInstanceMethod(_class, sel))
1591 return CYMakeMessage(context, sel, method_getImplementation(method), method_getTypeEncoding(method));
1596 static bool Messages_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
1597 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1598 Class _class(internal->GetValue());
1601 const char *name(CYPoolCString(pool, context, property));
1603 SEL sel(sel_registerName(name));
1605 objc_method *method(class_getInstanceMethod(_class, sel));
1610 if (JSValueIsObjectOfClass(context, value, Message_)) {
1611 Message_privateData *message(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate((JSObjectRef) value)));
1612 type = sig::Unparse(pool, &message->signature_);
1613 imp = reinterpret_cast<IMP>(message->GetValue());
1615 type = CYPoolTypeEncoding(pool, context, sel, method);
1616 imp = CYMakeMessage(context, value, type);
1620 method_setImplementation(method, imp);
1623 GSMethodList list(GSAllocMethodList(1));
1624 GSAppendMethodToList(list, sel, type, imp, YES);
1625 GSAddMethodList(_class, list, YES);
1626 GSFlushMethodCacheForClass(_class);
1628 class_addMethod(_class, sel, imp, type);
1635 #if 0 && OBJC_API_VERSION < 2
1636 static bool Messages_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1637 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1638 Class _class(internal->GetValue());
1641 const char *name(CYPoolCString(pool, context, property));
1643 if (SEL sel = sel_getUid(name))
1644 if (objc_method *method = class_getInstanceMethod(_class, sel)) {
1645 objc_method_list list = {NULL, 1, {method}};
1646 class_removeMethods(_class, &list);
1654 static void Messages_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1655 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1656 Class _class(internal->GetValue());
1658 #if OBJC_API_VERSION >= 2
1660 objc_method **data(class_copyMethodList(_class, &size));
1661 for (size_t i(0); i != size; ++i)
1662 JSPropertyNameAccumulatorAddName(names, CYJSString(sel_getName(method_getName(data[i]))));
1665 for (objc_method_list *methods(_class->methods); methods != NULL; methods = methods->method_next)
1666 for (int i(0); i != methods->method_count; ++i)
1667 JSPropertyNameAccumulatorAddName(names, CYJSString(sel_getName(method_getName(&methods->method_list[i]))));
1671 static bool CYHasImplicitProperties(Class _class) {
1672 // XXX: this is an evil hack to deal with NSProxy; fix elsewhere
1673 if (!CYImplements(_class, object_getClass(_class), @selector(cy$hasImplicitProperties)))
1675 return [_class cy$hasImplicitProperties];
1678 static bool Instance_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
1679 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1680 id self(internal->GetValue());
1682 if (JSStringIsEqualToUTF8CString(property, "$cyi"))
1686 NSString *name(CYCastNSString(&pool, context, property));
1688 if (CYInternal *internal = [CYInternal get:self])
1689 if ([internal hasProperty:property inContext:context])
1692 Class _class(object_getClass(self));
1695 // XXX: this is an evil hack to deal with NSProxy; fix elsewhere
1696 if (CYImplements(self, _class, @selector(cy$hasProperty:)))
1697 if ([self cy$hasProperty:name])
1699 } CYPoolCatch(false)
1701 const char *string(CYPoolCString(pool, context, name));
1704 if (class_getProperty(_class, string) != NULL)
1708 if (CYHasImplicitProperties(_class))
1709 if (SEL sel = sel_getUid(string))
1710 if (CYImplements(self, _class, sel, true))
1716 static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1717 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1718 id self(internal->GetValue());
1720 if (JSStringIsEqualToUTF8CString(property, "$cyi"))
1721 return Internal::Make(context, self, object);
1724 NSString *name(CYCastNSString(&pool, context, property));
1726 if (CYInternal *internal = [CYInternal get:self])
1727 if (JSValueRef value = [internal getProperty:property inContext:context])
1731 if (JSValueRef value = [self cy$getProperty:name inContext:context])
1735 const char *string(CYPoolCString(pool, context, name));
1736 Class _class(object_getClass(self));
1739 if (objc_property_t property = class_getProperty(_class, string)) {
1740 PropertyAttributes attributes(property);
1741 SEL sel(sel_registerName(attributes.Getter()));
1742 return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception);
1746 if (CYHasImplicitProperties(_class))
1747 if (SEL sel = sel_getUid(string))
1748 if (CYImplements(self, _class, sel, true))
1749 return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception);
1754 static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
1755 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1756 id self(internal->GetValue());
1760 NSString *name(CYCastNSString(&pool, context, property));
1761 NSObject *data(CYCastNSObject(&pool, context, value));
1764 if ([self cy$setProperty:name to:data])
1766 } CYPoolCatch(false)
1768 const char *string(CYPoolCString(pool, context, name));
1769 Class _class(object_getClass(self));
1772 if (objc_property_t property = class_getProperty(_class, string)) {
1773 PropertyAttributes attributes(property);
1774 if (const char *setter = attributes.Setter()) {
1775 SEL sel(sel_registerName(setter));
1776 JSValueRef arguments[1] = {value};
1777 CYSendMessage(pool, context, self, NULL, sel, 1, arguments, false, exception);
1783 size_t length(strlen(string));
1785 char set[length + 5];
1791 if (string[0] != '\0') {
1792 set[3] = toupper(string[0]);
1793 memcpy(set + 4, string + 1, length - 1);
1796 set[length + 3] = ':';
1797 set[length + 4] = '\0';
1799 if (SEL sel = sel_getUid(set))
1800 if (CYImplements(self, _class, sel)) {
1801 JSValueRef arguments[1] = {value};
1802 CYSendMessage(pool, context, self, NULL, sel, 1, arguments, false, exception);
1806 if (CYInternal *internal = [CYInternal set:self inContext:context]) {
1807 [internal setProperty:property toValue:value inContext:context];
1814 static bool Instance_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1815 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1816 id self(internal->GetValue());
1819 NSString *name(CYCastNSString(NULL, context, property));
1820 return [self cy$deleteProperty:name];
1821 } CYPoolCatch(false)
1822 } CYCatch(false) return /*XXX*/ false; }
1824 static void Instance_getPropertyNames_message(JSPropertyNameAccumulatorRef names, objc_method *method) {
1825 const char *name(sel_getName(method_getName(method)));
1826 if (strchr(name, ':') != NULL)
1829 const char *type(method_getTypeEncoding(method));
1830 if (type == NULL || *type == '\0' || *type == 'v')
1833 JSPropertyNameAccumulatorAddName(names, CYJSString(name));
1836 static void Instance_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1837 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1838 id self(internal->GetValue());
1841 Class _class(object_getClass(self));
1846 objc_property_t *data(class_copyPropertyList(_class, &size));
1847 for (size_t i(0); i != size; ++i)
1848 JSPropertyNameAccumulatorAddName(names, CYJSString(property_getName(data[i])));
1853 if (CYHasImplicitProperties(_class))
1854 for (Class current(_class); current != nil; current = class_getSuperclass(current)) {
1855 #if OBJC_API_VERSION >= 2
1857 objc_method **data(class_copyMethodList(current, &size));
1858 for (size_t i(0); i != size; ++i)
1859 Instance_getPropertyNames_message(names, data[i]);
1862 for (objc_method_list *methods(current->methods); methods != NULL; methods = methods->method_next)
1863 for (int i(0); i != methods->method_count; ++i)
1864 Instance_getPropertyNames_message(names, &methods->method_list[i]);
1869 // XXX: this is an evil hack to deal with NSProxy; fix elsewhere
1870 if (CYImplements(self, _class, @selector(cy$getPropertyNames:inContext:)))
1871 [self cy$getPropertyNames:names inContext:context];
1875 static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1876 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1877 JSObjectRef value(Instance::Make(context, [internal->GetValue() alloc], Instance::Uninitialized));
1881 static JSValueRef Instance_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1882 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1883 id self(internal->GetValue());
1885 if (![self isKindOfClass:NSBlock_])
1886 CYThrow("non-NSBlock object is not a function");
1887 // XXX: replace above logic with the following assertion
1888 //_assert([self isKindOfClass:NSBlock_]);
1889 // to do this, make it so FunctionInstance_ is the class of blocks
1890 // to do /that/, generalize the various "is exactly Instance_" checks
1891 // then, move Instance_callAsFunction to only be on FunctionInstance
1893 BlockLiteral *literal(reinterpret_cast<BlockLiteral *>(self));
1895 if ((literal->flags & BLOCK_HAS_SIGNATURE) != 0) {
1896 uint8_t *descriptor(reinterpret_cast<uint8_t *>(literal->descriptor));
1897 descriptor += sizeof(BlockDescriptor1);
1898 if ((literal->flags & BLOCK_HAS_COPY_DISPOSE) != 0)
1899 descriptor += sizeof(BlockDescriptor2);
1900 BlockDescriptor3 *descriptor3(reinterpret_cast<BlockDescriptor3 *>(descriptor));
1902 if (const char *type = descriptor3->signature) {
1908 sig::Signature signature;
1909 sig::Parse(pool, &signature, type, &Structor_);
1912 sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif);
1914 void (*function)() = reinterpret_cast<void (*)()>(literal->invoke);
1915 return CYCallFunction(pool, context, 1, setup, count, arguments, false, exception, &signature, &cif, function);
1920 CYThrow("NSBlock without signature field passed arguments");
1924 } CYPoolCatch(NULL);
1929 static bool Instance_hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef instance, JSValueRef *exception) { CYTry {
1930 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) constructor)));
1931 Class _class(internal->GetValue());
1932 if (!CYIsClass(_class))
1935 if (CYJSValueIsNSObject(context, instance)) {
1936 Instance *linternal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) instance)));
1937 // XXX: this isn't always safe
1938 return [linternal->GetValue() isKindOfClass:_class];
1944 static JSValueRef Instance_box_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1946 throw CYJSError(context, "incorrect number of arguments to Instance");
1948 id value(CYCastNSObject(&pool, context, arguments[0]));
1950 value = [NSNull null];
1951 return CYCastJSValue(context, [value cy$box]);
1954 static bool Internal_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
1955 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1958 id self(internal->GetValue());
1959 const char *name(CYPoolCString(pool, context, property));
1961 if (object_getInstanceVariable(self, name, NULL) != NULL)
1967 static JSValueRef Internal_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1968 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1971 id self(internal->GetValue());
1972 const char *name(CYPoolCString(pool, context, property));
1975 if (strcmp(name, "isa") == 0)
1976 return CYCastJSValue(context, object_getClass(self));
1979 if (objc_ivar *ivar = object_getInstanceVariable(self, name, NULL)) {
1980 Type_privateData type(pool, ivar_getTypeEncoding(ivar));
1981 // XXX: if this fails and throws an exception the person we are throwing it to gets the wrong exception
1982 return CYFromFFI(context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar));
1988 static bool Internal_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
1989 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1992 id self(internal->GetValue());
1993 const char *name(CYPoolCString(pool, context, property));
1995 if (objc_ivar *ivar = object_getInstanceVariable(self, name, NULL)) {
1996 Type_privateData type(pool, ivar_getTypeEncoding(ivar));
1997 CYPoolFFI(&pool, context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar), value);
2004 static void Internal_getPropertyNames_(Class _class, JSPropertyNameAccumulatorRef names) {
2005 if (Class super = class_getSuperclass(_class))
2006 Internal_getPropertyNames_(super, names);
2008 #if OBJC_API_VERSION >= 2
2010 objc_ivar **data(class_copyIvarList(_class, &size));
2011 for (size_t i(0); i != size; ++i)
2012 JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(data[i])));
2015 if (objc_ivar_list *ivars = _class->ivars)
2016 for (int i(0); i != ivars->ivar_count; ++i)
2017 JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(&ivars->ivar_list[i])));
2021 static void Internal_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
2022 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
2025 id self(internal->GetValue());
2026 Class _class(object_getClass(self));
2028 Internal_getPropertyNames_(_class, names);
2031 static JSValueRef Internal_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2032 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
2033 return internal->GetOwner();
2036 static JSValueRef ObjectiveC_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2038 NSString *name(CYCastNSString(&pool, context, property));
2039 if (Class _class = NSClassFromString(name))
2040 return CYMakeInstance(context, _class, true);
2044 static void ObjectiveC_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
2046 size_t size(objc_getClassList(NULL, 0));
2047 Class *data(reinterpret_cast<Class *>(malloc(sizeof(Class) * size)));
2050 size_t writ(objc_getClassList(data, size));
2053 if (Class *copy = reinterpret_cast<Class *>(realloc(data, sizeof(Class) * writ))) {
2059 for (size_t i(0); i != writ; ++i)
2060 JSPropertyNameAccumulatorAddName(names, CYJSString(class_getName(data[i])));
2066 while (Class _class = objc_next_class(&state))
2067 JSPropertyNameAccumulatorAddName(names, CYJSString(class_getName(_class)));
2071 #if OBJC_API_VERSION >= 2
2072 static JSValueRef ObjectiveC_Image_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2073 const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object)));
2076 const char *name(CYPoolCString(pool, context, property));
2078 const char **data(objc_copyClassNamesForImage(internal, &size));
2080 for (size_t i(0); i != size; ++i)
2081 if (strcmp(name, data[i]) == 0) {
2082 if (Class _class = objc_getClass(name)) {
2083 value = CYMakeInstance(context, _class, true);
2094 static void ObjectiveC_Image_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
2095 const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object)));
2097 const char **data(objc_copyClassNamesForImage(internal, &size));
2098 for (size_t i(0); i != size; ++i)
2099 JSPropertyNameAccumulatorAddName(names, CYJSString(data[i]));
2103 static JSValueRef ObjectiveC_Images_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2105 const char *name(CYPoolCString(pool, context, property));
2107 const char **data(objc_copyImageNames(&size));
2108 for (size_t i(0); i != size; ++i)
2109 if (strcmp(name, data[i]) == 0) {
2118 JSObjectRef value(JSObjectMake(context, NULL, NULL));
2119 CYSetProperty(context, value, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Image_Classes_, const_cast<char *>(name)));
2123 static void ObjectiveC_Images_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
2125 const char **data(objc_copyImageNames(&size));
2126 for (size_t i(0); i != size; ++i)
2127 JSPropertyNameAccumulatorAddName(names, CYJSString(data[i]));
2132 static JSValueRef ObjectiveC_Protocols_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2134 const char *name(CYPoolCString(pool, context, property));
2135 if (Protocol *protocol = objc_getProtocol(name))
2136 return CYMakeInstance(context, protocol, true);
2140 static void ObjectiveC_Protocols_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
2141 #if OBJC_API_VERSION >= 2
2143 Protocol **data(objc_copyProtocolList(&size));
2144 for (size_t i(0); i != size; ++i)
2145 JSPropertyNameAccumulatorAddName(names, CYJSString(protocol_getName(data[i])));
2152 static JSValueRef ObjectiveC_Constants_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2154 CYUTF8String name(CYPoolUTF8String(pool, context, property));
2156 return Instance::Make(context, nil);
2160 static void ObjectiveC_Constants_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
2161 JSPropertyNameAccumulatorAddName(names, CYJSString("nil"));
2165 #if defined(__i386__) || defined(__x86_64__)
2166 #define OBJC_MAX_STRUCT_BY_VALUE 8
2167 static int struct_forward_array[] = {
2168 0, 0, 0, 1, 0, 1, 1, 1, 0 };
2169 #elif defined(__arm__)
2170 #define OBJC_MAX_STRUCT_BY_VALUE 1
2171 static int struct_forward_array[] = {
2173 #elif defined(__arm64__)
2176 #error missing objc-runtime-info
2180 static bool stret(ffi_type *ffi_type) {
2181 return ffi_type->type == FFI_TYPE_STRUCT && (
2182 ffi_type->size > OBJC_MAX_STRUCT_BY_VALUE ||
2183 struct_forward_array[ffi_type->size] != 0
2189 JSValueRef CYSendMessage(CYPool &pool, JSContextRef context, id self, Class _class, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception) { CYTry {
2193 _class = object_getClass(self);
2197 if (objc_method *method = class_getInstanceMethod(_class, _cmd)) {
2198 imp = method_getImplementation(method);
2199 type = method_getTypeEncoding(method);
2204 NSMethodSignature *method([self methodSignatureForSelector:_cmd]);
2206 throw CYJSError(context, "unrecognized selector %s sent to object %p", sel_getName(_cmd), self);
2207 type = CYPoolCString(pool, context, [method _typeString]);
2215 sig::Signature signature;
2216 sig::Parse(pool, &signature, type, &Structor_);
2218 size_t used(count + 3);
2219 if (used > signature.count) {
2220 sig::Element *elements(new (pool) sig::Element[used]);
2221 memcpy(elements, signature.elements, used * sizeof(sig::Element));
2223 for (size_t index(signature.count); index != used; ++index) {
2224 sig::Element *element(&elements[index]);
2225 element->name = NULL;
2226 element->offset = _not(size_t);
2228 sig::Type *type(new (pool) sig::Type);
2229 memset(type, 0, sizeof(*type));
2230 type->primitive = sig::object_P;
2231 element->type = type;
2234 signature.elements = elements;
2235 signature.count = used;
2239 sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif);
2244 if (stret(cif.rtype))
2245 imp = class_getMethodImplementation_stret(_class, _cmd);
2248 imp = class_getMethodImplementation(_class, _cmd);
2250 objc_super super = {self, _class};
2251 imp = objc_msg_lookup_super(&super, _cmd);
2255 void (*function)() = reinterpret_cast<void (*)()>(imp);
2256 return CYCallFunction(pool, context, 2, setup, count, arguments, initialize, exception, &signature, &cif, function);
2259 static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2261 throw CYJSError(context, "too few arguments to objc_msgSend");
2271 if (JSValueIsObjectOfClass(context, arguments[0], Super_)) {
2272 cy::Super *internal(reinterpret_cast<cy::Super *>(JSObjectGetPrivate((JSObjectRef) arguments[0])));
2273 self = internal->GetValue();
2274 _class = internal->class_;;
2275 uninitialized = false;
2276 } else if (CYJSValueIsNSObject(context, arguments[0])) {
2277 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) arguments[0])));
2278 self = internal->GetValue();
2280 uninitialized = internal->IsUninitialized();
2282 internal->value_ = nil;
2284 self = CYCastNSObject(&pool, context, arguments[0]);
2286 uninitialized = false;
2290 return CYJSNull(context);
2292 _cmd = CYCastSEL(context, arguments[1]);
2294 return CYSendMessage(pool, context, self, _class, _cmd, count - 2, arguments + 2, uninitialized, exception);
2297 static JSValueRef Selector_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2298 JSValueRef setup[count + 2];
2301 memcpy(setup + 2, arguments, sizeof(JSValueRef) * count);
2302 return $objc_msgSend(context, NULL, NULL, count + 2, setup, exception);
2305 static JSValueRef Message_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2307 Message_privateData *internal(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate(object)));
2309 // XXX: handle Instance::Uninitialized?
2310 id self(CYCastNSObject(&pool, context, _this));
2314 setup[1] = &internal->sel_;
2316 return CYCallFunction(pool, context, 2, setup, count, arguments, false, exception, &internal->signature_, &internal->cif_, internal->GetValue());
2319 static JSObjectRef Super_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2321 throw CYJSError(context, "incorrect number of arguments to Super constructor");
2323 id self(CYCastNSObject(&pool, context, arguments[0]));
2324 Class _class(CYCastClass(pool, context, arguments[1]));
2325 return cy::Super::Make(context, self, _class);
2328 static JSObjectRef Selector_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2330 throw CYJSError(context, "incorrect number of arguments to Selector constructor");
2332 const char *name(CYPoolCString(pool, context, arguments[0]));
2333 return CYMakeSelector(context, sel_registerName(name));
2336 static JSObjectRef Instance_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2338 throw CYJSError(context, "incorrect number of arguments to Instance constructor");
2339 id self(count == 0 ? nil : CYCastPointer<id>(context, arguments[0]));
2340 return CYMakeInstance(context, self, false);
2343 static JSValueRef CYValue_getProperty_value(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2344 CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(object)));
2345 return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_));
2348 static JSValueRef CYValue_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2349 CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this)));
2350 Type_privateData *typical(internal->GetType());
2355 if (typical == NULL) {
2359 type = typical->type_;
2360 ffi = typical->ffi_;
2363 return CYMakePointer(context, &internal->value_, _not(size_t), type, ffi, object);
2366 static JSValueRef Instance_getProperty_constructor(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2367 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
2368 return Instance::Make(context, (id) object_getClass(internal->GetValue()));
2371 static JSValueRef Instance_getProperty_prototype(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2372 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
2373 id self(internal->GetValue());
2374 if (!CYIsClass(self))
2375 return CYJSUndefined(context);
2376 return CYGetClassPrototype(context, self);
2379 static JSValueRef Instance_getProperty_messages(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2380 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
2381 id self(internal->GetValue());
2382 if (!CYIsClass(self))
2383 return CYJSUndefined(context);
2384 return Messages::Make(context, (Class) self);
2387 static JSValueRef Instance_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2388 if (!CYJSValueIsNSObject(context, _this))
2391 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2392 return CYCastJSValue(context, CYJSString(context, CYCastNSCYON(internal->GetValue(), false)));
2395 static JSValueRef Instance_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2396 if (!CYJSValueIsNSObject(context, _this))
2399 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2400 id value(internal->GetValue());
2407 key = CYCastNSString(NULL, context, CYJSString(context, arguments[0]));
2409 if (!CYImplements(value, object_getClass(value), @selector(cy$toJSON:inContext:)))
2410 return CYJSUndefined(context);
2411 else if (JSValueRef json = [value cy$toJSON:key inContext:context])
2414 return CYCastJSValue(context, CYJSString(context, [value description]));
2416 } CYCatch(NULL) return /*XXX*/ NULL; }
2418 static JSValueRef Instance_callAsFunction_valueOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2419 if (!CYJSValueIsNSObject(context, _this))
2422 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2423 id value(internal->GetValue());
2425 if (![value respondsToSelector:@selector(cy$valueOfInContext:)])
2428 if (JSValueRef result = [value cy$valueOfInContext:context])
2432 } CYCatch(NULL) return /*XXX*/ NULL; }
2434 static JSValueRef Instance_callAsFunction_toPointer(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2435 if (!CYJSValueIsNSObject(context, _this))
2438 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2439 // XXX: but... but... THIS ISN'T A POINTER! :(
2440 return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->GetValue()));
2441 } CYCatch(NULL) return /*XXX*/ NULL; }
2443 static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2444 if (!CYJSValueIsNSObject(context, _this))
2447 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2448 id value(internal->GetValue());
2451 // XXX: this seems like a stupid implementation; what if it crashes? why not use the CYONifier backend?
2452 return CYCastJSValue(context, CYJSString(context, [value description]));
2454 } CYCatch(NULL) return /*XXX*/ NULL; }
2456 static JSValueRef Class_callAsFunction_pointerTo(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2457 if (!CYJSValueIsNSObject(context, _this))
2460 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2461 id value(internal->GetValue());
2463 if (!CYIsClass(value))
2464 CYThrow("non-Class object cannot be used as Type");
2466 // XXX: this is a very silly implementation
2468 std::ostringstream type;
2470 type << class_getName(value);
2474 return CYMakeType(context, type.str().c_str());
2476 } CYCatch(NULL) return /*XXX*/ NULL; }
2478 static JSValueRef Selector_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2479 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
2480 return CYCastJSValue(context, sel_getName(internal->GetValue()));
2483 static JSValueRef Selector_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2484 return Selector_callAsFunction_toString(context, object, _this, count, arguments, exception);
2487 static JSValueRef Selector_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2488 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
2489 const char *name(sel_getName(internal->GetValue()));
2492 NSString *string([NSString stringWithFormat:@"@selector(%s)", name]);
2493 return CYCastJSValue(context, CYJSString(context, string));
2495 } CYCatch(NULL) return /*XXX*/ NULL; }
2497 static JSValueRef Selector_callAsFunction_type(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2499 throw CYJSError(context, "incorrect number of arguments to Selector.type");
2502 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
2503 SEL sel(internal->GetValue());
2505 objc_method *method;
2506 if (Class _class = CYCastClass(pool, context, arguments[0]))
2507 method = class_getInstanceMethod(_class, sel);
2511 if (const char *type = CYPoolTypeEncoding(pool, context, sel, method))
2512 return CYCastJSValue(context, CYJSString(type));
2514 return CYJSNull(context);
2517 static JSStaticValue Selector_staticValues[2] = {
2518 {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete},
2519 {NULL, NULL, NULL, 0}
2522 static JSStaticValue Instance_staticValues[5] = {
2523 {"constructor", &Instance_getProperty_constructor, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2524 {"messages", &Instance_getProperty_messages, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2525 {"prototype", &Instance_getProperty_prototype, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2526 {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2527 {NULL, NULL, NULL, 0}
2530 static JSStaticFunction Instance_staticFunctions[7] = {
2531 {"$cya", &CYValue_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2532 {"toCYON", &Instance_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2533 {"toJSON", &Instance_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2534 {"valueOf", &Instance_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2535 {"toPointer", &Instance_callAsFunction_toPointer, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2536 {"toString", &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2540 static JSStaticFunction Class_staticFunctions[2] = {
2541 {"pointerTo", &Class_callAsFunction_pointerTo, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2545 static JSStaticFunction Internal_staticFunctions[2] = {
2546 {"$cya", &Internal_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2550 static JSStaticFunction Selector_staticFunctions[5] = {
2551 {"toCYON", &Selector_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2552 {"toJSON", &Selector_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2553 {"toString", &Selector_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2554 {"type", &Selector_callAsFunction_type, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2559 JSValueRef NSCFType$cy$toJSON$inContext$(id self, SEL sel, JSValueRef key, JSContextRef context) { CYObjectiveTry_(context) {
2560 return CYCastJSValue(context, [(NSString *) CFCopyDescription((CFTypeRef) self) autorelease]);
2561 } CYObjectiveCatch }
2564 void CYObjectiveC_Initialize() { /*XXX*/ JSContextRef context(NULL); CYPoolTry {
2565 $objc_setAssociatedObject = reinterpret_cast<void (*)(id, void *, id value, objc_AssociationPolicy)>(dlsym(RTLD_DEFAULT, "objc_setAssociatedObject"));
2566 $objc_getAssociatedObject = reinterpret_cast<id (*)(id, void *)>(dlsym(RTLD_DEFAULT, "objc_getAssociatedObject"));
2567 $objc_removeAssociatedObjects = reinterpret_cast<void (*)(id)>(dlsym(RTLD_DEFAULT, "objc_removeAssociatedObjects"));
2569 CYPool &pool(CYGetGlobalPool());
2571 Object_type = new(pool) Type_privateData("@");
2572 Selector_type = new(pool) Type_privateData(":");
2574 NSArray_ = objc_getClass("NSArray");
2575 NSBlock_ = objc_getClass("NSBlock");
2576 NSDictionary_ = objc_getClass("NSDictionary");
2577 NSString_ = objc_getClass("NSString");
2578 Object_ = objc_getClass("Object");
2581 // XXX: apparently, iOS now has both of these
2582 NSCFBoolean_ = objc_getClass("__NSCFBoolean");
2583 if (NSCFBoolean_ == nil)
2584 NSCFBoolean_ = objc_getClass("NSCFBoolean");
2586 NSCFType_ = objc_getClass("NSCFType");
2588 NSZombie_ = objc_getClass("_NSZombie_");
2590 banned_.insert(Object_);
2591 banned_.insert(objc_getClass("__NSAtom"));
2592 banned_.insert(objc_getClass("__NSGenericDeallocHandler"));
2593 banned_.insert(objc_getClass("NSMessageBuilder"));
2594 banned_.insert(objc_getClass("__NSMessageBuilder"));
2596 NSBoolNumber_ = objc_getClass("NSBoolNumber");
2599 JSClassDefinition definition;
2601 definition = kJSClassDefinitionEmpty;
2602 definition.className = "Instance";
2603 definition.staticValues = Instance_staticValues;
2604 definition.staticFunctions = Instance_staticFunctions;
2605 definition.hasProperty = &Instance_hasProperty;
2606 definition.getProperty = &Instance_getProperty;
2607 definition.setProperty = &Instance_setProperty;
2608 definition.deleteProperty = &Instance_deleteProperty;
2609 definition.getPropertyNames = &Instance_getPropertyNames;
2610 definition.callAsConstructor = &Instance_callAsConstructor;
2611 definition.callAsFunction = &Instance_callAsFunction;
2612 definition.hasInstance = &Instance_hasInstance;
2613 definition.finalize = &CYFinalize;
2614 Instance_ = JSClassCreate(&definition);
2616 definition.className = "ArrayInstance";
2617 ArrayInstance_ = JSClassCreate(&definition);
2619 definition.className = "FunctionInstance";
2620 FunctionInstance_ = JSClassCreate(&definition);
2622 definition.className = "ObjectInstance";
2623 ObjectInstance_ = JSClassCreate(&definition);
2625 definition.className = "StringInstance";
2626 StringInstance_ = JSClassCreate(&definition);
2628 definition = kJSClassDefinitionEmpty;
2629 definition.className = "Class";
2630 definition.staticFunctions = Class_staticFunctions;
2631 Class_ = JSClassCreate(&definition);
2633 definition = kJSClassDefinitionEmpty;
2634 definition.className = "Internal";
2635 definition.staticFunctions = Internal_staticFunctions;
2636 definition.hasProperty = &Internal_hasProperty;
2637 definition.getProperty = &Internal_getProperty;
2638 definition.setProperty = &Internal_setProperty;
2639 definition.getPropertyNames = &Internal_getPropertyNames;
2640 definition.finalize = &CYFinalize;
2641 Internal_ = JSClassCreate(&definition);
2643 definition = kJSClassDefinitionEmpty;
2644 definition.className = "Message";
2645 definition.staticFunctions = cy::Functor::StaticFunctions;
2646 definition.callAsFunction = &Message_callAsFunction;
2647 definition.finalize = &CYFinalize;
2648 Message_ = JSClassCreate(&definition);
2650 definition = kJSClassDefinitionEmpty;
2651 definition.className = "Messages";
2652 definition.hasProperty = &Messages_hasProperty;
2653 definition.getProperty = &Messages_getProperty;
2654 definition.setProperty = &Messages_setProperty;
2655 #if 0 && OBJC_API_VERSION < 2
2656 definition.deleteProperty = &Messages_deleteProperty;
2658 definition.getPropertyNames = &Messages_getPropertyNames;
2659 definition.finalize = &CYFinalize;
2660 Messages_ = JSClassCreate(&definition);
2662 definition = kJSClassDefinitionEmpty;
2663 definition.className = "Selector";
2664 definition.staticValues = Selector_staticValues;
2665 definition.staticFunctions = Selector_staticFunctions;
2666 definition.callAsFunction = &Selector_callAsFunction;
2667 definition.finalize = &CYFinalize;
2668 Selector_ = JSClassCreate(&definition);
2670 definition = kJSClassDefinitionEmpty;
2671 definition.className = "Super";
2672 definition.staticFunctions = Internal_staticFunctions;
2673 definition.finalize = &CYFinalize;
2674 Super_ = JSClassCreate(&definition);
2676 definition = kJSClassDefinitionEmpty;
2677 definition.className = "ObjectiveC::Classes";
2678 definition.getProperty = &ObjectiveC_Classes_getProperty;
2679 definition.getPropertyNames = &ObjectiveC_Classes_getPropertyNames;
2680 ObjectiveC_Classes_ = JSClassCreate(&definition);
2682 definition = kJSClassDefinitionEmpty;
2683 definition.className = "ObjectiveC::Constants";
2684 definition.getProperty = &ObjectiveC_Constants_getProperty;
2685 definition.getPropertyNames = &ObjectiveC_Constants_getPropertyNames;
2686 ObjectiveC_Constants_ = JSClassCreate(&definition);
2688 #if OBJC_API_VERSION >= 2
2689 definition = kJSClassDefinitionEmpty;
2690 definition.className = "ObjectiveC::Images";
2691 definition.getProperty = &ObjectiveC_Images_getProperty;
2692 definition.getPropertyNames = &ObjectiveC_Images_getPropertyNames;
2693 ObjectiveC_Images_ = JSClassCreate(&definition);
2695 definition = kJSClassDefinitionEmpty;
2696 definition.className = "ObjectiveC::Image::Classes";
2697 definition.getProperty = &ObjectiveC_Image_Classes_getProperty;
2698 definition.getPropertyNames = &ObjectiveC_Image_Classes_getPropertyNames;
2699 ObjectiveC_Image_Classes_ = JSClassCreate(&definition);
2702 definition = kJSClassDefinitionEmpty;
2703 definition.className = "ObjectiveC::Protocols";
2704 definition.getProperty = &ObjectiveC_Protocols_getProperty;
2705 definition.getPropertyNames = &ObjectiveC_Protocols_getPropertyNames;
2706 ObjectiveC_Protocols_ = JSClassCreate(&definition);
2709 class_addMethod(NSCFType_, @selector(cy$toJSON:inContext:), reinterpret_cast<IMP>(&NSCFType$cy$toJSON$inContext$), "^{OpaqueJSValue=}16@0:4@8^{OpaqueJSContext=}12");
2713 void CYObjectiveC_SetupContext(JSContextRef context) { CYPoolTry {
2714 JSObjectRef global(CYGetGlobalObject(context));
2715 JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s)));
2716 JSObjectRef cycript(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Cycript"))));
2717 JSObjectRef all(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("all"))));
2718 JSObjectRef alls(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("alls"))));
2720 JSObjectRef ObjectiveC(JSObjectMake(context, NULL, NULL));
2721 CYSetProperty(context, cycript, CYJSString("ObjectiveC"), ObjectiveC);
2723 JSObjectRef protocols(JSObjectMake(context, ObjectiveC_Protocols_, NULL));
2724 CYSetProperty(context, ObjectiveC, CYJSString("protocols"), protocols);
2725 CYArrayPush(context, alls, protocols);
2727 JSObjectRef classes(JSObjectMake(context, ObjectiveC_Classes_, NULL));
2728 CYSetProperty(context, ObjectiveC, CYJSString("classes"), classes);
2729 CYArrayPush(context, alls, classes);
2731 JSObjectRef constants(JSObjectMake(context, ObjectiveC_Constants_, NULL));
2732 CYSetProperty(context, ObjectiveC, CYJSString("constants"), constants);
2733 CYArrayPush(context, alls, constants);
2735 #if OBJC_API_VERSION >= 2
2736 CYSetProperty(context, ObjectiveC, CYJSString("images"), JSObjectMake(context, ObjectiveC_Images_, NULL));
2739 JSObjectRef Class(JSObjectMakeConstructor(context, Class_, NULL));
2740 JSObjectRef Instance(JSObjectMakeConstructor(context, Instance_, &Instance_new));
2741 JSObjectRef Message(JSObjectMakeConstructor(context, Message_, NULL));
2742 JSObjectRef Selector(JSObjectMakeConstructor(context, Selector_, &Selector_new));
2743 JSObjectRef Super(JSObjectMakeConstructor(context, Super_, &Super_new));
2745 JSObjectRef Instance_prototype(CYCastJSObject(context, CYGetProperty(context, Instance, prototype_s)));
2746 CYSetProperty(context, cy, CYJSString("Instance_prototype"), Instance_prototype);
2748 JSObjectRef ArrayInstance(JSObjectMakeConstructor(context, ArrayInstance_, NULL));
2749 JSObjectRef ArrayInstance_prototype(CYCastJSObject(context, CYGetProperty(context, ArrayInstance, prototype_s)));
2750 CYSetProperty(context, cy, CYJSString("ArrayInstance_prototype"), ArrayInstance_prototype);
2751 JSObjectRef Array_prototype(CYGetCachedObject(context, CYJSString("Array_prototype")));
2752 JSObjectSetPrototype(context, ArrayInstance_prototype, Array_prototype);
2754 JSObjectRef FunctionInstance(JSObjectMakeConstructor(context, FunctionInstance_, NULL));
2755 JSObjectRef FunctionInstance_prototype(CYCastJSObject(context, CYGetProperty(context, FunctionInstance, prototype_s)));
2756 CYSetProperty(context, cy, CYJSString("FunctionInstance_prototype"), FunctionInstance_prototype);
2757 JSObjectRef Function_prototype(CYGetCachedObject(context, CYJSString("Function_prototype")));
2758 JSObjectSetPrototype(context, FunctionInstance_prototype, Function_prototype);
2760 JSObjectRef ObjectInstance(JSObjectMakeConstructor(context, ObjectInstance_, NULL));
2761 JSObjectRef ObjectInstance_prototype(CYCastJSObject(context, CYGetProperty(context, ObjectInstance, prototype_s)));
2762 CYSetProperty(context, cy, CYJSString("ObjectInstance_prototype"), ObjectInstance_prototype);
2763 JSObjectRef Object_prototype(CYGetCachedObject(context, CYJSString("Object_prototype")));
2764 JSObjectSetPrototype(context, ObjectInstance_prototype, Object_prototype);
2766 JSObjectRef StringInstance(JSObjectMakeConstructor(context, StringInstance_, NULL));
2767 JSObjectRef StringInstance_prototype(CYCastJSObject(context, CYGetProperty(context, StringInstance, prototype_s)));
2768 CYSetProperty(context, cy, CYJSString("StringInstance_prototype"), StringInstance_prototype);
2769 JSObjectRef String_prototype(CYGetCachedObject(context, CYJSString("String_prototype")));
2770 JSObjectSetPrototype(context, StringInstance_prototype, String_prototype);
2772 JSObjectRef Class_prototype(CYCastJSObject(context, CYGetProperty(context, Class, prototype_s)));
2773 CYSetProperty(context, cy, CYJSString("Class_prototype"), Class_prototype);
2774 JSObjectSetPrototype(context, Class_prototype, Instance_prototype);
2776 CYSetProperty(context, cycript, CYJSString("Instance"), Instance);
2777 CYSetProperty(context, cycript, CYJSString("Selector"), Selector);
2778 CYSetProperty(context, cycript, CYJSString("Super"), Super);
2780 JSObjectRef box(JSObjectMakeFunctionWithCallback(context, CYJSString("box"), &Instance_box_callAsFunction));
2781 CYSetProperty(context, Instance, CYJSString("box"), box);
2783 #if defined(__APPLE__) && defined(__arm__) && 0
2784 CYSetProperty(context, all, CYJSString("objc_registerClassPair"), &objc_registerClassPair_, kJSPropertyAttributeDontEnum);
2787 CYSetProperty(context, all, CYJSString("objc_msgSend"), &$objc_msgSend, kJSPropertyAttributeDontEnum);
2789 JSObjectSetPrototype(context, CYCastJSObject(context, CYGetProperty(context, Message, prototype_s)), Function_prototype);
2790 JSObjectSetPrototype(context, CYCastJSObject(context, CYGetProperty(context, Selector, prototype_s)), Function_prototype);
2793 static CYHooks CYObjectiveCHooks = {
2794 &CYObjectiveC_ExecuteStart,
2795 &CYObjectiveC_ExecuteEnd,
2796 &CYObjectiveC_CallFunction,
2797 &CYObjectiveC_Initialize,
2798 &CYObjectiveC_SetupContext,
2799 &CYObjectiveC_PoolFFI,
2800 &CYObjectiveC_FromFFI,
2803 struct CYObjectiveC {
2805 hooks_ = &CYObjectiveCHooks;
2806 // XXX: evil magic juju to make this actually take effect on a Mac when compiled with autoconf/libtool doom!
2807 _assert(hooks_ != NULL);