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 // This test attempts to test the inline caching for keyed access.
     29 
     30 // ----------------------------------------------------------------------
     31 // Prototype accessor.
     32 // ----------------------------------------------------------------------
     33 var runTest = function() {
     34   var initial_P = 'prototype';
     35   var P = initial_P;
     36   var H = 'hasOwnProperty';
     37 
     38   var f = function() {};
     39 
     40   function prototypeTest(change_index) {
     41     for (var i = 0; i < 10; i++) {
     42       var property = f[P];
     43       if (i <= change_index) {
     44         assertEquals(f.prototype, property);
     45       } else {
     46         assertEquals(f.hasOwnProperty, property);
     47       }
     48       if (i == change_index) P = H;
     49     }
     50     P = initial_P;
     51   }
     52 
     53   for (var i = 0; i < 10; i++) prototypeTest(i);
     54 
     55   f.prototype = 43;
     56 
     57   for (var i = 0; i < 10; i++) prototypeTest(i);
     58 }
     59 
     60 runTest();
     61 
     62 // ----------------------------------------------------------------------
     63 // Array length accessor.
     64 // ----------------------------------------------------------------------
     65 runTest = function() {
     66   var initial_L = 'length';
     67   var L = initial_L;
     68   var zero = '0';
     69 
     70   var a = new Array(10);
     71 
     72   function arrayLengthTest(change_index) {
     73     for (var i = 0; i < 10; i++) {
     74       var l = a[L];
     75       if (i <= change_index) {
     76         assertEquals(10, l);
     77       } else {
     78         assertEquals(undefined, l);
     79       }
     80       if (i == change_index) L = zero;
     81     }
     82     L = initial_L;
     83   }
     84 
     85   for (var i = 0; i < 10; i++) arrayLengthTest(i);
     86 }
     87 
     88 runTest();
     89 
     90 // ----------------------------------------------------------------------
     91 // String length accessor.
     92 // ----------------------------------------------------------------------
     93 runTest = function() {
     94   var initial_L = 'length';
     95   var L = initial_L;
     96   var zero = '0';
     97 
     98   var s = "asdf"
     99 
    100   function stringLengthTest(change_index) {
    101     for (var i = 0; i < 10; i++) {
    102       var l = s[L];
    103       if (i <= change_index) {
    104         assertEquals(4, l);
    105       } else {
    106         assertEquals('a', l);
    107       }
    108       if (i == change_index) L = zero;
    109     }
    110     L = initial_L;
    111   }
    112 
    113   for (var i = 0; i < 10; i++) stringLengthTest(i);
    114 }
    115 
    116 runTest();
    117 
    118 // ----------------------------------------------------------------------
    119 // Field access.
    120 // ----------------------------------------------------------------------
    121 runTest = function() {
    122   var o = { x: 42, y: 43 }
    123 
    124   var initial_X = 'x';
    125   var X = initial_X;
    126   var Y = 'y';
    127 
    128   function fieldTest(change_index) {
    129     for (var i = 0; i < 10; i++) {
    130       var property = o[X];
    131       if (i <= change_index) {
    132         assertEquals(42, property);
    133       } else {
    134         assertEquals(43, property);
    135       }
    136       if (i == change_index) X = Y;
    137     }
    138     X = initial_X;
    139   };
    140 
    141   for (var i = 0; i < 10; i++) fieldTest(i);
    142 }
    143 
    144 runTest();
    145 
    146 
    147 // ----------------------------------------------------------------------
    148 // Indexed access.
    149 // ----------------------------------------------------------------------
    150 runTest = function() {
    151   var o = [ 42, 43 ];
    152 
    153   var initial_X = 0;
    154   var X = initial_X;
    155   var Y = 1;
    156 
    157   function fieldTest(change_index) {
    158     for (var i = 0; i < 10; i++) {
    159       var property = o[X];
    160       if (i <= change_index) {
    161         assertEquals(42, property);
    162       } else {
    163         assertEquals(43, property);
    164       }
    165       if (i == change_index) X = Y;
    166     }
    167     X = initial_X;
    168   };
    169 
    170   for (var i = 0; i < 10; i++) fieldTest(i);
    171 }
    172 
    173 runTest();
    174 
    175 
    176 // ----------------------------------------------------------------------
    177 // Constant function access.
    178 // ----------------------------------------------------------------------
    179 runTest = function() {
    180   function fun() { };
    181 
    182   var o = new Object();
    183   o.f = fun;
    184   o.x = 42;
    185 
    186   var initial_F = 'f';
    187   var F = initial_F;
    188   var X = 'x'
    189 
    190   function constantFunctionTest(change_index) {
    191     for (var i = 0; i < 10; i++) {
    192       var property = o[F];
    193       if (i <= change_index) {
    194         assertEquals(fun, property);
    195       } else {
    196         assertEquals(42, property);
    197       }
    198       if (i == change_index) F = X;
    199     }
    200     F = initial_F;
    201   };
    202 
    203   for (var i = 0; i < 10; i++) constantFunctionTest(i);
    204 }
    205 
    206 runTest();
    207 
    208 // ----------------------------------------------------------------------
    209 // Keyed store field.
    210 // ----------------------------------------------------------------------
    211 
    212 runTest = function() {
    213   var o = { x: 42, y: 43 }
    214 
    215   var initial_X = 'x';
    216   var X = initial_X;
    217   var Y = 'y';
    218 
    219   function fieldTest(change_index) {
    220     for (var i = 0; i < 10; i++) {
    221       o[X] = X;
    222       var property = o[X];
    223       if (i <= change_index) {
    224         assertEquals('x', property);
    225       } else {
    226         assertEquals('y', property);
    227       }
    228       if (i == change_index) X = Y;
    229     }
    230     X = initial_X;
    231   };
    232 
    233   for (var i = 0; i < 10; i++) fieldTest(i);
    234 }
    235 
    236 runTest();
    237