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