]> git.saurik.com Git - cycript.git/blobdiff - ObjectiveC/Library.mm
Only NSBlock should be a "function", not NSObject.
[cycript.git] / ObjectiveC / Library.mm
index b6410c7471507cddbd3e158379ab6861bcac5ca2..6f47cbf04035a127a3c96d62b8caf11528469644 100644 (file)
@@ -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_;
 
@@ -294,6 +296,7 @@ static JSClassRef ObjectiveC_Images_;
 #endif
 
 #ifdef __APPLE__
+static Class __NSMallocBlock__;
 static Class NSCFBoolean_;
 static Class NSCFType_;
 static Class NSGenericDeallocHandler_;
@@ -307,6 +310,7 @@ static Class NSBoolNumber_;
 static Class NSArray_;
 static Class NSBlock_;
 static Class NSDictionary_;
+static Class NSNumber_;
 static Class NSString_;
 static Class Object_;
 
@@ -343,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_)
@@ -383,16 +395,14 @@ JSObjectRef Super::Make(JSContextRef context, id object, Class _class) {
 } }
 
 JSObjectRef Instance::Make(JSContextRef context, id object, Flags flags) {
-    JSObjectRef value(JSObjectMake(context, Instance_, new Instance(object, flags)));
+    JSObjectRef value(JSObjectMake(context, [object isKindOfClass:NSBlock_] ? FunctionInstance_ : Instance_, new Instance(object, flags)));
     JSObjectSetPrototype(context, value, CYGetClassPrototype(context, object_getClass(object)));
     return value;
 }
 
 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 :
