js/src/tests/ecma_5/JSON/stringify-replacer-array-hijinks.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 // Any copyright is dedicated to the Public Domain.
     2 // http://creativecommons.org/licenses/publicdomain/
     4 var gTestfile = 'stringify-replacer-array-hijinks.js';
     5 //-----------------------------------------------------------------------------
     6 var BUGNUMBER = 648471;
     7 var summary =
     8   "Better/more correct handling for replacer arrays with getter array index " +
     9   "properties";
    11 print(BUGNUMBER + ": " + summary);
    13 /**************
    14  * BEGIN TEST *
    15  **************/
    17 var replacer = [0, 1, 2, 3];
    18 Object.prototype[3] = 3;
    19 Object.defineProperty(replacer, 1, {
    20   get: function()
    21   {
    22     Object.defineProperty(replacer, 4, { value: 4 });
    23     delete replacer[2];
    24     delete replacer[3];
    25     replacer[5] = 5;
    26     return 1;
    27   }
    28 });
    30 var s =
    31   JSON.stringify({0: { 1: { 3: { 4: { 5: { 2: "omitted" } } } } } }, replacer);
    33 // The replacer array's length is as seen on first query, so property names are
    34 // accumulated for indexes i ∈ {0, 1, 2, 3}, but index 1 deletes 2 and 3, so 2
    35 // isn't seen but 3 is seen as Object.prototype[3].
    36 assertEq('{"0":{"1":{"3":{"3":3}},"3":3},"3":3}', s);
    39 var replacer = [0, 1, 2, 3];
    40 Object.defineProperty(replacer, 0, {
    41   get: function()
    42   {
    43     replacer.length = 0;
    44     return {};
    45   }
    46 });
    48 // The replacer.length truncation means only properties on the prototype chain
    49 // shine through, but it doesn't affect the original bounds of the iteration
    50 // used to determine property names which will be included in the final string.
    51 assertEq(JSON.stringify({ 0: 0, 1: 1, 2: 2, 3: 3 }, replacer),
    52          '{"3":3}');
    54 /******************************************************************************/
    56 if (typeof reportCompare === "function")
    57   reportCompare(true, true);
    59 print("Tests complete");

mercurial