michael@0: // Any copyright is dedicated to the Public Domain. michael@0: // http://creativecommons.org/licenses/publicdomain/ michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: var BUGNUMBER = 578273; michael@0: var summary = michael@0: "ES5: Properly detect cycles in JSON.stringify (throw TypeError, check for " + michael@0: "cycles rather than imprecisely rely on recursion limits)"; michael@0: michael@0: print(BUGNUMBER + ": " + summary); michael@0: michael@0: /************** michael@0: * BEGIN TEST * michael@0: **************/ michael@0: michael@0: // objects michael@0: michael@0: var count = 0; michael@0: var desc = michael@0: { michael@0: get: function() { count++; return obj; }, michael@0: enumerable: true, michael@0: configurable: true michael@0: }; michael@0: var obj = Object.defineProperty({ p1: 0 }, "p2", desc); michael@0: michael@0: try michael@0: { michael@0: var str = JSON.stringify(obj); michael@0: assertEq(false, true, "should have thrown, got " + str); michael@0: } michael@0: catch (e) michael@0: { michael@0: assertEq(e instanceof TypeError, true, michael@0: "wrong error type: " + e.constructor.name); michael@0: assertEq(count, 1, michael@0: "cyclic data structures not detected immediately"); michael@0: } michael@0: michael@0: count = 0; michael@0: var obj2 = Object.defineProperty({}, "obj", desc); michael@0: try michael@0: { michael@0: var str = JSON.stringify(obj2); michael@0: assertEq(false, true, "should have thrown, got " + str); michael@0: } michael@0: catch (e) michael@0: { michael@0: assertEq(e instanceof TypeError, true, michael@0: "wrong error type: " + e.constructor.name); michael@0: assertEq(count, 2, michael@0: "cyclic data structures not detected immediately"); michael@0: } michael@0: michael@0: michael@0: // arrays michael@0: michael@0: var count = 0; michael@0: var desc = michael@0: { michael@0: get: function() { count++; return arr; }, michael@0: enumerable: true, michael@0: configurable: true michael@0: }; michael@0: var arr = Object.defineProperty([], "0", desc); michael@0: michael@0: try michael@0: { michael@0: var str = JSON.stringify(arr); michael@0: assertEq(false, true, "should have thrown, got " + str); michael@0: } michael@0: catch (e) michael@0: { michael@0: assertEq(e instanceof TypeError, true, michael@0: "wrong error type: " + e.constructor.name); michael@0: assertEq(count, 1, michael@0: "cyclic data structures not detected immediately"); michael@0: } michael@0: michael@0: count = 0; michael@0: var arr2 = Object.defineProperty([], "0", desc); michael@0: try michael@0: { michael@0: var str = JSON.stringify(arr2); michael@0: assertEq(false, true, "should have thrown, got " + str); michael@0: } michael@0: catch (e) michael@0: { michael@0: assertEq(e instanceof TypeError, true, michael@0: "wrong error type: " + e.constructor.name); michael@0: assertEq(count, 2, michael@0: "cyclic data structures not detected immediately"); michael@0: } michael@0: michael@0: /******************************************************************************/ michael@0: michael@0: if (typeof reportCompare === "function") michael@0: reportCompare(true, true); michael@0: michael@0: print("Tests complete");