Home | History | Annotate | Download | only in compiler
      1 // Copyright 2010 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 // Tests for compound assignments at the top level
     29 
     30 z = 2;
     31 z += 4;
     32 
     33 assertEquals(z, 6);
     34 
     35 a = new Array(10);
     36 
     37 a[2] += 7;
     38 a[2] = 15;
     39 a[2] += 2;
     40 
     41 assertEquals(17, a[2]);
     42 
     43 b = new Object();
     44 b.foo = 5;
     45 b.foo += 12;
     46 
     47 assertEquals(17, b.foo);
     48 
     49 // Test compound assignments in an anonymous function with local variables.
     50 (function () {
     51   var z = 2;
     52   z += 4;
     53 
     54   assertEquals(z, 6);
     55 
     56   var a = new Array(10);
     57 
     58   a[2] += 7;
     59   a[2] = 15;
     60   a[2] += 2;
     61 
     62   assertEquals(17, a[2]);
     63 
     64   var b = new Object();
     65   b.foo = 5;
     66   b.foo += 12;
     67 
     68   assertEquals(17, b.foo);
     69 })();
     70 
     71 // Test compound assignments in an anonymous function with global variables.
     72 (function () {
     73   z = 2;
     74   z += 4;
     75 
     76   assertEquals(z, 6);
     77 
     78   a = new Array(10);
     79 
     80   a[2] += 7;
     81   a[2] = 15;
     82   a[2] += 2;
     83 
     84   assertEquals(17, a[2]);
     85 
     86   b = new Object();
     87   b.foo = 5;
     88   b.foo += 12;
     89 
     90   assertEquals(17, b.foo);
     91 })();
     92 
     93 // Test compound assignments in a named function with local variables.
     94 function foo() {
     95   var z = 3;
     96   z += 4;
     97 
     98   assertEquals(z, 7);
     99 
    100   var a = new Array(10);
    101 
    102   a[2] += 7;
    103   a[2] = 15;
    104   a[2] += 2;
    105 
    106   assertEquals(17, a[2]);
    107 
    108   var b = new Object();
    109   b.foo = 5;
    110   b.foo += 12;
    111 
    112   assertEquals(17, b.foo);
    113 }
    114 
    115 foo();
    116 
    117 // Test compound assignments in a named function with global variables.
    118 function bar() {
    119   z = 2;
    120   z += 5;
    121 
    122   assertEquals(z, 7);
    123 
    124   a = new Array(10);
    125 
    126   a[2] += 7;
    127   a[2] = 15;
    128   a[2] += 2;
    129 
    130   assertEquals(17, a[2]);
    131 
    132   b = new Object();
    133   b.foo = 5;
    134   b.foo += 12;
    135 
    136   assertEquals(17, b.foo);
    137 }
    138 
    139 bar();
    140 
    141 // Entire series of tests repeated, in loops.
    142 // -------------------------------------------
    143 // Tests for compound assignments in a loop at the top level
    144 
    145 for (i = 0; i < 5; ++i) {
    146   z = 2;
    147   z += 4;
    148 
    149   assertEquals(z, 6);
    150 
    151   a = new Array(10);
    152 
    153   a[2] += 7;
    154   a[2] = 15;
    155   a[2] += 2;
    156 
    157   assertEquals(17, a[2]);
    158 
    159   b = new Object();
    160   b.foo = 5;
    161   b.foo += 12;
    162 
    163   assertEquals(17, b.foo);
    164 }
    165 
    166 // Test compound assignments in an anonymous function with local variables.
    167 (function () {
    168   for (var i = 0; i < 5; ++i) {
    169     var z = 2;
    170     z += 4;
    171 
    172     assertEquals(z, 6);
    173 
    174     var a = new Array(10);
    175 
    176     a[2] += 7;
    177     a[2] = 15;
    178     a[2] += 2;
    179 
    180     assertEquals(17, a[2]);
    181 
    182     var b = new Object();
    183     b.foo = 5;
    184     b.foo += 12;
    185 
    186     assertEquals(17, b.foo);
    187   }
    188 })();
    189 
    190 // Test compound assignments in an anonymous function with global variables.
    191 (function () {
    192   for (i = 0; i < 5; ++i) {
    193     z = 2;
    194     z += 4;
    195 
    196     assertEquals(z, 6);
    197 
    198     a = new Array(10);
    199 
    200     a[2] += 7;
    201     a[2] = 15;
    202     a[2] += 2;
    203 
    204     assertEquals(17, a[2]);
    205 
    206     b = new Object();
    207     b.foo = 5;
    208     b.foo += 12;
    209 
    210     assertEquals(17, b.foo);
    211   }
    212 })();
    213 
    214 // Test compound assignments in a named function with local variables.
    215 function foo_loop() {
    216   for (i = 0; i < 5; ++i) {
    217     var z = 3;
    218     z += 4;
    219 
    220     assertEquals(z, 7);
    221 
    222     var a = new Array(10);
    223 
    224     a[2] += 7;
    225     a[2] = 15;
    226     a[2] += 2;
    227 
    228     assertEquals(17, a[2]);
    229 
    230     var b = new Object();
    231     b.foo = 5;
    232     b.foo += 12;
    233 
    234     assertEquals(17, b.foo);
    235   }
    236 }
    237 
    238 foo_loop();
    239 
    240 // Test compound assignments in a named function with global variables.
    241 function bar_loop() {
    242   for (i = 0; i < 5; ++i) {
    243     z = 2;
    244     z += 5;
    245 
    246     assertEquals(z, 7);
    247 
    248     a = new Array(10);
    249 
    250     a[2] += 7;
    251     a[2] = 15;
    252     a[2] += 2;
    253 
    254     assertEquals(17, a[2]);
    255 
    256     b = new Object();
    257     b.foo = 5;
    258     b.foo += 12;
    259 
    260     assertEquals(17, b.foo);
    261   }
    262 }
    263 
    264 bar_loop();
    265 
    266 
    267 // Test assignment in test context.
    268 function test_assign(x, y) { if (x = y) return x; }
    269 
    270 assertEquals(42, test_assign(0, 42));
    271 
    272 assertEquals("undefined", typeof test_assign(42, 0));
    273 
    274 // Test for assignment using a keyed store ic:
    275 function store_i_in_element_i_of_object_i() {
    276   var i = new Object();
    277   i[i] = i;
    278 }
    279 
    280 // Run three times to exercise caches.
    281 store_i_in_element_i_of_object_i();
    282 store_i_in_element_i_of_object_i();
    283 store_i_in_element_i_of_object_i();
    284