]> git.saurik.com Git - cycript.git/commitdiff
Avoid infinite recursion while CYONifying objects.
authorJay Freeman (saurik) <saurik@saurik.com>
Wed, 4 Jun 2014 12:25:17 +0000 (05:25 -0700)
committerJay Freeman (saurik) <saurik@saurik.com>
Thu, 5 Jun 2014 09:17:47 +0000 (02:17 -0700)
Execute.cpp
JavaScript.hpp
ObjectiveC/Library.mm

index 93f923ff2f7875ceca68288959318dc424573be0..24c62d704d0f39f58224777063ece89486ac3de8 100644 (file)
@@ -360,7 +360,7 @@ static JSValueRef Cycript_gc_callAsFunction(JSContextRef context, JSObjectRef ob
     return CYJSUndefined(context);
 } CYCatch(NULL) }
 
-const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSValueRef value, JSValueRef *exception) { CYTry {
+const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSValueRef value, std::set<void *> &objects, JSValueRef *exception) { CYTry {
     switch (JSType type = JSValueGetType(context, value)) {
         case kJSTypeUndefined:
             return "undefined";
@@ -385,20 +385,31 @@ const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSValueRef value, JS
         } break;
 
         case kJSTypeObject:
-            return CYPoolCCYON(pool, context, (JSObjectRef) value);
+            return CYPoolCCYON(pool, context, (JSObjectRef) value, objects);
         default:
             throw CYJSError(context, "JSValueGetType() == 0x%x", type);
     }
 } CYCatch(NULL) }
 
-const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSValueRef value) {
-    return _jsccall(CYPoolCCYON, pool, context, value);
+const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSValueRef value, std::set<void *> &objects) {
+    return _jsccall(CYPoolCCYON, pool, context, value, objects);
 }
 
-const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSObjectRef object) {
+const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSValueRef value, std::set<void *> *objects) {
+    if (objects != NULL)
+        return CYPoolCCYON(pool, context, value, *objects);
+    else {
+        std::set<void *> objects;
+        return CYPoolCCYON(pool, context, value, objects);
+    }
+}
+
+const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSObjectRef object, std::set<void *> &objects) {
     JSValueRef toCYON(CYGetProperty(context, object, toCYON_s));
     if (CYIsCallable(context, toCYON)) {
-        JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toCYON, object, 0, NULL));
+        // XXX: this needs to be abstracted behind some kind of function
+        JSValueRef arguments[1] = {CYCastJSValue(context, static_cast<double>(reinterpret_cast<uintptr_t>(&objects)))};
+        JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toCYON, object, 1, arguments));
         _assert(value != NULL);
         return CYPoolCString(pool, context, value);
     }
@@ -406,7 +417,7 @@ const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSObjectRef object)
     JSValueRef toJSON(CYGetProperty(context, object, toJSON_s));
     if (CYIsCallable(context, toJSON)) {
         JSValueRef arguments[1] = {CYCastJSValue(context, CYJSString(""))};
-        return _jsccall(CYPoolCCYON, pool, context, CYCallAsFunction(context, (JSObjectRef) toJSON, object, 1, arguments));
+        return _jsccall(CYPoolCCYON, pool, context, CYCallAsFunction(context, (JSObjectRef) toJSON, object, 1, arguments), objects);
     }
 
     if (JSObjectIsFunction(context, object)) {
@@ -419,6 +430,8 @@ const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSObjectRef object)
         }
     }
 
+    _assert(objects.insert(object).second);
+
     std::ostringstream str;
 
     str << '{';
@@ -446,7 +459,7 @@ const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSObjectRef object)
 
         try {
             JSValueRef value(CYGetProperty(context, object, name));
-            str << CYPoolCCYON(pool, context, value);
+            str << CYPoolCCYON(pool, context, value, objects);
         } catch (const CYException &error) {
             str << "@error";
         }
@@ -460,7 +473,19 @@ const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSObjectRef object)
     return pool.strmemdup(string.c_str(), string.size());
 }
 
