block-const-assign.js revision 3ef787dbeca8a5fb1086949cda830dccee07bfbd
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// TODO(ES6): properly activate extended mode
34"use strict";
35
36
37// Function local const.
38function constDecl0(use) {
39  return "(function() { const constvar = 1; " + use + "; });";
40}
41
42
43function constDecl1(use) {
44  return "(function() { " + use + "; const constvar = 1; });";
45}
46
47
48// Function local const, assign from eval.
49function constDecl2(use) {
50  use = "eval('(function() { " + use + " })')";
51  return "(function() { const constvar = 1; " + use + "; })();";
52}
53
54
55function constDecl3(use) {
56  use = "eval('(function() { " + use + " })')";
57  return "(function() { " + use + "; const constvar = 1; })();";
58}
59
60
61// Block local const.
62function constDecl4(use) {
63  return "(function() { { const constvar = 1; " + use + "; } });";
64}
65
66
67function constDecl5(use) {
68  return "(function() { { " + use + "; const constvar = 1; } });";
69}
70
71
72// Block local const, assign from eval.
73function constDecl6(use) {
74  use = "eval('(function() {" + use + "})')";
75  return "(function() { { const constvar = 1; " + use + "; } })();";
76}
77
78
79function constDecl7(use) {
80  use = "eval('(function() {" + use + "})')";
81  return "(function() { { " + use + "; const constvar = 1; } })();";
82}
83
84
85// Function expression name.
86function constDecl8(use) {
87  return "(function constvar() { " + use + "; });";
88}
89
90
91// Function expression name, assign from eval.
92function constDecl9(use) {
93  use = "eval('(function(){" + use + "})')";
94  return "(function constvar() { " + use + "; })();";
95}
96
97let decls = [ constDecl0,
98              constDecl1,
99              constDecl2,
100              constDecl3,
101              constDecl4,
102              constDecl5,
103              constDecl6,
104              constDecl7,
105              constDecl8,
106              constDecl9
107              ];
108let uses = [ 'constvar = 1;',
109             'constvar += 1;',
110             '++constvar;',
111             'constvar++;'
112             ];
113
114function Test(d,u) {
115  'use strict';
116  try {
117    print(d(u));
118    eval(d(u));
119  } catch (e) {
120    assertInstanceof(e, SyntaxError);
121    assertTrue(e.toString().indexOf("Assignment to constant variable") >= 0);
122    return;
123  }
124  assertUnreachable();
125}
126
127for (var d = 0; d < decls.length; ++d) {
128  for (var u = 0; u < uses.length; ++u) {
129    Test(decls[d], uses[u]);
130  }
131}
132