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
|
// Copyright (C) 2012 Ecma International. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-serializejsonproperty
description: >
Number objects are converted to primitives using ToNumber.
info: |
JSON.stringify ( value [ , replacer [ , space ] ] )
[...]
12. Return ? SerializeJSONProperty(the empty String, wrapper).
SerializeJSONProperty ( key, holder )
[...]
4. If Type(value) is Object, then
a. If value has a [[NumberData]] internal slot, then
i. Set value to ? ToNumber(value).
[...]
9. If Type(value) is Number, then
a. If value is finite, return ! ToString(value).
---*/
assert.sameValue(JSON.stringify(new Number(8.5)), '8.5');
var toPrimitiveReplacer = function(_key, value) {
if (value === 'str') {
var num = new Number(42);
num.toString = function() { throw new Test262Error('should not be called'); };
num.valueOf = function() { return 2; };
return num;
}
return value;
};
assert.sameValue(JSON.stringify(['str'], toPrimitiveReplacer), '[2]');
var abruptToJSON = function() {
var num = new Number(3.14);
num.toString = function() { throw new Test262Error(); };
num.valueOf = function() { throw new Test262Error(); };
return num;
};
assert.throws(Test262Error, function() {
JSON.stringify({
key: {
toJSON: abruptToJSON,
},
});
});
reportCompare(0, 0);
|