]> git.saurik.com Git - cycript.git/blobdiff - ObjectiveC/Library.mm
Fix issue in 64-bit choose() reported by heardrwt.
[cycript.git] / ObjectiveC / Library.mm
index c4759a5a2c949c425f21e37370234027ba2d2916..a685816ecbb22f3d3c30d86494575f019da97150 100644 (file)
@@ -40,6 +40,7 @@
 #include <mach/mach.h>
 #endif
 
+#include "Code.hpp"
 #include "Error.hpp"
 #include "JavaScript.hpp"
 #include "String.hpp"
@@ -1469,8 +1470,10 @@ static SEL CYCastSEL(JSContextRef context, JSValueRef value) {
     if (JSValueIsObjectOfClass(context, value, Selector_)) {
         Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate((JSObjectRef) value)));
         return reinterpret_cast<SEL>(internal->value_);
-    } else
-        return CYCastPointer<SEL>(context, value);
+    } else {
+        CYPool pool;
+        return sel_registerName(CYPoolCString(pool, context, value));
+    }
 }
 
 void *CYObjectiveC_ExecuteStart(JSContextRef context) { CYSadTry {
@@ -1600,24 +1603,6 @@ static bool CYImplements(id object, Class _class, SEL selector, bool devoid = fa
     return false;
 }
 
-static const char *CYPoolTypeEncoding(CYPool &pool, JSContextRef context, SEL sel, objc_method *method) {
-    if (method != NULL)
-        return method_getTypeEncoding(method);
-
-    const char *name(sel_getName(sel));
-    size_t length(strlen(name));
-
-    char keyed[length + 2];
-    keyed[0] = '6';
-    keyed[length + 1] = '\0';
-    memcpy(keyed + 1, name, length);
-
-    if (CYBridgeEntry *entry = CYBridgeHash(keyed, length + 1))
-        return entry->value_;
-
-    return NULL;
-}
-
 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);
@@ -1685,11 +1670,10 @@ static bool Messages_setProperty(JSContextRef context, JSObjectRef object, JSStr
         Message_privateData *message(reinterpret_cast<Message_privateData *>(JSObjectGetPrivate((JSObjectRef) value)));
         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);
+    } else if (objc_method *method = class_getInstanceMethod(_class, sel)) {
+        type = method_getTypeEncoding(method);
         imp = CYMakeMessage(context, value, type);
-    }
+    } else _assert(false);
 
     objc_method *method(NULL);
 #if OBJC_API_VERSION >= 2
@@ -2350,7 +2334,12 @@ static void choose_(task_t task, void *baton, unsigned type, vm_range_t *ranges,
 
         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)
+
+        size_t boundary(496);
+#ifdef __LP64__
+        boundary *= 2;
+#endif
+        if (needed <= boundary && (needed + 15) / 16 * 16 != size || needed > boundary && (needed + 511) / 512 * 512 != size)
             continue;
         CYArrayPush(context, choice->results_, CYCastJSValue(context, reinterpret_cast<id>(data)));
     }
@@ -2759,15 +2748,9 @@ static JSValueRef Selector_callAsFunction_type(JSContextRef context, JSObjectRef
     Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
     SEL sel(internal->GetValue());
 
-    objc_method *method;
-    if (Class _class = CYCastClass(pool, context, arguments[0]))
-        method = class_getInstanceMethod(_class, sel);
-    else
-        method = NULL;
-
-    const char *encoding(CYPoolTypeEncoding(pool, context, sel, method));
-    if (encoding == NULL)
-        return CYJSNull(context);
+    Class _class(_require(CYCastClass(pool, context, arguments[0])));
+    objc_method *method(_require(class_getInstanceMethod(_class, sel)));
+    const char *encoding(method_getTypeEncoding(method));
 
     sig::Signature signature;
     sig::Parse(pool, &signature, encoding, &Structor_);
@@ -3116,7 +3099,8 @@ 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);
+    CYStream stream(utf8.data, utf8.data + utf8.size);
+    utf8 = CYPoolCode(pool, stream);
 
     CYUTF16String utf16(CYPoolUTF16String(pool, CYUTF8String(utf8.data, utf8.size)));
     size_t bytes(utf16.size * sizeof(uint16_t));