From: Jay Freeman (saurik) <saurik@saurik.com>
Date: Fri, 14 Sep 2012 14:14:17 +0000 (-0700)
Subject: Provide a .typeOf() to get a Type of an ObjC Class.
X-Git-Tag: v0.9.460~17
X-Git-Url: https://git.saurik.com/cycript.git/commitdiff_plain/80ea020e4c9cdd505ec355ae2ca1495e1110e7ea

Provide a .typeOf() to get a Type of an ObjC Class.
---

diff --git a/ObjectiveC/Library.mm b/ObjectiveC/Library.mm
index 3976d54..379a1a2 100644
--- a/ObjectiveC/Library.mm
+++ b/ObjectiveC/Library.mm
@@ -2412,6 +2412,28 @@ static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjec
     } CYPoolCatch(NULL)
 } CYCatch return /*XXX*/ NULL; }
 
+static JSValueRef Instance_callAsFunction_typeOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+    if (!CYJSValueIsNSObject(context, _this))
+        return NULL;
+
+    Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
+    id value(internal->GetValue());
+
+    if (!CYIsClass(value))
+        CYThrow("non-Class object cannot be used as Type");
+
+    // XXX: this is a very silly implementation
+
+    std::ostringstream type;
+    type << "@\"";
+    type << class_getName(value);
+    type << "\"";
+
+    CYPoolTry {
+        return CYMakeType(context, type.str().c_str());
+    } CYPoolCatch(NULL)
+} CYCatch return /*XXX*/ NULL; }
+
 static JSValueRef Selector_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
     Selector_privateData *internal(reinterpret_cast<Selector_privateData *>(JSObjectGetPrivate(_this)));
     return CYCastJSValue(context, sel_getName(internal->GetValue()));
@@ -2464,13 +2486,14 @@ static JSStaticValue Instance_staticValues[5] = {
     {NULL, NULL, NULL, 0}
 };
 
-static JSStaticFunction Instance_staticFunctions[7] = {
+static JSStaticFunction Instance_staticFunctions[8] = {
     {"$cya", &CYValue_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
     {"toCYON", &Instance_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
     {"toJSON", &Instance_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
     {"valueOf", &Instance_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
     {"toPointer", &Instance_callAsFunction_toPointer, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
     {"toString", &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+    {"typeOf", &Instance_callAsFunction_typeOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
     {NULL, NULL, 0}
 };