2011-08-11 16:29:28 +00:00
|
|
|
// Copyright 2011 the V8 project authors. All rights reserved.
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2011-10-12 12:23:06 +00:00
|
|
|
// Flags: --allow-natives-syntax --harmony-scoping
|
2011-08-11 16:29:28 +00:00
|
|
|
// Test functionality of block scopes.
|
|
|
|
|
2011-11-24 15:17:04 +00:00
|
|
|
"use strict";
|
|
|
|
|
2011-08-11 16:29:28 +00:00
|
|
|
// Hoisting of var declarations.
|
|
|
|
function f1() {
|
|
|
|
{
|
|
|
|
var x = 1;
|
|
|
|
var y;
|
|
|
|
}
|
|
|
|
assertEquals(1, x)
|
|
|
|
assertEquals(undefined, y)
|
|
|
|
}
|
2014-06-05 07:33:01 +00:00
|
|
|
for (var j = 0; j < 5; ++j) f1();
|
|
|
|
%OptimizeFunctionOnNextCall(f1);
|
2011-08-11 16:29:28 +00:00
|
|
|
f1();
|
2014-06-05 07:33:01 +00:00
|
|
|
assertTrue(%GetOptimizationStatus(f1) != 2);
|
2011-08-16 14:24:12 +00:00
|
|
|
|
|
|
|
// Dynamic lookup in and through block contexts.
|
2011-08-11 16:29:28 +00:00
|
|
|
function f2(one) {
|
|
|
|
var x = one + 1;
|
2011-08-16 14:24:12 +00:00
|
|
|
let y = one + 2;
|
2011-10-25 08:33:08 +00:00
|
|
|
const u = one + 4;
|
2011-08-16 14:24:12 +00:00
|
|
|
{
|
|
|
|
let z = one + 3;
|
2011-10-25 08:33:08 +00:00
|
|
|
const v = one + 5;
|
2011-08-11 16:29:28 +00:00
|
|
|
assertEquals(1, eval('one'));
|
|
|
|
assertEquals(2, eval('x'));
|
2011-08-16 14:24:12 +00:00
|
|
|
assertEquals(3, eval('y'));
|
|
|
|
assertEquals(4, eval('z'));
|
2011-10-25 08:33:08 +00:00
|
|
|
assertEquals(5, eval('u'));
|
|
|
|
assertEquals(6, eval('v'));
|
2011-08-11 16:29:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-05 07:33:01 +00:00
|
|
|
f2(1);
|
2011-08-16 14:24:12 +00:00
|
|
|
|
|
|
|
// Lookup in and through block contexts.
|
|
|
|
function f3(one) {
|
|
|
|
var x = one + 1;
|
|
|
|
let y = one + 2;
|
2011-10-25 08:33:08 +00:00
|
|
|
const u = one + 4;
|
2011-08-16 14:24:12 +00:00
|
|
|
{
|
|
|
|
let z = one + 3;
|
2011-10-25 08:33:08 +00:00
|
|
|
const v = one + 5;
|
2011-08-16 14:24:12 +00:00
|
|
|
assertEquals(1, one);
|
|
|
|
assertEquals(2, x);
|
|
|
|
assertEquals(3, y);
|
|
|
|
assertEquals(4, z);
|
2011-10-25 08:33:08 +00:00
|
|
|
assertEquals(5, u);
|
|
|
|
assertEquals(6, v);
|
2011-08-16 14:24:12 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-05 07:33:01 +00:00
|
|
|
for (var j = 0; j < 5; ++j) f3(1);
|
|
|
|
%OptimizeFunctionOnNextCall(f3);
|
2011-08-16 14:24:12 +00:00
|
|
|
f3(1);
|
2014-06-05 07:33:01 +00:00
|
|
|
assertTrue(%GetOptimizationStatus(f3) != 2);
|
|
|
|
|
2011-08-16 14:24:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Dynamic lookup from closure.
|
|
|
|
function f4(one) {
|
|
|
|
var x = one + 1;
|
|
|
|
let y = one + 2;
|
2011-10-25 08:33:08 +00:00
|
|
|
const u = one + 4;
|
2011-08-16 14:24:12 +00:00
|
|
|
{
|
|
|
|
let z = one + 3;
|
2011-10-25 08:33:08 +00:00
|
|
|
const v = one + 5;
|
2011-08-16 14:24:12 +00:00
|
|
|
function f() {
|
|
|
|
assertEquals(1, eval('one'));
|
|
|
|
assertEquals(2, eval('x'));
|
|
|
|
assertEquals(3, eval('y'));
|
|
|
|
assertEquals(4, eval('z'));
|
2011-10-25 08:33:08 +00:00
|
|
|
assertEquals(5, eval('u'));
|
|
|
|
assertEquals(6, eval('v'));
|
2011-08-16 14:24:12 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f4(1);
|
|
|
|
|
|
|
|
|
|
|
|
// Lookup from closure.
|
|
|
|
function f5(one) {
|
|
|
|
var x = one + 1;
|
|
|
|
let y = one + 2;
|
2011-10-25 08:33:08 +00:00
|
|
|
const u = one + 4;
|
2011-08-16 14:24:12 +00:00
|
|
|
{
|
|
|
|
let z = one + 3;
|
2011-10-25 08:33:08 +00:00
|
|
|
const v = one + 5;
|
2011-08-16 14:24:12 +00:00
|
|
|
function f() {
|
|
|
|
assertEquals(1, one);
|
|
|
|
assertEquals(2, x);
|
|
|
|
assertEquals(3, y);
|
|
|
|
assertEquals(4, z);
|
2011-10-25 08:33:08 +00:00
|
|
|
assertEquals(5, u);
|
|
|
|
assertEquals(6, v);
|
2011-08-16 14:24:12 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f5(1);
|
|
|
|
|
|
|
|
|
|
|
|
// Return from block.
|
|
|
|
function f6() {
|
|
|
|
let x = 1;
|
2011-10-25 08:33:08 +00:00
|
|
|
const u = 3;
|
2011-08-16 14:24:12 +00:00
|
|
|
{
|
|
|
|
let y = 2;
|
2011-10-25 08:33:08 +00:00
|
|
|
const v = 4;
|
2011-08-16 14:24:12 +00:00
|
|
|
return x + y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assertEquals(3, f6(6));
|
|
|
|
|
|
|
|
|
|
|
|
// Variable shadowing and lookup.
|
|
|
|
function f7(a) {
|
|
|
|
let b = 1;
|
|
|
|
var c = 1;
|
|
|
|
var d = 1;
|
2011-10-25 08:33:08 +00:00
|
|
|
const e = 1;
|
|
|
|
{ // let variables shadowing argument, let, const and var variables
|
2011-08-16 14:24:12 +00:00
|
|
|
let a = 2;
|
|
|
|
let b = 2;
|
|
|
|
let c = 2;
|
2011-10-25 08:33:08 +00:00
|
|
|
let e = 2;
|
|
|
|
assertEquals(2,a);
|
|
|
|
assertEquals(2,b);
|
|
|
|
assertEquals(2,c);
|
|
|
|
assertEquals(2,e);
|
|
|
|
}
|
|
|
|
{ // const variables shadowing argument, let, const and var variables
|
|
|
|
const a = 2;
|
|
|
|
const b = 2;
|
|
|
|
const c = 2;
|
|
|
|
const e = 2;
|
2011-08-16 14:24:12 +00:00
|
|
|
assertEquals(2,a);
|
|
|
|
assertEquals(2,b);
|
|
|
|
assertEquals(2,c);
|
2011-10-25 08:33:08 +00:00
|
|
|
assertEquals(2,e);
|
2011-08-16 14:24:12 +00:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
throw 'stuff1';
|
|
|
|
} catch (a) {
|
|
|
|
assertEquals('stuff1',a);
|
|
|
|
// catch variable shadowing argument
|
|
|
|
a = 2;
|
|
|
|
assertEquals(2,a);
|
|
|
|
{
|
|
|
|
// let variable shadowing catch variable
|
|
|
|
let a = 3;
|
|
|
|
assertEquals(3,a);
|
|
|
|
try {
|
|
|
|
throw 'stuff2';
|
|
|
|
} catch (a) {
|
|
|
|
assertEquals('stuff2',a);
|
|
|
|
// catch variable shadowing let variable
|
|
|
|
a = 4;
|
|
|
|
assertEquals(4,a);
|
|
|
|
}
|
|
|
|
assertEquals(3,a);
|
|
|
|
}
|
|
|
|
assertEquals(2,a);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
throw 'stuff3';
|
|
|
|
} catch (c) {
|
|
|
|
// catch variable shadowing var variable
|
|
|
|
assertEquals('stuff3',c);
|
2011-10-25 08:33:08 +00:00
|
|
|
{
|
|
|
|
// const variable shadowing catch variable
|
|
|
|
const c = 3;
|
|
|
|
assertEquals(3,c);
|
|
|
|
}
|
|
|
|
assertEquals('stuff3',c);
|
2011-08-16 14:24:12 +00:00
|
|
|
try {
|
|
|
|
throw 'stuff4';
|
|
|
|
} catch(c) {
|
|
|
|
assertEquals('stuff4',c);
|
|
|
|
// catch variable shadowing catch variable
|
|
|
|
c = 3;
|
|
|
|
assertEquals(3,c);
|
|
|
|
}
|
|
|
|
(function(c) {
|
|
|
|
// argument shadowing catch variable
|
|
|
|
c = 3;
|
|
|
|
assertEquals(3,c);
|
|
|
|
})();
|
|
|
|
assertEquals('stuff3', c);
|
|
|
|
(function() {
|
|
|
|
// var variable shadowing catch variable
|
|
|
|
var c = 3;
|
|
|
|
})();
|
|
|
|
assertEquals('stuff3', c);
|
|
|
|
c = 2;
|
|
|
|
}
|
|
|
|
assertEquals(1,c);
|
2011-10-25 08:33:08 +00:00
|
|
|
(function(a,b,c,e) {
|
|
|
|
// arguments shadowing argument, let, const and var variable
|
2011-08-16 14:24:12 +00:00
|
|
|
a = 2;
|
|
|
|
b = 2;
|
|
|
|
c = 2;
|
2011-10-25 08:33:08 +00:00
|
|
|
e = 2;
|
2011-08-16 14:24:12 +00:00
|
|
|
assertEquals(2,a);
|
|
|
|
assertEquals(2,b);
|
|
|
|
assertEquals(2,c);
|
2011-10-25 08:33:08 +00:00
|
|
|
assertEquals(2,e);
|
2011-08-16 14:24:12 +00:00
|
|
|
// var variable shadowing var variable
|
|
|
|
var d = 2;
|
|
|
|
})(1,1);
|
|
|
|
assertEquals(1,a);
|
|
|
|
assertEquals(1,b);
|
|
|
|
assertEquals(1,c);
|
|
|
|
assertEquals(1,d);
|
2011-10-25 08:33:08 +00:00
|
|
|
assertEquals(1,e);
|
2011-08-16 14:24:12 +00:00
|
|
|
}
|
|
|
|
f7(1);
|
|
|
|
|
|
|
|
|
2011-10-25 08:33:08 +00:00
|
|
|
// Ensure let and const variables are block local
|
|
|
|
// and var variables function local.
|
2011-08-16 14:24:12 +00:00
|
|
|
function f8() {
|
|
|
|
var let_accessors = [];
|
|
|
|
var var_accessors = [];
|
2011-10-25 08:33:08 +00:00
|
|
|
var const_accessors = [];
|
2011-08-16 14:24:12 +00:00
|
|
|
for (var i = 0; i < 10; i++) {
|
|
|
|
let x = i;
|
|
|
|
var y = i;
|
2011-10-25 08:33:08 +00:00
|
|
|
const z = i;
|
2011-08-16 14:24:12 +00:00
|
|
|
let_accessors[i] = function() { return x; }
|
|
|
|
var_accessors[i] = function() { return y; }
|
2011-10-25 08:33:08 +00:00
|
|
|
const_accessors[i] = function() { return z; }
|
2011-08-16 14:24:12 +00:00
|
|
|
}
|
|
|
|
for (var j = 0; j < 10; j++) {
|
|
|
|
y = j + 10;
|
|
|
|
assertEquals(j, let_accessors[j]());
|
|
|
|
assertEquals(y, var_accessors[j]());
|
2011-10-25 08:33:08 +00:00
|
|
|
assertEquals(j, const_accessors[j]());
|
2011-08-16 14:24:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
f8();
|