Debian Codegraph @

js/src/tests/test262/built-ins/JSON/parse/reviver-call-args-after-forward-modification.js

raw
1 // |reftest| shell-option(--enable-json-parse-with-source) skip-if(!JSON.hasOwnProperty('isRawJSON')||!xulRuntime.shell) -- json-parse-with-source is not enabled unconditionally, requires shell-options
2 // Copyright (C) 2023 the V8 project authors. All rights reserved.
3 // This code is governed by the BSD license found in the LICENSE file.
4
5 /*---
6 esid: sec-json.parse
7 description: >
8 JSON.parse reviver is called with the correct arguments when the object is
9 modified
10 includes: [compareArray.js]
11 features: [json-parse-with-source]
12 ---*/
13
14 // Test Array append
15 {
16 let log = [];
17 const o = JSON.parse('[1,[]]', function reviver(k, v, { source }) {
18 log.push(`key: |${k}| value: ${JSON.stringify(v)} source: |${source}|`);
19 if (v === 1) {
20 this[1].push('barf');
21 }
22 return this[k];
23 });
24 assert.compareArray(log, [
25 'key: |0| value: 1 source: |1|',
26 'key: |0| value: "barf" source: |undefined|',
27 'key: |1| value: ["barf"] source: |undefined|',
28 'key: || value: [1,["barf"]] source: |undefined|',
29 ]);
30 }
31
32 // Test Object add property
33 {
34 let log = [];
35 const o = JSON.parse('{"p":1,"q":{}}', function (k, v, { source }) {
36 log.push(`key: |${k}| value: ${JSON.stringify(v)} source: |${source}|`);
37 if (v === 1) {
38 this.q.added = 'barf';
39 }
40 return this[k];
41 });
42 assert.compareArray(log, [
43 'key: |p| value: 1 source: |1|',
44 'key: |added| value: "barf" source: |undefined|',
45 'key: |q| value: {"added":"barf"} source: |undefined|',
46 'key: || value: {"p":1,"q":{"added":"barf"}} source: |undefined|',
47 ]);
48 }
49
50 reportCompare(0, 0);