[parser] Improve error message for import()

Currently, dynamic `import()` throws the following SyntaxError when
used without a specifier:

    > import();
    < Uncaught SyntaxError: Unexpected token )

From the error message, it seems this the result of the code snippet
being seen as static `import` followed by parens, as opposed to
`import()` with no specifier.

This patch makes this error message more clear:

    > import();
    < SyntaxError: import() requires a specifier

BUG=v8:7020,v8:6513

Change-Id: I3519dfd0029f38d23da858a5499f1d226e794935
Reviewed-on: https://chromium-review.googlesource.com/747141
Reviewed-by: Sathya Gunasekaran <gsathya@chromium.org>
Commit-Queue: Mathias Bynens <mathias@chromium.org>
Cr-Commit-Position: refs/heads/master@{#49058}
This commit is contained in:
Mathias Bynens 2017-10-31 16:07:50 -03:00 committed by Commit Bot
parent 17b97fe928
commit dbcea11564
10 changed files with 38 additions and 20 deletions

View File

@ -324,6 +324,7 @@ class ErrorUtils : public AllStatic {
"Immutable prototype object '%' cannot have their prototype set") \
T(ImportCallNotNewExpression, "Cannot use new with import") \
T(ImportMetaOutsideModule, "Cannot use 'import.meta' outside a module") \
T(ImportMissingSpecifier, "import() requires a specifier") \
T(IncompatibleMethodReceiver, "Method % called on incompatible receiver %") \
T(InstanceofNonobjectProto, \
"Function has non-object prototype '%' in instanceof check") \

View File

@ -3565,6 +3565,12 @@ typename ParserBase<Impl>::ExpressionT ParserBase<Impl>::ParseImportExpressions(
return impl()->ImportMetaExpression(pos);
}
Expect(Token::LPAREN, CHECK_OK);
if (peek() == Token::RPAREN) {
impl()->ReportMessageAt(scanner()->location(),
MessageTemplate::kImportMissingSpecifier);
*ok = false;
return impl()->NullExpression();
}
ExpressionT arg = ParseAssignmentExpression(true, CHECK_OK);
Expect(Token::RPAREN, CHECK_OK);
return factory()->NewImportCallExpression(arg, pos);

View File

@ -428,7 +428,7 @@ bytecodes: [
B(TestTypeOf), U8(5),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(141),
B(Wide), B(LdaSmi), I16(142),
B(Star), R(18),
B(LdaConstant), U8(16),
B(Star), R(19),

View File

@ -143,7 +143,7 @@ bytecodes: [
B(TestTypeOf), U8(5),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(141),
B(Wide), B(LdaSmi), I16(142),
B(Star), R(19),
B(LdaConstant), U8(13),
B(Star), R(20),
@ -432,7 +432,7 @@ bytecodes: [
B(TestTypeOf), U8(5),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(141),
B(Wide), B(LdaSmi), I16(142),
B(Star), R(19),
B(LdaConstant), U8(13),
B(Star), R(20),
@ -743,7 +743,7 @@ bytecodes: [
B(TestTypeOf), U8(5),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(141),
B(Wide), B(LdaSmi), I16(142),
B(Star), R(19),
B(LdaConstant), U8(13),
B(Star), R(20),
@ -991,7 +991,7 @@ bytecodes: [
B(TestTypeOf), U8(5),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(141),
B(Wide), B(LdaSmi), I16(142),
B(Star), R(16),
B(LdaConstant), U8(10),
B(Star), R(17),

View File

@ -86,7 +86,7 @@ bytecodes: [
B(TestTypeOf), U8(5),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(141),
B(Wide), B(LdaSmi), I16(142),
B(Star), R(11),
B(LdaConstant), U8(8),
B(Star), R(12),
@ -227,7 +227,7 @@ bytecodes: [
B(TestTypeOf), U8(5),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(141),
B(Wide), B(LdaSmi), I16(142),
B(Star), R(12),
B(LdaConstant), U8(8),
B(Star), R(13),
@ -380,7 +380,7 @@ bytecodes: [
B(TestTypeOf), U8(5),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(141),
B(Wide), B(LdaSmi), I16(142),
B(Star), R(11),
B(LdaConstant), U8(8),
B(Star), R(12),
@ -523,7 +523,7 @@ bytecodes: [
B(TestTypeOf), U8(5),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(141),
B(Wide), B(LdaSmi), I16(142),
B(Star), R(10),
B(LdaConstant), U8(10),
B(Star), R(11),

View File

@ -90,7 +90,7 @@ bytecodes: [
B(TestTypeOf), U8(5),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(141),
B(Wide), B(LdaSmi), I16(142),
B(Star), R(13),
B(LdaConstant), U8(7),
B(Star), R(14),
@ -268,7 +268,7 @@ bytecodes: [
B(TestTypeOf), U8(5),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(141),
B(Wide), B(LdaSmi), I16(142),
B(Star), R(13),
B(LdaConstant), U8(11),
B(Star), R(14),
@ -422,7 +422,7 @@ bytecodes: [
B(TestTypeOf), U8(5),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(141),
B(Wide), B(LdaSmi), I16(142),
B(Star), R(11),
B(LdaConstant), U8(9),
B(Star), R(12),
@ -524,7 +524,7 @@ bytecodes: [
B(JumpIfUndefined), U8(6),
B(Ldar), R(6),
B(JumpIfNotNull), U8(16),
B(LdaSmi), I8(71),
B(LdaSmi), I8(72),
B(Star), R(17),
B(LdaConstant), U8(4),
B(Star), R(18),
@ -580,7 +580,7 @@ bytecodes: [
B(TestTypeOf), U8(5),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(141),
B(Wide), B(LdaSmi), I16(142),
B(Star), R(16),
B(LdaConstant), U8(9),
B(Star), R(17),
@ -754,7 +754,7 @@ bytecodes: [
B(TestTypeOf), U8(5),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(141),
B(Wide), B(LdaSmi), I16(142),
B(Star), R(16),
B(LdaConstant), U8(10),
B(Star), R(17),
@ -953,7 +953,7 @@ bytecodes: [
B(TestTypeOf), U8(5),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(141),
B(Wide), B(LdaSmi), I16(142),
B(Star), R(15),
B(LdaConstant), U8(14),
B(Star), R(16),
@ -1116,7 +1116,7 @@ bytecodes: [
B(TestTypeOf), U8(5),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(141),
B(Wide), B(LdaSmi), I16(142),
B(Star), R(20),
B(LdaConstant), U8(7),
B(Star), R(21),
@ -1358,7 +1358,7 @@ bytecodes: [
B(TestTypeOf), U8(5),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(141),
B(Wide), B(LdaSmi), I16(142),
B(Star), R(20),
B(LdaConstant), U8(9),
B(Star), R(21),

View File

@ -257,7 +257,7 @@ bytecodes: [
B(TestTypeOf), U8(5),
B(JumpIfFalse), U8(4),
B(Jump), U8(18),
B(Wide), B(LdaSmi), I16(141),
B(Wide), B(LdaSmi), I16(142),
B(Star), R(14),
B(LdaConstant), U8(15),
B(Star), R(15),

View File

@ -231,7 +231,7 @@ bytecodes: [
B(JumpIfUndefined), U8(6),
B(Ldar), R(3),
B(JumpIfNotNull), U8(16),
B(LdaSmi), I8(71),
B(LdaSmi), I8(72),
B(Star), R(4),
B(LdaConstant), U8(1),
B(Star), R(5),

View File

@ -0,0 +1,7 @@
// Copyright 2016 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: --harmony-dynamic-import
import();

View File

@ -0,0 +1,4 @@
*%(basename)s:7: SyntaxError: import() requires a specifier
import();
^
SyntaxError: import() requires a specifier