blob: 98b621e7922588acf0e5a73af91dc487a0ac2c6c [file] [log] [blame]
ager@chromium.org381abbb2009-02-25 13:23:22 +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;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000031function global_function() { return 'global'; }
32const const_uninitialized;
33const const_initialized = function() { return "const_global"; }
ager@chromium.org381abbb2009-02-25 13:23:22 +000034
35// Test loading across an eval call that does not shadow variables.
36function testNoShadowing() {
37 var y = 2;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000038 function local_function() { return 'local'; }
39 const local_const_uninitialized;
40 const local_const_initialized = function() { return "const_local"; }
ager@chromium.org381abbb2009-02-25 13:23:22 +000041 function f() {
42 eval('1');
43 assertEquals(1, x);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000044 try { typeof(asdf); } catch(e) { assertUnreachable(); }
ager@chromium.org381abbb2009-02-25 13:23:22 +000045 assertEquals(2, y);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000046 assertEquals('global', global_function());
47 assertEquals('local', local_function());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +000048 var exception = false;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000049 try {
50 const_uninitialized();
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000051 } catch(e) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +000052 exception = true;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000053 }
ricow@chromium.orgd2be9012011-06-01 06:00:58 +000054 assertTrue(exception);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000055 assertEquals('const_global', const_initialized());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +000056 exception = false;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000057 try {
58 local_const_uninitialized();
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000059 } catch(e) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +000060 exception = true;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000061 }
ricow@chromium.orgd2be9012011-06-01 06:00:58 +000062 assertTrue(exception);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000063 assertEquals('const_local', local_const_initialized());
ager@chromium.org381abbb2009-02-25 13:23:22 +000064 function g() {
65 assertEquals(1, x);
vegorov@chromium.orgdff694e2010-05-17 09:10:26 +000066 try { typeof(asdf); } catch(e) { assertUnreachable(); }
ager@chromium.org381abbb2009-02-25 13:23:22 +000067 assertEquals(2, y);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000068 assertEquals('global', global_function());
69 assertEquals('local', local_function());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +000070 var exception = false;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000071 try {
72 const_uninitialized();
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000073 } catch(e) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +000074 exception = true;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000075 }
ricow@chromium.orgd2be9012011-06-01 06:00:58 +000076 assertTrue(exception);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000077 assertEquals('const_global', const_initialized());
ricow@chromium.orgd2be9012011-06-01 06:00:58 +000078 exception = false;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000079 try {
80 local_const_uninitialized();
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000081 } catch(e) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +000082 exception = true;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000083 }
ricow@chromium.orgd2be9012011-06-01 06:00:58 +000084 assertTrue(exception);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000085 assertEquals('const_local', local_const_initialized());
ager@chromium.org381abbb2009-02-25 13:23:22 +000086 }
87 g();
88 }
89 f();
90}
91
92testNoShadowing();
93
94// Test loading across eval calls that do not shadow variables.
95function testNoShadowing2() {
96 var y = 2;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +000097 function local_function() { return 'local'; }
ager@chromium.org381abbb2009-02-25 13:23:22 +000098 eval('1');
99 function f() {
100 eval('1');
101 assertEquals(1, x);
102 assertEquals(2, y);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000103 assertEquals('global', global_function());
104 assertEquals('local', local_function());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000105 function g() {
106 assertEquals(1, x);
107 assertEquals(2, y);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000108 assertEquals('global', global_function());
109 assertEquals('local', local_function());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000110 }
111 g();
112 }
113 f();
114}
115
116testNoShadowing2();
117
118// Test loading across an eval call that shadows variables.
119function testShadowing() {
120 var y = 2;
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000121 function local_function() { return 'local'; }
ager@chromium.org381abbb2009-02-25 13:23:22 +0000122 function f() {
123 eval('var x = 3; var y = 4;');
124 assertEquals(3, x);
125 assertEquals(4, y);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000126 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());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000130 function g() {
131 assertEquals(3, x);
132 assertEquals(4, y);
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +0000133 assertEquals('new_nonglobal', global_function());
134 assertEquals('new_local', local_function());
ager@chromium.org381abbb2009-02-25 13:23:22 +0000135 }
136 g();
137 }
138 f();
139}
140
141testShadowing();