1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2012 Jay Freeman (saurik)
5 /* GNU Lesser General Public License, Version 3 {{{ */
7 * Cycript is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU Lesser General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
12 * Cycript is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
26 #include <Foundation/Foundation.h>
28 #include "ObjectiveC/Internal.hpp"
30 #include <objc/objc-api.h>
32 #include "cycript.hpp"
34 #include "ObjectiveC/Internal.hpp"
37 #include <CoreFoundation/CoreFoundation.h>
38 #include <JavaScriptCore/JSStringRefCF.h>
39 #include <WebKit/WebScriptObject.h>
40 #include <objc/runtime.h>
44 #include "JavaScript.hpp"
46 #include "Execute.hpp"
53 #define CYObjectiveTry_(context) { \
54 JSContextRef context_(context); \
56 #define CYObjectiveTry { \
58 #define CYObjectiveCatch \
59 catch (const CYException &error) { \
60 @throw CYCastNSObject(NULL, context_, error.CastJSValue(context_)); \
66 NSAutoreleasePool *_pool([[NSAutoreleasePool alloc] init]); \
68 #define CYPoolCatch(value) \
69 @catch (NSException *error) { \
70 _saved = [error retain]; \
71 throw CYJSError(context, CYCastJSValue(context, error)); \
76 [_saved autorelease]; \
82 #define CYSadCatch(value) \
83 @catch (NSException *error ) { \
84 throw CYJSError(context, CYCastJSValue(context, error)); \
89 #define class_getSuperclass GSObjCSuper
90 #define class_getInstanceVariable GSCGetInstanceVariableDefinition
91 #define class_getName GSNameFromClass
93 #define class_removeMethods(cls, list) GSRemoveMethodList(cls, list, YES)
95 #define ivar_getName(ivar) ((ivar)->ivar_name)
96 #define ivar_getOffset(ivar) ((ivar)->ivar_offset)
97 #define ivar_getTypeEncoding(ivar) ((ivar)->ivar_type)
99 #define method_getName(method) ((method)->method_name)
100 #define method_getImplementation(method) ((method)->method_imp)
101 #define method_getTypeEncoding(method) ((method)->method_types)
102 #define method_setImplementation(method, imp) ((void) ((method)->method_imp = (imp)))
105 #define objc_getClass GSClassFromName
107 #define objc_getProtocol GSProtocolFromName
109 #define object_getClass GSObjCClass
111 #define object_getInstanceVariable(object, name, value) ({ \
112 objc_ivar *ivar(class_getInstanceVariable(object_getClass(object), name)); \
113 _assert(value != NULL); \
115 GSObjCGetVariable(object, ivar_getOffset(ivar), sizeof(void *), value); \
119 #define object_setIvar(object, ivar, value) ({ \
120 void *data = (value); \
121 GSObjCSetVariable(object, ivar_getOffset(ivar), sizeof(void *), &data); \
124 #define protocol_getName(protocol) [(protocol) name]
127 static void (*$objc_setAssociatedObject)(id object, void *key, id value, objc_AssociationPolicy policy);
128 static id (*$objc_getAssociatedObject)(id object, void *key);
129 static void (*$objc_removeAssociatedObjects)(id object);
131 struct BlockLiteral {
135 void (*invoke)(void *, ...);
139 struct BlockDescriptor1 {
140 unsigned long int reserved;
141 unsigned long int size;
144 struct BlockDescriptor2 {
145 void (*copy_helper)(BlockLiteral *dst, BlockLiteral *src);
146 void (*dispose_helper)(BlockLiteral *src);
149 struct BlockDescriptor3 {
150 const char *signature;
155 BLOCK_DEALLOCATING = 0x0001,
156 BLOCK_REFCOUNT_MASK = 0xfffe,
157 BLOCK_NEEDS_FREE = 1 << 24,
158 BLOCK_HAS_COPY_DISPOSE = 1 << 25,
159 BLOCK_HAS_CTOR = 1 << 26,
160 BLOCK_IS_GC = 1 << 27,
161 BLOCK_IS_GLOBAL = 1 << 28,
162 BLOCK_HAS_STRET = 1 << 29,
163 BLOCK_HAS_SIGNATURE = 1 << 30,
166 JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, Class super, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception);
168 /* Objective-C Pool Release {{{ */
169 apr_status_t CYPoolRelease_(void *data) {
170 id object(reinterpret_cast<id>(data));
175 id CYPoolRelease_(apr_pool_t *pool, id object) {
178 else if (pool == NULL)
179 return [object autorelease];
181 apr_pool_cleanup_register(pool, object, &CYPoolRelease_, &apr_pool_cleanup_null);
186 template <typename Type_>
187 Type_ CYPoolRelease(apr_pool_t *pool, Type_ object) {
188 return (Type_) CYPoolRelease_(pool, (id) object);
191 /* Objective-C Strings {{{ */
192 const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, NSString *value) {
194 return [value UTF8String];
196 size_t size([value maximumLengthOfBytesUsingEncoding:NSUTF8StringEncoding] + 1);
197 char *string(new(pool) char[size]);
198 if (![value getCString:string maxLength:size encoding:NSUTF8StringEncoding])
199 throw CYJSError(context, "[NSString getCString:maxLength:encoding:] == NO");
204 JSStringRef CYCopyJSString(JSContextRef context, NSString *value) {
206 return JSStringCreateWithCFString(reinterpret_cast<CFStringRef>(value));
209 return CYCopyJSString(CYPoolCString(pool, context, value));
213 JSStringRef CYCopyJSString(JSContextRef context, NSObject *value) {
216 // XXX: this definition scares me; is anyone using this?!
217 NSString *string([value description]);
218 return CYCopyJSString(context, string);
221 NSString *CYCopyNSString(const CYUTF8String &value) {
223 return (NSString *) CFStringCreateWithBytes(kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(value.data), value.size, kCFStringEncodingUTF8, true);
225 return [[NSString alloc] initWithBytes:value.data length:value.size encoding:NSUTF8StringEncoding];
229 NSString *CYCopyNSString(JSContextRef context, JSStringRef value) {
231 return (NSString *) JSStringCopyCFString(kCFAllocatorDefault, value);
234 return CYCopyNSString(CYPoolUTF8String(pool, context, value));
238 NSString *CYCopyNSString(JSContextRef context, JSValueRef value) {
239 return CYCopyNSString(context, CYJSString(context, value));
242 NSString *CYCastNSString(apr_pool_t *pool, const CYUTF8String &value) {
243 return CYPoolRelease(pool, CYCopyNSString(value));
246 NSString *CYCastNSString(apr_pool_t *pool, SEL sel) {
247 const char *name(sel_getName(sel));
248 return CYPoolRelease(pool, CYCopyNSString(CYUTF8String(name, strlen(name))));
251 NSString *CYCastNSString(apr_pool_t *pool, JSContextRef context, JSStringRef value) {
252 return CYPoolRelease(pool, CYCopyNSString(context, value));
255 CYUTF8String CYCastUTF8String(NSString *value) {
256 NSData *data([value dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO]);
257 return CYUTF8String(reinterpret_cast<const char *>([data bytes]), [data length]);
261 JSValueRef CYCastJSValue(JSContextRef context, NSObject *value);
263 void CYThrow(JSContextRef context, NSException *error, JSValueRef *exception) {
264 if (exception == NULL)
266 *exception = CYCastJSValue(context, error);
269 size_t CYGetIndex(NSString *value) {
270 return CYGetIndex(CYCastUTF8String(value));
273 bool CYGetOffset(apr_pool_t *pool, JSContextRef context, NSString *value, ssize_t &index) {
274 return CYGetOffset(CYPoolCString(pool, context, value), index);
277 static JSClassRef Instance_;
279 static JSClassRef ArrayInstance_;
280 static JSClassRef FunctionInstance_;
281 static JSClassRef ObjectInstance_;
282 static JSClassRef StringInstance_;
284 static JSClassRef Class_;
285 static JSClassRef Internal_;
286 static JSClassRef Message_;
287 static JSClassRef Messages_;
288 static JSClassRef Selector_;
289 static JSClassRef Super_;
291 static JSClassRef ObjectiveC_Classes_;
292 static JSClassRef ObjectiveC_Constants_;
293 static JSClassRef ObjectiveC_Protocols_;
296 static JSClassRef ObjectiveC_Image_Classes_;
297 static JSClassRef ObjectiveC_Images_;
301 static Class NSCFBoolean_;
302 static Class NSCFType_;
303 static Class NSGenericDeallocHandler_;
304 static Class NSMessageBuilder_;
305 static Class NSZombie_;
307 static Class NSBoolNumber_;
310 static Class NSArray_;
311 static Class NSBlock_;
312 static Class NSDictionary_;
313 static Class NSString_;
314 static Class Object_;
316 static Type_privateData *Object_type;
317 static Type_privateData *Selector_type;
319 Type_privateData *Instance::GetType() const {
323 Type_privateData *Selector_privateData::GetType() const {
324 return Selector_type;
327 static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception);
329 JSValueRef CYGetClassPrototype(JSContextRef context, Class self, bool meta) {
331 return CYGetCachedObject(context, CYJSString("Instance_prototype"));
332 else if (meta && !class_isMetaClass(self))
333 return CYGetCachedObject(context, CYJSString("Class_prototype"));
335 JSObjectRef global(CYGetGlobalObject(context));
336 JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s)));
339 sprintf(label, "i%p", self);
340 CYJSString name(label);
342 JSValueRef value(CYGetProperty(context, cy, name));
343 if (!JSValueIsUndefined(context, value))
346 JSClassRef _class(NULL);
347 JSValueRef prototype;
349 if (self == NSArray_)
350 prototype = CYGetCachedObject(context, CYJSString("ArrayInstance_prototype"));
351 else if (self == NSBlock_)
352 prototype = CYGetCachedObject(context, CYJSString("FunctionInstance_prototype"));
353 else if (self == NSDictionary_)
354 prototype = CYGetCachedObject(context, CYJSString("ObjectInstance_prototype"));
355 else if (self == NSString_)
356 prototype = CYGetCachedObject(context, CYJSString("StringInstance_prototype"));
358 prototype = CYGetClassPrototype(context, class_getSuperclass(self), meta);
360 JSObjectRef object(JSObjectMake(context, _class, NULL));
361 JSObjectSetPrototype(context, object, prototype);
362 CYSetProperty(context, cy, name, object);
367 _finline JSValueRef CYGetClassPrototype(JSContextRef context, Class self) {
368 return CYGetClassPrototype(context, self, class_isMetaClass(self));
371 JSObjectRef Messages::Make(JSContextRef context, Class _class) {
372 JSObjectRef value(JSObjectMake(context, Messages_, new Messages(_class)));
373 if (Class super = class_getSuperclass(_class))
374 JSObjectSetPrototype(context, value, Messages::Make(context, super));
378 JSObjectRef Internal::Make(JSContextRef context, id object, JSObjectRef owner) {
379 return JSObjectMake(context, Internal_, new Internal(object, context, owner));
383 JSObjectRef Super::Make(JSContextRef context, id object, Class _class) {
384 JSObjectRef value(JSObjectMake(context, Super_, new Super(object, _class)));
388 JSObjectRef Instance::Make(JSContextRef context, id object, Flags flags) {
389 JSObjectRef value(JSObjectMake(context, Instance_, new Instance(object, flags)));
390 JSObjectSetPrototype(context, value, CYGetClassPrototype(context, object_getClass(object)));
394 Instance::~Instance() {
395 if ((flags_ & Transient) == 0)
396 // XXX: does this handle background threads correctly?
397 // XXX: this simply does not work on the console because I'm stupid
398 [GetValue() performSelector:@selector(release) withObject:nil afterDelay:0];
401 struct Message_privateData :
406 Message_privateData(SEL sel, const char *type, IMP value = NULL) :
407 cy::Functor(type, reinterpret_cast<void (*)()>(value)),
413 JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient) {
414 Instance::Flags flags;
417 flags = Instance::Transient;
419 flags = Instance::None;
420 object = [object retain];
423 return Instance::Make(context, object, flags);
426 @interface NSMethodSignature (Cycript)
427 - (NSString *) _typeString;
430 @interface NSObject (Cycript)
432 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context;
433 - (JSType) cy$JSType;
435 - (JSValueRef) cy$toJSON:(NSString *)key inContext:(JSContextRef)context;
436 - (NSString *) cy$toCYON:(bool)objective;
438 - (bool) cy$hasProperty:(NSString *)name;
439 - (NSObject *) cy$getProperty:(NSString *)name;
440 - (JSValueRef) cy$getProperty:(NSString *)name inContext:(JSContextRef)context;
441 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value;
442 - (bool) cy$deleteProperty:(NSString *)name;
443 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context;
445 + (bool) cy$hasImplicitProperties;
451 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context;
454 NSString *CYCastNSCYON(id value, bool objective) {
460 Class _class(object_getClass(value));
461 SEL sel(@selector(cy$toCYON:));
463 if (objc_method *toCYON = class_getInstanceMethod(_class, sel))
464 string = reinterpret_cast<NSString *(*)(id, SEL, bool)>(method_getImplementation(toCYON))(value, sel, objective);
465 else if (objc_method *methodSignatureForSelector = class_getInstanceMethod(_class, @selector(methodSignatureForSelector:))) {
466 if (reinterpret_cast<NSMethodSignature *(*)(id, SEL, SEL)>(method_getImplementation(methodSignatureForSelector))(value, @selector(methodSignatureForSelector:), sel) != nil)
467 string = [value cy$toCYON:objective];
472 else if (value == NSZombie_)
473 string = @"_NSZombie_";
474 else if (_class == NSZombie_)
475 string = [NSString stringWithFormat:@"<_NSZombie_: %p>", value];
476 // XXX: frowny /in/ the pants
477 else if (value == NSGenericDeallocHandler_ || value == NSMessageBuilder_ || value == Object_)
481 string = [NSString stringWithFormat:@"%@", value];
486 string = @"undefined";
493 struct PropertyAttributes {
498 const char *variable;
511 PropertyAttributes(objc_property_t property) :
523 name = property_getName(property);
524 const char *attributes(property_getAttributes(property));
526 for (char *state, *token(apr_strtok(apr_pstrdup(pool_, attributes), ",", &state)); token != NULL; token = apr_strtok(NULL, ",", &state)) {
528 case 'R': readonly = true; break;
529 case 'C': copy = true; break;
530 case '&': retain = true; break;
531 case 'N': nonatomic = true; break;
532 case 'G': getter_ = token + 1; break;
533 case 'S': setter_ = token + 1; break;
534 case 'V': variable = token + 1; break;
538 /*if (variable == NULL) {
539 variable = property_getName(property);
540 size_t size(strlen(variable));
541 char *name(new(pool_) char[size + 2]);
543 memcpy(name + 1, variable, size);
544 name[size + 1] = '\0';
549 const char *Getter() {
551 getter_ = apr_pstrdup(pool_, name);
555 const char *Setter() {
556 if (setter_ == NULL && !readonly) {
557 size_t length(strlen(name));
559 char *temp(new(pool_) char[length + 5]);
565 temp[3] = toupper(name[0]);
566 memcpy(temp + 4, name + 1, length - 1);
569 temp[length + 3] = ':';
570 temp[length + 4] = '\0';
581 @interface CYWebUndefined : NSObject {
584 + (CYWebUndefined *) undefined;
588 @implementation CYWebUndefined
590 + (CYWebUndefined *) undefined {
591 static CYWebUndefined *instance_([[CYWebUndefined alloc] init]);
597 #define WebUndefined CYWebUndefined
600 /* Bridge: CYJSObject {{{ */
601 @interface CYJSObject : NSMutableDictionary {
603 JSGlobalContextRef context_;
606 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context;
608 - (NSUInteger) count;
609 - (id) objectForKey:(id)key;
610 - (NSEnumerator *) keyEnumerator;
611 - (void) setObject:(id)object forKey:(id)key;
612 - (void) removeObjectForKey:(id)key;
616 /* Bridge: CYJSArray {{{ */
617 @interface CYJSArray : NSMutableArray {
619 JSGlobalContextRef context_;
622 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context;
624 - (NSUInteger) count;
625 - (id) objectAtIndex:(NSUInteger)index;
627 - (void) addObject:(id)anObject;
628 - (void) insertObject:(id)anObject atIndex:(NSUInteger)index;
629 - (void) removeLastObject;
630 - (void) removeObjectAtIndex:(NSUInteger)index;
631 - (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
636 _finline bool CYJSValueIsNSObject(JSContextRef context, JSValueRef value) {
637 return JSValueIsObjectOfClass(context, value, Instance_);
640 _finline bool CYJSValueIsInstanceOfCachedConstructor(JSContextRef context, JSValueRef value, JSStringRef cache) {
641 JSValueRef exception(NULL);
642 JSObjectRef constructor(CYGetCachedObject(context, cache));
643 bool is(JSValueIsInstanceOfConstructor(context, value, constructor, &exception));
644 CYThrow(context, exception);
648 NSObject *CYMakeBlock(void (*invoke)(), sig::Signature &signature) {
649 BlockLiteral *literal(reinterpret_cast<BlockLiteral *>(malloc(sizeof(BlockLiteral))));
653 BlockDescriptor1 one_;
654 BlockDescriptor2 two_;
655 BlockDescriptor3 three_;
661 Descriptor *descriptor(new Descriptor);
662 memset(&descriptor->d_, 0, sizeof(descriptor->d_));
664 literal->isa = objc_getClass("__NSGlobalBlock__");
665 literal->flags = BLOCK_HAS_SIGNATURE | BLOCK_HAS_COPY_DISPOSE | BLOCK_IS_GLOBAL;
666 literal->reserved = 0;
667 literal->invoke = reinterpret_cast<void (*)(void *, ...)>(invoke);
668 literal->descriptor = descriptor;
670 descriptor->d_.one_.size = sizeof(descriptor->d_);
671 descriptor->d_.three_.signature = sig::Unparse(descriptor->pool_, &signature);
673 return reinterpret_cast<NSObject *>(literal);
676 NSObject *CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSObjectRef object) {
677 if (CYJSValueIsNSObject(context, object)) {
678 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
679 return internal->GetValue();
682 if (JSValueIsObjectOfClass(context, object, Functor_)) {
683 cy::Functor *internal(reinterpret_cast<cy::Functor *>(JSObjectGetPrivate(object)));
684 return CYMakeBlock(internal->GetValue(), internal->signature_);
687 bool array(CYJSValueIsInstanceOfCachedConstructor(context, object, Array_s));
688 id value(array ? [CYJSArray alloc] : [CYJSObject alloc]);
689 return CYPoolRelease(pool, [value initWithJSObject:object inContext:context]);
692 NSNumber *CYCopyNSNumber(JSContextRef context, JSValueRef value) {
693 return [[NSNumber alloc] initWithDouble:CYCastDouble(context, value)];
697 @interface NSBoolNumber : NSNumber {
702 id CYNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value, bool cast) {
706 switch (JSType type = JSValueGetType(context, value)) {
707 case kJSTypeUndefined:
708 object = [WebUndefined undefined];
718 object = (id) (CYCastBool(context, value) ? kCFBooleanTrue : kCFBooleanFalse);
721 object = [[NSBoolNumber alloc] initWithBool:CYCastBool(context, value)];
727 object = CYCopyNSNumber(context, value);
732 object = CYCopyNSString(context, value);
737 // XXX: this might could be more efficient
738 object = CYCastNSObject(pool, context, (JSObjectRef) value);
743 throw CYJSError(context, "JSValueGetType() == 0x%x", type);
750 return CYPoolRelease(pool, object);
752 return [object retain];
755 NSObject *CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
756 return CYNSObject(pool, context, value, true);
759 NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
760 return CYNSObject(pool, context, value, false);
763 /* Bridge: NSArray {{{ */
764 @implementation NSArray (Cycript)
767 return [[self mutableCopy] autorelease];
770 - (NSString *) cy$toCYON:(bool)objective {
771 NSMutableString *json([[[NSMutableString alloc] init] autorelease]);
772 [json appendString:@"@["];
776 for (id object in self) {
778 for (size_t index(0), count([self count]); index != count; ++index) {
779 id object([self objectAtIndex:index]);
782 [json appendString:@","];
785 if (object == nil || [object cy$JSType] != kJSTypeUndefined)
786 [json appendString:CYCastNSCYON(object, true)];
788 [json appendString:@","];
793 [json appendString:@"]"];
797 - (bool) cy$hasProperty:(NSString *)name {
798 if ([name isEqualToString:@"length"])
801 size_t index(CYGetIndex(name));
802 if (index == _not(size_t) || index >= [self count])
803 return [super cy$hasProperty:name];
808 - (NSObject *) cy$getProperty:(NSString *)name {
809 size_t index(CYGetIndex(name));
810 if (index == _not(size_t) || index >= [self count])
811 return [super cy$getProperty:name];
813 return [self objectAtIndex:index];
816 - (JSValueRef) cy$getProperty:(NSString *)name inContext:(JSContextRef)context {
817 CYObjectiveTry_(context) {
818 if ([name isEqualToString:@"length"])
819 return CYCastJSValue(context, [self count]);
822 return [super cy$getProperty:name inContext:context];
825 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context {
826 [super cy$getPropertyNames:names inContext:context];
828 for (size_t index(0), count([self count]); index != count; ++index) {
829 id object([self objectAtIndex:index]);
830 if (object == nil || [object cy$JSType] != kJSTypeUndefined) {
832 sprintf(name, "%zu", index);
833 JSPropertyNameAccumulatorAddName(names, CYJSString(name));
838 + (bool) cy$hasImplicitProperties {
844 /* Bridge: NSBlock {{{ */
851 /* Bridge: NSBoolNumber {{{ */
853 @implementation NSBoolNumber (Cycript)
855 - (JSType) cy$JSType {
856 return kJSTypeBoolean;
859 - (NSString *) cy$toCYON:(bool)objective {
860 NSString *value([self boolValue] ? @"true" : @"false");
861 return objective ? value : [NSString stringWithFormat:@"@%@", value];
864 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
865 return CYCastJSValue(context, (bool) [self boolValue]);
871 /* Bridge: NSDictionary {{{ */
872 @implementation NSDictionary (Cycript)
875 return [[self mutableCopy] autorelease];
878 - (NSString *) cy$toCYON:(bool)objective {
879 NSMutableString *json([[[NSMutableString alloc] init] autorelease]);
880 [json appendString:@"@{"];
884 for (NSObject *key in self) {
886 NSEnumerator *keys([self keyEnumerator]);
887 while (NSObject *key = [keys nextObject]) {
890 [json appendString:@","];
893 [json appendString:CYCastNSCYON(key, true)];
894 [json appendString:@":"];
895 NSObject *object([self objectForKey:key]);
896 [json appendString:CYCastNSCYON(object, true)];
899 [json appendString:@"}"];
903 - (bool) cy$hasProperty:(NSString *)name {
904 return [self objectForKey:name] != nil;
907 - (NSObject *) cy$getProperty:(NSString *)name {
908 return [self objectForKey:name];
911 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context {
912 [super cy$getPropertyNames:names inContext:context];
915 for (NSObject *key in self) {
917 NSEnumerator *keys([self keyEnumerator]);
918 while (NSObject *key = [keys nextObject]) {
920 JSPropertyNameAccumulatorAddName(names, CYJSString(context, key));
924 + (bool) cy$hasImplicitProperties {
930 /* Bridge: NSMutableArray {{{ */
931 @implementation NSMutableArray (Cycript)
933 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
934 if ([name isEqualToString:@"length"]) {
935 // XXX: is this not intelligent?
936 NSNumber *number(reinterpret_cast<NSNumber *>(value));
938 NSUInteger size([number unsignedIntegerValue]);
940 NSUInteger size([number unsignedIntValue]);
942 NSUInteger count([self count]);
944 [self removeObjectsInRange:NSMakeRange(size, count - size)];
945 else if (size != count) {
946 WebUndefined *undefined([WebUndefined undefined]);
947 for (size_t i(count); i != size; ++i)
948 [self addObject:undefined];
953 size_t index(CYGetIndex(name));
954 if (index == _not(size_t))
955 return [super cy$setProperty:name to:value];
957 id object(value ?: [NSNull null]);
959 size_t count([self count]);
961 [self replaceObjectAtIndex:index withObject:object];
963 if (index != count) {
964 WebUndefined *undefined([WebUndefined undefined]);
965 for (size_t i(count); i != index; ++i)
966 [self addObject:undefined];
969 [self addObject:object];
975 - (bool) cy$deleteProperty:(NSString *)name {
976 size_t index(CYGetIndex(name));
977 if (index == _not(size_t) || index >= [self count])
978 return [super cy$deleteProperty:name];
979 [self replaceObjectAtIndex:index withObject:[WebUndefined undefined]];
985 /* Bridge: NSMutableDictionary {{{ */
986 @implementation NSMutableDictionary (Cycript)
988 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
989 [self setObject:(value ?: [NSNull null]) forKey:name];
993 - (bool) cy$deleteProperty:(NSString *)name {
994 if ([self objectForKey:name] == nil)
997 [self removeObjectForKey:name];
1004 /* Bridge: NSNumber {{{ */
1005 @implementation NSNumber (Cycript)
1007 - (JSType) cy$JSType {
1009 // XXX: this just seems stupid
1010 if ([self class] == NSCFBoolean_)
1011 return kJSTypeBoolean;
1013 return kJSTypeNumber;
1016 - (NSString *) cy$toCYON:(bool)objective {
1017 NSString *value([self cy$JSType] != kJSTypeBoolean ? [self stringValue] : [self boolValue] ? @"true" : @"false");
1018 return objective ? value : [NSString stringWithFormat:@"@%@", value];
1021 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
1022 return [self cy$JSType] != kJSTypeBoolean ? CYCastJSValue(context, [self doubleValue]) : CYCastJSValue(context, static_cast<bool>([self boolValue]));
1023 } CYObjectiveCatch }
1027 /* Bridge: NSNull {{{ */
1028 @implementation NSNull (Cycript)
1030 - (JSType) cy$JSType {
1034 - (NSString *) cy$toCYON:(bool)objective {
1035 NSString *value(@"null");
1036 return objective ? value : [NSString stringWithFormat:@"@%@", value];
1039 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
1040 return CYJSNull(context);
1041 } CYObjectiveCatch }
1045 /* Bridge: NSObject {{{ */
1046 @implementation NSObject (Cycript)
1052 - (JSValueRef) cy$toJSON:(NSString *)key inContext:(JSContextRef)context {
1053 return [self cy$valueOfInContext:context];
1056 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
1058 } CYObjectiveCatch }
1060 - (JSType) cy$JSType {
1061 return kJSTypeObject;
1064 - (NSString *) cy$toCYON:(bool)objective {
1065 return [[self description] cy$toCYON:objective];
1068 - (bool) cy$hasProperty:(NSString *)name {
1072 - (NSObject *) cy$getProperty:(NSString *)name {
1076 - (JSValueRef) cy$getProperty:(NSString *)name inContext:(JSContextRef)context { CYObjectiveTry_(context) {
1077 if (NSObject *value = [self cy$getProperty:name])
1078 return CYCastJSValue(context, value);
1080 } CYObjectiveCatch }
1082 - (bool) cy$setProperty:(NSString *)name to:(NSObject *)value {
1086 - (bool) cy$deleteProperty:(NSString *)name {
1090 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context {
1093 + (bool) cy$hasImplicitProperties {
1099 /* Bridge: NSProxy {{{ */
1100 @implementation NSProxy (Cycript)
1102 - (NSString *) cy$toCYON:(bool)objective {
1103 return [[self description] cy$toCYON:objective];
1108 /* Bridge: NSString {{{ */
1109 @implementation NSString (Cycript)
1112 return [[self copy] autorelease];
1115 - (JSType) cy$JSType {
1116 return kJSTypeString;
1119 - (NSString *) cy$toCYON:(bool)objective {
1120 std::ostringstream str;
1123 CYUTF8String string(CYCastUTF8String(self));
1124 CYStringify(str, string.data, string.size);
1125 std::string value(str.str());
1126 return CYCastNSString(NULL, CYUTF8String(value.c_str(), value.size()));
1129 - (bool) cy$hasProperty:(NSString *)name {
1130 size_t index(CYGetIndex(name));
1131 if (index == _not(size_t) || index >= [self length])
1132 return [super cy$hasProperty:name];
1137 - (NSObject *) cy$getProperty:(NSString *)name {
1138 size_t index(CYGetIndex(name));
1139 if (index == _not(size_t) || index >= [self length])
1140 return [super cy$getProperty:name];
1142 return [self substringWithRange:NSMakeRange(index, 1)];
1145 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context {
1146 [super cy$getPropertyNames:names inContext:context];
1148 for (size_t index(0), length([self length]); index != length; ++index) {
1150 sprintf(name, "%zu", index);
1151 JSPropertyNameAccumulatorAddName(names, CYJSString(name));
1155 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
1156 return CYCastJSValue(context, CYJSString(context, self));
1157 } CYObjectiveCatch }
1161 /* Bridge: WebUndefined {{{ */
1162 @implementation WebUndefined (Cycript)
1164 - (JSType) cy$JSType {
1165 return kJSTypeUndefined;
1168 - (NSString *) cy$toCYON:(bool)objective {
1169 NSString *value(@"undefined");
1170 return value; // XXX: maybe use the below code, adding @undefined?
1171 //return objective ? value : [NSString stringWithFormat:@"@%@", value];
1174 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) {
1175 return CYJSUndefined(context);
1176 } CYObjectiveCatch }
1181 static bool CYIsClass(id self) {
1183 return class_isMetaClass(object_getClass(self));
1185 return GSObjCIsClass(self);
1189 Class CYCastClass(apr_pool_t *pool, JSContextRef context, JSValueRef value) {
1190 id self(CYCastNSObject(pool, context, value));
1191 if (CYIsClass(self))
1192 return (Class) self;
1193 throw CYJSError(context, "got something that is not a Class");
1197 NSArray *CYCastNSArray(JSContextRef context, JSPropertyNameArrayRef names) {
1199 size_t size(JSPropertyNameArrayGetCount(names));
1200 NSMutableArray *array([NSMutableArray arrayWithCapacity:size]);
1201 for (size_t index(0); index != size; ++index)
1202 [array addObject:CYCastNSString(pool, context, JSPropertyNameArrayGetNameAtIndex(names, index))];
1206 JSValueRef CYCastJSValue(JSContextRef context, NSObject *value) { CYPoolTry {
1208 return CYJSNull(context);
1210 return CYMakeInstance(context, value, false);
1211 } CYPoolCatch(NULL) return /*XXX*/ NULL; }
1213 @implementation CYJSObject
1215 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { CYObjectiveTry {
1216 if ((self = [super init]) != nil) {
1218 context_ = CYGetJSContext(context);
1219 JSGlobalContextRetain(context_);
1220 JSValueProtect(context_, object_);
1222 } CYObjectiveCatch }
1224 - (void) dealloc { CYObjectiveTry {
1225 JSValueUnprotect(context_, object_);
1226 JSGlobalContextRelease(context_);
1228 } CYObjectiveCatch }
1230 - (NSString *) cy$toCYON:(bool)objective { CYObjectiveTry {
1232 JSValueRef exception(NULL);
1233 const char *cyon(CYPoolCCYON(pool, context_, object_));
1234 CYThrow(context_, exception);
1236 return [super cy$toCYON:objective];
1238 return [NSString stringWithUTF8String:cyon];
1239 } CYObjectiveCatch }
1241 - (NSUInteger) count { CYObjectiveTry {
1242 JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_));
1243 size_t size(JSPropertyNameArrayGetCount(names));
1244 JSPropertyNameArrayRelease(names);
1246 } CYObjectiveCatch }
1248 - (id) objectForKey:(id)key { CYObjectiveTry {
1249 JSValueRef value(CYGetProperty(context_, object_, CYJSString(context_, (NSObject *) key)));
1250 if (JSValueIsUndefined(context_, value))
1252 return CYCastNSObject(NULL, context_, value) ?: [NSNull null];
1253 } CYObjectiveCatch }
1255 - (NSEnumerator *) keyEnumerator { CYObjectiveTry {
1256 JSPropertyNameArrayRef names(JSObjectCopyPropertyNames(context_, object_));
1257 NSEnumerator *enumerator([CYCastNSArray(context_, names) objectEnumerator]);
1258 JSPropertyNameArrayRelease(names);
1260 } CYObjectiveCatch }
1262 - (void) setObject:(id)object forKey:(id)key { CYObjectiveTry {
1263 CYSetProperty(context_, object_, CYJSString(context_, (NSObject *) key), CYCastJSValue(context_, (NSString *) object));
1264 } CYObjectiveCatch }
1266 - (void) removeObjectForKey:(id)key { CYObjectiveTry {
1267 JSValueRef exception(NULL);
1268 (void) JSObjectDeleteProperty(context_, object_, CYJSString(context_, (NSObject *) key), &exception);
1269 CYThrow(context_, exception);
1270 } CYObjectiveCatch }
1274 @implementation CYJSArray
1276 - (NSString *) cy$toCYON:(bool)objective {
1278 return [NSString stringWithUTF8String:CYPoolCCYON(pool, context_, object_)];
1281 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { CYObjectiveTry {
1282 if ((self = [super init]) != nil) {
1284 context_ = CYGetJSContext(context);
1285 JSGlobalContextRetain(context_);
1286 JSValueProtect(context_, object_);
1288 } CYObjectiveCatch }
1290 - (void) dealloc { CYObjectiveTry {
1291 JSValueUnprotect(context_, object_);
1292 JSGlobalContextRelease(context_);
1294 } CYObjectiveCatch }
1296 - (NSUInteger) count { CYObjectiveTry {
1297 return CYArrayLength(context_, object_);
1298 } CYObjectiveCatch }
1300 - (id) objectAtIndex:(NSUInteger)index { CYObjectiveTry {
1301 size_t bounds([self count]);
1302 if (index >= bounds)
1303 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray objectAtIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
1304 JSValueRef exception(NULL);
1305 JSValueRef value(JSObjectGetPropertyAtIndex(context_, object_, index, &exception));
1306 CYThrow(context_, exception);
1307 return CYCastNSObject(NULL, context_, value) ?: [NSNull null];
1308 } CYObjectiveCatch }
1310 - (void) addObject:(id)object { CYObjectiveTry {
1311 CYArrayPush(context_, object_, CYCastJSValue(context_, (NSObject *) object));
1312 } CYObjectiveCatch }
1314 - (void) insertObject:(id)object atIndex:(NSUInteger)index { CYObjectiveTry {
1315 size_t bounds([self count] + 1);
1316 if (index >= bounds)
1317 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray insertObject:atIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
1318 JSValueRef exception(NULL);
1319 JSValueRef arguments[3];
1320 arguments[0] = CYCastJSValue(context_, index);
1321 arguments[1] = CYCastJSValue(context_, 0);
1322 arguments[2] = CYCastJSValue(context_, (NSObject *) object);
1323 JSObjectRef Array(CYGetCachedObject(context_, CYJSString("Array_prototype")));
1324 JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array, splice_s)), object_, 3, arguments, &exception);
1325 CYThrow(context_, exception);
1326 } CYObjectiveCatch }
1328 - (void) removeLastObject { CYObjectiveTry {
1329 JSValueRef exception(NULL);
1330 JSObjectRef Array(CYGetCachedObject(context_, CYJSString("Array_prototype")));
1331 JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array, pop_s)), object_, 0, NULL, &exception);
1332 CYThrow(context_, exception);
1333 } CYObjectiveCatch }
1335 - (void) removeObjectAtIndex:(NSUInteger)index { CYObjectiveTry {
1336 size_t bounds([self count]);
1337 if (index >= bounds)
1338 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray removeObjectAtIndex:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
1339 JSValueRef exception(NULL);
1340 JSValueRef arguments[2];
1341 arguments[0] = CYCastJSValue(context_, index);
1342 arguments[1] = CYCastJSValue(context_, 1);
1343 JSObjectRef Array(CYGetCachedObject(context_, CYJSString("Array_prototype")));
1344 JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array, splice_s)), object_, 2, arguments, &exception);
1345 CYThrow(context_, exception);
1346 } CYObjectiveCatch }
1348 - (void) replaceObjectAtIndex:(NSUInteger)index withObject:(id)object { CYObjectiveTry {
1349 size_t bounds([self count]);
1350 if (index >= bounds)
1351 @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"*** -[CYJSArray replaceObjectAtIndex:withObject:]: index (%zu) beyond bounds (%zu)", index, bounds] userInfo:nil];
1352 CYSetProperty(context_, object_, index, CYCastJSValue(context_, (NSObject *) object));
1353 } CYObjectiveCatch }
1357 // XXX: inherit from or replace with CYJSObject
1358 @interface CYInternal : NSObject {
1359 JSGlobalContextRef context_;
1360 JSObjectRef object_;
1365 @implementation CYInternal
1368 JSValueUnprotect(context_, object_);
1369 JSGlobalContextRelease(context_);
1373 - (id) initInContext:(JSContextRef)context {
1374 if ((self = [super init]) != nil) {
1375 context_ = CYGetJSContext(context);
1376 JSGlobalContextRetain(context_);
1380 - (bool) hasProperty:(JSStringRef)name inContext:(JSContextRef)context {
1381 if (object_ == NULL)
1384 return JSObjectHasProperty(context, object_, name);
1387 - (JSValueRef) getProperty:(JSStringRef)name inContext:(JSContextRef)context {
1388 if (object_ == NULL)
1391 return CYGetProperty(context, object_, name);
1394 - (void) setProperty:(JSStringRef)name toValue:(JSValueRef)value inContext:(JSContextRef)context {
1395 @synchronized (self) {
1396 if (object_ == NULL) {
1397 object_ = JSObjectMake(context, NULL, NULL);
1398 JSValueProtect(context, object_);
1402 CYSetProperty(context, object_, name, value);
1405 + (CYInternal *) get:(id)object {
1406 if ($objc_getAssociatedObject == NULL)
1409 @synchronized (object) {
1410 if (CYInternal *internal = $objc_getAssociatedObject(object, @selector(cy$internal)))
1417 + (CYInternal *) set:(id)object inContext:(JSContextRef)context {
1418 if ($objc_getAssociatedObject == NULL)
1421 @synchronized (object) {
1422 if (CYInternal *internal = $objc_getAssociatedObject(object, @selector(cy$internal)))
1425 if ($objc_setAssociatedObject == NULL)
1428 CYInternal *internal([[[CYInternal alloc] initInContext:context] autorelease]);
1429 objc_setAssociatedObject(object, @selector(cy$internal), internal, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
1438 static JSObjectRef CYMakeSelector(JSContextRef context, SEL sel) {
1439 Selector_privateData *internal(new Selector_privateData(sel));
1440 return JSObjectMake(context, Selector_, internal);
1443 static SEL CYCastSEL(JSContextRef context, JSValueRef value) {
1444 if (JSValueIsObjectOfClass(context, value, Selector_)) {
1445 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate((JSObjectRef) value)));
1446 return reinterpret_cast<SEL>(internal->value_);
1448 return CYCastPointer<SEL>(context, value);
1451 void *CYObjectiveC_ExecuteStart(JSContextRef context) { CYSadTry {
1452 return (void *) [[NSAutoreleasePool alloc] init];
1453 } CYSadCatch(NULL) }
1455 void CYObjectiveC_ExecuteEnd(JSContextRef context, void *handle) { CYSadTry {
1456 return [(NSAutoreleasePool *) handle release];
1459 JSValueRef CYObjectiveC_RuntimeProperty(JSContextRef context, CYUTF8String name) { CYPoolTry {
1461 return Instance::Make(context, nil);
1462 if (Class _class = objc_getClass(name.data))
1463 return CYMakeInstance(context, _class, true);
1464 if (Protocol *protocol = objc_getProtocol(name.data))
1465 return CYMakeInstance(context, protocol, true);
1467 } CYPoolCatch(NULL) return /*XXX*/ NULL; }
1469 static void CYObjectiveC_CallFunction(JSContextRef context, ffi_cif *cif, void (*function)(), uint8_t *value, void **values) { CYSadTry {
1470 ffi_call(cif, function, value, values);
1473 static bool CYObjectiveC_PoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { CYSadTry {
1474 switch (type->primitive) {
1475 // XXX: do something epic about blocks
1478 case sig::typename_P:
1479 *reinterpret_cast<id *>(data) = CYCastNSObject(pool, context, value);
1482 case sig::selector_P:
1483 *reinterpret_cast<SEL *>(data) = CYCastSEL(context, value);
1491 } CYSadCatch(false) }
1493 static JSValueRef CYObjectiveC_FromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) { CYPoolTry {
1494 switch (type->primitive) {
1495 // XXX: do something epic about blocks
1498 if (NSObject *object = *reinterpret_cast<NSObject **>(data)) {
1499 JSValueRef value(CYCastJSValue(context, object));
1505 case sig::typename_P:
1506 return CYMakeInstance(context, *reinterpret_cast<Class *>(data), true);
1508 case sig::selector_P:
1509 if (SEL sel = *reinterpret_cast<SEL *>(data))
1510 return CYMakeSelector(context, sel);
1514 return CYJSNull(context);
1518 } CYPoolCatch(NULL) return /*XXX*/ NULL; }
1520 static bool CYImplements(id object, Class _class, SEL selector, bool devoid = false) {
1521 if (objc_method *method = class_getInstanceMethod(_class, selector)) {
1524 #if OBJC_API_VERSION >= 2
1526 method_getReturnType(method, type, sizeof(type));
1528 const char *type(method_getTypeEncoding(method));
1534 // XXX: possibly use a more "awesome" check?
1538 static const char *CYPoolTypeEncoding(apr_pool_t *pool, JSContextRef context, SEL sel, objc_method *method) {
1540 return method_getTypeEncoding(method);
1542 const char *name(sel_getName(sel));
1543 size_t length(strlen(name));
1545 char keyed[length + 2];
1547 keyed[length + 1] = '\0';
1548 memcpy(keyed + 1, name, length);
1550 if (CYBridgeEntry *entry = CYBridgeHash(keyed, length + 1))
1551 return entry->value_;
1556 static JSValueRef MessageAdapter_(JSContextRef context, size_t count, JSValueRef values[], JSObjectRef function) {
1557 JSObjectRef _this(CYCastJSObject(context, values[0]));
1558 return CYCallAsFunction(context, function, _this, count - 2, values + 2);
1561 static void MessageClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) {
1562 CYExecuteClosure(cif, result, arguments, arg, &MessageAdapter_);
1565 static JSObjectRef CYMakeMessage(JSContextRef context, SEL sel, IMP imp, const char *type) {
1566 Message_privateData *internal(new Message_privateData(sel, type, imp));
1567 return JSObjectMake(context, Message_, internal);
1570 static IMP CYMakeMessage(JSContextRef context, JSValueRef value, const char *type) {
1571 JSObjectRef function(CYCastJSObject(context, value));
1572 Closure_privateData *internal(CYMakeFunctor_(context, function, type, &MessageClosure_));
1573 // XXX: see notes in Library.cpp about needing to leak
1574 return reinterpret_cast<IMP>(internal->GetValue());
1577 static bool Messages_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
1578 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1579 Class _class(internal->GetValue());
1582 const char *name(CYPoolCString(pool, context, property));
1584 if (SEL sel = sel_getUid(name))
1585 if (class_getInstanceMethod(_class, sel) != NULL)
1591 static JSValueRef Messages_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
1592 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1593 Class _class(internal->GetValue());
1596 const char *name(CYPoolCString(pool, context, property));
1598 if (SEL sel = sel_getUid(name))
1599 if (objc_method *method = class_getInstanceMethod(_class, sel))
1600 return CYMakeMessage(context, sel, method_getImplementation(method), method_getTypeEncoding(method));
1605 static bool Messages_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
1606 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1607 Class _class(internal->GetValue());
1610 const char *name(CYPoolCString(pool, context, property));
1612 SEL sel(sel_registerName(name));
1614 objc_method *method(class_getInstanceMethod(_class, sel));
1619 if (JSValueIsObjectOfClass(context, value, Message_)) {
1620 Message_privateData *message(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate((JSObjectRef) value)));
1621 type = sig::Unparse(pool, &message->signature_);
1622 imp = reinterpret_cast<IMP>(message->GetValue());
1624 type = CYPoolTypeEncoding(pool, context, sel, method);
1625 imp = CYMakeMessage(context, value, type);
1629 method_setImplementation(method, imp);
1632 GSMethodList list(GSAllocMethodList(1));
1633 GSAppendMethodToList(list, sel, type, imp, YES);
1634 GSAddMethodList(_class, list, YES);
1635 GSFlushMethodCacheForClass(_class);
1637 class_addMethod(_class, sel, imp, type);
1644 #if 0 && OBJC_API_VERSION < 2
1645 static bool Messages_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
1646 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1647 Class _class(internal->GetValue());
1650 const char *name(CYPoolCString(pool, context, property));
1652 if (SEL sel = sel_getUid(name))
1653 if (objc_method *method = class_getInstanceMethod(_class, sel)) {
1654 objc_method_list list = {NULL, 1, {method}};
1655 class_removeMethods(_class, &list);
1663 static void Messages_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1664 Messages *internal(reinterpret_cast<Messages *>(JSObjectGetPrivate(object)));
1665 Class _class(internal->GetValue());
1667 #if OBJC_API_VERSION >= 2
1669 objc_method **data(class_copyMethodList(_class, &size));
1670 for (size_t i(0); i != size; ++i)
1671 JSPropertyNameAccumulatorAddName(names, CYJSString(sel_getName(method_getName(data[i]))));
1674 for (objc_method_list *methods(_class->methods); methods != NULL; methods = methods->method_next)
1675 for (int i(0); i != methods->method_count; ++i)
1676 JSPropertyNameAccumulatorAddName(names, CYJSString(sel_getName(method_getName(&methods->method_list[i]))));
1680 static bool CYHasImplicitProperties(Class _class) {
1681 // XXX: this is an evil hack to deal with NSProxy; fix elsewhere
1682 if (!CYImplements(_class, object_getClass(_class), @selector(cy$hasImplicitProperties)))
1684 return [_class cy$hasImplicitProperties];
1687 static bool Instance_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
1688 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1689 id self(internal->GetValue());
1691 if (JSStringIsEqualToUTF8CString(property, "$cyi"))
1695 NSString *name(CYCastNSString(pool, context, property));
1697 if (CYInternal *internal = [CYInternal get:self])
1698 if ([internal hasProperty:property inContext:context])
1701 Class _class(object_getClass(self));
1704 // XXX: this is an evil hack to deal with NSProxy; fix elsewhere
1705 if (CYImplements(self, _class, @selector(cy$hasProperty:)))
1706 if ([self cy$hasProperty:name])
1708 } CYPoolCatch(false)
1710 const char *string(CYPoolCString(pool, context, name));
1713 if (class_getProperty(_class, string) != NULL)
1717 if (CYHasImplicitProperties(_class))
1718 if (SEL sel = sel_getUid(string))
1719 if (CYImplements(self, _class, sel, true))
1725 static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1726 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1727 id self(internal->GetValue());
1729 if (JSStringIsEqualToUTF8CString(property, "$cyi"))
1730 return Internal::Make(context, self, object);
1733 NSString *name(CYCastNSString(pool, context, property));
1735 if (CYInternal *internal = [CYInternal get:self])
1736 if (JSValueRef value = [internal getProperty:property inContext:context])
1740 if (JSValueRef value = [self cy$getProperty:name inContext:context])
1744 const char *string(CYPoolCString(pool, context, name));
1745 Class _class(object_getClass(self));
1748 if (objc_property_t property = class_getProperty(_class, string)) {
1749 PropertyAttributes attributes(property);
1750 SEL sel(sel_registerName(attributes.Getter()));
1751 return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception);
1755 if (CYHasImplicitProperties(_class))
1756 if (SEL sel = sel_getUid(string))
1757 if (CYImplements(self, _class, sel, true))
1758 return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception);
1763 static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
1764 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1765 id self(internal->GetValue());
1769 NSString *name(CYCastNSString(pool, context, property));
1770 NSObject *data(CYCastNSObject(pool, context, value));
1773 if ([self cy$setProperty:name to:data])
1775 } CYPoolCatch(false)
1777 const char *string(CYPoolCString(pool, context, name));
1778 Class _class(object_getClass(self));
1781 if (objc_property_t property = class_getProperty(_class, string)) {
1782 PropertyAttributes attributes(property);
1783 if (const char *setter = attributes.Setter()) {
1784 SEL sel(sel_registerName(setter));
1785 JSValueRef arguments[1] = {value};
1786 CYSendMessage(pool, context, self, NULL, sel, 1, arguments, false, exception);
1792 size_t length(strlen(string));
1794 char set[length + 5];
1800 if (string[0] != '\0') {
1801 set[3] = toupper(string[0]);
1802 memcpy(set + 4, string + 1, length - 1);
1805 set[length + 3] = ':';
1806 set[length + 4] = '\0';
1808 if (SEL sel = sel_getUid(set))
1809 if (CYImplements(self, _class, sel)) {
1810 JSValueRef arguments[1] = {value};
1811 CYSendMessage(pool, context, self, NULL, sel, 1, arguments, false, exception);
1815 if (CYInternal *internal = [CYInternal set:self inContext:context]) {
1816 [internal setProperty:property toValue:value inContext:context];
1823 static bool Instance_deleteProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1824 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1825 id self(internal->GetValue());
1828 NSString *name(CYCastNSString(NULL, context, property));
1829 return [self cy$deleteProperty:name];
1830 } CYPoolCatch(false)
1831 } CYCatch(false) return /*XXX*/ false; }
1833 static void Instance_getPropertyNames_message(JSPropertyNameAccumulatorRef names, objc_method *method) {
1834 const char *name(sel_getName(method_getName(method)));
1835 if (strchr(name, ':') != NULL)
1838 const char *type(method_getTypeEncoding(method));
1839 if (type == NULL || *type == '\0' || *type == 'v')
1842 JSPropertyNameAccumulatorAddName(names, CYJSString(name));
1845 static void Instance_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
1846 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1847 id self(internal->GetValue());
1850 Class _class(object_getClass(self));
1855 objc_property_t *data(class_copyPropertyList(_class, &size));
1856 for (size_t i(0); i != size; ++i)
1857 JSPropertyNameAccumulatorAddName(names, CYJSString(property_getName(data[i])));
1862 if (CYHasImplicitProperties(_class))
1863 for (Class current(_class); current != nil; current = class_getSuperclass(current)) {
1864 #if OBJC_API_VERSION >= 2
1866 objc_method **data(class_copyMethodList(current, &size));
1867 for (size_t i(0); i != size; ++i)
1868 Instance_getPropertyNames_message(names, data[i]);
1871 for (objc_method_list *methods(current->methods); methods != NULL; methods = methods->method_next)
1872 for (int i(0); i != methods->method_count; ++i)
1873 Instance_getPropertyNames_message(names, &methods->method_list[i]);
1878 // XXX: this is an evil hack to deal with NSProxy; fix elsewhere
1879 if (CYImplements(self, _class, @selector(cy$getPropertyNames:inContext:)))
1880 [self cy$getPropertyNames:names inContext:context];
1884 static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1885 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1886 JSObjectRef value(Instance::Make(context, [internal->GetValue() alloc], Instance::Uninitialized));
1890 static JSValueRef Instance_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1891 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
1892 id self(internal->GetValue());
1894 if (![self isKindOfClass:NSBlock_])
1895 CYThrow("non-NSBlock object is not a function");
1896 // XXX: replace above logic with the following assertion
1897 //_assert([self isKindOfClass:NSBlock_]);
1898 // to do this, make it so FunctionInstance_ is the class of blocks
1899 // to do /that/, generalize the various "is exactly Instance_" checks
1900 // then, move Instance_callAsFunction to only be on FunctionInstance
1902 BlockLiteral *literal(reinterpret_cast<BlockLiteral *>(self));
1904 if ((literal->flags & BLOCK_HAS_SIGNATURE) != 0) {
1905 uint8_t *descriptor(reinterpret_cast<uint8_t *>(literal->descriptor));
1906 descriptor += sizeof(BlockDescriptor1);
1907 if ((literal->flags & BLOCK_HAS_COPY_DISPOSE) != 0)
1908 descriptor += sizeof(BlockDescriptor2);
1909 BlockDescriptor3 *descriptor3(reinterpret_cast<BlockDescriptor3 *>(descriptor));
1911 if (const char *type = descriptor3->signature) {
1917 sig::Signature signature;
1918 sig::Parse(pool, &signature, type, &Structor_);
1921 sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif);
1923 void (*function)() = reinterpret_cast<void (*)()>(literal->invoke);
1924 return CYCallFunction(pool, context, 1, setup, count, arguments, false, exception, &signature, &cif, function);
1929 CYThrow("NSBlock without signature field passed arguments");
1933 } CYPoolCatch(NULL);
1938 static bool Instance_hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef instance, JSValueRef *exception) { CYTry {
1939 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) constructor)));
1940 Class _class(internal->GetValue());
1941 if (!CYIsClass(_class))
1944 if (CYJSValueIsNSObject(context, instance)) {
1945 Instance *linternal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) instance)));
1946 // XXX: this isn't always safe
1947 return [linternal->GetValue() isKindOfClass:_class];
1953 static JSValueRef Instance_box_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
1955 throw CYJSError(context, "incorrect number of arguments to Instance");
1957 id value(CYCastNSObject(pool, context, arguments[0]));
1959 value = [NSNull null];
1960 return CYCastJSValue(context, [value cy$box]);
1963 static bool Internal_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
1964 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1967 id self(internal->GetValue());
1968 const char *name(CYPoolCString(pool, context, property));
1970 if (object_getInstanceVariable(self, name, NULL) != NULL)
1976 static JSValueRef Internal_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
1977 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1980 id self(internal->GetValue());
1981 const char *name(CYPoolCString(pool, context, property));
1983 if (objc_ivar *ivar = object_getInstanceVariable(self, name, NULL)) {
1984 Type_privateData type(pool, ivar_getTypeEncoding(ivar));
1985 // XXX: if this fails and throws an exception the person we are throwing it to gets the wrong exception
1986 return CYFromFFI(context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar));
1992 static bool Internal_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
1993 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
1996 id self(internal->GetValue());
1997 const char *name(CYPoolCString(pool, context, property));
1999 if (objc_ivar *ivar = object_getInstanceVariable(self, name, NULL)) {
2000 Type_privateData type(pool, ivar_getTypeEncoding(ivar));
2001 CYPoolFFI(pool, context, type.type_, type.GetFFI(), reinterpret_cast<uint8_t *>(self) + ivar_getOffset(ivar), value);
2008 static void Internal_getPropertyNames_(Class _class, JSPropertyNameAccumulatorRef names) {
2009 if (Class super = class_getSuperclass(_class))
2010 Internal_getPropertyNames_(super, names);
2012 #if OBJC_API_VERSION >= 2
2014 objc_ivar **data(class_copyIvarList(_class, &size));
2015 for (size_t i(0); i != size; ++i)
2016 JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(data[i])));
2019 if (objc_ivar_list *ivars = _class->ivars)
2020 for (int i(0); i != ivars->ivar_count; ++i)
2021 JSPropertyNameAccumulatorAddName(names, CYJSString(ivar_getName(&ivars->ivar_list[i])));
2025 static void Internal_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
2026 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
2029 id self(internal->GetValue());
2030 Class _class(object_getClass(self));
2032 Internal_getPropertyNames_(_class, names);
2035 static JSValueRef Internal_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2036 Internal *internal(reinterpret_cast<Internal *>(JSObjectGetPrivate(object)));
2037 return internal->GetOwner();
2040 static JSValueRef ObjectiveC_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2042 NSString *name(CYCastNSString(pool, context, property));
2043 if (Class _class = NSClassFromString(name))
2044 return CYMakeInstance(context, _class, true);
2048 static void ObjectiveC_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
2050 size_t size(objc_getClassList(NULL, 0));
2051 Class *data(reinterpret_cast<Class *>(malloc(sizeof(Class) * size)));
2054 size_t writ(objc_getClassList(data, size));
2057 if (Class *copy = reinterpret_cast<Class *>(realloc(data, sizeof(Class) * writ))) {
2063 for (size_t i(0); i != writ; ++i)
2064 JSPropertyNameAccumulatorAddName(names, CYJSString(class_getName(data[i])));
2070 while (Class _class = objc_next_class(&state))
2071 JSPropertyNameAccumulatorAddName(names, CYJSString(class_getName(_class)));
2075 #if OBJC_API_VERSION >= 2
2076 static JSValueRef ObjectiveC_Image_Classes_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2077 const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object)));
2080 const char *name(CYPoolCString(pool, context, property));
2082 const char **data(objc_copyClassNamesForImage(internal, &size));
2084 for (size_t i(0); i != size; ++i)
2085 if (strcmp(name, data[i]) == 0) {
2086 if (Class _class = objc_getClass(name)) {
2087 value = CYMakeInstance(context, _class, true);
2098 static void ObjectiveC_Image_Classes_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
2099 const char *internal(reinterpret_cast<const char *>(JSObjectGetPrivate(object)));
2101 const char **data(objc_copyClassNamesForImage(internal, &size));
2102 for (size_t i(0); i != size; ++i)
2103 JSPropertyNameAccumulatorAddName(names, CYJSString(data[i]));
2107 static JSValueRef ObjectiveC_Images_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2109 const char *name(CYPoolCString(pool, context, property));
2111 const char **data(objc_copyImageNames(&size));
2112 for (size_t i(0); i != size; ++i)
2113 if (strcmp(name, data[i]) == 0) {
2122 JSObjectRef value(JSObjectMake(context, NULL, NULL));
2123 CYSetProperty(context, value, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Image_Classes_, const_cast<char *>(name)));
2127 static void ObjectiveC_Images_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
2129 const char **data(objc_copyImageNames(&size));
2130 for (size_t i(0); i != size; ++i)
2131 JSPropertyNameAccumulatorAddName(names, CYJSString(data[i]));
2136 static JSValueRef ObjectiveC_Protocols_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2138 const char *name(CYPoolCString(pool, context, property));
2139 if (Protocol *protocol = objc_getProtocol(name))
2140 return CYMakeInstance(context, protocol, true);
2144 static void ObjectiveC_Protocols_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
2145 #if OBJC_API_VERSION >= 2
2147 Protocol **data(objc_copyProtocolList(&size));
2148 for (size_t i(0); i != size; ++i)
2149 JSPropertyNameAccumulatorAddName(names, CYJSString(protocol_getName(data[i])));
2156 static JSValueRef ObjectiveC_Constants_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2158 CYUTF8String name(CYPoolUTF8String(pool, context, property));
2160 return Instance::Make(context, nil);
2164 static void ObjectiveC_Constants_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
2165 JSPropertyNameAccumulatorAddName(names, CYJSString("nil"));
2169 static bool stret(ffi_type *ffi_type) {
2170 return ffi_type->type == FFI_TYPE_STRUCT && (
2171 ffi_type->size > OBJC_MAX_STRUCT_BY_VALUE ||
2172 struct_forward_array[ffi_type->size] != 0
2177 JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, Class _class, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception) { CYTry {
2181 _class = object_getClass(self);
2185 if (objc_method *method = class_getInstanceMethod(_class, _cmd)) {
2186 imp = method_getImplementation(method);
2187 type = method_getTypeEncoding(method);
2192 NSMethodSignature *method([self methodSignatureForSelector:_cmd]);
2194 throw CYJSError(context, "unrecognized selector %s sent to object %p", sel_getName(_cmd), self);
2195 type = CYPoolCString(pool, context, [method _typeString]);
2203 sig::Signature signature;
2204 sig::Parse(pool, &signature, type, &Structor_);
2206 size_t used(count + 3);
2207 if (used > signature.count) {
2208 sig::Element *elements(new (pool) sig::Element[used]);
2209 memcpy(elements, signature.elements, used * sizeof(sig::Element));
2211 for (size_t index(signature.count); index != used; ++index) {
2212 sig::Element *element(&elements[index]);
2213 element->name = NULL;
2214 element->offset = _not(size_t);
2216 sig::Type *type(new (pool) sig::Type);
2217 memset(type, 0, sizeof(*type));
2218 type->primitive = sig::object_P;
2219 element->type = type;
2222 signature.elements = elements;
2223 signature.count = used;
2227 sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif);
2231 if (stret(cif.rtype))
2232 imp = class_getMethodImplementation_stret(_class, _cmd);
2234 imp = class_getMethodImplementation(_class, _cmd);
2236 objc_super super = {self, _class};
2237 imp = objc_msg_lookup_super(&super, _cmd);
2241 void (*function)() = reinterpret_cast<void (*)()>(imp);
2242 return CYCallFunction(pool, context, 2, setup, count, arguments, initialize, exception, &signature, &cif, function);
2245 static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2247 throw CYJSError(context, "too few arguments to objc_msgSend");
2257 if (JSValueIsObjectOfClass(context, arguments[0], Super_)) {
2258 cy::Super *internal(reinterpret_cast<cy::Super *>(JSObjectGetPrivate((JSObjectRef) arguments[0])));
2259 self = internal->GetValue();
2260 _class = internal->class_;;
2261 uninitialized = false;
2262 } else if (CYJSValueIsNSObject(context, arguments[0])) {
2263 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate((JSObjectRef) arguments[0])));
2264 self = internal->GetValue();
2266 uninitialized = internal->IsUninitialized();
2268 internal->value_ = nil;
2270 self = CYCastNSObject(pool, context, arguments[0]);
2272 uninitialized = false;
2276 return CYJSNull(context);
2278 _cmd = CYCastSEL(context, arguments[1]);
2280 return CYSendMessage(pool, context, self, _class, _cmd, count - 2, arguments + 2, uninitialized, exception);
2283 static JSValueRef Selector_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2284 JSValueRef setup[count + 2];
2287 memcpy(setup + 2, arguments, sizeof(JSValueRef) * count);
2288 return $objc_msgSend(context, NULL, NULL, count + 2, setup, exception);
2291 static JSValueRef Message_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2293 Message_privateData *internal(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate(object)));
2295 // XXX: handle Instance::Uninitialized?
2296 id self(CYCastNSObject(pool, context, _this));
2300 setup[1] = &internal->sel_;
2302 return CYCallFunction(pool, context, 2, setup, count, arguments, false, exception, &internal->signature_, &internal->cif_, internal->GetValue());
2305 static JSObjectRef Super_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2307 throw CYJSError(context, "incorrect number of arguments to Super constructor");
2309 id self(CYCastNSObject(pool, context, arguments[0]));
2310 Class _class(CYCastClass(pool, context, arguments[1]));
2311 return cy::Super::Make(context, self, _class);
2314 static JSObjectRef Selector_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2316 throw CYJSError(context, "incorrect number of arguments to Selector constructor");
2318 const char *name(CYPoolCString(pool, context, arguments[0]));
2319 return CYMakeSelector(context, sel_registerName(name));
2322 static JSObjectRef Instance_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2324 throw CYJSError(context, "incorrect number of arguments to Instance constructor");
2325 id self(count == 0 ? nil : CYCastPointer<id>(context, arguments[0]));
2326 return CYMakeInstance(context, self, false);
2329 static JSValueRef CYValue_getProperty_value(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
2330 CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(object)));
2331 return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_));
2334 static JSValueRef CYValue_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2335 CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this)));
2336 Type_privateData *typical(internal->GetType());
2341 if (typical == NULL) {
2345 type = typical->type_;
2346 ffi = typical->ffi_;
2349 return CYMakePointer(context, &internal->value_, _not(size_t), type, ffi, object);
2352 static JSValueRef Instance_getProperty_constructor(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
2353 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
2354 return Instance::Make(context, (id) object_getClass(internal->GetValue()));
2357 static JSValueRef Instance_getProperty_prototype(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
2358 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
2359 id self(internal->GetValue());
2360 if (!CYIsClass(self))
2361 return CYJSUndefined(context);
2362 return CYGetClassPrototype(context, self);
2365 static JSValueRef Instance_getProperty_messages(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
2366 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
2367 id self(internal->GetValue());
2368 if (!CYIsClass(self))
2369 return CYJSUndefined(context);
2370 return Messages::Make(context, (Class) self);
2373 static JSValueRef Instance_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2374 if (!CYJSValueIsNSObject(context, _this))
2377 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2378 return CYCastJSValue(context, CYJSString(context, CYCastNSCYON(internal->GetValue(), false)));
2381 static JSValueRef Instance_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2382 if (!CYJSValueIsNSObject(context, _this))
2385 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2386 id value(internal->GetValue());
2393 key = CYCastNSString(NULL, context, CYJSString(context, arguments[0]));
2395 if (!CYImplements(value, object_getClass(value), @selector(cy$toJSON:inContext:)))
2396 return CYJSUndefined(context);
2397 else if (JSValueRef json = [value cy$toJSON:key inContext:context])
2400 return CYCastJSValue(context, CYJSString(context, [value description]));
2402 } CYCatch(NULL) return /*XXX*/ NULL; }
2404 static JSValueRef Instance_callAsFunction_valueOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2405 if (!CYJSValueIsNSObject(context, _this))
2408 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2409 id value(internal->GetValue());
2411 if (![value respondsToSelector:@selector(cy$valueOfInContext:)])
2414 if (JSValueRef result = [value cy$valueOfInContext:context])
2418 } CYCatch(NULL) return /*XXX*/ NULL; }
2420 static JSValueRef Instance_callAsFunction_toPointer(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2421 if (!CYJSValueIsNSObject(context, _this))
2424 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2425 // XXX: but... but... THIS ISN'T A POINTER! :(
2426 return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->GetValue()));
2427 } CYCatch(NULL) return /*XXX*/ NULL; }
2429 static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2430 if (!CYJSValueIsNSObject(context, _this))
2433 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2434 id value(internal->GetValue());
2437 // XXX: this seems like a stupid implementation; what if it crashes? why not use the CYONifier backend?
2438 return CYCastJSValue(context, CYJSString(context, [value description]));
2440 } CYCatch(NULL) return /*XXX*/ NULL; }
2442 static JSValueRef Class_callAsFunction_pointerTo(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2443 if (!CYJSValueIsNSObject(context, _this))
2446 Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
2447 id value(internal->GetValue());
2449 if (!CYIsClass(value))
2450 CYThrow("non-Class object cannot be used as Type");
2452 // XXX: this is a very silly implementation
2454 std::ostringstream type;
2456 type << class_getName(value);
2460 return CYMakeType(context, type.str().c_str());
2462 } CYCatch(NULL) return /*XXX*/ NULL; }
2464 static JSValueRef Selector_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2465 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
2466 return CYCastJSValue(context, sel_getName(internal->GetValue()));
2469 static JSValueRef Selector_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
2470 return Selector_callAsFunction_toString(context, object, _this, count, arguments, exception);
2473 static JSValueRef Selector_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2474 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
2475 const char *name(sel_getName(internal->GetValue()));
2478 NSString *string([NSString stringWithFormat:@"@selector(%s)", name]);
2479 return CYCastJSValue(context, CYJSString(context, string));
2481 } CYCatch(NULL) return /*XXX*/ NULL; }
2483 static JSValueRef Selector_callAsFunction_type(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
2485 throw CYJSError(context, "incorrect number of arguments to Selector.type");
2488 Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
2489 SEL sel(internal->GetValue());
2491 objc_method *method;
2492 if (Class _class = CYCastClass(pool, context, arguments[0]))
2493 method = class_getInstanceMethod(_class, sel);
2497 if (const char *type = CYPoolTypeEncoding(pool, context, sel, method))
2498 return CYCastJSValue(context, CYJSString(type));
2500 return CYJSNull(context);
2503 static JSStaticValue Selector_staticValues[2] = {
2504 {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete},
2505 {NULL, NULL, NULL, 0}
2508 static JSStaticValue Instance_staticValues[5] = {
2509 {"constructor", &Instance_getProperty_constructor, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2510 {"messages", &Instance_getProperty_messages, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2511 {"prototype", &Instance_getProperty_prototype, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2512 {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2513 {NULL, NULL, NULL, 0}
2516 static JSStaticFunction Instance_staticFunctions[7] = {
2517 {"$cya", &CYValue_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2518 {"toCYON", &Instance_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2519 {"toJSON", &Instance_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2520 {"valueOf", &Instance_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2521 {"toPointer", &Instance_callAsFunction_toPointer, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2522 {"toString", &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2526 static JSStaticFunction Class_staticFunctions[2] = {
2527 {"pointerTo", &Class_callAsFunction_pointerTo, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2531 static JSStaticFunction Internal_staticFunctions[2] = {
2532 {"$cya", &Internal_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2536 static JSStaticFunction Selector_staticFunctions[5] = {
2537 {"toCYON", &Selector_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2538 {"toJSON", &Selector_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2539 {"toString", &Selector_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2540 {"type", &Selector_callAsFunction_type, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
2545 JSValueRef NSCFType$cy$toJSON$inContext$(id self, SEL sel, JSValueRef key, JSContextRef context) { CYObjectiveTry_(context) {
2546 return CYCastJSValue(context, [(NSString *) CFCopyDescription((CFTypeRef) self) autorelease]);
2547 } CYObjectiveCatch }
2550 void CYObjectiveC_Initialize() { /*XXX*/ JSContextRef context(NULL); CYPoolTry {
2551 $objc_setAssociatedObject = reinterpret_cast<void (*)(id, void *, id value, objc_AssociationPolicy)>(dlsym(RTLD_DEFAULT, "objc_setAssociatedObject"));
2552 $objc_getAssociatedObject = reinterpret_cast<id (*)(id, void *)>(dlsym(RTLD_DEFAULT, "objc_getAssociatedObject"));
2553 $objc_removeAssociatedObjects = reinterpret_cast<void (*)(id)>(dlsym(RTLD_DEFAULT, "objc_removeAssociatedObjects"));
2555 apr_pool_t *pool(CYGetGlobalPool());
2557 Object_type = new(pool) Type_privateData("@");
2558 Selector_type = new(pool) Type_privateData(":");
2561 // XXX: apparently, iOS now has both of these
2562 NSCFBoolean_ = objc_getClass("__NSCFBoolean");
2563 if (NSCFBoolean_ == nil)
2564 NSCFBoolean_ = objc_getClass("NSCFBoolean");
2566 NSCFType_ = objc_getClass("NSCFType");
2567 NSGenericDeallocHandler_ = objc_getClass("__NSGenericDeallocHandler");
2568 NSMessageBuilder_ = objc_getClass("NSMessageBuilder");
2569 NSZombie_ = objc_getClass("_NSZombie_");
2571 NSBoolNumber_ = objc_getClass("NSBoolNumber");
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");
2580 JSClassDefinition definition;
2582 definition = kJSClassDefinitionEmpty;
2583 definition.className = "Instance";
2584 definition.staticValues = Instance_staticValues;
2585 definition.staticFunctions = Instance_staticFunctions;
2586 definition.hasProperty = &Instance_hasProperty;
2587 definition.getProperty = &Instance_getProperty;
2588 definition.setProperty = &Instance_setProperty;
2589 definition.deleteProperty = &Instance_deleteProperty;
2590 definition.getPropertyNames = &Instance_getPropertyNames;
2591 definition.callAsConstructor = &Instance_callAsConstructor;
2592 definition.callAsFunction = &Instance_callAsFunction;
2593 definition.hasInstance = &Instance_hasInstance;
2594 definition.finalize = &CYFinalize;
2595 Instance_ = JSClassCreate(&definition);
2597 definition.className = "ArrayInstance";
2598 ArrayInstance_ = JSClassCreate(&definition);
2600 definition.className = "FunctionInstance";
2601 FunctionInstance_ = JSClassCreate(&definition);
2603 definition.className = "ObjectInstance";
2604 ObjectInstance_ = JSClassCreate(&definition);
2606 definition.className = "StringInstance";
2607 StringInstance_ = JSClassCreate(&definition);
2609 definition = kJSClassDefinitionEmpty;
2610 definition.className = "Class";
2611 definition.staticFunctions = Class_staticFunctions;
2612 Class_ = JSClassCreate(&definition);
2614 definition = kJSClassDefinitionEmpty;
2615 definition.className = "Internal";
2616 definition.staticFunctions = Internal_staticFunctions;
2617 definition.hasProperty = &Internal_hasProperty;
2618 definition.getProperty = &Internal_getProperty;
2619 definition.setProperty = &Internal_setProperty;
2620 definition.getPropertyNames = &Internal_getPropertyNames;
2621 definition.finalize = &CYFinalize;
2622 Internal_ = JSClassCreate(&definition);
2624 definition = kJSClassDefinitionEmpty;
2625 definition.className = "Message";
2626 definition.staticFunctions = cy::Functor::StaticFunctions;
2627 definition.callAsFunction = &Message_callAsFunction;
2628 definition.finalize = &CYFinalize;
2629 Message_ = JSClassCreate(&definition);
2631 definition = kJSClassDefinitionEmpty;
2632 definition.className = "Messages";
2633 definition.hasProperty = &Messages_hasProperty;
2634 definition.getProperty = &Messages_getProperty;
2635 definition.setProperty = &Messages_setProperty;
2636 #if 0 && OBJC_API_VERSION < 2
2637 definition.deleteProperty = &Messages_deleteProperty;
2639 definition.getPropertyNames = &Messages_getPropertyNames;
2640 definition.finalize = &CYFinalize;
2641 Messages_ = JSClassCreate(&definition);
2643 definition = kJSClassDefinitionEmpty;
2644 definition.className = "Selector";
2645 definition.staticValues = Selector_staticValues;
2646 definition.staticFunctions = Selector_staticFunctions;
2647 definition.callAsFunction = &Selector_callAsFunction;
2648 definition.finalize = &CYFinalize;
2649 Selector_ = JSClassCreate(&definition);
2651 definition = kJSClassDefinitionEmpty;
2652 definition.className = "Super";
2653 definition.staticFunctions = Internal_staticFunctions;
2654 definition.finalize = &CYFinalize;
2655 Super_ = JSClassCreate(&definition);
2657 definition = kJSClassDefinitionEmpty;
2658 definition.className = "ObjectiveC::Classes";
2659 definition.getProperty = &ObjectiveC_Classes_getProperty;
2660 definition.getPropertyNames = &ObjectiveC_Classes_getPropertyNames;
2661 ObjectiveC_Classes_ = JSClassCreate(&definition);
2663 definition = kJSClassDefinitionEmpty;
2664 definition.className = "ObjectiveC::Constants";
2665 definition.getProperty = &ObjectiveC_Constants_getProperty;
2666 definition.getPropertyNames = &ObjectiveC_Constants_getPropertyNames;
2667 ObjectiveC_Constants_ = JSClassCreate(&definition);
2669 #if OBJC_API_VERSION >= 2
2670 definition = kJSClassDefinitionEmpty;
2671 definition.className = "ObjectiveC::Images";
2672 definition.getProperty = &ObjectiveC_Images_getProperty;
2673 definition.getPropertyNames = &ObjectiveC_Images_getPropertyNames;
2674 ObjectiveC_Images_ = JSClassCreate(&definition);
2676 definition = kJSClassDefinitionEmpty;
2677 definition.className = "ObjectiveC::Image::Classes";
2678 definition.getProperty = &ObjectiveC_Image_Classes_getProperty;
2679 definition.getPropertyNames = &ObjectiveC_Image_Classes_getPropertyNames;
2680 ObjectiveC_Image_Classes_ = JSClassCreate(&definition);
2683 definition = kJSClassDefinitionEmpty;
2684 definition.className = "ObjectiveC::Protocols";
2685 definition.getProperty = &ObjectiveC_Protocols_getProperty;
2686 definition.getPropertyNames = &ObjectiveC_Protocols_getPropertyNames;
2687 ObjectiveC_Protocols_ = JSClassCreate(&definition);
2690 class_addMethod(NSCFType_, @selector(cy$toJSON:inContext:), reinterpret_cast<IMP>(&NSCFType$cy$toJSON$inContext$), "^{OpaqueJSValue=}16@0:4@8^{OpaqueJSContext=}12");
2694 void CYObjectiveC_SetupContext(JSContextRef context) { CYPoolTry {
2695 JSObjectRef global(CYGetGlobalObject(context));
2696 JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s)));
2697 JSObjectRef cycript(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Cycript"))));
2698 JSObjectRef all(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("all"))));
2699 JSObjectRef alls(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("alls"))));
2701 JSObjectRef ObjectiveC(JSObjectMake(context, NULL, NULL));
2702 CYSetProperty(context, cycript, CYJSString("ObjectiveC"), ObjectiveC);
2704 JSObjectRef protocols(JSObjectMake(context, ObjectiveC_Protocols_, NULL));
2705 CYSetProperty(context, ObjectiveC, CYJSString("protocols"), protocols);
2706 CYArrayPush(context, alls, protocols);
2708 JSObjectRef classes(JSObjectMake(context, ObjectiveC_Classes_, NULL));
2709 CYSetProperty(context, ObjectiveC, CYJSString("classes"), classes);
2710 CYArrayPush(context, alls, classes);
2712 JSObjectRef constants(JSObjectMake(context, ObjectiveC_Constants_, NULL));
2713 CYSetProperty(context, ObjectiveC, CYJSString("constants"), constants);
2714 CYArrayPush(context, alls, constants);
2716 #if OBJC_API_VERSION >= 2
2717 CYSetProperty(context, ObjectiveC, CYJSString("images"), JSObjectMake(context, ObjectiveC_Images_, NULL));
2720 JSObjectRef Class(JSObjectMakeConstructor(context, Class_, NULL));
2721 JSObjectRef Instance(JSObjectMakeConstructor(context, Instance_, &Instance_new));
2722 JSObjectRef Message(JSObjectMakeConstructor(context, Message_, NULL));
2723 JSObjectRef Selector(JSObjectMakeConstructor(context, Selector_, &Selector_new));
2724 JSObjectRef Super(JSObjectMakeConstructor(context, Super_, &Super_new));
2726 JSObjectRef Instance_prototype(CYCastJSObject(context, CYGetProperty(context, Instance, prototype_s)));
2727 CYSetProperty(context, cy, CYJSString("Instance_prototype"), Instance_prototype);
2729 JSObjectRef ArrayInstance(JSObjectMakeConstructor(context, ArrayInstance_, NULL));
2730 JSObjectRef ArrayInstance_prototype(CYCastJSObject(context, CYGetProperty(context, ArrayInstance, prototype_s)));
2731 CYSetProperty(context, cy, CYJSString("ArrayInstance_prototype"), ArrayInstance_prototype);
2732 JSObjectRef Array_prototype(CYGetCachedObject(context, CYJSString("Array_prototype")));
2733 JSObjectSetPrototype(context, ArrayInstance_prototype, Array_prototype);
2735 JSObjectRef FunctionInstance(JSObjectMakeConstructor(context, FunctionInstance_, NULL));
2736 JSObjectRef FunctionInstance_prototype(CYCastJSObject(context, CYGetProperty(context, FunctionInstance, prototype_s)));
2737 CYSetProperty(context, cy, CYJSString("FunctionInstance_prototype"), FunctionInstance_prototype);
2738 JSObjectRef Function_prototype(CYGetCachedObject(context, CYJSString("Function_prototype")));
2739 JSObjectSetPrototype(context, FunctionInstance_prototype, Function_prototype);
2741 JSObjectRef ObjectInstance(JSObjectMakeConstructor(context, ObjectInstance_, NULL));
2742 JSObjectRef ObjectInstance_prototype(CYCastJSObject(context, CYGetProperty(context, ObjectInstance, prototype_s)));
2743 CYSetProperty(context, cy, CYJSString("ObjectInstance_prototype"), ObjectInstance_prototype);
2744 JSObjectRef Object_prototype(CYGetCachedObject(context, CYJSString("Object_prototype")));
2745 JSObjectSetPrototype(context, ObjectInstance_prototype, Object_prototype);
2747 JSObjectRef StringInstance(JSObjectMakeConstructor(context, StringInstance_, NULL));
2748 JSObjectRef StringInstance_prototype(CYCastJSObject(context, CYGetProperty(context, StringInstance, prototype_s)));
2749 CYSetProperty(context, cy, CYJSString("StringInstance_prototype"), StringInstance_prototype);
2750 JSObjectRef String_prototype(CYGetCachedObject(context, CYJSString("String_prototype")));
2751 JSObjectSetPrototype(context, StringInstance_prototype, String_prototype);
2753 JSObjectRef Class_prototype(CYCastJSObject(context, CYGetProperty(context, Class, prototype_s)));
2754 CYSetProperty(context, cy, CYJSString("Class_prototype"), Class_prototype);
2755 JSObjectSetPrototype(context, Class_prototype, Instance_prototype);
2757 CYSetProperty(context, cycript, CYJSString("Instance"), Instance);
2758 CYSetProperty(context, cycript, CYJSString("Selector"), Selector);
2759 CYSetProperty(context, cycript, CYJSString("Super"), Super);
2761 JSObjectRef box(JSObjectMakeFunctionWithCallback(context, CYJSString("box"), &Instance_box_callAsFunction));
2762 CYSetProperty(context, Instance, CYJSString("box"), box);
2764 #if defined(__APPLE__) && defined(__arm__) && 0
2765 CYSetProperty(context, all, CYJSString("objc_registerClassPair"), &objc_registerClassPair_, kJSPropertyAttributeDontEnum);
2768 CYSetProperty(context, all, CYJSString("objc_msgSend"), &$objc_msgSend, kJSPropertyAttributeDontEnum);
2770 JSObjectSetPrototype(context, CYCastJSObject(context, CYGetProperty(context, Message, prototype_s)), Function_prototype);
2771 JSObjectSetPrototype(context, CYCastJSObject(context, CYGetProperty(context, Selector, prototype_s)), Function_prototype);
2774 static CYHooks CYObjectiveCHooks = {
2775 &CYObjectiveC_ExecuteStart,
2776 &CYObjectiveC_ExecuteEnd,
2777 &CYObjectiveC_CallFunction,
2778 &CYObjectiveC_Initialize,
2779 &CYObjectiveC_SetupContext,
2780 &CYObjectiveC_PoolFFI,
2781 &CYObjectiveC_FromFFI,
2784 struct CYObjectiveC {
2786 hooks_ = &CYObjectiveCHooks;
2787 // XXX: evil magic juju to make this actually take effect on a Mac when compiled with autoconf/libtool doom!
2788 _assert(hooks_ != NULL);