]> git.saurik.com Git - cycript.git/blobdiff - ObjectiveC/Library.mm
Use CYStringify in Type.toCYON to handle quotations.
[cycript.git] / ObjectiveC / Library.mm
index f8d83f0c68dd0dba7a956c34ba06f86446866ce0..77511f8508b90b283a0d60bdf129de0e62a281bb 100644 (file)
@@ -128,6 +128,41 @@ static void (*$objc_setAssociatedObject)(id object, void *key, id value, objc_As
 static id (*$objc_getAssociatedObject)(id object, void *key);
 static void (*$objc_removeAssociatedObjects)(id object);
 
+struct BlockLiteral {
+    Class isa;
+    int flags;
+    int reserved;
+    void (*invoke)(void *, ...);
+    void *descriptor;
+};
+
+struct BlockDescriptor1 {
+    unsigned long int reserved;
+    unsigned long int size;
+};
+
+struct BlockDescriptor2 {
+    void (*copy_helper)(BlockLiteral *dst, BlockLiteral *src);
+    void (*dispose_helper)(BlockLiteral *src);
+};
+
+struct BlockDescriptor3 {
+    const char *signature;
+    const char *layout;
+};
+
+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,
+};
+
 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 {{{ */
@@ -610,12 +645,45 @@ _finline bool CYJSValueIsInstanceOfCachedConstructor(JSContextRef context, JSVal
     return is;
 }
 
+NSObject *CYMakeBlock(void (*invoke)(), sig::Signature &signature) {
+    BlockLiteral *literal(reinterpret_cast<BlockLiteral *>(malloc(sizeof(BlockLiteral))));
+
+    struct Descriptor {
+        struct {
+            BlockDescriptor1 one_;
+            BlockDescriptor2 two_;
+            BlockDescriptor3 three_;
+        } d_;
+
+        CYPool pool_;
+    };
+
+    Descriptor *descriptor(new Descriptor);
+    memset(&descriptor->d_, 0, sizeof(descriptor->d_));
+
+    literal->isa = objc_getClass("__NSGlobalBlock__");
+    literal->flags = BLOCK_HAS_SIGNATURE | BLOCK_HAS_COPY_DISPOSE | BLOCK_IS_GLOBAL;
+    literal->reserved = 0;
+    literal->invoke = reinterpret_cast<void (*)(void *, ...)>(invoke);
+    literal->descriptor = descriptor;
+
+    descriptor->d_.one_.size = sizeof(descriptor->d_);
+    descriptor->d_.three_.signature = sig::Unparse(descriptor->pool_, &signature);
+
+    return reinterpret_cast<NSObject *>(literal);
+}
+
 NSObject *CYCastNSObject(apr_pool_t *pool, JSContextRef context, JSObjectRef object) {
     if (CYJSValueIsNSObject(context, object)) {
         Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
         return internal->GetValue();
     }
 
+    if (JSValueIsObjectOfClass(context, object, Functor_)) {
+        cy::Functor *internal(reinterpret_cast<cy::Functor *>(JSObjectGetPrivate(object)));
+        return CYMakeBlock(internal->GetValue(), internal->signature_);
+    }
+
     bool array(CYJSValueIsInstanceOfCachedConstructor(context, object, Array_s));
     id value(array ? [CYJSArray alloc] : [CYJSObject alloc]);
     return CYPoolRelease(pool, [value initWithJSObject:object inContext:context]);
@@ -1485,21 +1553,13 @@ static const char *CYPoolTypeEncoding(apr_pool_t *pool, JSContextRef context, SE
     return NULL;
 }
 
-static void MessageClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) {
-    Closure_privateData *internal(reinterpret_cast<Closure_privateData *>(arg));
-
-    JSContextRef context(internal->context_);
-
-    size_t count(internal->cif_.nargs);
-    JSValueRef values[count];
-
-    for (size_t index(0); index != count; ++index)
-        values[index] = CYFromFFI(context, internal->signature_.elements[1 + index].type, internal->cif_.arg_types[index], arguments[index]);
-
+static JSValueRef MessageAdapter_(JSContextRef context, size_t count, JSValueRef values[], JSObjectRef function) {
     JSObjectRef _this(CYCastJSObject(context, values[0]));
+    return CYCallAsFunction(context, function, _this, count - 2, values + 2);
+}
 
-    JSValueRef value(CYCallAsFunction(context, internal->function_, _this, count - 2, values + 2));
-    CYPoolFFI(NULL, context, internal->signature_.elements[0].type, internal->cif_.rtype, result, value);
+static void MessageClosure_(ffi_cif *cif, void *result, void **arguments, void *arg) {
+    CYExecuteClosure(cif, result, arguments, arg, &MessageAdapter_);
 }
 
 static JSObjectRef CYMakeMessage(JSContextRef context, SEL sel, IMP imp, const char *type) {
@@ -1839,40 +1899,7 @@ static JSValueRef Instance_callAsFunction(JSContextRef context, JSObjectRef obje
     // 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<BlockLiteral *>(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,
-    };
+    BlockLiteral *literal(reinterpret_cast<BlockLiteral *>(self));
 
     if ((literal->flags & BLOCK_HAS_SIGNATURE) != 0) {
         uint8_t *descriptor(reinterpret_cast<uint8_t *>(literal->descriptor));