summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/JSON/stringify-boxed-primitives.js
blob: 0769c2ec6fec469d09bd9ce80285e219396d7a6c (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/licenses/publicdomain/

var gTestfile = 'stringify-boxed-primitives.js';
//-----------------------------------------------------------------------------
var BUGNUMBER = 584909;
var summary = "Stringification of Boolean/String/Number objects";

print(BUGNUMBER + ": " + summary);

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

function redefine(obj, prop, fun)
{
  var desc =
    { value: fun, writable: true, configurable: true, enumerable: false };
  Object.defineProperty(obj, prop, desc);
}

assertEq(JSON.stringify(new Boolean(false)), "false");

assertEq(JSON.stringify(new Number(5)), "5");

assertEq(JSON.stringify(new String("foopy")), '"foopy"');


var numToString = Number.prototype.toString;
var numValueOf = Number.prototype.valueOf;
var objToString = Object.prototype.toString;
var objValueOf = Object.prototype.valueOf;
var boolToString = Boolean.prototype.toString;
var boolValueOf = Boolean.prototype.valueOf;

redefine(Boolean.prototype, "toString", function() { return 17; });
assertEq(JSON.stringify(new Boolean(false)), "false")
delete Boolean.prototype.toString;
assertEq(JSON.stringify(new Boolean(false)), "false");
delete Object.prototype.toString;
assertEq(JSON.stringify(new Boolean(false)), "false");
delete Boolean.prototype.valueOf;
assertEq(JSON.stringify(new Boolean(false)), "false");
delete Object.prototype.valueOf;
assertEq(JSON.stringify(new Boolean(false)), "false");


redefine(Boolean.prototype, "toString", boolToString);
redefine(Boolean.prototype, "valueOf", boolValueOf);
redefine(Object.prototype, "toString", objToString);
redefine(Object.prototype, "valueOf", objValueOf);

redefine(Number.prototype, "toString", function() { return 42; });
assertEq(JSON.stringify(new Number(5)), "5");
redefine(Number.prototype, "valueOf", function() { return 17; });
assertEq(JSON.stringify(new Number(5)), "17");
delete Number.prototype.toString;
assertEq(JSON.stringify(new Number(5)), "17");
delete Number.prototype.valueOf;
assertEq(JSON.stringify(new Number(5)), "null"); // isNaN(Number("[object Number]"))
delete Object.prototype.toString;
try
{
  JSON.stringify(new Number(5));
  throw new Error("didn't throw");
}
catch (e)
{
  assertEq(e instanceof TypeError, true,
           "ToNumber failure, should throw TypeError");
}
delete Object.prototype.valueOf;
try
{
  JSON.stringify(new Number(5));
  throw new Error("didn't throw");
}
catch (e)
{
  assertEq(e instanceof TypeError, true,
           "ToNumber failure, should throw TypeError");
}


redefine(Number.prototype, "toString", numToString);
redefine(Number.prototype, "valueOf", numValueOf);
redefine(Object.prototype, "toString", objToString);
redefine(Object.prototype, "valueOf", objValueOf);


redefine(String.prototype, "valueOf", function() { return 17; });
assertEq(JSON.stringify(new String(5)), '"5"');
redefine(String.prototype, "toString", function() { return 42; });
assertEq(JSON.stringify(new String(5)), '"42"');
delete String.prototype.toString;
assertEq(JSON.stringify(new String(5)), '"[object String]"');
delete Object.prototype.toString;
assertEq(JSON.stringify(new String(5)), '"17"');
delete String.prototype.valueOf;
try
{
  JSON.stringify(new String(5));
  throw new Error("didn't throw");
}
catch (e)
{
  assertEq(e instanceof TypeError, true,
           "ToString failure, should throw TypeError");
}
delete Object.prototype.valueOf;
try
{
  JSON.stringify(new String(5));
  throw new Error("didn't throw");
}
catch (e)
{
  assertEq(e instanceof TypeError, true,
           "ToString failure, should throw TypeError");
}

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

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

print("All tests passed!");