Debian Codegraph @

js/src/tests/test262/built-ins/JSON/stringify/replacer-function-object-circular.js

raw
1 // Copyright (C) 2019 Aleksey Shvayka. All rights reserved.
2 // This code is governed by the BSD license found in the LICENSE file.
3 /*---
4 esid: sec-serializejsonobject
5 description: >
6 Circular object value (returned from replacer function) throws a TypeError.
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 3. If ReplacerFunction is not undefined, then
17 a. Set value to ? Call(ReplacerFunction, holder, « key, value »).
18 [...]
19 10. If Type(value) is Object and IsCallable(value) is false, then
20 [...]
21 c. Return ? SerializeJSONObject(value).
22
23 SerializeJSONObject ( value )
24
25 1. If stack contains value, throw a TypeError exception because the structure is cyclical.
26 ---*/
27
28 var direct = {prop: {}};
29 var directReplacer = function(k, v) {
30 return direct;
31 };
32
33 assert.throws(TypeError, function() {
34 JSON.stringify(direct, directReplacer);
35 });
36
37 var indirect = {p1: {p2: {}}};
38 var indirectReplacer = function(key, value) {
39 if (key === 'p2') {
40 return indirect;
41 }
42
43 return value;
44 };
45
46 assert.throws(TypeError, function() {
47 JSON.stringify(indirect, indirectReplacer);
48 });
49
50 reportCompare(0, 0);