+std::set<void *> *CYCastObjects(JSContextRef context, JSObjectRef _this, size_t count, const JSValueRef arguments[]) {
+    if (count == 0)
+        return NULL;
+    return CYCastPointer<std::set<void *> *>(context, arguments[0]);
+}
+
 static JSValueRef Array_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+    std::set<void *> *objects(CYCastObjects(context, _this, count, arguments));
+    // XXX: this is horribly inefficient
+    std::set<void *> backup;
+    if (objects == NULL)
+        objects = &backup;
+
     CYPool pool;
     std::ostringstream str;
 
@@ -478,7 +503,7 @@ static JSValueRef Array_callAsFunction_toCYON(JSContextRef context, JSObjectRef
         try {
             JSValueRef value(CYGetProperty(context, _this, index));
             if (!JSValueIsUndefined(context, value))
-                str << CYPoolCCYON(pool, context, value);
+                str << CYPoolCCYON(pool, context, value, *objects);
             else {
                 str << ',';
                 comma = false;
@@ -1355,6 +1380,8 @@ static JSValueRef CYValue_callAsFunction_toCYON(JSContextRef context, JSObjectRe
 } CYCatch(NULL) }
 
 static JSValueRef Pointer_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+    std::set<void *> *objects(CYCastObjects(context, _this, count, arguments));
+
     Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(_this)));
     if (internal->length_ != _not(size_t)) {
         JSObjectRef Array(CYGetCachedObject(context, CYJSString("Array_prototype")));
@@ -1369,7 +1396,7 @@ static JSValueRef Pointer_callAsFunction_toCYON(JSContextRef context, JSObjectRe
         if (JSValueIsUndefined(context, value))
             goto pointer;
         CYPool pool;
-        return CYCastJSValue(context, pool.strcat("&", CYPoolCCYON(pool, context, value), NULL));
+        return CYCastJSValue(context, pool.strcat("&", CYPoolCCYON(pool, context, value, objects), NULL));
     } catch (const CYException &e) {
         goto pointer;
     }
@@ -1546,7 +1573,8 @@ const char *CYExecute(JSContextRef context, CYPool &pool, CYUTF8String code) {
         return NULL;
 
     const char *json; try {
-        json = CYPoolCCYON(pool, context, result, &exception);
+        std::set<void *> objects;
+        json = CYPoolCCYON(pool, context, result, objects, &exception);
     } catch (const char *error) {
         return error;
     }
@@ -1650,8 +1678,9 @@ void CYThrow(JSContextRef context, JSValueRef value) {
 }
 
 const char *CYJSError::PoolCString(CYPool &pool) const {
+    std::set<void *> objects;
     // XXX: this used to be CYPoolCString
-    return CYPoolCCYON(pool, context_, value_);
+    return CYPoolCCYON(pool, context_, value_, objects);
 }
 
 JSValueRef CYJSError::CastJSValue(JSContextRef context) const {
index 0c538b1aafec0e3b30e70805c8692a60d09cada5..920df8cc40dc2d0ec61405ece84e2cc500ddc193 100644 (file)
@@ -22,6 +22,8 @@
 #ifndef CYCRIPT_JAVASCRIPT_HPP
 #define CYCRIPT_JAVASCRIPT_HPP
 
+#include <set>
+
 #include <JavaScriptCore/JSBase.h>
 #include <JavaScriptCore/JSContextRef.h>
 #include <JavaScriptCore/JSStringRef.h>
@@ -110,7 +112,8 @@ JSValueRef CYCallFunction(CYPool &pool, JSContextRef context, size_t setups, voi
 bool CYIsCallable(JSContextRef context, JSValueRef value);
 JSValueRef CYCallAsFunction(JSContextRef context, JSObjectRef function, JSObjectRef _this, size_t count, const JSValueRef arguments[]);
 
-const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSObjectRef object);
+const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSObjectRef object, std::set<void *> &objects);
+std::set<void *> *CYCastObjects(JSContextRef context, JSObjectRef _this, size_t count, const JSValueRef arguments[]);
 
 struct CYHooks {
     void *(*ExecuteStart)(JSContextRef);
index a8c3b3310288e3f1b2b1e84f31763ed58b034a5d..bb7acf3b85e8413670539c118ccb81dff874dcfa 100644 (file)
     } return value; \
 }
 
+#define _oassert(test) \
+    if (!(test)) \
+        @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"_assert(" #test ")" userInfo:nil];
+
 #ifndef __APPLE__
 #define class_getSuperclass GSObjCSuper
 #define class_getInstanceVariable GSCGetInstanceVariableDefinition
@@ -450,7 +454,7 @@ JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient) {
 - (JSType) cy$JSType;
 
 - (JSValueRef) cy$toJSON:(NSString *)key inContext:(JSContextRef)context;
-- (NSString *) cy$toCYON:(bool)objective;
+- (NSString *) cy$toCYON:(bool)objective inSet:(std::set<void *> &)objects;
 
 - (bool) cy$hasProperty:(NSString *)name;
 - (NSObject *) cy$getProperty:(NSString *)name;
@@ -468,20 +472,20 @@ JSObjectRef CYMakeInstance(JSContextRef context, id object, bool transient) {
 - (JSValueRef) cy$valueOfInContext:(JSContextRef)context;
 @end
 
-NSString *CYCastNSCYON(id value, bool objective) {
+NSString *CYCastNSCYON(id value, bool objective, std::set<void *> &objects) {
     NSString *string;
 
     if (value == nil)
         string = @"nil";
     else {
         Class _class(object_getClass(value));
-        SEL sel(@selector(cy$toCYON:));
+        SEL sel(@selector(cy$toCYON:inSet:));
 
         if (objc_method *toCYON = class_getInstanceMethod(_class, sel))
-            string = reinterpret_cast<NSString *(*)(id, SEL, bool)>(method_getImplementation(toCYON))(value, sel, objective);
+            string = reinterpret_cast<NSString *(*)(id, SEL, bool, std::set<void *> &)>(method_getImplementation(toCYON))(value, sel, objective, objects);
         else if (objc_method *methodSignatureForSelector = class_getInstanceMethod(_class, @selector(methodSignatureForSelector:))) {
             if (reinterpret_cast<NSMethodSignature *(*)(id, SEL, SEL)>(method_getImplementation(methodSignatureForSelector))(value, @selector(methodSignatureForSelector:), sel) != nil)
-                string = [value cy$toCYON:objective];
+                string = [value cy$toCYON:objective inSet:objects];
             else goto fail;
         } else fail: {
             if (false);
@@ -506,6 +510,15 @@ NSString *CYCastNSCYON(id value, bool objective) {
     return string;
 }
 
+NSString *CYCastNSCYON(id value, bool objective, std::set<void *> *objects) {
+    if (objects != NULL)
+        return CYCastNSCYON(value, objective, *objects);
+    else {
+        std::set<void *> objects;
+        return CYCastNSCYON(value, objective, objects);
+    }
+}
+
 #ifdef __APPLE__
 struct PropertyAttributes {
     CYPool pool_;
@@ -792,7 +805,9 @@ NSObject *CYCopyNSObject(CYPool &pool, JSContextRef context, JSValueRef value) {
     return [[self mutableCopy] autorelease];
 }
 
-- (NSString *) cy$toCYON:(bool)objective {
+- (NSString *) cy$toCYON:(bool)objective inSet:(std::set<void *> &)objects {
+    _oassert(objects.insert(self).second);
+
     NSMutableString *json([[[NSMutableString alloc] init] autorelease]);
     [json appendString:@"@["];
 
@@ -808,7 +823,7 @@ NSObject *CYCopyNSObject(CYPool &pool, JSContextRef context, JSValueRef value) {
         else
             comma = true;
         if (object == nil || [object cy$JSType] != kJSTypeUndefined)
-            [json appendString:CYCastNSCYON(object, true)];
+            [json appendString:CYCastNSCYON(object, true, objects)];
         else {
             [json appendString:@","];
             comma = false;
@@ -881,7 +896,7 @@ NSObject *CYCopyNSObject(CYPool &pool, JSContextRef context, JSValueRef value) {
     return kJSTypeBoolean;
 }
 
-- (NSString *) cy$toCYON:(bool)objective {
+- (NSString *) cy$toCYON:(bool)objective inSet:(std::set<void *> &)objects {
     NSString *value([self boolValue] ? @"true" : @"false");
     return objective ? value : [NSString stringWithFormat:@"@%@", value];
 }
@@ -900,7 +915,9 @@ NSObject *CYCopyNSObject(CYPool &pool, JSContextRef context, JSValueRef value) {
     return [[self mutableCopy] autorelease];
 }
 
-- (NSString *) cy$toCYON:(bool)objective {
+- (NSString *) cy$toCYON:(bool)objective inSet:(std::set<void *> &)objects {
+    _oassert(objects.insert(self).second);
+
     NSMutableString *json([[[NSMutableString alloc] init] autorelease]);
     [json appendString:@"@{"];
 
@@ -915,10 +932,10 @@ NSObject *CYCopyNSObject(CYPool &pool, JSContextRef context, JSValueRef value) {
             [json appendString:@","];
         else
             comma = true;
-        [json appendString:CYCastNSCYON(key, true)];
+        [json appendString:CYCastNSCYON(key, true, objects)];
         [json appendString:@":"];
         NSObject *object([self objectForKey:key]);
-        [json appendString:CYCastNSCYON(object, true)];
+        [json appendString:CYCastNSCYON(object, true, objects)];
     }
 
     [json appendString:@"}"];
@@ -1038,7 +1055,7 @@ NSObject *CYCopyNSObject(CYPool &pool, JSContextRef context, JSValueRef value) {
     return kJSTypeNumber;
 }
 
-- (NSString *) cy$toCYON:(bool)objective {
+- (NSString *) cy$toCYON:(bool)objective inSet:(std::set<void *> &)objects {
     NSString *value([self cy$JSType] != kJSTypeBoolean ? [self stringValue] : [self boolValue] ? @"true" : @"false");
     return objective ? value : [NSString stringWithFormat:@"@%@", value];
 }
@@ -1056,7 +1073,7 @@ NSObject *CYCopyNSObject(CYPool &pool, JSContextRef context, JSValueRef value) {
     return kJSTypeNull;
 }
 
-- (NSString *) cy$toCYON:(bool)objective {
+- (NSString *) cy$toCYON:(bool)objective inSet:(std::set<void *> &)objects {
     NSString *value(@"null");
     return objective ? value : [NSString stringWithFormat:@"@%@", value];
 }
@@ -1086,8 +1103,8 @@ NSObject *CYCopyNSObject(CYPool &pool, JSContextRef context, JSValueRef value) {
     return kJSTypeObject;
 }
 
-- (NSString *) cy$toCYON:(bool)objective {
-    return [@"#" stringByAppendingString:[[self description] cy$toCYON:true]];
+- (NSString *) cy$toCYON:(bool)objective inSet:(std::set<void *> &)objects {
+    return [@"#" stringByAppendingString:[[self description] cy$toCYON:true inSet:objects]];
 }
 
 - (bool) cy$hasProperty:(NSString *)name {
@@ -1124,8 +1141,8 @@ NSObject *CYCopyNSObject(CYPool &pool, JSContextRef context, JSValueRef value) {
 /* Bridge: NSProxy {{{ */
 @implementation NSProxy (Cycript)
 
-- (NSString *) cy$toCYON:(bool)objective {
-    return [[self description] cy$toCYON:objective];
+- (NSString *) cy$toCYON:(bool)objective inSet:(std::set<void *> &)objects {
+    return [[self description] cy$toCYON:objective inSet:objects];
 }
 
 @end
@@ -1133,10 +1150,12 @@ NSObject *CYCopyNSObject(CYPool &pool, JSContextRef context, JSValueRef value) {
 /* Bridge: NSSet {{{ */
 @implementation NSSet (Cycript)
 
-- (NSString *) cy$toCYON:(bool)objective {
+- (NSString *) cy$toCYON:(bool)objective inSet:(std::set<void *> &)objects {
+    _oassert(objects.insert(self).second);
+
     NSMutableString *json([[[NSMutableString alloc] init] autorelease]);
     [json appendString:@"[NSSet setWithArray:"];
-    [json appendString:CYCastNSCYON([self allObjects], true)];
+    [json appendString:CYCastNSCYON([self allObjects], true, objects)];
     [json appendString:@"]]"];
     return json;
 }
@@ -1154,7 +1173,7 @@ NSObject *CYCopyNSObject(CYPool &pool, JSContextRef context, JSValueRef value) {
     return kJSTypeString;
 }
 
-- (NSString *) cy$toCYON:(bool)objective {
+- (NSString *) cy$toCYON:(bool)objective inSet:(std::set<void *> &)objects {
     std::ostringstream str;
     if (!objective)
         str << '@';
@@ -1203,7 +1222,7 @@ NSObject *CYCopyNSObject(CYPool &pool, JSContextRef context, JSValueRef value) {
     return kJSTypeUndefined;
 }
 
-- (NSString *) cy$toCYON:(bool)objective {
+- (NSString *) cy$toCYON:(bool)objective inSet:(std::set<void *> &)objects {
     NSString *value(@"undefined");
     return value; // XXX: maybe use the below code, adding @undefined?
     //return objective ? value : [NSString stringWithFormat:@"@%@", value];
@@ -1265,11 +1284,11 @@ JSValueRef CYCastJSValue(JSContextRef context, NSObject *value) { CYPoolTry {
     [super dealloc];
 } CYObjectiveCatch }
 
-- (NSString *) cy$toCYON:(bool)objective { CYObjectiveTry {
+- (NSString *) cy$toCYON:(bool)objective inSet:(std::set<void *> &)objects { CYObjectiveTry {
     CYPool pool;
-    const char *cyon(CYPoolCCYON(pool, context, object_));
+    const char *cyon(CYPoolCCYON(pool, context, object_, objects));
     if (cyon == NULL)
-        return [super cy$toCYON:objective];
+        return [super cy$toCYON:objective inSet:objects];
     else
         return [NSString stringWithUTF8String:cyon];
 } CYObjectiveCatch }
@@ -1307,9 +1326,9 @@ JSValueRef CYCastJSValue(JSContextRef context, NSObject *value) { CYPoolTry {
 
 @implementation CYJSArray
 
-- (NSString *) cy$toCYON:(bool)objective { CYObjectiveTry {
+- (NSString *) cy$toCYON:(bool)objective inSet:(std::set<void *> &)objects { CYObjectiveTry {
     CYPool pool;
-    return [NSString stringWithUTF8String:CYPoolCCYON(pool, context, object_)];
+    return [NSString stringWithUTF8String:CYPoolCCYON(pool, context, object_, objects)];
 } CYObjectiveCatch }
 
 - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { CYObjectiveTry_ {
@@ -2634,11 +2653,13 @@ static JSValueRef Instance_getProperty_messages(JSContextRef context, JSObjectRe
 } CYCatch(NULL) }
 
 static JSValueRef Instance_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+    std::set<void *> *objects(CYCastObjects(context, _this, count, arguments));
+
     if (!CYJSValueIsNSObject(context, _this))
         return NULL;
 
     Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
-    return CYCastJSValue(context, CYJSString(context, CYCastNSCYON(internal->GetValue(), false)));
+    return CYCastJSValue(context, CYJSString(context, CYCastNSCYON(internal->GetValue(), false, objects)));
 } CYCatch(NULL) }
 
 static JSValueRef Instance_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {