Bug 1314128 - Do not parse await expression in module. r=till
authorTooru Fujisawa <arai_a@mac.com>
Tue, 01 Nov 2016 16:53:50 +0900
changeset 432247 d2526689ef032790ef20dd129e054b5bbc60b458
parent 432246 4561836d44dcadefc813936a194fb42567a0ab2b
child 432248 c49ec67995bcee88868679ff1e8410bb980393ef
push id34240
push userbmo:ttromey@mozilla.com
push dateTue, 01 Nov 2016 14:41:32 +0000
reviewerstill
bugs1314128
milestone52.0a1
Bug 1314128 - Do not parse await expression in module. r=till
js/src/frontend/Parser.cpp
js/src/tests/ecma_7/AsyncFunctions/syntax-modules.js
--- a/js/src/frontend/Parser.cpp
+++ b/js/src/frontend/Parser.cpp
@@ -7952,16 +7952,23 @@ Parser<ParseHandler>::unaryExpr(YieldHan
                 return null();
             pc->sc()->setBindingsAccessedDynamically();
         }
 
         return handler.newDelete(begin, expr);
       }
 
       case TOK_AWAIT: {
+        if (!pc->isAsync()) {
+            // TOK_AWAIT can be returned in module, even if it's not inside
+            // async function.
+            report(ParseError, false, null(), JSMSG_RESERVED_ID, "await");
+            return null();
+        }
+
         TokenKind nextSameLine = TOK_EOF;
         if (!tokenStream.peekTokenSameLine(&nextSameLine, TokenStream::Operand))
             return null();
         if (nextSameLine != TOK_EOL) {
             Node kid = unaryExpr(yieldHandling, tripledotHandling, possibleError, invoked);
             if (!kid)
                 return null();
             pc->lastAwaitOffset = begin;
--- a/js/src/tests/ecma_7/AsyncFunctions/syntax-modules.js
+++ b/js/src/tests/ecma_7/AsyncFunctions/syntax-modules.js
@@ -2,17 +2,22 @@ var BUGNUMBER = 1185106;
 var summary = "async/await syntax in module";
 
 print(BUGNUMBER + ": " + summary);
 
 if (typeof parseModule === "function") {
     parseModule("async function f() { await 3; }");
     parseModule("async function f() { await 3; }");
     assertThrows(() => parseModule("var await = 5;"), SyntaxError);
+    assertThrows(() => parseModule("await;"), SyntaxError);
+    assertThrows(() => parseModule("await 5;"), SyntaxError);
+    assertThrows(() => parseModule("function f() { await 5; }"), SyntaxError);
+    assertThrows(() => parseModule("() => { await 5; }"), SyntaxError);
     assertThrows(() => parseModule("export var await;"), SyntaxError);
+    assertThrows(() => parseModule("await => 1;"), SyntaxError);
     assertThrows(() => parseModule("async function f() { function g() { await 3; } }"), SyntaxError);
 
     if (typeof Reflect !== "undefined" && Reflect.parse) {
         assertThrows(() => Reflect.parse("export default async function() { yield; }", { target: "module" }), SyntaxError);
         assertThrows(() => Reflect.parse("export default async function() { yield = 1; }", { target: "module" }), SyntaxError);
     }
 }