@@ -631,7 +641,7 @@ struct PropertyAttributes {
 /* }}} */
 
 _finline bool CYJSValueIsNSObject(JSContextRef context, JSValueRef value) {
-    return JSValueIsObjectOfClass(context, value, Instance_);
+    return JSValueIsObjectOfClass(context, value, Instance_) || JSValueIsObjectOfClass(context, value, FunctionInstance_);
 }
 
 _finline bool CYJSValueIsInstanceOfCachedConstructor(JSContextRef context, JSValueRef value, JSStringRef cache) {
@@ -662,6 +672,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 +681,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 +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 {
@@ -1107,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 {{{ */
@@ -1624,11 +1648,8 @@ static bool Messages_setProperty(JSContextRef context, JSObjectRef object, JSStr
 
     CYPool pool;
     const char *name(CYPoolCString(pool, context, property));
-
     SEL sel(sel_registerName(name));
 
-    objc_method *method(class_getInstanceMethod(_class, sel));
-
     const char *type;
     IMP imp;
 
@@ -1637,10 +1658,30 @@ static bool Messages_setProperty(JSContextRef context, JSObjectRef object, JSStr
         type = sig::Unparse(pool, &message->signature_);
         imp = reinterpret_cast<IMP>(message->GetValue());
     } else {
+        objc_method *method(class_getInstanceMethod(_class, sel));
         type = CYPoolTypeEncoding(pool, context, sel, method);
         imp = CYMakeMessage(context, value, type);
     }
 
+    objc_method *method(NULL);
+#if OBJC_API_VERSION >= 2
+    unsigned int size;
+    objc_method **methods(class_copyMethodList(_class, &size));
+    for (size_t i(0); i != size; ++i)
+        if (sel_isEqual(method_getName(methods[i]), sel)) {
+            method = methods[i];
+            break;
+        }
+    free(methods);
+#else
+    for (objc_method_list *methods(_class->methods); methods != NULL; methods = methods->method_next)
+        for (int i(0); i != methods->method_count; ++i)
+            if (sel_isEqual(method_getName(&methods->method_list[i]), sel)) {
+                method = &methods->method_list[i];
+                break;
+            }
+#endif
+
     if (method != NULL)
         method_setImplementation(method, imp);
     else {
@@ -1903,42 +1944,37 @@ static JSObjectRef Instance_callAsConstructor(JSContextRef context, JSObjectRef
     return value;
 } CYCatch(NULL) }
 
-static JSValueRef Instance_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
-    Instance *internal(reinterpret_cast<Instance *>(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
-
+static const char *CYBlockEncoding(NSBlock *self) {
     BlockLiteral *literal(reinterpret_cast<BlockLiteral *>(self));
+    if ((literal->flags & BLOCK_HAS_SIGNATURE) == 0)
+        return NULL;
+    uint8_t *descriptor(reinterpret_cast<uint8_t *>(literal->descriptor));
+    descriptor += sizeof(BlockDescriptor1);
+    if ((literal->flags & BLOCK_HAS_COPY_DISPOSE) != 0)
+        descriptor += sizeof(BlockDescriptor2);
+    BlockDescriptor3 *descriptor3(reinterpret_cast<BlockDescriptor3 *>(descriptor));
+    return descriptor3->signature;
+}
 
-    if ((literal->flags & BLOCK_HAS_SIGNATURE) != 0) {
-        uint8_t *descriptor(reinterpret_cast<uint8_t *>(literal->descriptor));
-        descriptor += sizeof(BlockDescriptor1);
-        if ((literal->flags & BLOCK_HAS_COPY_DISPOSE) != 0)
-            descriptor += sizeof(BlockDescriptor2);
-        BlockDescriptor3 *descriptor3(reinterpret_cast<BlockDescriptor3 *>(descriptor));
+static JSValueRef FunctionInstance_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+    Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
+    id self(internal->GetValue());
 
-        if (const char *type = descriptor3->signature) {
-            CYPool pool;
+    if (const char *encoding = CYBlockEncoding(self)) {
+        CYPool pool;
 
-            void *setup[1];
-            setup[0] = &self;
+        void *setup[1];
+        setup[0] = &self;
 
-            sig::Signature signature;
-            sig::Parse(pool, &signature, type, &Structor_);
+        sig::Signature signature;
+        sig::Parse(pool, &signature, encoding, &Structor_);
 
-            ffi_cif cif;
-            sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif);
+        ffi_cif cif;
+        sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif);
 
-            void (*function)() = reinterpret_cast<void (*)()>(literal->invoke);
-            return CYCallFunction(pool, context, 1, setup, count, arguments, false, &signature, &cif, function);
-        }
+        BlockLiteral *literal(reinterpret_cast<BlockLiteral *>(self));
+        void (*function)() = reinterpret_cast<void (*)()>(literal->invoke);
+        return CYCallFunction(pool, context, 1, setup, count, arguments, false, &signature, &cif, function);
     }
 
     if (count != 0)
@@ -2290,6 +2326,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]));
 
@@ -2491,7 +2529,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]));
@@ -2750,10 +2788,13 @@ 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");
 
 #ifdef __APPLE__
+    __NSMallocBlock__ = objc_getClass("__NSMallocBlock__");
+
     // XXX: apparently, iOS now has both of these
     NSCFBoolean_ = objc_getClass("__NSCFBoolean");
     if (NSCFBoolean_ == nil)
@@ -2784,7 +2825,6 @@ 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);
@@ -2792,9 +2832,16 @@ void CYObjectiveC_Initialize() { /*XXX*/ JSContextRef context(NULL); CYPoolTry {
     definition.className = "ArrayInstance";
     ArrayInstance_ = JSClassCreate(&definition);
 
+    definition.className = "BooleanInstance";
+    BooleanInstance_ = JSClassCreate(&definition);
+
     definition.className = "FunctionInstance";
+    definition.callAsFunction = &FunctionInstance_callAsFunction;
     FunctionInstance_ = JSClassCreate(&definition);
 
+    definition.className = "NumberInstance";
+    NumberInstance_ = JSClassCreate(&definition);
+
     definition.className = "ObjectInstance";
     ObjectInstance_ = JSClassCreate(&definition);
 
@@ -2819,6 +2866,7 @@ void CYObjectiveC_Initialize() { /*XXX*/ JSContextRef context(NULL); CYPoolTry {
     definition = kJSClassDefinitionEmpty;
     definition.className = "Message";
     definition.staticFunctions = cy::Functor::StaticFunctions;
+    definition.staticValues = cy::Functor::StaticValues;
     definition.callAsFunction = &Message_callAsFunction;
     definition.finalize = &CYFinalize;
     Message_ = JSClassCreate(&definition);
@@ -2932,12 +2980,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);
@@ -2956,10 +3016,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);