Commit e101b9c0 authored by Sathya Gunasekaran's avatar Sathya Gunasekaran Committed by Commit Bot

[parser] Improve error when using import decl in a script

Perform a best-effort check for module context and provide an
appropriate error.

As seen from the import-blah-script.js test, we could have invalid
import expressions in a script context that could result in an error
saying "Cannot use import statement outside a module" which isn't
the ideal error because the error is an incorrect import
expression.

But, when the developer changes to a module context, the
correct error is thrown.

To fix this, we'd have to refactor and call ParseImportDeclaration,
and then throw an appropriate error, which seems like a lot of
overhead for not enough gain.

Bug: v8:9392, v8:6513
Change-Id: I520ebb490fff4d95743a7c751d4095db9a35d41b
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1675948Reviewed-by: 's avatarMythri Alle <mythria@chromium.org>
Reviewed-by: 's avatarGeorg Neis <neis@chromium.org>
Commit-Queue: Sathya Gunasekaran <gsathya@chromium.org>
Cr-Commit-Position: refs/heads/master@{#62358}
parent f89d4e12
......@@ -90,6 +90,7 @@ namespace internal {
T(ImmutablePrototypeSet, \
"Immutable prototype object '%' cannot have their prototype set") \
T(ImportCallNotNewExpression, "Cannot use new with import") \
T(ImportOutsideModule, "Cannot use import statement outside a module") \
T(ImportMetaOutsideModule, "Cannot use 'import.meta' outside a module") \
T(ImportMissingSpecifier, "import() requires a specifier") \
T(IncompatibleMethodReceiver, "Method % called on incompatible receiver %") \
......
......@@ -3296,7 +3296,18 @@ ParserBase<Impl>::ParseImportExpressions() {
return impl()->ImportMetaExpression(pos);
}
Expect(Token::LPAREN);
if (V8_UNLIKELY(peek() != Token::LPAREN)) {
if (!parsing_module_) {
impl()->ReportMessageAt(scanner()->location(),
MessageTemplate::kImportOutsideModule);
} else {
ReportUnexpectedToken(Next());
}
return impl()->FailureExpression();
}
Consume(Token::LPAREN);
if (peek() == Token::RPAREN) {
impl()->ReportMessageAt(scanner()->location(),
MessageTemplate::kImportMissingSpecifier);
......
......@@ -301,7 +301,7 @@ bytecodes: [
B(Mov), R(context), R(18),
B(TestTypeOf), U8(6),
B(JumpIfTrue), U8(18),
B(Wide), B(LdaSmi), I16(158),
B(Wide), B(LdaSmi), I16(159),
B(Star), R(19),
B(LdaConstant), U8(12),
B(Star), R(20),
......
......@@ -67,7 +67,7 @@ bytecodes: [
B(Mov), R(context), R(13),
B(TestTypeOf), U8(6),
B(JumpIfTrue), U8(18),
B(Wide), B(LdaSmi), I16(158),
B(Wide), B(LdaSmi), I16(159),
B(Star), R(14),
B(LdaConstant), U8(6),
B(Star), R(15),
......@@ -204,7 +204,7 @@ bytecodes: [
B(Mov), R(context), R(14),
B(TestTypeOf), U8(6),
B(JumpIfTrue), U8(18),
B(Wide), B(LdaSmi), I16(158),
B(Wide), B(LdaSmi), I16(159),
B(Star), R(15),
B(LdaConstant), U8(6),
B(Star), R(16),
......@@ -328,7 +328,7 @@ bytecodes: [
B(Mov), R(context), R(15),
B(TestTypeOf), U8(6),
B(JumpIfTrue), U8(18),
B(Wide), B(LdaSmi), I16(158),
B(Wide), B(LdaSmi), I16(159),
B(Star), R(16),
B(LdaConstant), U8(7),
B(Star), R(17),
......
......@@ -95,7 +95,7 @@ bytecodes: [
B(Mov), R(context), R(17),
B(TestTypeOf), U8(6),
B(JumpIfTrue), U8(18),
B(Wide), B(LdaSmi), I16(158),
B(Wide), B(LdaSmi), I16(159),
B(Star), R(18),
B(LdaConstant), U8(9),
B(Star), R(19),
......@@ -264,7 +264,7 @@ bytecodes: [
B(Mov), R(context), R(17),
B(TestTypeOf), U8(6),
B(JumpIfTrue), U8(18),
B(Wide), B(LdaSmi), I16(158),
B(Wide), B(LdaSmi), I16(159),
B(Star), R(18),
B(LdaConstant), U8(9),
B(Star), R(19),
......@@ -449,7 +449,7 @@ bytecodes: [
B(Mov), R(context), R(17),
B(TestTypeOf), U8(6),
B(JumpIfTrue), U8(18),
B(Wide), B(LdaSmi), I16(158),
B(Wide), B(LdaSmi), I16(159),
B(Star), R(18),
B(LdaConstant), U8(9),
B(Star), R(19),
......@@ -601,7 +601,7 @@ bytecodes: [
B(Mov), R(context), R(14),
B(TestTypeOf), U8(6),
B(JumpIfTrue), U8(18),
B(Wide), B(LdaSmi), I16(158),
B(Wide), B(LdaSmi), I16(159),
B(Star), R(15),
B(LdaConstant), U8(8),
B(Star), R(16),
......
......@@ -63,7 +63,7 @@ bytecodes: [
B(Mov), R(context), R(12),
B(TestTypeOf), U8(6),
B(JumpIfTrue), U8(18),
B(Wide), B(LdaSmi), I16(158),
B(Wide), B(LdaSmi), I16(159),
B(Star), R(13),
B(LdaConstant), U8(6),
B(Star), R(14),
......@@ -166,7 +166,7 @@ bytecodes: [
B(Mov), R(context), R(13),
B(TestTypeOf), U8(6),
B(JumpIfTrue), U8(18),
B(Wide), B(LdaSmi), I16(158),
B(Wide), B(LdaSmi), I16(159),
B(Star), R(14),
B(LdaConstant), U8(6),
B(Star), R(15),
......@@ -279,7 +279,7 @@ bytecodes: [
B(Mov), R(context), R(12),
B(TestTypeOf), U8(6),
B(JumpIfTrue), U8(18),
B(Wide), B(LdaSmi), I16(158),
B(Wide), B(LdaSmi), I16(159),
B(Star), R(13),
B(LdaConstant), U8(6),
B(Star), R(14),
......@@ -385,7 +385,7 @@ bytecodes: [
B(Mov), R(context), R(12),
B(TestTypeOf), U8(6),
B(JumpIfTrue), U8(18),
B(Wide), B(LdaSmi), I16(158),
B(Wide), B(LdaSmi), I16(159),
B(Star), R(13),
B(LdaConstant), U8(8),
B(Star), R(14),
......
......@@ -67,7 +67,7 @@ bytecodes: [
B(Mov), R(context), R(14),
B(TestTypeOf), U8(6),
B(JumpIfTrue), U8(18),
B(Wide), B(LdaSmi), I16(158),
B(Wide), B(LdaSmi), I16(159),
B(Star), R(15),
B(LdaConstant), U8(5),
B(Star), R(16),
......@@ -204,7 +204,7 @@ bytecodes: [
B(Mov), R(context), R(15),
B(TestTypeOf), U8(6),
B(JumpIfTrue), U8(18),
B(Wide), B(LdaSmi), I16(158),
B(Wide), B(LdaSmi), I16(159),
B(Star), R(16),
B(LdaConstant), U8(10),
B(Star), R(17),
......@@ -319,7 +319,7 @@ bytecodes: [
B(Mov), R(context), R(13),
B(TestTypeOf), U8(6),
B(JumpIfTrue), U8(18),
B(Wide), B(LdaSmi), I16(158),
B(Wide), B(LdaSmi), I16(159),
B(Star), R(14),
B(LdaConstant), U8(7),
B(Star), R(15),
......@@ -431,7 +431,7 @@ bytecodes: [
B(Mov), R(context), R(16),
B(TestTypeOf), U8(6),
B(JumpIfTrue), U8(18),
B(Wide), B(LdaSmi), I16(158),
B(Wide), B(LdaSmi), I16(159),
B(Star), R(17),
B(LdaConstant), U8(7),
B(Star), R(18),
......@@ -548,7 +548,7 @@ bytecodes: [
B(Mov), R(context), R(15),
B(TestTypeOf), U8(6),
B(JumpIfTrue), U8(18),
B(Wide), B(LdaSmi), I16(158),
B(Wide), B(LdaSmi), I16(159),
B(Star), R(16),
B(LdaConstant), U8(8),
B(Star), R(17),
......@@ -680,7 +680,7 @@ bytecodes: [
B(Mov), R(context), R(14),
B(TestTypeOf), U8(6),
B(JumpIfTrue), U8(18),
B(Wide), B(LdaSmi), I16(158),
B(Wide), B(LdaSmi), I16(159),
B(Star), R(15),
B(LdaConstant), U8(11),
B(Star), R(16),
......@@ -796,7 +796,7 @@ bytecodes: [
B(Mov), R(context), R(16),
B(TestTypeOf), U8(6),
B(JumpIfTrue), U8(18),
B(Wide), B(LdaSmi), I16(158),
B(Wide), B(LdaSmi), I16(159),
B(Star), R(17),
B(LdaConstant), U8(5),
B(Star), R(18),
......@@ -936,7 +936,7 @@ bytecodes: [
B(Mov), R(context), R(15),
B(TestTypeOf), U8(6),
B(JumpIfTrue), U8(18),
B(Wide), B(LdaSmi), I16(158),
B(Wide), B(LdaSmi), I16(159),
B(Star), R(16),
B(LdaConstant), U8(6),
B(Star), R(17),
......
......@@ -181,7 +181,7 @@ bytecodes: [
B(Mov), R(context), R(14),
B(TestTypeOf), U8(6),
B(JumpIfTrue), U8(18),
B(Wide), B(LdaSmi), I16(158),
B(Wide), B(LdaSmi), I16(159),
B(Star), R(15),
B(LdaConstant), U8(12),
B(Star), R(16),
......
// Copyright 2019 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.
//
// MODULE
(import{blah})
# Copyright 2019 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.
*%(basename)s:7: SyntaxError: Unexpected token '{'
(import{blah})
^
SyntaxError: Unexpected token '{'
// Copyright 2019 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.
(import{blah})
# Copyright 2019 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.
*%(basename)s:5: SyntaxError: Cannot use import statement outside a module
(import{blah})
^^^^^^
SyntaxError: Cannot use import statement outside a module
// Copyright 2019 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.
//
import { test } from "mod";
# Copyright 2019 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.
*%(basename)s:6: SyntaxError: Cannot use import statement outside a module
import { test } from "mod";
^^^^^^
SyntaxError: Cannot use import statement outside a module
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment