replacer-function-arguments.js (1400B)
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-serializejsonproperty 5 description: > 6 Replacer function is called with correct context and arguments. 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 1. Let value be ? Get(holder, key). 16 [...] 17 3. If ReplacerFunction is not undefined, then 18 a. Set value to ? Call(ReplacerFunction, holder, « key, value »). 19 includes: [compareArray.js] 20 ---*/ 21 22 var calls = []; 23 var replacer = function(key, value) { 24 if (key !== '') { 25 calls.push([this, key, value]); 26 } 27 28 return value; 29 }; 30 31 var b1 = [1, 2]; 32 var b2 = {c1: true, c2: false}; 33 var a1 = { 34 b1: b1, 35 b2: { 36 toJSON: function() { return b2; }, 37 }, 38 }; 39 var obj = {a1: a1, a2: 'a2'}; 40 41 assert.sameValue( 42 JSON.stringify(obj, replacer), 43 JSON.stringify(obj) 44 ); 45 46 assert.compareArray(calls[0], [obj, 'a1', a1]); 47 assert.compareArray(calls[1], [a1, 'b1', b1]); 48 assert.compareArray(calls[2], [b1, '0', 1]); 49 assert.compareArray(calls[3], [b1, '1', 2]); 50 assert.compareArray(calls[4], [a1, 'b2', b2]); 51 assert.compareArray(calls[5], [b2, 'c1', true]); 52 assert.compareArray(calls[6], [b2, 'c2', false]); 53 assert.compareArray(calls[7], [obj, 'a2', 'a2']); 54 55 reportCompare(0, 0);