[strong] Forbid var.

R=rossberg@chromium.org
BUG=

Review URL: https://codereview.chromium.org/927953003

Cr-Commit-Position: refs/heads/master@{#26697}
This commit is contained in:
marja 2015-02-17 07:41:15 -08:00 committed by Commit bot
parent 119cb56617
commit 34281c8322
5 changed files with 89 additions and 0 deletions

View File

@ -162,6 +162,7 @@ var kMessages = {
strict_poison_pill: ["'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them"],
strict_caller: ["Illegal access to a strict mode caller function."],
strong_equal: ["Please don't use '==' or '!=' in strong mode, use '===' or '!==' instead"],
strong_var: ["Please don't use 'var' in strong mode, use 'let' or 'const' instead"],
malformed_arrow_function_parameter_list: ["Malformed arrow function parameter list"],
generator_poison_pill: ["'caller' and 'arguments' properties may not be accessed on generator functions."],
cant_prevent_ext_external_array_elements: ["Cannot prevent extension of an object with external array elements"],

View File

@ -2156,6 +2156,12 @@ Block* Parser::ParseVariableDeclarations(
bool is_const = false;
Token::Value init_op = Token::INIT_VAR;
if (peek() == Token::VAR) {
if (is_strong(language_mode())) {
Scanner::Location location = scanner()->peek_location();
ReportMessageAt(location, "strong_var");
*ok = false;
return NULL;
}
Consume(Token::VAR);
} else if (peek() == Token::CONST) {
Consume(Token::CONST);

View File

@ -429,6 +429,12 @@ PreParser::Statement PreParser::ParseVariableDeclarations(
bool require_initializer = false;
bool is_strict_const = false;
if (peek() == Token::VAR) {
if (is_strong(language_mode())) {
Scanner::Location location = scanner()->peek_location();
ReportMessageAt(location, "strong_var");
*ok = false;
return Statement::Default();
}
Consume(Token::VAR);
} else if (peek() == Token::CONST) {
// TODO(ES6): The ES6 Draft Rev4 section 12.2.2 reads:

View File

@ -5453,3 +5453,57 @@ TEST(FunctionLiteralDuplicateParameters) {
arraysize(always_flags));
RunParserSyncTest(sloppy_context_data, data, kSuccess, NULL, 0, NULL, 0);
}
TEST(VarForbiddenInStrongMode) {
const char* strong_context_data[][2] =
{{"'use strong'; ", ""},
{"function f() {'use strong'; ", "}"},
{"function f() {'use strong'; while (true) { ", "} }"},
{NULL, NULL}};
const char* strict_context_data[][2] =
{{"'use strict'; ", ""},
{"function f() {'use strict'; ", "}"},
{"function f() {'use strict'; while (true) { ", "} }"},
{NULL, NULL}};
const char* sloppy_context_data[][2] =
{{"", ""},
{"function f() { ", "}"},
{NULL, NULL}};
const char* var_declarations[] = {
"var x = 0;",
"for (var i = 0; i < 10; i++) { }",
NULL};
const char* let_declarations[] = {
"let x = 0;",
"for (let i = 0; i < 10; i++) { }",
NULL};
const char* const_declarations[] = {
"const x = 0;",
NULL};
static const ParserFlag always_flags[] = {kAllowStrongMode,
kAllowHarmonyScoping};
RunParserSyncTest(strong_context_data, var_declarations, kError, NULL, 0,
always_flags, arraysize(always_flags));
RunParserSyncTest(strong_context_data, let_declarations, kSuccess, NULL, 0,
always_flags, arraysize(always_flags));
RunParserSyncTest(strong_context_data, const_declarations, kSuccess, NULL, 0,
always_flags, arraysize(always_flags));
RunParserSyncTest(strict_context_data, var_declarations, kSuccess, NULL, 0,
always_flags, arraysize(always_flags));
RunParserSyncTest(strict_context_data, let_declarations, kSuccess, NULL, 0,
always_flags, arraysize(always_flags));
RunParserSyncTest(sloppy_context_data, var_declarations, kSuccess, NULL, 0,
always_flags, arraysize(always_flags));
// At the moment, let declarations are only available in strict mode.
RunParserSyncTest(sloppy_context_data, let_declarations, kError, NULL, 0,
always_flags, arraysize(always_flags));
}

View File

@ -0,0 +1,22 @@
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Flags: --strong-mode
(function NoVar() {
assertThrows("'use strong'; var x = 0;", SyntaxError);
assertThrows("'use strong'; for(var i = 0; i < 10; ++i) { };", SyntaxError);
})();
(function LetIsOkay() {
assertTrue(eval("'use strong'; let x = 0; x === 0;"));
assertTrue(eval("'use strong'; for(let i = 0; i < 10; ++i) { } 0 === 0;"));
})();
(function ConstIsOkay() {
assertTrue(eval("'use strong'; const x = 0; x === 0;"));
assertTrue(eval("'use strong'; for(const i = 0; false;) { } 0 === 0;"));
})();