const-eval-init.js revision 7f4d5bd8c03935e2c0cd412e561b8fc5a6a880ae
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 the handling of initialization of deleted const variables.
29// This only makes sense in local scopes since the declaration and
30// initialization of consts in the global scope happen at the same
31// time.
32
33function testIntroduceGlobal() {
34  var source =
35      // Deleting 'x' removes the local const property.
36      "delete x;" +
37      // Initialization turns into assignment to global 'x'.
38      "const x = 3; assertEquals(3, x);" +
39      // No constness of the global 'x'.
40      "x = 4; assertEquals(4, x);";
41  eval(source);
42}
43
44testIntroduceGlobal();
45assertEquals(4, x);
46
47function testAssignExistingGlobal() {
48  var source =
49      // Delete 'x' to remove the local const property.
50      "delete x;" +
51      // Initialization turns into assignment to global 'x'.
52      "const x = 5; assertEquals(5, x);" +
53      // No constness of the global 'x'.
54      "x = 6; assertEquals(6, x);";
55  eval(source);
56}
57
58testAssignExistingGlobal();
59assertEquals(6, x);
60
61function testAssignmentArgument(x) {
62  function local() {
63    var source = "delete x; const x = 7; assertEquals(7, x)";
64    eval(source);
65  }
66  local();
67  assertEquals(7, x);
68}
69
70testAssignmentArgument();
71assertEquals(6, x);
72
73__defineSetter__('x', function() { throw 42; });
74function testAssignGlobalThrows() {
75  // Initialization turns into assignment to global 'x' which
76  // throws an exception.
77  var source = "delete x; const x = 8";
78  eval(source);
79}
80
81assertThrows("testAssignGlobalThrows()");
82
83function testInitFastCaseExtension() {
84  var source = "const x = 9; assertEquals(9, x); x = 10; assertEquals(9, x)";
85  eval(source);
86}
87
88testInitFastCaseExtension();
89
90function testInitSlowCaseExtension() {
91  var source = "";
92  // Introduce 100 properties on the context extension object to force
93  // it in slow case.
94  for (var i = 0; i < 100; i++) source += ("var a" + i + " = " + i + ";");
95  source += "const x = 10; assertEquals(10, x); x = 11; assertEquals(10, x)";
96  eval(source);
97}
98
99testInitSlowCaseExtension();
100
101function testAssignSurroundingContextSlot() {
102  var x = 12;
103  function local() {
104    var source = "delete x; const x = 13; assertEquals(13, x)";
105    eval(source);
106  }
107  local();
108  assertEquals(13, x);
109}
110
111testAssignSurroundingContextSlot();
112