]> git.saurik.com Git - cycript.git/blobdiff - Library.mm
Added some common methods to Type class, implemented new Type(size), removed some...
[cycript.git] / Library.mm
index c4289e7086a8d937c709cae8f01709384b9ecfc4..d2c03fe24efdf5b6b36ac1e3c5b74f1573b528e3 100644 (file)
@@ -2107,8 +2107,9 @@ static JSObjectRef Type_callAsConstructor(JSContextRef context, JSObjectRef obje
         if (count > 1)
             @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to type cast function" userInfo:nil];
         Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(object)));
+        size_t size(count == 0 ? 0 : CYCastDouble(context, arguments[0]));
         // XXX: alignment?
-        void *value(malloc(internal->GetFFI()->size));
+        void *value(malloc(internal->GetFFI()->size * size));
         return CYMakePointer(context, value, internal->type_, NULL);
     } CYCatch
 }
@@ -2201,8 +2202,8 @@ static JSValueRef Selector_callAsFunction_toJSON(JSContextRef context, JSObjectR
 
 static JSValueRef Selector_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
     CYTry {
-        Selector_privateData *data(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
-        const char *name(sel_getName(data->GetValue()));
+        Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
+        const char *name(sel_getName(internal->GetValue()));
         CYPoolTry {
             return CYCastJSValue(context, CYJSString([NSString stringWithFormat:@"@selector(%s)", name]));
         } CYPoolCatch(NULL)
@@ -2211,14 +2212,13 @@ static JSValueRef Selector_callAsFunction_toCYON(JSContextRef context, JSObjectR
 
 static JSValueRef Selector_callAsFunction_type(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
     CYTry {
-        if (count != 2)
+        if (count != 1)
             @throw [NSException exceptionWithName:NSInvalidArgumentException reason:@"incorrect number of arguments to Selector.type" userInfo:nil];
         CYPool pool;
-        Selector_privateData *data(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
+        Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
         Class _class(CYCastNSObject(pool, context, arguments[0]));
-        bool instance(CYCastBool(context, arguments[1]));
-        SEL sel(data->GetValue());
-        if (Method method = (*(instance ? &class_getInstanceMethod : class_getClassMethod))(_class, sel))
+        SEL sel(internal->GetValue());
+        if (Method method = class_getInstanceMethod(_class, sel))
             return CYCastJSValue(context, method_getTypeEncoding(method));
         else if (NSString *type = [[Bridge_ objectAtIndex:1] objectForKey:CYCastNSString(pool, sel_getName(sel))])
             return CYCastJSValue(context, CYJSString(type));
@@ -2227,6 +2227,32 @@ static JSValueRef Selector_callAsFunction_type(JSContextRef context, JSObjectRef
     } CYCatch
 }
 
+static JSValueRef Type_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+    CYTry {
+        Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this)));
+        CYPool pool;
+        const char *type(sig::Unparse(pool, internal->type_));
+        CYPoolTry {
+            return CYCastJSValue(context, CYJSString(type));
+        } CYPoolCatch(NULL)
+    } CYCatch
+}
+
+static JSValueRef Type_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+    CYTry {
+        Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this)));
+        CYPool pool;
+        const char *type(sig::Unparse(pool, internal->type_));
+        CYPoolTry {
+            return CYCastJSValue(context, CYJSString([NSString stringWithFormat:@"new Type(%@)", [[NSString stringWithUTF8String:type] cy$toCYON]]));
+        } CYPoolCatch(NULL)
+    } CYCatch
+}
+
+static JSValueRef Type_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
+    return Type_callAsFunction_toString(context, object, _this, count, arguments, exception);
+}
+
 static JSStaticValue CYValue_staticValues[2] = {
     {"value", &CYValue_getProperty_value, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete},
     {NULL, NULL, NULL, 0}
@@ -2266,6 +2292,13 @@ static JSStaticFunction Selector_staticFunctions[5] = {
     {NULL, NULL, 0}
 };
 
+static JSStaticFunction Type_staticFunctions[5] = {
+    {"toCYON", &Type_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+    {"toJSON", &Type_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+    {"toString", &Type_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+    {NULL, NULL, 0}
+};
+
 CYDriver::CYDriver(const std::string &filename) :
     state_(CYClear),
     data_(NULL),
@@ -2365,10 +2398,7 @@ struct CYExecute_ {
 
 - (void) execute:(NSValue *)value {
     CYExecute_ *execute(reinterpret_cast<CYExecute_ *>([value pointerValue]));
-    NSLog(@"b:%p", execute->data_);
-    NSLog(@"s:%s", execute->data_);
     execute->data_ = CYExecute(execute->pool_, execute->data_);
-    NSLog(@"a:%p", execute->data_);
 }
 
 @end
@@ -2537,6 +2567,7 @@ JSGlobalContextRef CYGetJSContext() {
 
         definition = kJSClassDefinitionEmpty;
         definition.className = "Type";
+        definition.staticFunctions = Type_staticFunctions;
         definition.callAsFunction = &Type_callAsFunction;
         definition.callAsConstructor = &Type_callAsConstructor;
         definition.finalize = &CYData::Finalize;