Home | History | Annotate | Download | only in mjsunit
      1 // Copyright 2008 the V8 project authors. All rights reserved.
      2 // Redistribution and use in source and binary forms, with or without
      3 // modification, are permitted provided that the following conditions are
      4 // met:
      5 //
      6 //     * Redistributions of source code must retain the above copyright
      7 //       notice, this list of conditions and the following disclaimer.
      8 //     * Redistributions in binary form must reproduce the above
      9 //       copyright notice, this list of conditions and the following
     10 //       disclaimer in the documentation and/or other materials provided
     11 //       with the distribution.
     12 //     * Neither the name of Google Inc. nor the names of its
     13 //       contributors may be used to endorse or promote products derived
     14 //       from this software without specific prior written permission.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 
     28 // Flags: --noharmony-for-in
     29 
     30 function props(x) {
     31   var array = [];
     32   for (var p in x) array.push(p);
     33   return array.sort();
     34 }
     35 
     36 assertEquals(0, props({}).length, "olen0");
     37 assertEquals(1, props({x:1}).length, "olen1");
     38 assertEquals(2, props({x:1, y:2}).length, "olen2");
     39 
     40 assertArrayEquals(["x"], props({x:1}), "x");
     41 assertArrayEquals(["x", "y"], props({x:1, y:2}), "xy");
     42 assertArrayEquals(["x", "y", "zoom"], props({x:1, y:2, zoom:3}), "xyzoom");
     43 
     44 assertEquals(0, props([]).length, "alen0");
     45 assertEquals(1, props([1]).length, "alen1");
     46 assertEquals(2, props([1,2]).length, "alen2");
     47 
     48 assertArrayEquals(["0"], props([1]), "0");
     49 assertArrayEquals(["0", "1"], props([1,2]), "01");
     50 assertArrayEquals(["0", "1", "2"], props([1,2,3]), "012");
     51 
     52 var o = {};
     53 var a = [];
     54 for (var i = 0x0020; i < 0x01ff; i+=2) {
     55   var s = 'char:' + String.fromCharCode(i);
     56   a.push(s);
     57   o[s] = i;
     58 }
     59 assertArrayEquals(a, props(o), "charcodes");
     60 
     61 var a = [];
     62 assertEquals(0, props(a).length, "proplen0");
     63 a[Math.pow(2,30)-1] = 0;
     64 assertEquals(1, props(a).length, "proplen1");
     65 a[Math.pow(2,31)-1] = 0;
     66 assertEquals(2, props(a).length, "proplen2");
     67 a[1] = 0;
     68 assertEquals(3, props(a).length, "proplen3");
     69 
     70 for (var hest = 'hest' in {}) { }
     71 assertEquals('hest', hest, "empty-no-override");
     72 
     73 var result = '';
     74 for (var p in {a : [0], b : 1}) { result += p; }
     75 assertEquals('ab', result, "ab");
     76 
     77 var result = '';
     78 for (var p in {a : {v:1}, b : 1}) { result += p; }
     79 assertEquals('ab', result, "ab-nodeep");
     80 
     81 var result = '';
     82 for (var p in { get a() {}, b : 1}) { result += p; }
     83 assertEquals('ab', result, "abget");
     84 
     85 var result = '';
     86 for (var p in { get a() {}, set a(x) {}, b : 1}) { result += p; }
     87 assertEquals('ab', result, "abgetset");
     88 
     89 
     90 // Test that for-in in the global scope works with a keyed property as "each".
     91 // Test outside a loop and in a loop for multiple iterations.
     92 a = [1,2,3,4];
     93 x = {foo:5, bar:6, zip:7, glep:9, 10:11};
     94 delete x.bar;
     95 y = {}
     96 
     97 for (a[2] in x) {
     98   y[a[2]] = x[a[2]];
     99 }
    100 
    101 assertEquals(5, y.foo, "y.foo");
    102 assertEquals("undefined", typeof y.bar, "y.bar");
    103 assertEquals(7, y.zip, "y.zip");
    104 assertEquals(9, y.glep, "y.glep");
    105 assertEquals(11, y[10], "y[10]");
    106 assertEquals("undefined", typeof y[2], "y[2]");
    107 assertEquals("undefined", typeof y[0], "y[0]");
    108 
    109 for (i=0 ; i < 3; ++i) {
    110   y = {}
    111 
    112   for (a[2] in x) {
    113     y[a[2]] = x[a[2]];
    114   }
    115 
    116   assertEquals(5, y.foo, "y.foo");
    117   assertEquals("undefined", typeof y.bar, "y.bar");
    118   assertEquals(7, y.zip, "y.zip");
    119   assertEquals(9, y.glep, "y.glep");
    120   assertEquals(11, y[10], "y[10]");
    121   assertEquals("undefined", typeof y[2], "y[2]");
    122   assertEquals("undefined", typeof y[0], "y[0]");
    123 }
    124 
    125 (function testLargeElementKeys() {
    126   // Key out of SMI range but well within safe double representaion.
    127   var large_key = 2147483650;
    128   var o = [];
    129   // Trigger dictionary elements with HeapNumber keys.
    130   o[large_key] = 0;
    131   o[large_key+1] = 1;
    132   o[large_key+2] = 2;
    133   o[large_key+3] = 3;
    134   var keys = [];
    135   for (var k in o) {
    136     keys.push(k);
    137   }
    138   assertEquals(["2147483650", "2147483651", "2147483652", "2147483653"], keys);
    139 })();
    140 
    141 (function testLargeElementKeysWithProto() {
    142   var large_key = 2147483650;
    143   var o = {__proto__: {}};
    144   o[large_key] = 1;
    145   o.__proto__[large_key] = 1;
    146   var keys = [];
    147   for (var k in o) {
    148     keys.push(k);
    149   }
    150   assertEquals(["2147483650"], keys);
    151 })();
    152 
    153 (function testNonEnumerableArgumentsIndex() {
    154   Object.defineProperty(arguments, 0, {enumerable:false});
    155   for (var k in arguments) {
    156     assertUnreachable();
    157   }
    158 })();
    159 
    160 (function testNonEnumerableSloppyArgumentsIndex(a) {
    161   Object.defineProperty(arguments, 0, {enumerable:false});
    162   for (var k in arguments) {
    163     assertUnreachable();
    164   }
    165 })(true);
    166