Home | History | Annotate | Download | only in mjsunit
      1 // Copyright 2014 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // Flags: --allow-natives-syntax
      6 
      7 function testAdd(mode) {
      8   var a = [];
      9   Object.defineProperty(a, "length", { writable : false});
     10 
     11   function check(f) {
     12     assertThrows(function() { f(a) }, TypeError);
     13     assertFalse(0 in a);
     14     assertEquals(0, a.length);
     15   }
     16 
     17   function push(a) {
     18     a.push(3);
     19   }
     20 
     21   if (mode == "fast properties") %ToFastProperties(a);
     22 
     23   check(push);
     24   check(push);
     25   check(push);
     26   %OptimizeFunctionOnNextCall(push);
     27   check(push);
     28 
     29   function unshift(a) {
     30     a.unshift(3);
     31   }
     32 
     33   check(unshift);
     34   check(unshift);
     35   check(unshift);
     36   %OptimizeFunctionOnNextCall(unshift);
     37   check(unshift);
     38 
     39   function splice(a) {
     40     a.splice(0, 0, 3);
     41   }
     42 
     43   check(splice);
     44   check(splice);
     45   check(splice);
     46   %OptimizeFunctionOnNextCall(splice);
     47   check(splice);
     48 }
     49 
     50 testAdd("fast properties");
     51 
     52 testAdd("normalized");
     53 
     54 function testRemove(a, mode) {
     55   Object.defineProperty(a, "length", { writable : false});
     56 
     57   function check(f) {
     58     assertThrows(function() { f(a) }, TypeError);
     59     assertEquals(3, a.length);
     60   }
     61 
     62   if (mode == "fast properties") %ToFastProperties(a);
     63 
     64   function pop(a) {
     65     a.pop();
     66   }
     67 
     68   check(pop);
     69   check(pop);
     70   check(pop);
     71   %OptimizeFunctionOnNextCall(pop);
     72   check(pop);
     73 
     74   function shift(a) {
     75     a.shift();
     76   }
     77 
     78   check(shift);
     79   check(shift);
     80   check(shift);
     81   %OptimizeFunctionOnNextCall(shift);
     82   check(shift);
     83 
     84   function splice(a) {
     85     a.splice(0, 1);
     86   }
     87 
     88   check(splice);
     89   check(splice);
     90   check(splice);
     91   %OptimizeFunctionOnNextCall(splice);
     92   check(splice);
     93 
     94   %ClearFunctionTypeFeedback(pop);
     95   %ClearFunctionTypeFeedback(shift);
     96   %ClearFunctionTypeFeedback(splice);
     97 }
     98 
     99 for (var i = 0; i < 3; i++) {
    100   var a = [1, 2, 3];
    101   if (i == 1) {
    102     a = [1, 2, 3.5];
    103   } else if (i == 2) {
    104     a = [1, 2, "string"];
    105   }
    106   testRemove(a, "fast properties");
    107   testRemove(a, "normalized");
    108 }
    109 
    110 var b = [];
    111 Object.defineProperty(b.__proto__, "0", {
    112   set : function(v) {
    113     b.x = v;
    114     Object.defineProperty(b, "length", { writable : false });
    115   },
    116   get: function() {
    117     return b.x;
    118   }
    119 });
    120 
    121 b = [];
    122 try {
    123   b.push(3, 4, 5);
    124 } catch(e) { }
    125 assertFalse(1 in b);
    126 assertFalse(2 in b);
    127 assertEquals(0, b.length);
    128 
    129 b = [];
    130 try {
    131   b.unshift(3, 4, 5);
    132 } catch(e) { }
    133 assertFalse(1 in b);
    134 assertFalse(2 in b);
    135 assertEquals(0, b.length);
    136 
    137 b = [1, 2];
    138 try {
    139   b.unshift(3, 4, 5);
    140 } catch(e) { }
    141 assertEquals(3, b[0]);
    142 assertEquals(4, b[1]);
    143 assertEquals(5, b[2]);
    144 assertEquals(1, b[3]);
    145 assertEquals(2, b[4]);
    146 assertEquals(5, b.length);
    147 
    148 b = [1, 2];
    149 
    150 Object.defineProperty(b.__proto__, "4", {
    151   set : function(v) {
    152     b.z = v;
    153     Object.defineProperty(b, "length", { writable : false });
    154   },
    155   get: function() {
    156     return b.z;
    157   }
    158 });
    159 
    160 try {
    161   b.unshift(3, 4, 5);
    162 } catch(e) { }
    163 
    164 assertFalse(2 in b);
    165 assertFalse(3 in b);
    166 assertEquals(2, b.length);
    167