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 // Flags: --allow-natives-syntax --smi-only-arrays
     29 // Flags: --nostress-opt
     30 
     31 support_smi_only_arrays = %HasFastSmiElements(new Array(1,2,3,4,5,6,7,8));
     32 
     33 if (support_smi_only_arrays) {
     34   print("Tests include smi-only arrays.");
     35 } else {
     36   print("Tests do NOT include smi-only arrays.");
     37 }
     38 
     39 if (support_smi_only_arrays) {
     40   // This code exists to eliminate the learning influence of AllocationSites
     41   // on the following tests.
     42   var __sequence = 0;
     43   function make_array_string(length) {
     44     this.__sequence = this.__sequence + 1;
     45     return "/* " + this.__sequence + " */  new Array(" + length + ");";
     46   }
     47   function make_array(length) {
     48     return eval(make_array_string(length));
     49   }
     50 
     51   function test(test_double, test_object, set, length) {
     52     // We apply the same operations to two identical arrays.  The first array
     53     // triggers an IC miss, upon which the conversion stub is generated, but the
     54     // actual conversion is done in runtime.  The second array, arriving at
     55     // the previously patched IC, is then converted using the conversion stub.
     56     var array_1 = make_array(length);
     57     var array_2 = make_array(length);
     58 
     59     // false, true, nice setter function, 20
     60     assertTrue(%HasFastSmiElements(array_1));
     61     assertTrue(%HasFastSmiElements(array_2));
     62     for (var i = 0; i < length; i++) {
     63       if (i == length - 5 && test_double) {
     64         // Trigger conversion to fast double elements at length-5.
     65         set(array_1, i, 0.5);
     66         set(array_2, i, 0.5);
     67         assertTrue(%HasFastDoubleElements(array_1));
     68         assertTrue(%HasFastDoubleElements(array_2));
     69       } else if (i == length - 3 && test_object) {
     70         // Trigger conversion to fast object elements at length-3.
     71         set(array_1, i, 'object');
     72         set(array_2, i, 'object');
     73         assertTrue(%HasFastObjectElements(array_1));
     74         assertTrue(%HasFastObjectElements(array_2));
     75       } else if (i != length - 7) {
     76         // Set the element to an integer but leave a hole at length-7.
     77         set(array_1, i, 2*i+1);
     78         set(array_2, i, 2*i+1);
     79       }
     80     }
     81 
     82     for (var i = 0; i < length; i++) {
     83       if (i == length - 5 && test_double) {
     84         assertEquals(0.5, array_1[i]);
     85         assertEquals(0.5, array_2[i]);
     86       } else if (i == length - 3 && test_object) {
     87         assertEquals('object', array_1[i]);
     88         assertEquals('object', array_2[i]);
     89       } else if (i != length - 7) {
     90         assertEquals(2*i+1, array_1[i]);
     91         assertEquals(2*i+1, array_2[i]);
     92       } else {
     93         assertEquals(undefined, array_1[i]);
     94         assertEquals(undefined, array_2[i]);
     95       }
     96     }
     97 
     98     assertEquals(length, array_1.length);
     99     assertEquals(length, array_2.length);
    100   }
    101 
    102   function run_test(test_double, test_object, set, length) {
    103     test(test_double, test_object, set, length);
    104     %ClearFunctionTypeFeedback(test);
    105   }
    106 
    107   run_test(false, false, function(a,i,v){ a[i] = v; }, 20);
    108   run_test(true,  false, function(a,i,v){ a[i] = v; }, 20);
    109   run_test(false, true,  function(a,i,v){ a[i] = v; }, 20);
    110   run_test(true,  true,  function(a,i,v){ a[i] = v; }, 20);
    111 
    112   run_test(false, false, function(a,i,v){ a[i] = v; }, 10000);
    113   run_test(true,  false, function(a,i,v){ a[i] = v; }, 10000);
    114   run_test(false, true,  function(a,i,v){ a[i] = v; }, 10000);
    115   run_test(true,  true,  function(a,i,v){ a[i] = v; }, 10000);
    116 
    117   // Check COW arrays
    118   function get_cow() { return [1, 2, 3]; }
    119 
    120   function transition(x) { x[0] = 1.5; }
    121 
    122   var ignore = get_cow();
    123   transition(ignore);  // Handled by runtime.
    124   var a = get_cow();
    125   var b = get_cow();
    126   transition(a);  // Handled by IC.
    127   assertEquals(1.5, a[0]);
    128   assertEquals(1, b[0]);
    129 } else {
    130   print("Test skipped because smi only arrays are not supported.");
    131 }
    132