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