Debian Codegraph @

js/src/tests/test262/built-ins/JSON/stringify/value-tojson-array-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-serializejsonarray
5 description: >
6 Circular array value (returned from toJSON method) 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 2. If Type(value) is Object, then
17 a. Let toJSON be ? Get(value, "toJSON").
18 b. If IsCallable(toJSON) is true, then
19 i. Set value to ? Call(toJSON, value, « key »).
20 [...]
21 10. If Type(value) is Object and IsCallable(value) is false, then
22 a. Let isArray be ? IsArray(value).
23 b. If isArray is true, return ? SerializeJSONArray(value).
24
25 SerializeJSONArray ( value )
26
27 1. If stack contains value, throw a TypeError exception because the structure is cyclical.
28 ---*/
29
30 var arr = [];
31 var circular = [arr];
32
33 arr.toJSON = function() {
34 return circular;
35 };
36
37 assert.throws(TypeError, function() {
38 JSON.stringify(circular);
39 });
40
41 reportCompare(0, 0);