blob: a7f6b6947537d24074fc8a14edf31421fa703041 [file] [log] [blame]
Ben Murdoch589d6972011-11-30 16:04:58 +00001// Copyright 2011 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
Ben Murdoch592a9fc2012-03-05 11:04:45 +000028// Flags: --harmony-scoping
29
30// TODO(ES6): properly activate extended mode
31"use strict";
Ben Murdoch589d6972011-11-30 16:04:58 +000032
33// We want to test the context chain shape. In each of the tests cases
34// below, the outer with is to force a runtime lookup of the identifier 'x'
35// to actually verify that the inner context has been discarded. A static
36// lookup of 'x' might accidentally succeed.
37
38{
39 let x = 2;
40 L: {
41 let x = 3;
42 assertEquals(3, x);
43 break L;
44 assertTrue(false);
45 }
46 assertEquals(2, x);
47}
48
49do {
50 let x = 4;
51 assertEquals(4,x);
52 {
53 let x = 5;
54 assertEquals(5, x);
55 continue;
56 assertTrue(false);
57 }
58} while (false);
59
60var caught = false;
61try {
62 {
63 let xx = 18;
64 throw 25;
65 assertTrue(false);
66 }
67} catch (e) {
68 caught = true;
69 assertEquals(25, e);
Ben Murdoch592a9fc2012-03-05 11:04:45 +000070 (function () {
Ben Murdoch589d6972011-11-30 16:04:58 +000071 try {
72 // NOTE: This checks that the block scope containing xx has been
73 // removed from the context chain.
Ben Murdoch592a9fc2012-03-05 11:04:45 +000074 eval('xx');
Ben Murdoch589d6972011-11-30 16:04:58 +000075 assertTrue(false); // should not reach here
76 } catch (e2) {
77 assertTrue(e2 instanceof ReferenceError);
78 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +000079 })();
Ben Murdoch589d6972011-11-30 16:04:58 +000080}
81assertTrue(caught);
82
83
Ben Murdoch592a9fc2012-03-05 11:04:45 +000084(function(x) {
Ben Murdoch589d6972011-11-30 16:04:58 +000085 label: {
86 let x = 'inner';
87 break label;
88 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +000089 assertEquals('outer', eval('x'));
90})('outer');
Ben Murdoch589d6972011-11-30 16:04:58 +000091
92
Ben Murdoch592a9fc2012-03-05 11:04:45 +000093(function(x) {
Ben Murdoch589d6972011-11-30 16:04:58 +000094 label: {
95 let x = 'middle';
96 {
97 let x = 'inner';
98 break label;
99 }
100 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000101 assertEquals('outer', eval('x'));
102})('outer');
Ben Murdoch589d6972011-11-30 16:04:58 +0000103
104
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000105(function(x) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000106 for (var i = 0; i < 10; ++i) {
107 let x = 'inner' + i;
108 continue;
109 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000110 assertEquals('outer', eval('x'));
111})('outer');
Ben Murdoch589d6972011-11-30 16:04:58 +0000112
113
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000114(function(x) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000115 label: for (var i = 0; i < 10; ++i) {
116 let x = 'middle' + i;
117 for (var j = 0; j < 10; ++j) {
118 let x = 'inner' + j;
119 continue label;
120 }
121 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000122 assertEquals('outer', eval('x'));
123})('outer');
Ben Murdoch589d6972011-11-30 16:04:58 +0000124
125
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000126(function(x) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000127 try {
128 let x = 'inner';
129 throw 0;
130 } catch (e) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000131 assertEquals('outer', eval('x'));
Ben Murdoch589d6972011-11-30 16:04:58 +0000132 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000133})('outer');
Ben Murdoch589d6972011-11-30 16:04:58 +0000134
135
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000136(function(x) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000137 try {
138 let x = 'middle';
139 {
140 let x = 'inner';
141 throw 0;
142 }
143 } catch (e) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000144 assertEquals('outer', eval('x'));
Ben Murdoch589d6972011-11-30 16:04:58 +0000145 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000146})('outer');
Ben Murdoch589d6972011-11-30 16:04:58 +0000147
148
149try {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000150 (function(x) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000151 try {
152 let x = 'inner';
153 throw 0;
154 } finally {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000155 assertEquals('outer', eval('x'));
Ben Murdoch589d6972011-11-30 16:04:58 +0000156 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000157 })('outer');
Ben Murdoch589d6972011-11-30 16:04:58 +0000158} catch (e) {
159 if (e instanceof MjsUnitAssertionError) throw e;
160}
161
162
163try {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000164 (function(x) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000165 try {
166 let x = 'middle';
167 {
168 let x = 'inner';
169 throw 0;
170 }
171 } finally {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000172 assertEquals('outer', eval('x'));
Ben Murdoch589d6972011-11-30 16:04:58 +0000173 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000174 })('outer');
Ben Murdoch589d6972011-11-30 16:04:58 +0000175} catch (e) {
176 if (e instanceof MjsUnitAssertionError) throw e;
177}
178
179
180// Verify that the context is correctly set in the stack frame after exiting
181// from with.
182function f() {}
183
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000184(function(x) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000185 label: {
186 let x = 'inner';
187 break label;
188 }
189 f(); // The context could be restored from the stack after the call.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000190 assertEquals('outer', eval('x'));
191})('outer');
Ben Murdoch589d6972011-11-30 16:04:58 +0000192
193
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000194(function(x) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000195 for (var i = 0; i < 10; ++i) {
196 let x = 'inner';
197 continue;
198 }
199 f();
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000200 assertEquals('outer', eval('x'));
201})('outer');
Ben Murdoch589d6972011-11-30 16:04:58 +0000202
203
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000204(function(x) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000205 try {
206 let x = 'inner';
207 throw 0;
208 } catch (e) {
209 f();
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000210 assertEquals('outer', eval('x'));
Ben Murdoch589d6972011-11-30 16:04:58 +0000211 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000212})('outer');
Ben Murdoch589d6972011-11-30 16:04:58 +0000213
214
215try {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000216 (function(x) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000217 try {
218 let x = 'inner';
219 throw 0;
220 } finally {
221 f();
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000222 assertEquals('outer', eval('x'));
Ben Murdoch589d6972011-11-30 16:04:58 +0000223 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000224 })('outer');
Ben Murdoch589d6972011-11-30 16:04:58 +0000225} catch (e) {
226 if (e instanceof MjsUnitAssertionError) throw e;
227}