js/src/tests/ecma_5/Array/length-truncate-nonconfigurable-sparse.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 /*
     2  * Any copyright is dedicated to the Public Domain.
     3  * http://creativecommons.org/licenses/publicdomain/
     4  * Contributor:
     5  *   Jeff Walden <jwalden+code@mit.edu>
     6  */
     8 //-----------------------------------------------------------------------------
     9 var BUGNUMBER = 858381;
    10 var summary =
    11   "Array length redefinition behavior with non-configurable elements";
    13 print(BUGNUMBER + ": " + summary);
    15 /**************
    16  * BEGIN TEST *
    17  **************/
    19 function addDataProperty(obj, prop, value, enumerable, configurable, writable)
    20 {
    21   var desc =
    22     { enumerable: enumerable,
    23       configurable: configurable,
    24       writable: writable,
    25       value: value };
    26   Object.defineProperty(obj, prop, desc);
    27 }
    29 function nonstrict()
    30 {
    31   var arr = [0, , 2, , , 5];
    33   addDataProperty(arr,  31415926, "foo", true,  true,  true);
    34   addDataProperty(arr, 123456789, "bar", true,  true,  false);
    35   addDataProperty(arr,   8675309, "qux", false, true,  true);
    36   addDataProperty(arr,   1735039, "eit", false, true,  false);
    37   addDataProperty(arr, 987654321, "fun", false, true,  false);
    39   // non-array indexes to spice things up
    40   addDataProperty(arr, "foopy", "sdfsd", false, false, false);
    41   addDataProperty(arr, 4294967296, "psych", true, false, false);
    42   addDataProperty(arr, 4294967295, "psych", true, false, false);
    44   addDataProperty(arr,  27182818, "eep", false, false, false);
    46   // Truncate...but only as far as possible.
    47   arr.length = 1;
    49   assertEq(arr.length, 27182819);
    51   var props = Object.getOwnPropertyNames(arr).sort();
    52   var expected =
    53     ["0", "2", "5", "1735039", "8675309", "27182818",
    54      "foopy", "4294967296", "4294967295", "length"].sort();
    56   assertEq(props.length, expected.length);
    57   for (var i = 0; i < props.length; i++)
    58     assertEq(props[i], expected[i], "unexpected property: " + props[i]);
    59 }
    60 nonstrict();
    62 function strict()
    63 {
    64   "use strict";
    66   var arr = [0, , 2, , , 5];
    68   addDataProperty(arr,  31415926, "foo", true,  true,  true);
    69   addDataProperty(arr, 123456789, "bar", true,  true,  false);
    70   addDataProperty(arr,   8675309, "qux", false, true,  true);
    71   addDataProperty(arr,   1735039, "eit", false, true,  false);
    72   addDataProperty(arr, 987654321, "fun", false, true,  false);
    74   // non-array indexes to spice things up
    75   addDataProperty(arr, "foopy", "sdfsd", false, false, false);
    76   addDataProperty(arr, 4294967296, "psych", true, false, false);
    77   addDataProperty(arr, 4294967295, "psych", true, false, false);
    79   addDataProperty(arr,  27182818, "eep", false, false, false);
    81   try
    82   {
    83     arr.length = 1;
    84     throw new Error("didn't throw?!");
    85   }
    86   catch (e)
    87   {
    88     assertEq(e instanceof TypeError, true,
    89              "non-configurable property should trigger TypeError, got " + e);
    90   }
    92   assertEq(arr.length, 27182819);
    94   var props = Object.getOwnPropertyNames(arr).sort();
    95   var expected =
    96     ["0", "2", "5", "1735039", "8675309", "27182818",
    97      "foopy", "4294967296", "4294967295", "length"].sort();
    99   assertEq(props.length, expected.length);
   100   for (var i = 0; i < props.length; i++)
   101     assertEq(props[i], expected[i], "unexpected property: " + props[i]);
   102 }
   103 strict();
   105 /******************************************************************************/
   107 if (typeof reportCompare === "function")
   108   reportCompare(true, true);
   110 print("Tests complete");

mercurial