X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/7a3459cee18e9e63919801c74175baab3e0ae0f8..654bf401ac7c3063b503a56e4726317aeab9f6ee:/ObjectiveC/Library.mm diff --git a/ObjectiveC/Library.mm b/ObjectiveC/Library.mm index 3cf9e8a..6c7bbd2 100644 --- a/ObjectiveC/Library.mm +++ b/ObjectiveC/Library.mm @@ -273,7 +273,9 @@ bool CYGetOffset(CYPool &pool, JSContextRef context, NSString *value, ssize_t &i static JSClassRef Instance_; static JSClassRef ArrayInstance_; +static JSClassRef BooleanInstance_; static JSClassRef FunctionInstance_; +static JSClassRef NumberInstance_; static JSClassRef ObjectInstance_; static JSClassRef StringInstance_; @@ -308,6 +310,7 @@ static Class NSBoolNumber_; static Class NSArray_; static Class NSBlock_; static Class NSDictionary_; +static Class NSNumber_; static Class NSString_; static Class Object_; @@ -344,10 +347,18 @@ JSValueRef CYGetClassPrototype(JSContextRef context, Class self, bool meta) { JSClassRef _class(NULL); JSValueRef prototype; - if (self == NSArray_) +#ifdef __APPLE__ + if (self == NSCFBoolean_) +#else + if (self == NSBoolNumber_) +#endif + prototype = CYGetCachedObject(context, CYJSString("BooleanInstance_prototype")); + else if (self == NSArray_) prototype = CYGetCachedObject(context, CYJSString("ArrayInstance_prototype")); else if (self == NSBlock_) prototype = CYGetCachedObject(context, CYJSString("FunctionInstance_prototype")); + else if (self == NSNumber_) + prototype = CYGetCachedObject(context, CYJSString("NumberInstance_prototype")); else if (self == NSDictionary_) prototype = CYGetCachedObject(context, CYJSString("ObjectInstance_prototype")); else if (self == NSString_) @@ -391,9 +402,7 @@ JSObjectRef Instance::Make(JSContextRef context, id object, Flags flags) { Instance::~Instance() { if ((flags_ & Transient) == 0) - // XXX: does this handle background threads correctly? - // XXX: this simply does not work on the console because I'm stupid - [GetValue() performSelector:@selector(release) withObject:nil afterDelay:0]; + [GetValue() release]; } struct Message_privateData : @@ -1068,7 +1077,7 @@ NSObject *CYCopyNSObject(CYPool &pool, JSContextRef context, JSValueRef value) { } - (NSString *) cy$toCYON:(bool)objective { - return [[self description] cy$toCYON:objective]; + return [@"#" stringByAppendingString:[[self description] cy$toCYON:true]]; } - (bool) cy$hasProperty:(NSString *)name { @@ -1109,6 +1118,19 @@ NSObject *CYCopyNSObject(CYPool &pool, JSContextRef context, JSValueRef value) { return [[self description] cy$toCYON:objective]; } +@end +/* }}} */ +/* Bridge: NSSet {{{ */ +@implementation NSSet (Cycript) + +- (NSString *) cy$toCYON:(bool)objective { + NSMutableString *json([[[NSMutableString alloc] init] autorelease]); + [json appendString:@"[NSSet setWithArray:"]; + [json appendString:CYCastNSCYON([self allObjects], true)]; + [json appendString:@"]]"]; + return json; +} + @end /* }}} */ /* Bridge: NSString {{{ */ @@ -2292,6 +2314,8 @@ static JSValueRef choose(JSContextRef context, JSObjectRef object, JSObjectRef _ if (count != 1) throw CYJSError(context, "choose() takes a class argument"); + CYGarbageCollect(context); + CYPool pool; Class _class(CYCastNSObject(&pool, context, arguments[0])); @@ -2752,6 +2776,7 @@ void CYObjectiveC_Initialize() { /*XXX*/ JSContextRef context(NULL); CYPoolTry { NSArray_ = objc_getClass("NSArray"); NSBlock_ = objc_getClass("NSBlock"); NSDictionary_ = objc_getClass("NSDictionary"); + NSNumber_ = objc_getClass("NSNumber"); NSString_ = objc_getClass("NSString"); Object_ = objc_getClass("Object"); @@ -2796,9 +2821,15 @@ void CYObjectiveC_Initialize() { /*XXX*/ JSContextRef context(NULL); CYPoolTry { definition.className = "ArrayInstance"; ArrayInstance_ = JSClassCreate(&definition); + definition.className = "BooleanInstance"; + BooleanInstance_ = JSClassCreate(&definition); + definition.className = "FunctionInstance"; FunctionInstance_ = JSClassCreate(&definition); + definition.className = "NumberInstance"; + NumberInstance_ = JSClassCreate(&definition); + definition.className = "ObjectInstance"; ObjectInstance_ = JSClassCreate(&definition); @@ -2936,12 +2967,24 @@ void CYObjectiveC_SetupContext(JSContextRef context) { CYPoolTry { JSObjectRef Array_prototype(CYGetCachedObject(context, CYJSString("Array_prototype"))); JSObjectSetPrototype(context, ArrayInstance_prototype, Array_prototype); + JSObjectRef BooleanInstance(JSObjectMakeConstructor(context, BooleanInstance_, NULL)); + JSObjectRef BooleanInstance_prototype(CYCastJSObject(context, CYGetProperty(context, BooleanInstance, prototype_s))); + CYSetProperty(context, cy, CYJSString("BooleanInstance_prototype"), BooleanInstance_prototype); + JSObjectRef Boolean_prototype(CYGetCachedObject(context, CYJSString("Boolean_prototype"))); + JSObjectSetPrototype(context, BooleanInstance_prototype, Boolean_prototype); + 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 NumberInstance(JSObjectMakeConstructor(context, NumberInstance_, NULL)); + JSObjectRef NumberInstance_prototype(CYCastJSObject(context, CYGetProperty(context, NumberInstance, prototype_s))); + CYSetProperty(context, cy, CYJSString("NumberInstance_prototype"), NumberInstance_prototype); + JSObjectRef Number_prototype(CYGetCachedObject(context, CYJSString("Number_prototype"))); + JSObjectSetPrototype(context, NumberInstance_prototype, Number_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); @@ -2963,7 +3006,7 @@ void CYObjectiveC_SetupContext(JSContextRef context) { CYPoolTry { CYSetProperty(context, cycript, CYJSString("objc_super"), Super); JSObjectRef box(JSObjectMakeFunctionWithCallback(context, CYJSString("box"), &Instance_box_callAsFunction)); - CYSetProperty(context, Instance, CYJSString("box"), box); + CYSetProperty(context, Instance, CYJSString("box"), box, kJSPropertyAttributeDontEnum); #ifdef __APPLE__ CYSetProperty(context, all, CYJSString("choose"), &choose, kJSPropertyAttributeDontEnum);