blob: 808ac5829e7a006f6e7179a0b27ea72b7d7236a2 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// 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// Tests loading of properties across eval calls.
29
30var x = 1;
Kristian Monsen25f61362010-05-21 11:50:48 +010031function global_function() { return 'global'; }
Steve Blocka7e24c12009-10-30 11:49:00 +000032
33// Test loading across an eval call that does not shadow variables.
34function testNoShadowing() {
35 var y = 2;
Kristian Monsen25f61362010-05-21 11:50:48 +010036 function local_function() { return 'local'; }
Steve Blocka7e24c12009-10-30 11:49:00 +000037 function f() {
38 eval('1');
39 assertEquals(1, x);
Kristian Monsen25f61362010-05-21 11:50:48 +010040 try { typeof(asdf); } catch(e) { assertUnreachable(); }
Steve Blocka7e24c12009-10-30 11:49:00 +000041 assertEquals(2, y);
Kristian Monsen25f61362010-05-21 11:50:48 +010042 assertEquals('global', global_function());
43 assertEquals('local', local_function());
Steve Blocka7e24c12009-10-30 11:49:00 +000044 function g() {
45 assertEquals(1, x);
Kristian Monsen25f61362010-05-21 11:50:48 +010046 try { typeof(asdf); } catch(e) { assertUnreachable(); }
Steve Blocka7e24c12009-10-30 11:49:00 +000047 assertEquals(2, y);
Kristian Monsen25f61362010-05-21 11:50:48 +010048 assertEquals('global', global_function());
49 assertEquals('local', local_function());
Steve Blocka7e24c12009-10-30 11:49:00 +000050 }
51 g();
52 }
53 f();
54}
55
56testNoShadowing();
57
58// Test loading across eval calls that do not shadow variables.
59function testNoShadowing2() {
60 var y = 2;
Kristian Monsen25f61362010-05-21 11:50:48 +010061 function local_function() { return 'local'; }
Steve Blocka7e24c12009-10-30 11:49:00 +000062 eval('1');
63 function f() {
64 eval('1');
65 assertEquals(1, x);
66 assertEquals(2, y);
Kristian Monsen25f61362010-05-21 11:50:48 +010067 assertEquals('global', global_function());
68 assertEquals('local', local_function());
Steve Blocka7e24c12009-10-30 11:49:00 +000069 function g() {
70 assertEquals(1, x);
71 assertEquals(2, y);
Kristian Monsen25f61362010-05-21 11:50:48 +010072 assertEquals('global', global_function());
73 assertEquals('local', local_function());
Steve Blocka7e24c12009-10-30 11:49:00 +000074 }
75 g();
76 }
77 f();
78}
79
80testNoShadowing2();
81
82// Test loading across an eval call that shadows variables.
83function testShadowing() {
84 var y = 2;
Kristian Monsen25f61362010-05-21 11:50:48 +010085 function local_function() { return 'local'; }
Steve Blocka7e24c12009-10-30 11:49:00 +000086 function f() {
87 eval('var x = 3; var y = 4;');
88 assertEquals(3, x);
89 assertEquals(4, y);
Kristian Monsen25f61362010-05-21 11:50:48 +010090 eval('function local_function() { return "new_local"; }');
91 eval('function global_function() { return "new_nonglobal"; }');
92 assertEquals('new_nonglobal', global_function());
93 assertEquals('new_local', local_function());
Steve Blocka7e24c12009-10-30 11:49:00 +000094 function g() {
95 assertEquals(3, x);
96 assertEquals(4, y);
Kristian Monsen25f61362010-05-21 11:50:48 +010097 assertEquals('new_nonglobal', global_function());
98 assertEquals('new_local', local_function());
Steve Blocka7e24c12009-10-30 11:49:00 +000099 }
100 g();
101 }
102 f();
103}
104
105testShadowing();