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
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.
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.
28 // Tests loading of properties across eval calls.
31 function global_function() { return 'global'; }
32 const const_uninitialized;
33 const const_initialized = function() { return "const_global"; }
35 // Test loading across an eval call that does not shadow variables.
36 function testNoShadowing() {
38 function local_function() { return 'local'; }
39 const local_const_uninitialized;
40 const local_const_initialized = function() { return "const_local"; }
44 try { typeof(asdf); } catch(e) { assertUnreachable(); }
46 assertEquals('global', global_function());
47 assertEquals('local', local_function());
48 var exception = false;
50 const_uninitialized();
54 assertTrue(exception);
55 assertEquals('const_global', const_initialized());
58 local_const_uninitialized();
62 assertTrue(exception);
63 assertEquals('const_local', local_const_initialized());
66 try { typeof(asdf); } catch(e) { assertUnreachable(); }
68 assertEquals('global', global_function());
69 assertEquals('local', local_function());
70 var exception = false;
72 const_uninitialized();
76 assertTrue(exception);
77 assertEquals('const_global', const_initialized());
80 local_const_uninitialized();
84 assertTrue(exception);
85 assertEquals('const_local', local_const_initialized());
94 // Test loading across eval calls that do not shadow variables.
95 function testNoShadowing2() {
97 function local_function() { return 'local'; }
103 assertEquals('global', global_function());
104 assertEquals('local', local_function());
108 assertEquals('global', global_function());
109 assertEquals('local', local_function());
118 // Test loading across an eval call that shadows variables.
119 function testShadowing() {
121 function local_function() { return 'local'; }
123 eval('var x = 3; var y = 4;');
126 eval('function local_function() { return "new_local"; }');
127 eval('function global_function() { return "new_nonglobal"; }');
128 assertEquals('new_nonglobal', global_function());
129 assertEquals('new_local', local_function());
133 assertEquals('new_nonglobal', global_function());
134 assertEquals('new_local', local_function());