[modules] Infer strict mode from within scope object

Refactor the Scope object to automatically enable strict mode when
initialized as a "module" scope, relieving the caller of this
responsibility.

BUG=v8:4941
LOG=N
R=adamk@chromium.org

Review URL: https://codereview.chromium.org/1906923002

Cr-Commit-Position: refs/heads/master@{#35730}
This commit is contained in:
mike 2016-04-22 06:37:10 -07:00 committed by Commit bot
parent 4b580cad0c
commit b86ec74395
5 changed files with 13 additions and 9 deletions

View File

@ -175,7 +175,10 @@ void Scope::SetDefaults(ScopeType scope_type, Scope* outer_scope,
asm_module_ = false; asm_module_ = false;
asm_function_ = outer_scope != NULL && outer_scope->asm_module_; asm_function_ = outer_scope != NULL && outer_scope->asm_module_;
// Inherit the language mode from the parent scope. // Inherit the language mode from the parent scope.
language_mode_ = outer_scope != NULL ? outer_scope->language_mode_ : SLOPPY; language_mode_ =
is_module_scope()
? STRICT
: (outer_scope != NULL ? outer_scope->language_mode_ : SLOPPY);
outer_scope_calls_sloppy_eval_ = false; outer_scope_calls_sloppy_eval_ = false;
inner_scope_calls_eval_ = false; inner_scope_calls_eval_ = false;
scope_nonlinear_ = false; scope_nonlinear_ = false;

View File

@ -243,6 +243,7 @@ class Scope: public ZoneObject {
// Set the language mode flag (unless disabled by a global flag). // Set the language mode flag (unless disabled by a global flag).
void SetLanguageMode(LanguageMode language_mode) { void SetLanguageMode(LanguageMode language_mode) {
DCHECK(!is_module_scope());
language_mode_ = language_mode; language_mode_ = language_mode;
} }

View File

@ -917,15 +917,15 @@ FunctionLiteral* Parser::DoParseProgram(ParseInfo* info) {
FunctionState function_state(&function_state_, &scope_, scope, FunctionState function_state(&function_state_, &scope_, scope,
kNormalFunction, &function_factory); kNormalFunction, &function_factory);
// Don't count the mode in the use counters--give the program a chance
// to enable script/module-wide strict mode below.
scope_->SetLanguageMode(info->language_mode());
ZoneList<Statement*>* body = new(zone()) ZoneList<Statement*>(16, zone()); ZoneList<Statement*>* body = new(zone()) ZoneList<Statement*>(16, zone());
bool ok = true; bool ok = true;
int beg_pos = scanner()->location().beg_pos; int beg_pos = scanner()->location().beg_pos;
if (info->is_module()) { if (info->is_module()) {
ParseModuleItemList(body, &ok); ParseModuleItemList(body, &ok);
} else { } else {
// Don't count the mode in the use counters--give the program a chance
// to enable script-wide strict mode below.
scope_->SetLanguageMode(info->language_mode());
ParseStatementList(body, Token::EOS, &ok); ParseStatementList(body, Token::EOS, &ok);
} }
@ -1295,7 +1295,6 @@ void* Parser::ParseModuleItemList(ZoneList<Statement*>* body, bool* ok) {
// ModuleItem* // ModuleItem*
DCHECK(scope_->is_module_scope()); DCHECK(scope_->is_module_scope());
RaiseLanguageMode(STRICT);
while (peek() != Token::EOS) { while (peek() != Token::EOS) {
Statement* stat = ParseModuleItem(CHECK_OK); Statement* stat = ParseModuleItem(CHECK_OK);

View File

@ -969,8 +969,9 @@ class PreParser : public ParserBase<PreParserTraits> {
// success (even if parsing failed, the pre-parse data successfully // success (even if parsing failed, the pre-parse data successfully
// captured the syntax error), and false if a stack-overflow happened // captured the syntax error), and false if a stack-overflow happened
// during parsing. // during parsing.
PreParseResult PreParseProgram(int* materialized_literals = 0) { PreParseResult PreParseProgram(int* materialized_literals = 0,
Scope* scope = NewScope(scope_, SCRIPT_SCOPE); bool is_module = false) {
Scope* scope = NewScope(scope_, is_module ? MODULE_SCOPE : SCRIPT_SCOPE);
PreParserFactory factory(NULL); PreParserFactory factory(NULL);
FunctionState top_scope(&function_state_, &scope_, scope, kNormalFunction, FunctionState top_scope(&function_state_, &scope_, scope, kNormalFunction,
&factory); &factory);

View File

@ -1555,8 +1555,8 @@ void TestParserSyncWithFlags(i::Handle<i::String> source,
stack_limit); stack_limit);
SetParserFlags(&preparser, flags); SetParserFlags(&preparser, flags);
scanner.Initialize(&stream); scanner.Initialize(&stream);
i::PreParser::PreParseResult result = preparser.PreParseProgram( i::PreParser::PreParseResult result =
&preparser_materialized_literals); preparser.PreParseProgram(&preparser_materialized_literals, is_module);
CHECK_EQ(i::PreParser::kPreParseSuccess, result); CHECK_EQ(i::PreParser::kPreParseSuccess, result);
} }
bool preparse_error = log.HasError(); bool preparse_error = log.HasError();