Reviewers: rossberg,

Message:
rossberg, ptal

Description:
[strong] Forbid var.

[email protected]
BUG=

Please review this at https://codereview.chromium.org/927953003/

Base URL: https://chromium.googlesource.com/v8/v8.git@master

Affected files (+67, -0 lines):
  M src/messages.js
  M src/parser.cc
  M src/preparser.cc
  M test/cctest/test-parsing.cc


Index: src/messages.js
diff --git a/src/messages.js b/src/messages.js
index e60ab92586e0a5f4aa2002397492d1d171769e2a..701f1ee3aaf9b61e84dfc5112932b5f693ca9fe9 100644
--- a/src/messages.js
+++ b/src/messages.js
@@ -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"],
Index: src/parser.cc
diff --git a/src/parser.cc b/src/parser.cc
index 08941f501f333511f612603e597e627327fc9373..5f849a599e1715a1198c66fa55d6514ee72a5eb4 100644
--- a/src/parser.cc
+++ b/src/parser.cc
@@ -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);
Index: src/preparser.cc
diff --git a/src/preparser.cc b/src/preparser.cc
index 8c61b7db85159a6405abada086faee0c6b535767..964cdbf9fb9e5d6d24c70fdbf5eded829fa7e38b 100644
--- a/src/preparser.cc
+++ b/src/preparser.cc
@@ -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:
Index: test/cctest/test-parsing.cc
diff --git a/test/cctest/test-parsing.cc b/test/cctest/test-parsing.cc
index 7a624cb0bf38f99a55ade2350f72af62986ed960..9eb5d947a6e7ec63e262cd15d73e590ef7e2840c 100644
--- a/test/cctest/test-parsing.cc
+++ b/test/cctest/test-parsing.cc
@@ -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));
+}


--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to