value-tojson-arguments.js (1210B)
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 toJSON 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 [...] 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 22 var callCount = 0; 23 var _this, _key; 24 var obj = { 25 toJSON: function(key) { 26 callCount += 1; 27 _this = this; 28 _key = key; 29 }, 30 }; 31 32 assert.sameValue(JSON.stringify(obj), undefined); 33 assert.sameValue(callCount, 1); 34 assert.sameValue(_this, obj); 35 assert.sameValue(_key, ''); 36 37 assert.sameValue(JSON.stringify([1, obj, 3]), '[1,null,3]'); 38 assert.sameValue(callCount, 2); 39 assert.sameValue(_this, obj); 40 assert.sameValue(_key, '1'); 41 42 assert.sameValue(JSON.stringify({key: obj}), '{}'); 43 assert.sameValue(callCount, 3); 44 assert.sameValue(_this, obj); 45 assert.sameValue(_key, 'key'); 46 47 reportCompare(0, 0);