]> git.saurik.com Git - cycript.git/blobdiff - ObjectiveC/Library.mm
As FunctionInstance is different, it must be last.
[cycript.git] / ObjectiveC / Library.mm
index 6c7bbd252348221fe61119ecb3437cd70f9457a0..33ccaf0dade7df56081a8be618f57617c62fa3b5 100644 (file)
@@ -395,7 +395,7 @@ 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;
 }
@@ -641,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) {
@@ -1648,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;
 
@@ -1661,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 {
@@ -1927,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)
@@ -2303,8 +2315,9 @@ static void choose_(task_t task, void *baton, unsigned type, vm_range_t *ranges,
         if (result == choice->query_.end())
             continue;
 
-        // XXX: if (size < class_getInstanceSize(*result))
-        if ((class_getInstanceSize(*result) + 15) / 16 * 16 != size)
+        size_t needed(class_getInstanceSize(*result));
+        // XXX: if (size < needed)
+        if (needed <= 496 && (needed + 15) / 16 * 16 != size || needed > 496 && (needed + 511) / 512 * 512 != size)
             continue;
         CYArrayPush(context, choice->results_, CYCastJSValue(context, reinterpret_cast<id>(data)));
     }
@@ -2562,6 +2575,18 @@ static JSValueRef CYValue_callAsFunction_$cya(JSContextRef context, JSObjectRef
     return CYMakePointer(context, &internal->value_, _not(size_t), type, ffi, object);
 } CYCatch(NULL) }
 
+static JSValueRef FunctionInstance_getProperty_type(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
+    Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
+    const char *encoding(CYBlockEncoding(internal->GetValue()));
+    if (encoding == NULL)
+        return CYJSNull(context);
+    // XXX: this should be stored on a FunctionInstance private value subclass
+    CYPool pool;
+    sig::Signature signature;
+    sig::Parse(pool, &signature, encoding, &Structor_);
+    return CYMakeType(context, &signature);
+} CYCatch(NULL) }
+
 static JSValueRef Instance_getProperty_constructor(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
     Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
     return Instance::Make(context, (id) object_getClass(internal->GetValue()));
@@ -2721,6 +2746,7 @@ static JSStaticValue Selector_staticValues[2] = {
     {NULL, NULL, NULL, 0}
 };
 
+// XXX: this is sadly duplicated in FunctionInstance_staticValues
 static JSStaticValue Instance_staticValues[5] = {
     {"constructor", &Instance_getProperty_constructor, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
     {"messages", &Instance_getProperty_messages, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
@@ -2729,6 +2755,16 @@ static JSStaticValue Instance_staticValues[5] = {
     {NULL, NULL, NULL, 0}
 };
 
+static JSStaticValue FunctionInstance_staticValues[6] = {
+    {"type", &FunctionInstance_getProperty_type, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+    // XXX: this is sadly a duplicate of Instance_staticValues
+    {"constructor", &Instance_getProperty_constructor, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+    {"messages", &Instance_getProperty_messages, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+    {"prototype", &Instance_getProperty_prototype, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+    {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+    {NULL, NULL, NULL, 0}
+};
+
 static JSStaticFunction Instance_staticFunctions[7] = {
     {"$cya", &CYValue_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
     {"toCYON", &Instance_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
@@ -2813,7 +2849,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);
@@ -2824,9 +2859,6 @@ void CYObjectiveC_Initialize() { /*XXX*/ JSContextRef context(NULL); CYPoolTry {
     definition.className = "BooleanInstance";
     BooleanInstance_ = JSClassCreate(&definition);
 
-    definition.className = "FunctionInstance";
-    FunctionInstance_ = JSClassCreate(&definition);
-
     definition.className = "NumberInstance";
     NumberInstance_ = JSClassCreate(&definition);
 
@@ -2836,6 +2868,11 @@ void CYObjectiveC_Initialize() { /*XXX*/ JSContextRef context(NULL); CYPoolTry {
     definition.className = "StringInstance";
     StringInstance_ = JSClassCreate(&definition);
 
+    definition.className = "FunctionInstance";
+    definition.staticValues = FunctionInstance_staticValues;
+    definition.callAsFunction = &FunctionInstance_callAsFunction;
+    FunctionInstance_ = JSClassCreate(&definition);
+
     definition = kJSClassDefinitionEmpty;
     definition.className = "Class";
     definition.staticFunctions = Class_staticFunctions;
@@ -2854,6 +2891,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);