Home | History | Annotate | Download | only in mjsunit
      1 // Copyright 2009 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 // Test handling of const variables in various settings.
     29 
     30 (function () {
     31   function f() {
     32     function g() {
     33       x = 42;  //  should be ignored
     34       return x;  // force x into context
     35     }
     36     x = 43;  // should be ignored
     37     assertEquals(undefined, g());
     38     x = 44;  // should be ignored
     39     const x = 0;
     40     x = 45;  // should be ignored
     41     assertEquals(0, g());
     42   }
     43   f();
     44 })();
     45 
     46 
     47 (function () {
     48   function f() {
     49     function g() {
     50       with ({foo: 0}) {
     51         x = 42;  //  should be ignored
     52         return x;  // force x into context
     53       }
     54     }
     55     x = 43;  // should be ignored
     56     assertEquals(undefined, g());
     57     x = 44;  // should be ignored
     58     const x = 0;
     59     x = 45;  // should be ignored
     60     assertEquals(0, g());
     61   }
     62   f();
     63 })();
     64 
     65 
     66 (function () {
     67   function f() {
     68     function g(s) {
     69       eval(s);
     70       return x;  // force x into context
     71     }
     72     x = 43;  // should be ignored
     73     assertEquals(undefined, g("x = 42;"));
     74     x = 44;  // should be ignored
     75     const x = 0;
     76     x = 45;  // should be ignored
     77     assertEquals(0, g("x = 46;"));
     78   }
     79   f();
     80 })();
     81 
     82 
     83 (function () {
     84   function f() {
     85     function g(s) {
     86       with ({foo: 0}) {
     87         eval(s);
     88         return x;  // force x into context
     89       }
     90     }
     91     x = 43;  // should be ignored
     92     assertEquals(undefined, g("x = 42;"));
     93     x = 44;  // should be ignored
     94     const x = 0;
     95     x = 45;  // should be ignored
     96     assertEquals(0, g("x = 46;"));
     97   }
     98   f();
     99 })();
    100 
    101 
    102 (function () {
    103   function f(s) {
    104     function g() {
    105       x = 42;  // assign to global x, or to const x
    106       return x;
    107     }
    108     x = 43;  // declare global x
    109     assertEquals(42, g());
    110     x = 44;  // assign to global x
    111     eval(s);
    112     x = 45;  // should be ignored (assign to const x)
    113     assertEquals(0, g());
    114   }
    115   f("const x = 0;");
    116 })();
    117 
    118 
    119 (function () {
    120   function f(s) {
    121     function g() {
    122       with ({foo: 0}) {
    123         x = 42;  // assign to global x, or to const x
    124         return x;
    125       }
    126     }
    127     x = 43;  // declare global x
    128     assertEquals(42, g());
    129     x = 44;  // assign to global x
    130     eval(s);
    131     x = 45;  // should be ignored (assign to const x)
    132     assertEquals(0, g());
    133   }
    134   f("const x = 0;");
    135 })();
    136 
    137 
    138 (function () {
    139   function f(s) {
    140     function g(s) {
    141       eval(s);
    142       return x;
    143     }
    144     x = 43;  // declare global x
    145     assertEquals(42, g("x = 42;"));
    146     x = 44;  // assign to global x
    147     eval(s);
    148     x = 45;  // should be ignored (assign to const x)
    149     assertEquals(0, g("x = 46;"));
    150   }
    151   f("const x = 0;");
    152 })();
    153 
    154 
    155 (function () {
    156   function f(s) {
    157     function g(s) {
    158       with ({foo: 0}) {
    159         eval(s);
    160         return x;
    161       }
    162     }
    163     x = 43;  // declare global x
    164     assertEquals(42, g("x = 42;"));
    165     x = 44;  // assign to global x
    166     eval(s);
    167     x = 45;  // should be ignored (assign to const x)
    168     assertEquals(0, g("x = 46;"));
    169   }
    170   f("const x = 0;");
    171 })();
    172 
    173