Home | History | Annotate | Download | only in mjsunit
      1 // Copyright 2012 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 // Verifies that the KeyedStoreIC correctly handles out-of-bounds stores
     29 // to an array that grow it by a single element. Test functions are
     30 // called twice to make sure that the IC is used, first call is handled
     31 // by the runtime in the miss stub.
     32 
     33 function array_store_1(a,b,c) {
     34   return (a[b] = c);
     35 }
     36 
     37 // Check handling of the empty array.
     38 var a = [];
     39 array_store_1(a, 0, 1);
     40 a = [];
     41 array_store_1(a, 0, 1);
     42 assertEquals(1, a[0]);
     43 assertEquals(1, array_store_1([], 0, 1));
     44 
     45 a = [];
     46 for (x=0;x<100000;++x) {
     47   assertEquals(x, array_store_1(a, x, x));
     48 }
     49 
     50 for (x=0;x<100000;++x) {
     51   assertEquals(x, array_store_1([], 0, x));
     52 }
     53 
     54 function array_store_2(a,b,c) {
     55   return (a[b] = c);
     56 }
     57 
     58 a = [];
     59 array_store_2(a, 0, 0.5);
     60 a = [];
     61 array_store_2(a, 0, 0.5);
     62 assertEquals(0.5, a[0]);
     63 assertEquals(0.5, array_store_2([], 0, 0.5));
     64 
     65 function array_store_3(a,b,c) {
     66   return (a[b] = c);
     67 }
     68 
     69 x = new Object();
     70 a = [];
     71 array_store_3(a, 0, x);
     72 a = [];
     73 array_store_3(a, 0, x);
     74 assertEquals(x, a[0]);
     75 assertEquals(x, array_store_3([], 0, x));
     76 
     77 // Check the handling of COW arrays
     78 function makeCOW() {
     79   return [1];
     80 }
     81 
     82 function array_store_4(a,b,c) {
     83   return (a[b] = c);
     84 }
     85 
     86 a = makeCOW();
     87 array_store_4(a, 1, 1);
     88 a = makeCOW();
     89 array_store_4(a, 1, 1);
     90 assertEquals(1, a[1]);
     91 assertEquals(1, array_store_4([], 1, 1));
     92 
     93 function array_store_5(a,b,c) {
     94   return (a[b] = c);
     95 }
     96 
     97 a = makeCOW();
     98 array_store_5(a, 1, 0.5);
     99 a = makeCOW();
    100 array_store_5(a, 1, 0.5);
    101 assertEquals(0.5, a[1]);
    102 assertEquals(0.5, array_store_5([], 1, 0.5));
    103 
    104 function array_store_6(a,b,c) {
    105   return (a[b] = c);
    106 }
    107 
    108 a = makeCOW();
    109 array_store_6(a, 1, x);
    110 a = makeCOW();
    111 array_store_6(a, 1, x);
    112 assertEquals(x, a[1]);
    113 assertEquals(x, array_store_6([], 1, x));
    114 
    115 // Check the handling of mutable arrays.
    116 a = new Array(1,2,3);
    117 array_store_4(a, 3, 1);
    118 a = new Array(1,2,3);
    119 array_store_4(a, 3, 1);
    120 assertEquals(1, a[3]);
    121 assertEquals(1, array_store_4([], 3, 1));
    122 
    123 function array_store_5(a,b,c) {
    124   return (a[b] = c);
    125 }
    126 
    127 a = new Array(1,2,3);
    128 array_store_5(a, 3, 0.5);
    129 a = new Array(1,2,3);
    130 array_store_5(a, 3, 0.5);
    131 assertEquals(0.5, a[3]);
    132 assertEquals(0.5, array_store_5([], 3, 0.5));
    133 
    134 function array_store_6(a,b,c) {
    135   return (a[b] = c);
    136 }
    137 
    138 a = new Array(1,2,3);
    139 array_store_6(a, 3, x);
    140 a = new Array(1,2,3);
    141 array_store_6(a, 3, x);
    142 assertEquals(x, a[3]);
    143 assertEquals(x, array_store_6([], 3, x));
    144 
    145 function array_store_7(a,b,c) {
    146   return (a[b] = c);
    147 }
    148 
    149 // Check the handling of mutable arrays of doubles
    150 var a = new Array(0.5, 1.5);
    151 array_store_7(a, 2, .5);
    152 a = new Array(0.5, 1.5);
    153 array_store_7(a, 2, .5);
    154 assertEquals(0.5, a[2]);
    155 a = new Array(0.5, 1.5);
    156 assertEquals(0.5, array_store_7(a, 2, 0.5));
    157 
    158 for (x=0;x<100000;++x) {
    159   a = new Array(0.5, 1.5);
    160   assertEquals(x, array_store_7(a, 2, x));
    161 }
    162 
    163 function array_store_8(a,b,c) {
    164   return (a[b] = c);
    165 }
    166 
    167 var a = new Array(0.5, 1.5);
    168 array_store_8(a, 2, .5);
    169 a = new Array(0.5, 1.5);
    170 array_store_8(a, 10, .5);
    171 assertEquals(0.5, a[10]);
    172 
    173 // Grow the empty array with a double store.
    174 function array_store_9(a,b,c) {
    175   return (a[b] = c);
    176 }
    177 
    178 var a = [];
    179 array_store_9(a, 0, 0.5);
    180 a = [];
    181 array_store_1(a, 0, 0.5);
    182 assertEquals(0.5, a[0]);
    183 assertEquals(0.5, array_store_1([], 0, 0.5));
    184