From 22905745f182344dae5d0476aef45b514802baab Mon Sep 17 00:00:00 2001 From: Alexander Ljungberg Date: Thu, 13 Jun 2013 19:19:22 +0100 Subject: [PATCH] Fixed: crash on -CPArray/CPDictionary description if a value was the window object. Without this fix, code like `[@{ "a": window } description]` would crash with a "Maximum call stack size exceeded" exception. Now`CPDescriptionOfObject()` function simply describes the window object as `window` rather than trying to serialise it into a huge description string. --- Foundation/CPObject.j | 3 +++ Tests/Foundation/CPDictionaryTest.j | 11 ++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Foundation/CPObject.j b/Foundation/CPObject.j index 71b1bd6b5..613f1e457 100644 --- a/Foundation/CPObject.j +++ b/Foundation/CPObject.j @@ -544,6 +544,9 @@ function CPDescriptionOfObject(anObject) if (anObject === undefined) return "undefined"; + if (anObject === window) + return "window"; + if (anObject.isa) { if ([anObject isKindOfClass:CPString]) diff --git a/Tests/Foundation/CPDictionaryTest.j b/Tests/Foundation/CPDictionaryTest.j index 78352a65a..bd1447fd2 100644 --- a/Tests/Foundation/CPDictionaryTest.j +++ b/Tests/Foundation/CPDictionaryTest.j @@ -389,7 +389,7 @@ - (void)testJSObjectDescription { - var dict = [[CPDictionary alloc] initWithObjects:[CGRectMake(1, 2, 3, 4), CGPointMake(5, 6)] forKeys:[@"key1", @"key2"]], + var dict = @{ "key1": CGRectMake(1, 2, 3, 4), "key2": CGPointMake(5, 6) }, d = [dict description]; [self assertTrue:d.indexOf("(1, 2)") !== -1 message:"Can't find '(1, 2)' in description of dictionary " + d]; @@ -399,6 +399,15 @@ [self assert:'@{\n @"key1": @[\n @"1",\n @"2",\n @"3"\n ],\n @"key2": @"This is a string",\n @"key3": @{\n @"another": @"object"\n }\n}' equals:[json_dict description]]; } +- (void)testWindowJSObjectDescription +{ + var dict = @{ "Key": window }; + + // 'window' is the global namespace so we should never try to fully describe it. If we do, we're likely + // to get into an infinite loop, and even if we don't it'll be huge. + [self assert:'@{\n @"Key": window\n}' equals:[dict description]]; +} + - (void)testInitWithObjectsAndKeys { var dict = [[CPDictionary alloc] initWithObjectsAndKeys:@"Value1", @"Key1", nil, @"Key2", @"Value3", @"Key3"];