Debian Codegraph @

js/src/tests/test262/built-ins/JSON/stringify/value-string-object.js

raw
1 // Copyright (C) 2012 Ecma International. All rights reserved.
2 // This code is governed by the BSD license found in the LICENSE file.
3 /*---
4 esid: sec-serializejsonproperty
5 description: >
6 String exotic objects are converted to primitives using ToString.
7 info: |
8 JSON.stringify ( value [ , replacer [ , space ] ] )
9
10 [...]
11 12. Return ? SerializeJSONProperty(the empty String, wrapper).
12
13 SerializeJSONProperty ( key, holder )
14
15 [...]
16 4. If Type(value) is Object, then
17 [...]
18 b. Else if value has a [[StringData]] internal slot, then
19 i. Set value to ? ToString(value).
20 [...]
21 8. If Type(value) is String, return QuoteJSONString(value).
22 ---*/
23
24 assert.sameValue(JSON.stringify(new String('str')), '"str"');
25
26 var toJSON = function() {
27 var str = new String('str');
28 str.toString = function() { return 'toString'; };
29 str.valueOf = function() { throw new Test262Error('should not be called'); };
30 return str;
31 };
32
33 assert.sameValue(
34 JSON.stringify({
35 key: {
36 toJSON: toJSON,
37 },
38 }),
39 '{"key":"toString"}'
40 );
41
42 var abruptReplacer = function(_key, value) {
43 if (value === true) {
44 var str = new String('str');
45 str.toString = function() { throw new Test262Error(); };
46 str.valueOf = function() { throw new Test262Error(); };
47 return str;
48 }
49
50 return value;
51 };
52
53 assert.throws(Test262Error, function() {
54 JSON.stringify([true], abruptReplacer);
55 });
56
57 reportCompare(0, 0);