Reviewers: Lasse Reichstein, Mads Ager,

Message:
Syntax error if function is in the block in strict mode.
Only as part of SourceElements it is allowed.


Description:
Strict mode - allow function only in SourceElements.


BUG=
TEST=test/mjsunit/strict-mode.js

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

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

Affected files:
  M src/messages.js
  M src/parser.cc
  M test/mjsunit/strict-mode.js


Index: src/messages.js
diff --git a/src/messages.js b/src/messages.js
index b7e57aa228ea22f7db50da0fabb2aecd8445e5f7..1201a4336b13eab2928d2dbe11a91e6ff6823d7b 100644
--- a/src/messages.js
+++ b/src/messages.js
@@ -226,6 +226,7 @@ function FormatMessage(message) {
strict_reserved_word: ["Use of future reserved word in strict mode"], strict_delete: ["Delete of an unqualified identifier in strict mode."], strict_delete_property: ["Cannot delete property '", "%0", "' of ", "%1"], + strict_function: ["In strict mode code, functions can only be declared at top level or immediately within another function." ],
     };
   }
   var message_type = %MessageGetType(message);
Index: src/parser.cc
diff --git a/src/parser.cc b/src/parser.cc
index 249c9ced35309b935b441a8d917f149c017f5ef5..2c143ecbf89255ad0e8501eee736768e5f3b5521 100644
--- a/src/parser.cc
+++ b/src/parser.cc
@@ -1106,7 +1106,13 @@ void* Parser::ParseSourceElements(ZoneList<Statement*>* processor,
     }

     Scanner::Location token_loc = scanner().peek_location();
-    Statement* stat = ParseStatement(NULL, CHECK_OK);
+
+    Statement* stat;
+    if (peek() == Token::FUNCTION) {
+     stat = ParseFunctionDeclaration(CHECK_OK);
+    } else {
+     stat = ParseStatement(NULL, CHECK_OK);
+    }

     if (stat == NULL || stat->IsEmpty()) {
       directive_prologue = false;   // End of directive prologue.
@@ -1263,8 +1269,17 @@ Statement* Parser::ParseStatement(ZoneStringList* labels, bool* ok) {
       return result;
     }

-    case Token::FUNCTION:
+    case Token::FUNCTION: {
+      // In strict mode, FunctionDeclaration is only allowed in the context
+      // of SourceElements.
+      if (temp_scope_->StrictMode()) {
+        ReportMessageAt(scanner().peek_location(), "strict_function",
+                        Vector<const char*>::empty());
+        *ok = false;
+        return NULL;
+      }
       return ParseFunctionDeclaration(ok);
+    }

     case Token::NATIVE:
       return ParseNativeDeclaration(ok);
Index: test/mjsunit/strict-mode.js
diff --git a/test/mjsunit/strict-mode.js b/test/mjsunit/strict-mode.js
index ab3e535ec33055a8245ff5280c8b196a74170d3c..7ea0a73286257547a919c29ae8aa85f961fd753e 100644
--- a/test/mjsunit/strict-mode.js
+++ b/test/mjsunit/strict-mode.js
@@ -280,6 +280,14 @@ CheckStrictMode("function strict() { print(--arguments); }", SyntaxError);
 CheckStrictMode("function strict() { var x = --eval; }", SyntaxError);
 CheckStrictMode("function strict() { var x = --arguments; }", SyntaxError);

+// Strict mode only allows functions in SourceElements
+CheckStrictMode("if (true) { function invalid() {} }", SyntaxError);
+CheckStrictMode("for (;false;) { function invalid() {} }", SyntaxError);
+CheckStrictMode("{ function invalid() {} }", SyntaxError);
+CheckStrictMode("try { function invalid() {} } catch(e) {}", SyntaxError);
+CheckStrictMode("try { } catch(e) { function invalid() {} }", SyntaxError);
+CheckStrictMode("function outer() {{ function invalid() {} }}", SyntaxError);
+
 // Delete of an unqualified identifier
 CheckStrictMode("delete unqualified;", SyntaxError);
 CheckStrictMode("function strict() { delete unqualified; }", SyntaxError);


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

Reply via email to