1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma_5/JSON/stringify-replacer-array-hijinks.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,59 @@ 1.4 +// Any copyright is dedicated to the Public Domain. 1.5 +// http://creativecommons.org/licenses/publicdomain/ 1.6 + 1.7 +var gTestfile = 'stringify-replacer-array-hijinks.js'; 1.8 +//----------------------------------------------------------------------------- 1.9 +var BUGNUMBER = 648471; 1.10 +var summary = 1.11 + "Better/more correct handling for replacer arrays with getter array index " + 1.12 + "properties"; 1.13 + 1.14 +print(BUGNUMBER + ": " + summary); 1.15 + 1.16 +/************** 1.17 + * BEGIN TEST * 1.18 + **************/ 1.19 + 1.20 +var replacer = [0, 1, 2, 3]; 1.21 +Object.prototype[3] = 3; 1.22 +Object.defineProperty(replacer, 1, { 1.23 + get: function() 1.24 + { 1.25 + Object.defineProperty(replacer, 4, { value: 4 }); 1.26 + delete replacer[2]; 1.27 + delete replacer[3]; 1.28 + replacer[5] = 5; 1.29 + return 1; 1.30 + } 1.31 +}); 1.32 + 1.33 +var s = 1.34 + JSON.stringify({0: { 1: { 3: { 4: { 5: { 2: "omitted" } } } } } }, replacer); 1.35 + 1.36 +// The replacer array's length is as seen on first query, so property names are 1.37 +// accumulated for indexes i ∈ {0, 1, 2, 3}, but index 1 deletes 2 and 3, so 2 1.38 +// isn't seen but 3 is seen as Object.prototype[3]. 1.39 +assertEq('{"0":{"1":{"3":{"3":3}},"3":3},"3":3}', s); 1.40 + 1.41 + 1.42 +var replacer = [0, 1, 2, 3]; 1.43 +Object.defineProperty(replacer, 0, { 1.44 + get: function() 1.45 + { 1.46 + replacer.length = 0; 1.47 + return {}; 1.48 + } 1.49 +}); 1.50 + 1.51 +// The replacer.length truncation means only properties on the prototype chain 1.52 +// shine through, but it doesn't affect the original bounds of the iteration 1.53 +// used to determine property names which will be included in the final string. 1.54 +assertEq(JSON.stringify({ 0: 0, 1: 1, 2: 2, 3: 3 }, replacer), 1.55 + '{"3":3}'); 1.56 + 1.57 +/******************************************************************************/ 1.58 + 1.59 +if (typeof reportCompare === "function") 1.60 + reportCompare(true, true); 1.61 + 1.62 +print("Tests complete");