summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/JSON/cyclic-stringify.js
blob: 5eb6273a9701c9de5d4b6579d7d7ebf0bf6ee0e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/licenses/publicdomain/

//-----------------------------------------------------------------------------
var BUGNUMBER = 578273;
var summary =
  "ES5: Properly detect cycles in JSON.stringify (throw TypeError, check for " +
  "cycles rather than imprecisely rely on recursion limits)";

print(BUGNUMBER + ": " + summary);

/**************
 * BEGIN TEST *
 **************/

// objects

var count = 0;
var desc =
  {
    get: function() { count++; return obj; },
    enumerable: true,
    configurable: true
  };
var obj = Object.defineProperty({ p1: 0 }, "p2", desc);

try
{
  var str = JSON.stringify(obj);
  assertEq(false, true, "should have thrown, got " + str);
}
catch (e)
{
  assertEq(e instanceof TypeError, true,
           "wrong error type: " + e.constructor.name);
  assertEq(count, 1,
           "cyclic data structures not detected immediately");
}

count = 0;
var obj2 = Object.defineProperty({}, "obj", desc);
try
{
  var str = JSON.stringify(obj2);
  assertEq(false, true, "should have thrown, got " + str);
}
catch (e)
{
  assertEq(e instanceof TypeError, true,
           "wrong error type: " + e.constructor.name);
  assertEq(count, 2,
           "cyclic data structures not detected immediately");
}


// arrays

var count = 0;
var desc =
  {
    get: function() { count++; return arr; },
    enumerable: true,
    configurable: true
  };
var arr = Object.defineProperty([], "0", desc);

try
{
  var str = JSON.stringify(arr);
  assertEq(false, true, "should have thrown, got " + str);
}
catch (e)
{
  assertEq(e instanceof TypeError, true,
           "wrong error type: " + e.constructor.name);
  assertEq(count, 1,
           "cyclic data structures not detected immediately");
}

count = 0;
var arr2 = Object.defineProperty([], "0", desc);
try
{
  var str = JSON.stringify(arr2);
  assertEq(false, true, "should have thrown, got " + str);
}
catch (e)
{
  assertEq(e instanceof TypeError, true,
           "wrong error type: " + e.constructor.name);
  assertEq(count, 2,
           "cyclic data structures not detected immediately");
}

/******************************************************************************/

if (typeof reportCompare === "function")
  reportCompare(true, true);

print("Tests complete");