1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma_5/JSON/parse-reviver-array-delete.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,89 @@ 1.4 +// Any copyright is dedicated to the Public Domain. 1.5 +// http://creativecommons.org/licenses/publicdomain/ 1.6 + 1.7 +var gTestfile = 'parse-reviver-array-delete.js'; 1.8 +//----------------------------------------------------------------------------- 1.9 +var BUGNUMBER = 999999; 1.10 +var summary = "JSON.parse with a reviver which elides array elements"; 1.11 + 1.12 +print(BUGNUMBER + ": " + summary); 1.13 + 1.14 +/************** 1.15 + * BEGIN TEST * 1.16 + **************/ 1.17 + 1.18 +/* 1.19 + * The reviver deletes all properties from the to-be-returned array. Thus 1.20 + * stringification reveals properties on the prototype chain -- but there are 1.21 + * none, so this result is unsurprising. 1.22 + */ 1.23 +assertEq(JSON.parse('[1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]', 1.24 + function revive(k, v) 1.25 + { 1.26 + if (k === "") 1.27 + return v; 1.28 + return undefined; 1.29 + }) + "", 1.30 + ",,,,,,,,,,,,,,,,,,,"); 1.31 + 1.32 +/* 1.33 + * Now let's try a reviver that deletes every property but a mega-huge one. 1.34 + */ 1.35 +var str = "["; 1.36 +var expected = ""; 1.37 +var expected2 = ""; 1.38 +for (var i = 0; i < 2048; i++) 1.39 +{ 1.40 + str += "1,"; 1.41 + if (i === 2047) 1.42 + { 1.43 + expected += "1"; 1.44 + expected2 += "1"; 1.45 + } 1.46 + if (i === 3) 1.47 + expected2 += "17"; 1.48 + expected += ","; 1.49 + expected2 += ","; 1.50 +} 1.51 +str += "1]"; 1.52 + 1.53 +assertEq(JSON.parse(str, 1.54 + function reviver(k, v) 1.55 + { 1.56 + if (k === "" || k === "2047") 1.57 + return v; 1.58 + return undefined; 1.59 + }) + "", 1.60 + expected); 1.61 + 1.62 + 1.63 +Array.prototype[3] = 17; 1.64 + 1.65 +/* Now, with a property on the prototype chain, it'll show through. */ 1.66 +assertEq(JSON.parse('[1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0]', 1.67 + function revive(k, v) 1.68 + { 1.69 + if (k === "") 1.70 + return v; 1.71 + return undefined; 1.72 + }) + "", 1.73 + ",,,17,,,,,,,,,,,,,,,,"); 1.74 + 1.75 + 1.76 +/* And here too. */ 1.77 +assertEq(JSON.parse(str, 1.78 + function reviver(k, v) 1.79 + { 1.80 + if (k === "" || k === "2047") 1.81 + return v; 1.82 + return undefined; 1.83 + }) + "", 1.84 + expected2); 1.85 + 1.86 + 1.87 +/******************************************************************************/ 1.88 + 1.89 +if (typeof reportCompare === "function") 1.90 + reportCompare(true, true); 1.91 + 1.92 +print("Tests complete");