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// Tests loading of properties across eval calls.
31
32var x = 1;
33function global_function() { return 'global'; }
34const const_uninitialized;
35const const_initialized = function() { return "const_global"; }
36
37// Test loading across an eval call that does not shadow variables.
38function testNoShadowing() {
39  var y = 2;
40  function local_function() { return 'local'; }
41  const local_const_uninitialized;
42  const local_const_initialized = function() { return "const_local"; }
43  function f() {
44    eval('1');
45    assertEquals(1, x);
46    try { typeof(asdf); } catch(e) { assertUnreachable(); }
47    assertEquals(2, y);
48    assertEquals('global', global_function());
49    assertEquals('local', local_function());
50    var exception = false;
51    try {
52      const_uninitialized();
53    } catch(e) {
54      exception = true;
55    }
56    assertTrue(exception);
57    assertEquals('const_global', const_initialized());
58    exception = false;
59    try {
60      local_const_uninitialized();
61    } catch(e) {
62      exception = true;
63    }
64    assertTrue(exception);
65    assertEquals('const_local', local_const_initialized());
66    function g() {
67      assertEquals(1, x);
68      try { typeof(asdf); } catch(e) { assertUnreachable(); }
69      assertEquals(2, y);
70      assertEquals('global', global_function());
71      assertEquals('local', local_function());
72      var exception = false;
73      try {
74        const_uninitialized();
75      } catch(e) {
76        exception = true;
77      }
78      assertTrue(exception);
79      assertEquals('const_global', const_initialized());
80      exception = false;
81      try {
82        local_const_uninitialized();
83      } catch(e) {
84        exception = true;
85      }
86      assertTrue(exception);
87      assertEquals('const_local', local_const_initialized());
88    }
89    g();
90  }
91  f();
92}
93
94testNoShadowing();
95
96// Test loading across eval calls that do not shadow variables.
97function testNoShadowing2() {
98  var y = 2;
99  function local_function() { return 'local'; }
100  eval('1');
101  function f() {
102    eval('1');
103    assertEquals(1, x);
104    assertEquals(2, y);
105    assertEquals('global', global_function());
106    assertEquals('local', local_function());
107    function g() {
108      assertEquals(1, x);
109      assertEquals(2, y);
110      assertEquals('global', global_function());
111      assertEquals('local', local_function());
112    }
113    g();
114  }
115  f();
116}
117
118testNoShadowing2();
119
120// Test loading across an eval call that shadows variables.
121function testShadowing() {
122  var y = 2;
123  function local_function() { return 'local'; }
124  function f() {
125    eval('var x = 3; var y = 4;');
126    assertEquals(3, x);
127    assertEquals(4, y);
128    eval('function local_function() { return "new_local"; }');
129    eval('function global_function() { return "new_nonglobal"; }');
130    assertEquals('new_nonglobal', global_function());
131    assertEquals('new_local', local_function());
132    function g() {
133      assertEquals(3, x);
134      assertEquals(4, y);
135      assertEquals('new_nonglobal', global_function());
136      assertEquals('new_local', local_function());
137    }
138    g();
139  }
140  f();
141}
142
143testShadowing();
144