1a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Copyright 2009 the V8 project authors. All rights reserved.
2a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Redistribution and use in source and binary forms, with or without
3a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// modification, are permitted provided that the following conditions are
4a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// met:
5a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
6a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//     * Redistributions of source code must retain the above copyright
7a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       notice, this list of conditions and the following disclaimer.
8a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//     * Redistributions in binary form must reproduce the above
9a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       copyright notice, this list of conditions and the following
10a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       disclaimer in the documentation and/or other materials provided
11a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       with the distribution.
12a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//     * Neither the name of Google Inc. nor the names of its
13a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       contributors may be used to endorse or promote products derived
14a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       from this software without specific prior written permission.
15a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
16a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
288b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdoch// Flags: --allow-natives-syntax
298b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdoch
30a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Test the handling of initialization of deleted const variables.
31a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// This only makes sense in local scopes since the declaration and
32a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// initialization of consts in the global scope happen at the same
33a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// time.
34a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
35a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfunction testIntroduceGlobal() {
36a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  var source =
37a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      // Deleting 'x' removes the local const property.
38a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      "delete x;" +
39a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      // Initialization turns into assignment to global 'x'.
40a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      "const x = 3; assertEquals(3, x);" +
41a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      // No constness of the global 'x'.
42a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      "x = 4; assertEquals(4, x);";
43a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  eval(source);
44a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
45a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
46a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlocktestIntroduceGlobal();
47a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(4, x);
48a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
49a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfunction testAssignExistingGlobal() {
50a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  var source =
51a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      // Delete 'x' to remove the local const property.
52a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      "delete x;" +
53a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      // Initialization turns into assignment to global 'x'.
54a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      "const x = 5; assertEquals(5, x);" +
55a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      // No constness of the global 'x'.
56a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block      "x = 6; assertEquals(6, x);";
57a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  eval(source);
58a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
59a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
60a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlocktestAssignExistingGlobal();
61a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(6, x);
62a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
63a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfunction testAssignmentArgument(x) {
64a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  function local() {
65a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    var source = "delete x; const x = 7; assertEquals(7, x)";
66a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    eval(source);
67a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
68a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  local();
69a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertEquals(7, x);
70a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
71a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
728b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdochfor (var i = 0; i < 5; i++) {
7380d68eab642096c1a48b6474d6ec33064b0ad1f5Kristian Monsen  testAssignmentArgument();
7480d68eab642096c1a48b6474d6ec33064b0ad1f5Kristian Monsen}
758b112d2025046f85ef7f6be087c6129c872ebad2Ben Murdoch%OptimizeFunctionOnNextCall(testAssignmentArgument);
768b112d2025046f85ef7f6be087c6129c872ebad2Ben MurdochtestAssignmentArgument();
77a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertEquals(6, x);
78a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
79a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block__defineSetter__('x', function() { throw 42; });
80a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfunction testAssignGlobalThrows() {
81a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Initialization turns into assignment to global 'x' which
82a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // throws an exception.
83a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  var source = "delete x; const x = 8";
84a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  eval(source);
85a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
86a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
87a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlockassertThrows("testAssignGlobalThrows()");
88a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
89a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfunction testInitFastCaseExtension() {
90a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  var source = "const x = 9; assertEquals(9, x); x = 10; assertEquals(9, x)";
91a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  eval(source);
92a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
93a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
94a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlocktestInitFastCaseExtension();
95a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
96a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfunction testInitSlowCaseExtension() {
97a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  var source = "";
98a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // Introduce 100 properties on the context extension object to force
99a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  // it in slow case.
1007f4d5bd8c03935e2c0cd412e561b8fc5a6a880aeBen Murdoch  for (var i = 0; i < 100; i++) source += ("var a" + i + " = " + i + ";");
101a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  source += "const x = 10; assertEquals(10, x); x = 11; assertEquals(10, x)";
102a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  eval(source);
103a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
104a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
105a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlocktestInitSlowCaseExtension();
106a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
107a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blockfunction testAssignSurroundingContextSlot() {
108a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  var x = 12;
109a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  function local() {
110a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    var source = "delete x; const x = 13; assertEquals(13, x)";
111a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block    eval(source);
112a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  }
113a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  local();
114a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block  assertEquals(13, x);
115a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block}
116a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
117a7e24c173cf37484693b9abb38e494fa7bd7baebSteve BlocktestAssignSurroundingContextSlot();
118