Home | History | Annotate | Download | only in harmony
      1 // Copyright 2011 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: --harmony-scoping
     29 
     30 // Test that we throw early syntax errors in harmony mode
     31 // when using an immutable binding in an assigment or with
     32 // prefix/postfix decrement/increment operators.
     33 
     34 "use strict";
     35 
     36 // Function local const.
     37 function constDecl0(use) {
     38   return "(function() { const constvar = 1; " + use + "; });";
     39 }
     40 
     41 
     42 function constDecl1(use) {
     43   return "(function() { " + use + "; const constvar = 1; });";
     44 }
     45 
     46 
     47 // Function local const, assign from eval.
     48 function constDecl2(use) {
     49   use = "eval('(function() { " + use + " })')";
     50   return "(function() { const constvar = 1; " + use + "; })();";
     51 }
     52 
     53 
     54 function constDecl3(use) {
     55   use = "eval('(function() { " + use + " })')";
     56   return "(function() { " + use + "; const constvar = 1; })();";
     57 }
     58 
     59 
     60 // Block local const.
     61 function constDecl4(use) {
     62   return "(function() { { const constvar = 1; " + use + "; } });";
     63 }
     64 
     65 
     66 function constDecl5(use) {
     67   return "(function() { { " + use + "; const constvar = 1; } });";
     68 }
     69 
     70 
     71 // Block local const, assign from eval.
     72 function constDecl6(use) {
     73   use = "eval('(function() {" + use + "})')";
     74   return "(function() { { const constvar = 1; " + use + "; } })();";
     75 }
     76 
     77 
     78 function constDecl7(use) {
     79   use = "eval('(function() {" + use + "})')";
     80   return "(function() { { " + use + "; const constvar = 1; } })();";
     81 }
     82 
     83 
     84 // Function expression name.
     85 function constDecl8(use) {
     86   return "(function constvar() { " + use + "; });";
     87 }
     88 
     89 
     90 // Function expression name, assign from eval.
     91 function constDecl9(use) {
     92   use = "eval('(function(){" + use + "})')";
     93   return "(function constvar() { " + use + "; })();";
     94 }
     95 
     96 let decls = [ constDecl0,
     97               constDecl1,
     98               constDecl2,
     99               constDecl3,
    100               constDecl4,
    101               constDecl5,
    102               constDecl6,
    103               constDecl7,
    104               constDecl8,
    105               constDecl9
    106               ];
    107 let uses = [ 'constvar = 1;',
    108              'constvar += 1;',
    109              '++constvar;',
    110              'constvar++;'
    111              ];
    112 
    113 function Test(d,u) {
    114   'use strict';
    115   try {
    116     print(d(u));
    117     eval(d(u));
    118   } catch (e) {
    119     assertInstanceof(e, SyntaxError);
    120     assertTrue(e.toString().indexOf("Assignment to constant variable") >= 0);
    121     return;
    122   }
    123   assertUnreachable();
    124 }
    125 
    126 for (var d = 0; d < decls.length; ++d) {
    127   for (var u = 0; u < uses.length; ++u) {
    128     Test(decls[d], uses[u]);
    129   }
    130 }
    131