X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/11f3e89e6b60ef97ca4f885126e50621dce37bde..77f56624a75e39b19c72e6076e46e230ce3abbb1:/ObjectiveC/Library.mm diff --git a/ObjectiveC/Library.mm b/ObjectiveC/Library.mm index 64281a3..a113779 100644 --- a/ObjectiveC/Library.mm +++ b/ObjectiveC/Library.mm @@ -1,5 +1,5 @@ /* Cycript - Optimizing JavaScript Compiler/Runtime - * Copyright (C) 2009-2010 Jay Freeman (saurik) + * Copyright (C) 2009-2012 Jay Freeman (saurik) */ /* GNU Lesser General Public License, Version 3 {{{ */ @@ -48,6 +48,8 @@ #include #include +#include + #define CYObjectiveTry_(context) { \ JSContextRef context_(context); \ try @@ -122,6 +124,10 @@ #define protocol_getName(protocol) [(protocol) name] #endif +static void (*$objc_setAssociatedObject)(id object, void *key, id value, objc_AssociationPolicy policy); +static id (*$objc_getAssociatedObject)(id object, void *key); +static void (*$objc_removeAssociatedObjects)(id object); + JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, Class super, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception); /* Objective-C Pool Release {{{ */ @@ -234,6 +240,12 @@ bool CYGetOffset(apr_pool_t *pool, JSContextRef context, NSString *value, ssize_ } static JSClassRef Instance_; + +static JSClassRef ArrayInstance_; +static JSClassRef FunctionInstance_; +static JSClassRef ObjectInstance_; +static JSClassRef StringInstance_; + static JSClassRef Internal_; static JSClassRef Message_; static JSClassRef Messages_; @@ -260,6 +272,7 @@ static Class NSBoolNumber_; #endif static Class NSArray_; +static Class NSBlock_; static Class NSDictionary_; static Class NSString_; static Class Object_; @@ -297,6 +310,8 @@ JSValueRef CYGetClassPrototype(JSContextRef context, id self) { if (self == NSArray_) prototype = CYGetCachedObject(context, CYJSString("ArrayInstance_prototype")); + else if (self == NSBlock_) + prototype = CYGetCachedObject(context, CYJSString("FunctionInstance_prototype")); else if (self == NSDictionary_) prototype = CYGetCachedObject(context, CYJSString("ObjectInstance_prototype")); else if (self == NSString_) @@ -376,12 +391,11 @@ JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient) { @interface NSObject (Cycript) -- (JSValueRef) cy$JSValueInContext:(JSContextRef)context; +- (JSValueRef) cy$valueOfInContext:(JSContextRef)context; - (JSType) cy$JSType; - (NSObject *) cy$toJSON:(NSString *)key; -- (NSString *) cy$toCYON; -- (NSString *) cy$toKey; +- (NSString *) cy$toCYON:(bool)objective; - (bool) cy$hasProperty:(NSString *)name; - (NSObject *) cy$getProperty:(NSString *)name; @@ -394,23 +408,24 @@ JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient) { @end @protocol Cycript -- (JSValueRef) cy$JSValueInContext:(JSContextRef)context; +- (id) cy$box; +- (JSValueRef) cy$valueOfInContext:(JSContextRef)context; @end -NSString *CYCastNSCYON(id value) { +NSString *CYCastNSCYON(id value, bool objective) { NSString *string; if (value == nil) string = @"nil"; else { Class _class(object_getClass(value)); - SEL sel(@selector(cy$toCYON)); + SEL sel(@selector(cy$toCYON:)); if (objc_method *toCYON = class_getInstanceMethod(_class, sel)) - string = reinterpret_cast(method_getImplementation(toCYON))(value, sel); + string = reinterpret_cast(method_getImplementation(toCYON))(value, sel, objective); else if (objc_method *methodSignatureForSelector = class_getInstanceMethod(_class, @selector(methodSignatureForSelector:))) { if (reinterpret_cast(method_getImplementation(methodSignatureForSelector))(value, @selector(methodSignatureForSelector:), sel) != nil) - string = [value cy$toCYON]; + string = [value cy$toCYON:objective]; else goto fail; } else fail: { if (false); @@ -552,7 +567,7 @@ NSObject *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) { /* Bridge: CYJSObject {{{ */ @interface CYJSObject : NSMutableDictionary { JSObjectRef object_; - JSContextRef context_; + JSGlobalContextRef context_; } - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context; @@ -570,7 +585,7 @@ NSObject *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) { /* Bridge: CYJSArray {{{ */ @interface CYJSArray : NSMutableArray { JSObjectRef object_; - JSContextRef context_; + JSGlobalContextRef context_; } - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context; @@ -587,22 +602,23 @@ NSObject *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) { @end /* }}} */ -NSObject *CYCastNSObject_(apr_pool_t *pool, JSContextRef context, JSObjectRef object) { - JSObjectRef Array(CYGetCachedObject(context, Array_s)); +_finline bool CYJSValueIsInstanceOfCachedConstructor(JSContextRef context, JSValueRef value, JSStringRef cache) { JSValueRef exception(NULL); - bool array(JSValueIsInstanceOfConstructor(context, object, Array, &exception)); + JSObjectRef constructor(CYGetCachedObject(context, cache)); + bool is(JSValueIsInstanceOfConstructor(context, value, constructor, &exception)); CYThrow(context, exception); - id value(array ? [CYJSArray alloc] : [CYJSObject alloc]); - return CYPoolRelease(pool, [value initWithJSObject:object inContext:context]); + return is; } NSObject *CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSObjectRef object) { - if (!JSValueIsObjectOfClass(context, object, Instance_)) - return CYCastNSObject_(pool, context, object); - else { + if (JSValueIsObjectOfClass(context, object, Instance_)) { Instance *internal(reinterpret_cast(JSObjectGetPrivate(object))); return internal->GetValue(); } + + bool array(CYJSValueIsInstanceOfCachedConstructor(context, object, Array_s)); + id value(array ? [CYJSArray alloc] : [CYJSObject alloc]); + return CYPoolRelease(pool, [value initWithJSObject:object inContext:context]); } NSNumber *CYCopyNSNumber(JSContextRef context, JSValueRef value) { @@ -679,7 +695,11 @@ NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef valu /* Bridge: NSArray {{{ */ @implementation NSArray (Cycript) -- (NSString *) cy$toCYON { +- (id) cy$box { + return [[self mutableCopy] autorelease]; +} + +- (NSString *) cy$toCYON:(bool)objective { NSMutableString *json([[[NSMutableString alloc] init] autorelease]); [json appendString:@"@["]; @@ -695,7 +715,7 @@ NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef valu else comma = true; if (object == nil || [object cy$JSType] != kJSTypeUndefined) - [json appendString:CYCastNSCYON(object)]; + [json appendString:CYCastNSCYON(object, true)]; else { [json appendString:@","]; comma = false; @@ -753,6 +773,13 @@ NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef valu @end /* }}} */ +/* Bridge: NSBlock {{{ */ +#ifdef __APPLE__ +@interface NSBlock +- (void) invoke; +@end +#endif +/* }}} */ /* Bridge: NSBoolNumber {{{ */ #ifndef __APPLE__ @implementation NSBoolNumber (Cycript) @@ -765,11 +792,12 @@ NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef valu return self; } -- (NSString *) cy$toCYON { - return [self boolValue] ? @"@true" : @"@false"; +- (NSString *) cy$toCYON:(bool)objective { + NSString *value([self boolValue] ? @"true" : @"false"); + return objective ? value : [NSString stringWithFormat:@"@%@", value]; } -- (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) { +- (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) { return CYCastJSValue(context, (bool) [self boolValue]); } CYObjectiveCatch } @@ -779,7 +807,11 @@ NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef valu /* Bridge: NSDictionary {{{ */ @implementation NSDictionary (Cycript) -- (NSString *) cy$toCYON { +- (id) cy$box { + return [[self mutableCopy] autorelease]; +} + +- (NSString *) cy$toCYON:(bool)objective { NSMutableString *json([[[NSMutableString alloc] init] autorelease]); [json appendString:@"@{"]; @@ -794,10 +826,10 @@ NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef valu [json appendString:@","]; else comma = true; - [json appendString:[key cy$toKey]]; + [json appendString:CYCastNSCYON(key, true)]; [json appendString:@":"]; NSObject *object([self objectForKey:key]); - [json appendString:CYCastNSCYON(object)]; + [json appendString:CYCastNSCYON(object, true)]; } [json appendString:@"}"]; @@ -921,12 +953,13 @@ NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef valu return self; } -- (NSString *) cy$toCYON { - return [self cy$JSType] != kJSTypeBoolean ? [NSString stringWithFormat:@"@%@", self] : [self boolValue] ? @"@true" : @"@false"; +- (NSString *) cy$toCYON:(bool)objective { + NSString *value([self cy$JSType] != kJSTypeBoolean ? [self stringValue] : [self boolValue] ? @"true" : @"false"); + return objective ? value : [NSString stringWithFormat:@"@%@", value]; } -- (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) { - return [self cy$JSType] != kJSTypeBoolean ? CYCastJSValue(context, [self doubleValue]) : CYCastJSValue(context, [self boolValue]); +- (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) { + return [self cy$JSType] != kJSTypeBoolean ? CYCastJSValue(context, [self doubleValue]) : CYCastJSValue(context, static_cast([self boolValue])); } CYObjectiveCatch } @end @@ -942,8 +975,9 @@ NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef valu return self; } -- (NSString *) cy$toCYON { - return @"@null"; +- (NSString *) cy$toCYON:(bool)objective { + NSString *value(@"null"); + return objective ? value : [NSString stringWithFormat:@"@%@", value]; } @end @@ -951,7 +985,11 @@ NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef valu /* Bridge: NSObject {{{ */ @implementation NSObject (Cycript) -- (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) { +- (id) cy$box { + return self; +} + +- (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) { return NULL; } CYObjectiveCatch } @@ -963,12 +1001,8 @@ NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef valu return [self description]; } -- (NSString *) cy$toCYON { - return [[self cy$toJSON:@""] cy$toCYON]; -} - -- (NSString *) cy$toKey { - return [self cy$toCYON]; +- (NSString *) cy$toCYON:(bool)objective { + return [[self cy$toJSON:@""] cy$toCYON:objective]; } - (bool) cy$hasProperty:(NSString *)name { @@ -1003,8 +1037,8 @@ NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef valu return [self description]; } -- (NSString *) cy$toCYON { - return [[self cy$toJSON:@""] cy$toCYON]; +- (NSString *) cy$toCYON:(bool)objective { + return [[self cy$toJSON:@""] cy$toCYON:objective]; } @end @@ -1012,6 +1046,10 @@ NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef valu /* Bridge: NSString {{{ */ @implementation NSString (Cycript) +- (id) cy$box { + return [[self copy] autorelease]; +} + - (JSType) cy$JSType { return kJSTypeString; } @@ -1020,21 +1058,16 @@ NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef valu return self; } -- (NSString *) cy$toCYON { +- (NSString *) cy$toCYON:(bool)objective { std::ostringstream str; - str << '@'; + if (!objective) + str << '@'; CYUTF8String string(CYCastUTF8String(self)); CYStringify(str, string.data, string.size); std::string value(str.str()); return CYCastNSString(NULL, CYUTF8String(value.c_str(), value.size())); } -- (NSString *) cy$toKey { - if (CYIsKey(CYCastUTF8String(self))) - return self; - return [self cy$toCYON]; -} - - (bool) cy$hasProperty:(NSString *)name { if ([name isEqualToString:@"length"]) return true; @@ -1078,7 +1111,7 @@ NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef valu return false; } -- (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) { +- (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) { return CYCastJSValue(context, CYJSString(context, self)); } CYObjectiveCatch } @@ -1096,10 +1129,12 @@ NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef valu } - (NSString *) cy$toCYON { - return @"undefined"; + NSString *value(@"undefined"); + return value; // XXX: maybe use the below code, adding @undefined? + //return objective ? value : [NSString stringWithFormat:@"@%@", value]; } -- (JSValueRef) cy$JSValueInContext:(JSContextRef)context { CYObjectiveTry_(context) { +- (JSValueRef) cy$valueOfInContext:(JSContextRef)context { CYObjectiveTry_(context) { return CYJSUndefined(context); } CYObjectiveCatch } @@ -1145,14 +1180,14 @@ JSValueRef CYCastJSValue(JSContextRef context, NSObject *value) { CYPoolTry { if ((self = [super init]) != nil) { object_ = object; context_ = CYGetJSContext(context); - //XXX:JSGlobalContextRetain(context_); + JSGlobalContextRetain(context_); JSValueProtect(context_, object_); } return self; } CYObjectiveCatch } - (void) dealloc { CYObjectiveTry { JSValueUnprotect(context_, object_); - //XXX:JSGlobalContextRelease(context_); + JSGlobalContextRelease(context_); [super dealloc]; } CYObjectiveCatch } @@ -1168,13 +1203,13 @@ JSValueRef CYCastJSValue(JSContextRef context, NSObject *value) { CYPoolTry { } } CYObjectiveCatch } -- (NSString *) cy$toCYON { CYObjectiveTry { +- (NSString *) cy$toCYON:(bool)objective { CYObjectiveTry { CYPool pool; JSValueRef exception(NULL); const char *cyon(CYPoolCCYON(pool, context_, object_)); CYThrow(context_, exception); if (cyon == NULL) - return [super cy$toCYON]; + return [super cy$toCYON:objective]; else return [NSString stringWithUTF8String:cyon]; } CYObjectiveCatch } @@ -1214,18 +1249,23 @@ JSValueRef CYCastJSValue(JSContextRef context, NSObject *value) { CYPoolTry { @implementation CYJSArray +- (NSString *) cy$toCYON:(bool)objective { + CYPool pool; + return [NSString stringWithUTF8String:CYPoolCCYON(pool, context_, object_)]; +} + - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { CYObjectiveTry { if ((self = [super init]) != nil) { object_ = object; context_ = CYGetJSContext(context); - //XXX:JSGlobalContextRetain(context_); + JSGlobalContextRetain(context_); JSValueProtect(context_, object_); } return self; } CYObjectiveCatch } - (void) dealloc { CYObjectiveTry { JSValueUnprotect(context_, object_); - //XXX:JSGlobalContextRelease(context_); + JSGlobalContextRelease(context_); [super dealloc]; } CYObjectiveCatch } @@ -1290,62 +1330,86 @@ JSValueRef CYCastJSValue(JSContextRef context, NSObject *value) { CYPoolTry { @end -// XXX: use objc_getAssociatedObject and objc_setAssociatedObject on 10.6 -struct CYInternal : - CYData -{ +// XXX: inherit from or replace with CYJSObject +@interface CYInternal : NSObject { + JSGlobalContextRef context_; JSObjectRef object_; +} - CYInternal() : - object_(NULL) - { - } +@end - ~CYInternal() { - // XXX: delete object_? ;( - } +@implementation CYInternal - static CYInternal *Get(id self) { - CYInternal *internal(NULL); - if (object_getInstanceVariable(self, "cy$internal_", reinterpret_cast(&internal)) == NULL) { - // XXX: do something epic? ;P - } +- (void) dealloc { + JSValueUnprotect(context_, object_); + JSGlobalContextRelease(context_); + [super dealloc]; +} - return internal; - } +- (id) initInContext:(JSContextRef)context { + if ((self = [super init]) != nil) { + context_ = CYGetJSContext(context); + JSGlobalContextRetain(context_); + } return self; +} - static CYInternal *Set(id self) { - CYInternal *internal(NULL); - if (objc_ivar *ivar = object_getInstanceVariable(self, "cy$internal_", reinterpret_cast(&internal))) { - if (internal == NULL) { - internal = new CYInternal(); - object_setIvar(self, ivar, reinterpret_cast(internal)); - } - } else { - // XXX: do something epic? ;P - } +- (bool) hasProperty:(JSStringRef)name inContext:(JSContextRef)context { + if (object_ == NULL) + return false; - return internal; - } + return JSObjectHasProperty(context, object_, name); +} - bool HasProperty(JSContextRef context, JSStringRef name) { - if (object_ == NULL) - return false; - return JSObjectHasProperty(context, object_, name); +- (JSValueRef) getProperty:(JSStringRef)name inContext:(JSContextRef)context { + if (object_ == NULL) + return NULL; + + return CYGetProperty(context, object_, name); +} + +- (void) setProperty:(JSStringRef)name toValue:(JSValueRef)value inContext:(JSContextRef)context { + @synchronized (self) { + if (object_ == NULL) { + object_ = JSObjectMake(context, NULL, NULL); + JSValueProtect(context, object_); + } } - JSValueRef GetProperty(JSContextRef context, JSStringRef name) { - if (object_ == NULL) - return NULL; - return CYGetProperty(context, object_, name); + CYSetProperty(context, object_, name, value); +} + ++ (CYInternal *) get:(id)object { + if ($objc_getAssociatedObject == NULL) + return nil; + + @synchronized (object) { + if (CYInternal *internal = $objc_getAssociatedObject(object, @selector(cy$internal))) + return internal; } - void SetProperty(JSContextRef context, JSStringRef name, JSValueRef value) { - if (object_ == NULL) - object_ = JSObjectMake(context, NULL, NULL); - CYSetProperty(context, object_, name, value); + return nil; +} + ++ (CYInternal *) set:(id)object inContext:(JSContextRef)context { + if ($objc_getAssociatedObject == NULL) + return nil; + + @synchronized (object) { + if (CYInternal *internal = $objc_getAssociatedObject(object, @selector(cy$internal))) + return internal; + + if ($objc_setAssociatedObject == NULL) + return nil; + + CYInternal *internal([[[CYInternal alloc] initInContext:context] autorelease]); + objc_setAssociatedObject(object, @selector(cy$internal), internal, OBJC_ASSOCIATION_RETAIN_NONATOMIC); + return internal; } -}; + + return nil; +} + +@end static JSObjectRef CYMakeSelector(JSContextRef context, SEL sel) { Selector_privateData *internal(new Selector_privateData(sel)); @@ -1429,7 +1493,7 @@ static JSValueRef CYObjectiveC_FromFFI(JSContextRef context, sig::Type *type, ff } } CYPoolCatch(NULL) return /*XXX*/ NULL; } -static bool CYImplements(id object, Class _class, SEL selector, bool devoid) { +static bool CYImplements(id object, Class _class, SEL selector, bool devoid = false) { if (objc_method *method = class_getInstanceMethod(_class, selector)) { if (!devoid) return true; @@ -1599,7 +1663,7 @@ static void Messages_getPropertyNames(JSContextRef context, JSObjectRef object, static bool CYHasImplicitProperties(Class _class) { // XXX: this is an evil hack to deal with NSProxy; fix elsewhere - if (!CYImplements(_class, object_getClass(_class), @selector(cy$hasImplicitProperties), false)) + if (!CYImplements(_class, object_getClass(_class), @selector(cy$hasImplicitProperties))) return true; return [_class cy$hasImplicitProperties]; } @@ -1614,15 +1678,15 @@ static bool Instance_hasProperty(JSContextRef context, JSObjectRef object, JSStr CYPool pool; NSString *name(CYCastNSString(pool, context, property)); - if (CYInternal *internal = CYInternal::Get(self)) - if (internal->HasProperty(context, property)) + if (CYInternal *internal = [CYInternal get:self]) + if ([internal hasProperty:property inContext:context]) return true; Class _class(object_getClass(self)); CYPoolTry { // XXX: this is an evil hack to deal with NSProxy; fix elsewhere - if (CYImplements(self, _class, @selector(cy$hasProperty:), false)) + if (CYImplements(self, _class, @selector(cy$hasProperty:))) if ([self cy$hasProperty:name]) return true; } CYPoolCatch(false) @@ -1652,8 +1716,8 @@ static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, CYPool pool; NSString *name(CYCastNSString(pool, context, property)); - if (CYInternal *internal = CYInternal::Get(self)) - if (JSValueRef value = internal->GetProperty(context, property)) + if (CYInternal *internal = [CYInternal get:self]) + if (JSValueRef value = [internal getProperty:property inContext:context]) return value; CYPoolTry { @@ -1726,13 +1790,14 @@ static bool Instance_setProperty(JSContextRef context, JSObjectRef object, JSStr set[length + 4] = '\0'; if (SEL sel = sel_getUid(set)) - if (CYImplements(self, _class, sel, false)) { + if (CYImplements(self, _class, sel)) { JSValueRef arguments[1] = {value}; CYSendMessage(pool, context, self, NULL, sel, 1, arguments, false, exception); + return true; } - if (CYInternal *internal = CYInternal::Set(self)) { - internal->SetProperty(context, property, value); + if (CYInternal *internal = [CYInternal set:self inContext:context]) { + [internal setProperty:property toValue:value inContext:context]; return true; } @@ -1795,7 +1860,7 @@ static void Instance_getPropertyNames(JSContextRef context, JSObjectRef object, CYPoolTry { // XXX: this is an evil hack to deal with NSProxy; fix elsewhere - if (CYImplements(self, _class, @selector(cy$getPropertyNames:inContext:), false)) + if (CYImplements(self, _class, @selector(cy$getPropertyNames:inContext:))) [self cy$getPropertyNames:names inContext:context]; } CYPoolCatch() } @@ -1806,6 +1871,87 @@ static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef return value; } CYCatch } +static JSValueRef Instance_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { + Instance *internal(reinterpret_cast(JSObjectGetPrivate(object))); + id self(internal->GetValue()); + + if (![self isKindOfClass:NSBlock_]) + CYThrow("non-NSBlock object is not a function"); + // XXX: replace above logic with the following assertion + //_assert([self isKindOfClass:NSBlock_]); + // to do this, make it so FunctionInstance_ is the class of blocks + // to do /that/, generalize the various "is exactly Instance_" checks + // then, move Instance_callAsFunction to only be on FunctionInstance + + struct BlockDescriptor1 { + unsigned long int reserved; + unsigned long int size; + }; + + struct BlockDescriptor2 { + void (*copy_helper)(void *dst, void *src); + void (*dispose_helper)(void *src); + }; + + struct BlockDescriptor3 { + const char *signature; + const char *layout; + }; + + struct BlockLiteral { + Class isa; + int flags; + int reserved; + void (*invoke)(void *, ...); + void *descriptor; + } *literal = reinterpret_cast(self); + + enum { + BLOCK_DEALLOCATING = 0x0001, + BLOCK_REFCOUNT_MASK = 0xfffe, + BLOCK_NEEDS_FREE = 1 << 24, + BLOCK_HAS_COPY_DISPOSE = 1 << 25, + BLOCK_HAS_CTOR = 1 << 26, + BLOCK_IS_GC = 1 << 27, + BLOCK_IS_GLOBAL = 1 << 28, + BLOCK_HAS_STRET = 1 << 29, + BLOCK_HAS_SIGNATURE = 1 << 30, + }; + + if ((literal->flags & BLOCK_HAS_SIGNATURE) != 0) { + uint8_t *descriptor(reinterpret_cast(literal->descriptor)); + descriptor += sizeof(BlockDescriptor1); + if ((literal->flags & BLOCK_HAS_COPY_DISPOSE) != 0) + descriptor += sizeof(BlockDescriptor2); + BlockDescriptor3 *descriptor3(reinterpret_cast(descriptor)); + + if (const char *type = descriptor3->signature) { + CYPool pool; + + void *setup[1]; + setup[0] = &self; + + sig::Signature signature; + sig::Parse(pool, &signature, type, &Structor_); + + ffi_cif cif; + sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); + + void (*function)() = reinterpret_cast(literal->invoke); + return CYCallFunction(pool, context, 1, setup, count, arguments, false, exception, &signature, &cif, function); + } + } + + if (count != 0) + CYThrow("NSBlock without signature field passed arguments"); + + CYPoolTry { + [self invoke]; + } CYPoolCatch(NULL); + + return NULL; +} CYCatch } + static bool Instance_hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef instance, JSValueRef *exception) { CYTry { Instance *internal(reinterpret_cast(JSObjectGetPrivate((JSObjectRef) constructor))); Class _class(internal->GetValue()); @@ -1821,6 +1967,16 @@ static bool Instance_hasInstance(JSContextRef context, JSObjectRef constructor, return false; } CYCatch } +static JSValueRef Instance_box_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { + if (count == 0) + throw CYJSError(context, "incorrect number of arguments to Instance"); + CYPool pool; + id value(CYCastNSObject(pool, context, arguments[0])); + if (value == nil) + value = [NSNull null]; + return CYCastJSValue(context, [value cy$box]); +} CYCatch } + static bool Internal_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { Internal *internal(reinterpret_cast(JSObjectGetPrivate(object))); CYPool pool; @@ -2064,6 +2220,26 @@ JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, Class sig::Signature signature; sig::Parse(pool, &signature, type, &Structor_); + size_t used(count + 3); + if (used > signature.count) { + sig::Element *elements(new (pool) sig::Element[used]); + memcpy(elements, signature.elements, used * sizeof(sig::Element)); + + for (size_t index(signature.count); index != used; ++index) { + sig::Element *element(&elements[index]); + element->name = NULL; + element->offset = _not(size_t); + + sig::Type *type(new (pool) sig::Type); + memset(type, 0, sizeof(*type)); + type->primitive = sig::object_P; + element->type = type; + } + + signature.elements = elements; + signature.count = used; + } + ffi_cif cif; sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); @@ -2121,42 +2297,6 @@ static JSValueRef $objc_msgSend(JSContextRef context, JSObjectRef object, JSObje return CYSendMessage(pool, context, self, _class, _cmd, count - 2, arguments + 2, uninitialized, exception); } CYCatch } -/* Hook: objc_registerClassPair {{{ */ -#if defined(__APPLE__) && defined(__arm__) && 0 -// XXX: replace this with associated objects - -MSHook(void, CYDealloc, id self, SEL sel) { - CYInternal *internal; - object_getInstanceVariable(self, "cy$internal_", reinterpret_cast(&internal)); - if (internal != NULL) - delete internal; - _CYDealloc(self, sel); -} - -MSHook(void, objc_registerClassPair, Class _class) { - Class super(class_getSuperclass(_class)); - if (super == NULL || class_getInstanceVariable(super, "cy$internal_") == NULL) { - class_addIvar(_class, "cy$internal_", sizeof(CYInternal *), log2(sizeof(CYInternal *)), "^{CYInternal}"); - MSHookMessage(_class, @selector(dealloc), MSHake(CYDealloc)); - } - - _objc_registerClassPair(_class); -} - -static JSValueRef objc_registerClassPair_(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { - if (count != 1) - throw CYJSError(context, "incorrect number of arguments to objc_registerClassPair"); - CYPool pool; - NSObject *value(CYCastNSObject(pool, context, arguments[0])); - if (value == NULL || !CYIsClass(value)) - throw CYJSError(context, "incorrect number of arguments to objc_registerClassPair"); - Class _class((Class) value); - $objc_registerClassPair(_class); - return CYJSUndefined(context); -} CYCatch } -#endif -/* }}} */ - static JSValueRef Selector_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { JSValueRef setup[count + 2]; setup[0] = _this; @@ -2252,7 +2392,7 @@ static JSValueRef Instance_callAsFunction_toCYON(JSContextRef context, JSObjectR return NULL; Instance *internal(reinterpret_cast(JSObjectGetPrivate(_this))); - return CYCastJSValue(context, CYJSString(context, CYCastNSCYON(internal->GetValue()))); + return CYCastJSValue(context, CYJSString(context, CYCastNSCYON(internal->GetValue(), false))); } CYCatch } static JSValueRef Instance_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { @@ -2279,10 +2419,10 @@ static JSValueRef Instance_callAsFunction_valueOf(JSContextRef context, JSObject Instance *internal(reinterpret_cast(JSObjectGetPrivate(_this))); id value(internal->GetValue()); - if (![value respondsToSelector:@selector(cy$JSValueInContext:)]) + if (![value respondsToSelector:@selector(cy$valueOfInContext:)]) return _this; - if (JSValueRef result = [value cy$JSValueInContext:context]) + if (JSValueRef result = [value cy$valueOfInContext:context]) return result; return _this; @@ -2389,13 +2529,21 @@ static JSStaticFunction Selector_staticFunctions[5] = { }; void CYObjectiveC_Initialize() { /*XXX*/ JSContextRef context(NULL); CYPoolTry { + $objc_setAssociatedObject = reinterpret_cast(dlsym(RTLD_DEFAULT, "objc_setAssociatedObject")); + $objc_getAssociatedObject = reinterpret_cast(dlsym(RTLD_DEFAULT, "objc_getAssociatedObject")); + $objc_removeAssociatedObjects = reinterpret_cast(dlsym(RTLD_DEFAULT, "objc_removeAssociatedObjects")); + apr_pool_t *pool(CYGetGlobalPool()); Object_type = new(pool) Type_privateData("@"); Selector_type = new(pool) Type_privateData(":"); #ifdef __APPLE__ - NSCFBoolean_ = objc_getClass("NSCFBoolean"); + // XXX: apparently, iOS now has both of these + NSCFBoolean_ = objc_getClass("__NSCFBoolean"); + if (NSCFBoolean_ == nil) + NSCFBoolean_ = objc_getClass("NSCFBoolean"); + NSCFType_ = objc_getClass("NSCFType"); NSGenericDeallocHandler_ = objc_getClass("__NSGenericDeallocHandler"); NSMessageBuilder_ = objc_getClass("NSMessageBuilder"); @@ -2405,6 +2553,7 @@ void CYObjectiveC_Initialize() { /*XXX*/ JSContextRef context(NULL); CYPoolTry { #endif NSArray_ = objc_getClass("NSArray"); + NSBlock_ = objc_getClass("NSBlock"); NSDictionary_ = objc_getClass("NSDictionary"); NSString_ = objc_getClass("NSString"); Object_ = objc_getClass("Object"); @@ -2421,10 +2570,23 @@ void CYObjectiveC_Initialize() { /*XXX*/ JSContextRef context(NULL); CYPoolTry { definition.deleteProperty = &Instance_deleteProperty; definition.getPropertyNames = &Instance_getPropertyNames; definition.callAsConstructor = &Instance_callAsConstructor; + definition.callAsFunction = &Instance_callAsFunction; definition.hasInstance = &Instance_hasInstance; definition.finalize = &CYFinalize; Instance_ = JSClassCreate(&definition); + definition.className = "ArrayInstance"; + ArrayInstance_ = JSClassCreate(&definition); + + definition.className = "ObjectInstance"; + ObjectInstance_ = JSClassCreate(&definition); + + definition.className = "StringInstance"; + StringInstance_ = JSClassCreate(&definition); + + definition.className = "FunctionInstance"; + FunctionInstance_ = JSClassCreate(&definition); + definition = kJSClassDefinitionEmpty; definition.className = "Internal"; definition.staticFunctions = Internal_staticFunctions; @@ -2500,10 +2662,6 @@ void CYObjectiveC_Initialize() { /*XXX*/ JSContextRef context(NULL); CYPoolTry { definition.getPropertyNames = &ObjectiveC_Protocols_getPropertyNames; ObjectiveC_Protocols_ = JSClassCreate(&definition); -#if defined(__APPLE__) && defined(__arm__) && 0 - MSHookFunction(&objc_registerClassPair, MSHake(objc_registerClassPair)); -#endif - #ifdef __APPLE__ class_addMethod(NSCFType_, @selector(cy$toJSON:), reinterpret_cast(&NSCFType$cy$toJSON), "@12@0:4@8"); #endif @@ -2543,19 +2701,25 @@ void CYObjectiveC_SetupContext(JSContextRef context) { CYPoolTry { JSObjectRef Instance_prototype(CYCastJSObject(context, CYGetProperty(context, Instance, prototype_s))); CYSetProperty(context, cy, CYJSString("Instance_prototype"), Instance_prototype); - JSObjectRef ArrayInstance(JSObjectMakeConstructor(context, Instance_, NULL)); + JSObjectRef ArrayInstance(JSObjectMakeConstructor(context, ArrayInstance_, NULL)); JSObjectRef ArrayInstance_prototype(CYCastJSObject(context, CYGetProperty(context, ArrayInstance, prototype_s))); CYSetProperty(context, cy, CYJSString("ArrayInstance_prototype"), ArrayInstance_prototype); JSObjectRef Array_prototype(CYGetCachedObject(context, CYJSString("Array_prototype"))); JSObjectSetPrototype(context, ArrayInstance_prototype, Array_prototype); - JSObjectRef ObjectInstance(JSObjectMakeConstructor(context, Instance_, NULL)); + JSObjectRef FunctionInstance(JSObjectMakeConstructor(context, FunctionInstance_, NULL)); + JSObjectRef FunctionInstance_prototype(CYCastJSObject(context, CYGetProperty(context, FunctionInstance, prototype_s))); + CYSetProperty(context, cy, CYJSString("FunctionInstance_prototype"), FunctionInstance_prototype); + JSObjectRef Function_prototype(CYGetCachedObject(context, CYJSString("Function_prototype"))); + JSObjectSetPrototype(context, FunctionInstance_prototype, Function_prototype); + + JSObjectRef ObjectInstance(JSObjectMakeConstructor(context, ObjectInstance_, NULL)); JSObjectRef ObjectInstance_prototype(CYCastJSObject(context, CYGetProperty(context, ObjectInstance, prototype_s))); CYSetProperty(context, cy, CYJSString("ObjectInstance_prototype"), ObjectInstance_prototype); JSObjectRef Object_prototype(CYGetCachedObject(context, CYJSString("Object_prototype"))); JSObjectSetPrototype(context, ObjectInstance_prototype, Object_prototype); - JSObjectRef StringInstance(JSObjectMakeConstructor(context, Instance_, NULL)); + JSObjectRef StringInstance(JSObjectMakeConstructor(context, StringInstance_, NULL)); JSObjectRef StringInstance_prototype(CYCastJSObject(context, CYGetProperty(context, StringInstance, prototype_s))); CYSetProperty(context, cy, CYJSString("StringInstance_prototype"), StringInstance_prototype); JSObjectRef String_prototype(CYGetCachedObject(context, CYJSString("String_prototype"))); @@ -2565,13 +2729,15 @@ void CYObjectiveC_SetupContext(JSContextRef context) { CYPoolTry { CYSetProperty(context, cycript, CYJSString("Selector"), Selector); CYSetProperty(context, cycript, CYJSString("Super"), Super); + JSObjectRef box(JSObjectMakeFunctionWithCallback(context, CYJSString("box"), &Instance_box_callAsFunction)); + CYSetProperty(context, Instance, CYJSString("box"), box); + #if defined(__APPLE__) && defined(__arm__) && 0 CYSetProperty(context, all, CYJSString("objc_registerClassPair"), &objc_registerClassPair_, kJSPropertyAttributeDontEnum); #endif CYSetProperty(context, all, CYJSString("objc_msgSend"), &$objc_msgSend, kJSPropertyAttributeDontEnum); - JSObjectRef Function_prototype(CYGetCachedObject(context, CYJSString("Function_prototype"))); JSObjectSetPrototype(context, CYCastJSObject(context, CYGetProperty(context, Message, prototype_s)), Function_prototype); JSObjectSetPrototype(context, CYCastJSObject(context, CYGetProperty(context, Selector, prototype_s)), Function_prototype); } CYPoolCatch() }