Reviewers: Jakob,

Message:
PTAL.

Description:
Make let/const outside of the extended mode early syntax errors.

This depends on:
http://codereview.chromium.org/8562002/

TEST=mjsunit/harmony/block-early-errors.js


Please review this at http://codereview.chromium.org/8564001/

SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge

Affected files:
  M src/messages.js
  M src/parser.cc
  A test/mjsunit/harmony/block-early-errors.js


Index: src/messages.js
diff --git a/src/messages.js b/src/messages.js
index 2df734cecc19634bff62c6b00428efcc0e5230ea..85ba340f5454969b1430f0be68803fb17979e9d2 100644
--- a/src/messages.js
+++ b/src/messages.js
@@ -206,6 +206,8 @@ function FormatMessage(message) {
       "illegal_break",                ["Illegal break statement"],
       "illegal_continue",             ["Illegal continue statement"],
       "illegal_return",               ["Illegal return statement"],
+ "illegal_let", ["Illegal let declaration outside extended mode"], + "illegal_const", ["Illegal const declaration outside extended mode"],
       "error_loading_debugger",       ["Error loading debugger"],
       "no_input_to_regexp",           ["No input to ", "%0"],
"invalid_json", ["String '", "%0", "' is not valid JSON"],
Index: src/parser.cc
diff --git a/src/parser.cc b/src/parser.cc
index de4457c3dd991b3ab36bc54ff16024000608a66c..ab9714cbcb703b94a02d2c3f96016aaf04b8e8ad 100644
--- a/src/parser.cc
+++ b/src/parser.cc
@@ -1263,6 +1263,7 @@ Statement* Parser::ParseStatement(ZoneStringList* labels, bool* ok) {
       return ParseBlock(labels, ok);

     case Token::CONST:  // fall through
+    case Token::LET:
     case Token::VAR:
       stmt = ParseVariableStatement(kStatement, ok);
       break;
@@ -1690,6 +1691,22 @@ Block* Parser::ParseVariableDeclarations(
   if (peek() == Token::VAR) {
     Consume(Token::VAR);
   } else if (peek() == Token::CONST) {
+    // ES6 Draft Rev4 section 12.2.2:
+    //
+    // ConstDeclaration : const ConstBinding (',' ConstBinding)* ';'
+    //
+ // * It is a Syntax Error if the code that matches this production is not
+    //   contained in extended code.
+    //
+ // Several V8 native files use const declarations in classic mode, so even
+    // when harmony semantics are enabled we allow those declarations iff
+ // natives syntax is allowed and use the non-harmony semantics for them, to
+    // match what has been compiled into snapshots.
+ if (FLAG_harmony_scoping && !is_extended_mode() && !allow_natives_syntax_) {
+      ReportMessage("illegal_const", Vector<const char*>::empty());
+      *ok = false;
+      return NULL;
+    }
     Consume(Token::CONST);
     switch (top_scope_->language_mode()) {
       case CLASSIC_MODE:
@@ -1715,7 +1732,17 @@ Block* Parser::ParseVariableDeclarations(
     is_const = true;
     needs_init = true;
   } else if (peek() == Token::LET) {
-    ASSERT(top_scope_->is_extended_mode());
+    // ES6 Draft Rev4 section 12.2.1:
+    //
+    // LetDeclaration : let LetBindingList ;
+    //
+ // * It is a Syntax Error if the code that matches this production is not
+    //   contained in extended code.
+    if (!is_extended_mode()) {
+      ReportMessage("illegal_let", Vector<const char*>::empty());
+      *ok = false;
+      return NULL;
+    }
     Consume(Token::LET);
     if (var_context != kSourceElement &&
         var_context != kForStatement) {
Index: test/mjsunit/harmony/block-early-errors.js
diff --git a/test/mjsunit/harmony/block-early-errors.js b/test/mjsunit/harmony/block-early-errors.js
new file mode 100644
index 0000000000000000000000000000000000000000..f63b6c65261186f901c2e017a43f22eb6b3ae63b
--- /dev/null
+++ b/test/mjsunit/harmony/block-early-errors.js
@@ -0,0 +1,60 @@
+// 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.
+
+// Flags: --harmony-scoping
+
+function CheckException(e) {
+  var string = e.toString();
+  assertInstanceof(e, SyntaxError);
+  assertTrue(string.indexOf("Illegal let") >= 0 ||
+             string.indexOf("Illegal const") >= 0);
+}
+
+function Check(str) {
+  try {
+    eval("(function () { " + str + " })");
+    assertUnreachable();
+  } catch (e) {
+    CheckException(e);
+  }
+  try {
+    eval("(function () { { " + str + " } })");
+    assertUnreachable();
+  } catch (e) {
+    CheckException(e);
+  }
+}
+
+// Check for early syntax errors when using let
+// declarations outside of extended mode.
+Check("let x;");
+Check("let x = 1;");
+Check("let x, y;");
+
+// Check for early syntax errors when using const
+// declarations outside of extended mode.
+Check("const x = 1;");


--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to