]> git.saurik.com Git - cycript.git/blobdiff - ObjectiveC/Library.mm
Add a new ?gc to run GC without running a script.
[cycript.git] / ObjectiveC / Library.mm
index 268edcd87741811534fb09bd0cd7b9c304aa7dea..742e3ef481e0b9857b012a17cb210ca9a3c80827 100644 (file)
@@ -294,6 +294,7 @@ static JSClassRef ObjectiveC_Images_;
 #endif
 
 #ifdef __APPLE__
+static Class __NSMallocBlock__;
 static Class NSCFBoolean_;
 static Class NSCFType_;
 static Class NSGenericDeallocHandler_;
@@ -662,6 +663,7 @@ static void BlockClosure_(ffi_cif *cif, void *result, void **arguments, void *ar
 }
 
 NSObject *CYMakeBlock(JSContextRef context, JSObjectRef function, sig::Signature &signature) {
+    _assert(__NSMallocBlock__ != Nil);
     BlockLiteral *literal(reinterpret_cast<BlockLiteral *>(malloc(sizeof(BlockLiteral))));
 
     CYBlockDescriptor *descriptor(new CYBlockDescriptor);
@@ -670,7 +672,7 @@ NSObject *CYMakeBlock(JSContextRef context, JSObjectRef function, sig::Signature
     descriptor->internal_ = CYMakeFunctor_(context, function, signature, &BlockClosure_);
     literal->invoke = reinterpret_cast<void (*)(void *, ...)>(descriptor->internal_->GetValue());
 
-    literal->isa = objc_getClass("__NSMallocBlock__");
+    literal->isa = __NSMallocBlock__;
     literal->flags = BLOCK_HAS_SIGNATURE | BLOCK_HAS_COPY_DISPOSE | BLOCK_IS_GLOBAL;
     literal->reserved = 0;
     literal->descriptor = descriptor;
@@ -1066,7 +1068,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 {
@@ -1107,6 +1109,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 {{{ */
@@ -2491,7 +2506,7 @@ static JSValueRef Message_callAsFunction(JSContextRef context, JSObjectRef objec
 
 static JSObjectRef Super_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
     if (count != 2)
-        throw CYJSError(context, "incorrect number of arguments to Super constructor");
+        throw CYJSError(context, "incorrect number of arguments to objc_super constructor");
     CYPool pool;
     id self(CYCastNSObject(&pool, context, arguments[0]));
     Class _class(CYCastClass(pool, context, arguments[1]));
@@ -2754,6 +2769,8 @@ void CYObjectiveC_Initialize() { /*XXX*/ JSContextRef context(NULL); CYPoolTry {
     Object_ = objc_getClass("Object");
 
 #ifdef __APPLE__
+    __NSMallocBlock__ = objc_getClass("__NSMallocBlock__");
+
     // XXX: apparently, iOS now has both of these
     NSCFBoolean_ = objc_getClass("__NSCFBoolean");
     if (NSCFBoolean_ == nil)
@@ -2956,10 +2973,10 @@ void CYObjectiveC_SetupContext(JSContextRef context) { CYPoolTry {
 
     CYSetProperty(context, cycript, CYJSString("Instance"), Instance);
     CYSetProperty(context, cycript, CYJSString("Selector"), Selector);
-    CYSetProperty(context, cycript, CYJSString("Super"), Super);
+    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);
@@ -2988,3 +3005,25 @@ struct CYObjectiveC {
         _assert(hooks_ != NULL);
     }
 } CYObjectiveC;
+
+extern "C" void CydgetSetupContext(JSGlobalContextRef context) { CYObjectiveTry_ {
+    CYSetupContext(context);
+} CYObjectiveCatch }
+
+extern "C" void CydgetMemoryParse(const uint16_t **data, size_t *size) { try {
+    CYPool pool;
+
+    CYUTF8String utf8(CYPoolUTF8String(pool, CYUTF16String(*data, *size)));
+    utf8 = CYPoolCode(pool, utf8);
+
+    CYUTF16String utf16(CYPoolUTF16String(pool, CYUTF8String(utf8.data, utf8.size)));
+    size_t bytes(utf16.size * sizeof(uint16_t));
+    uint16_t *copy(reinterpret_cast<uint16_t *>(malloc(bytes)));
+    memcpy(copy, utf16.data, bytes);
+
+    *data = copy;
+    *size = utf16.size;
+} catch (const CYException &exception) {
+    CYPool pool;
+    @throw [NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"%s", exception.PoolCString(pool)] userInfo:nil];
+} }