]> git.saurik.com Git - cycript.git/commitdiff
Add a concept of .toPointer().
authorJay Freeman (saurik) <saurik@saurik.com>
Thu, 4 Mar 2010 07:03:21 +0000 (07:03 +0000)
committerJay Freeman (saurik) <saurik@saurik.com>
Thu, 4 Mar 2010 07:03:21 +0000 (07:03 +0000)
Execute.cpp
ObjectiveC/Library.mm

index b781a397ecd18725f8fde8261edfd8a8e8497a2e..8ac1b14657925697d8624e9c56de008a7d014a9c 100644 (file)
@@ -165,6 +165,7 @@ JSStringRef push_s;
 JSStringRef splice_s;
 JSStringRef toCYON_s;
 JSStringRef toJSON_s;
+JSStringRef toPointer_s;
 
 static JSStringRef Result_;
 
@@ -514,12 +515,19 @@ void *CYCastPointer_(JSContextRef context, JSValueRef value) {
     switch (JSValueGetType(context, value)) {
         case kJSTypeNull:
             return NULL;
-        /*case kJSTypeObject:
+        case kJSTypeObject: {
+            JSObjectRef object((JSObjectRef) value);
             if (JSValueIsObjectOfClass(context, value, Pointer_)) {
-                Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate((JSObjectRef) value)));
+                Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object)));
                 return internal->value_;
-            }*/
-        default:
+            }
+            JSValueRef toPointer(CYGetProperty(context, object, toPointer_s));
+            if (CYIsCallable(context, toPointer)) {
+                JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toPointer, object, 0, NULL));
+                _assert(value != NULL);
+                return CYCastPointer_(context, value);
+            }
+        } default:
             double number(CYCastDouble(context, value));
             if (std::isnan(number))
                 throw CYJSError(context, "cannot convert value to pointer");
@@ -1308,6 +1316,7 @@ void CYInitializeDynamic() {
     splice_s = JSStringCreateWithUTF8CString("splice");
     toCYON_s = JSStringCreateWithUTF8CString("toCYON");
     toJSON_s = JSStringCreateWithUTF8CString("toJSON");
+    toPointer_s = JSStringCreateWithUTF8CString("toPointer");
 
     Result_ = JSStringCreateWithUTF8CString("_");
 
index f67fac20498cd4816dfcabd83159fb968d180d29..e8777b48a51571527404ca55fdb9f748bfd4a282 100644 (file)
@@ -2179,6 +2179,7 @@ static JSValueRef Instance_callAsFunction_toJSON(JSContextRef context, JSObjectR
     } CYPoolCatch(NULL)
 } CYCatch return /*XXX*/ NULL; }
 
+#if 0
 static JSValueRef Instance_callAsFunction_valueOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
     if (!JSValueIsObjectOfClass(context, _this, Instance_))
         return NULL;
@@ -2186,6 +2187,16 @@ static JSValueRef Instance_callAsFunction_valueOf(JSContextRef context, JSObject
     Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
     return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->GetValue()));
 } CYCatch return /*XXX*/ NULL; }
+#endif
+
+static JSValueRef Instance_callAsFunction_toPointer(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+    if (!JSValueIsObjectOfClass(context, _this, Instance_))
+        return NULL;
+
+    Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
+    // XXX: but... but... THIS ISN'T A POINTER! :(
+    return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->GetValue()));
+} CYCatch return /*XXX*/ NULL; }
 
 static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
     if (!JSValueIsObjectOfClass(context, _this, Instance_))
@@ -2259,7 +2270,8 @@ static JSStaticFunction Instance_staticFunctions[6] = {
     {"$cya", &CYValue_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
     {"toCYON", &Instance_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
     {"toJSON", &Instance_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
-    {"valueOf", &Instance_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+    //{"valueOf", &Instance_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+    {"toPointer", &Instance_callAsFunction_toPointer, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
     {"toString", &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
     {NULL, NULL, 0}
 };