]> git.saurik.com Git - cycript.git/blobdiff - ObjectiveC/Library.mm
Walk superclasses while generating implicit property list.
[cycript.git] / ObjectiveC / Library.mm
index f67fac20498cd4816dfcabd83159fb968d180d29..6a8c0eb07b68ed719daa58a4c1954429eabe4ea9 100644 (file)
@@ -403,6 +403,8 @@ JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient) {
 - (bool) cy$deleteProperty:(NSString *)name;
 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context;
 
++ (bool) cy$hasImplicitProperties;
+
 @end
 
 @protocol Cycript
@@ -833,6 +835,10 @@ NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef valu
     }
 }
 
++ (bool) cy$hasImplicitProperties {
+    return false;
+}
+
 @end
 /* }}} */
 /* Bridge: NSMutableArray {{{ */
@@ -994,6 +1000,10 @@ NSObject *CYCopyNSObject(apr_pool_t *pool, JSContextRef context, JSValueRef valu
 - (void) cy$getPropertyNames:(JSPropertyNameAccumulatorRef)names inContext:(JSContextRef)context {
 }
 
++ (bool) cy$hasImplicitProperties {
+    return true;
+}
+
 @end
 /* }}} */
 /* Bridge: NSProxy {{{ */
@@ -1343,6 +1353,8 @@ static void CYObjectiveC_CallFunction(JSContextRef context, ffi_cif *cif, void (
 
 static bool CYObjectiveC_PoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { CYSadTry {
     switch (type->primitive) {
+        // XXX: do something epic about blocks
+        case sig::block_P:
         case sig::object_P:
         case sig::typename_P:
             *reinterpret_cast<id *>(data) = CYCastNSObject(pool, context, value);
@@ -1361,6 +1373,8 @@ static bool CYObjectiveC_PoolFFI(apr_pool_t *pool, JSContextRef context, sig::Ty
 
 static JSValueRef CYObjectiveC_FromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) { CYPoolTry {
     switch (type->primitive) {
+        // XXX: do something epic about blocks
+        case sig::block_P:
         case sig::object_P:
             if (NSObject *object = *reinterpret_cast<NSObject **>(data)) {
                 JSValueRef value(CYCastJSValue(context, object));
@@ -1552,6 +1566,13 @@ static void Messages_getPropertyNames(JSContextRef context, JSObjectRef object,
 #endif
 }
 
+static bool CYHasImplicitProperties(Class _class) {
+    // XXX: this is an evil hack to deal with NSProxy; fix elsewhere
+    if (!CYImplements(_class, object_getClass(_class), @selector(cy$hasImplicitProperties), false))
+        return true;
+    return [_class cy$hasImplicitProperties];
+}
+
 static bool Instance_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) {
     Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
     id self(internal->GetValue());
@@ -1582,9 +1603,10 @@ static bool Instance_hasProperty(JSContextRef context, JSObjectRef object, JSStr
         return true;
 #endif
 
-    if (SEL sel = sel_getUid(string))
-        if (CYImplements(self, _class, sel, true))
-            return true;
+    if (CYHasImplicitProperties(_class))
+        if (SEL sel = sel_getUid(string))
+            if (CYImplements(self, _class, sel, true))
+                return true;
 
     return false;
 }
@@ -1619,9 +1641,10 @@ static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object,
     }
 #endif
 
-    if (SEL sel = sel_getUid(string))
-        if (CYImplements(self, _class, sel, true))
-            return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception);
+    if (CYHasImplicitProperties(_class))
+        if (SEL sel = sel_getUid(string))
+            if (CYImplements(self, _class, sel, true))
+                return CYSendMessage(pool, context, self, NULL, sel, 0, NULL, false, exception);
 
     return NULL;
 } CYCatch }
@@ -1695,6 +1718,18 @@ static bool Instance_deleteProperty(JSContextRef context, JSObjectRef object, JS
     } CYPoolCatch(NULL)
 } CYCatch return /*XXX*/ NULL; }
 
+static void Instance_getPropertyNames_message(JSPropertyNameAccumulatorRef names, objc_method *method) {
+    const char *name(sel_getName(method_getName(method)));
+    if (strchr(name, ':') != NULL)
+        return;
+
+    const char *type(method_getTypeEncoding(method));
+    if (type == NULL || *type == '\0' || *type == 'v')
+        return;
+
+    JSPropertyNameAccumulatorAddName(names, CYJSString(name));
+}
+
 static void Instance_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) {
     Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(object)));
     id self(internal->GetValue());
@@ -1712,6 +1747,21 @@ static void Instance_getPropertyNames(JSContextRef context, JSObjectRef object,
     }
 #endif
 
+    if (CYHasImplicitProperties(_class))
+        for (Class current(_class); current != nil; current = class_getSuperclass(current)) {
+#if OBJC_API_VERSION >= 2
+            unsigned int size;
+            objc_method **data(class_copyMethodList(current, &size));
+            for (size_t i(0); i != size; ++i)
+                Instance_getPropertyNames_message(names, data[i]);
+            free(data);
+#else
+            for (objc_method_list *methods(current->methods); methods != NULL; methods = methods->method_next)
+                for (int i(0); i != methods->method_count; ++i)
+                    Instance_getPropertyNames_message(names, &methods->method_list[i]);
+#endif
+        }
+
     CYPoolTry {
         // XXX: this is an evil hack to deal with NSProxy; fix elsewhere
         if (CYImplements(self, _class, @selector(cy$getPropertyNames:inContext:), false))
@@ -2107,7 +2157,7 @@ static JSObjectRef Instance_new(JSContextRef context, JSObjectRef object, size_t
     if (count > 1)
         throw CYJSError(context, "incorrect number of arguments to Instance constructor");
     id self(count == 0 ? nil : CYCastPointer<id>(context, arguments[0]));
-    return Instance::Make(context, self);
+    return CYMakeInstance(context, self, false);
 } CYCatch }
 
 static JSValueRef CYValue_getProperty_value(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
@@ -2179,6 +2229,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 +2237,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 +2320,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}
 };
@@ -2443,5 +2505,7 @@ static CYHooks CYObjectiveCHooks = {
 struct CYObjectiveC {
     CYObjectiveC() {
         hooks_ = &CYObjectiveCHooks;
+        // XXX: evil magic juju to make this actually take effect on a Mac when compiled with autoconf/libtool doom!
+        _assert(hooks_ != NULL);
     }
 } CYObjectiveC;