From 60a783c246ba8c46b35dd38e19584d2135b4a321 Mon Sep 17 00:00:00 2001 From: jochen Date: Wed, 31 Aug 2016 06:33:17 -0700 Subject: [PATCH] Make the condition for when this is predeclared easier to understand. Just always predeclare it R=marja@chromium.org,verwaest@chromium.org BUG=v8:5215 Review-Url: https://codereview.chromium.org/2298743002 Cr-Commit-Position: refs/heads/master@{#39048} --- src/ast/scopes.cc | 17 +++++++++-------- src/ast/scopes.h | 2 +- src/parsing/parser-base.h | 2 +- test/cctest/test-parsing.cc | 6 ++++-- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/ast/scopes.cc b/src/ast/scopes.cc index f073b0e7a5..f1a9a0a7b0 100644 --- a/src/ast/scopes.cc +++ b/src/ast/scopes.cc @@ -129,12 +129,18 @@ Scope::Snapshot::Snapshot(Scope* scope) top_local_(scope->GetClosureScope()->locals_.length()), top_decl_(scope->GetClosureScope()->decls_.length()) {} -DeclarationScope::DeclarationScope(Zone* zone) +DeclarationScope::DeclarationScope(Zone* zone, + AstValueFactory* ast_value_factory) : Scope(zone), function_kind_(kNormalFunction), params_(4, zone), sloppy_block_function_map_(zone) { + DCHECK_EQ(scope_type_, SCRIPT_SCOPE); SetDefaults(); + + // Make sure that if we don't find the global 'this', it won't be declared as + // a regular dynamic global by predeclaring it with the right variable kind. + DeclareDynamicGlobal(ast_value_factory->this_string(), Variable::THIS); } DeclarationScope::DeclarationScope(Zone* zone, Scope* outer_scope, @@ -144,6 +150,7 @@ DeclarationScope::DeclarationScope(Zone* zone, Scope* outer_scope, function_kind_(function_kind), params_(4, zone), sloppy_block_function_map_(zone) { + DCHECK_NE(scope_type, SCRIPT_SCOPE); SetDefaults(); asm_function_ = outer_scope_->IsAsmModule(); } @@ -182,6 +189,7 @@ DeclarationScope::DeclarationScope(Zone* zone, ScopeType scope_type, function_kind_(scope_info->function_kind()), params_(0, zone), sloppy_block_function_map_(zone) { + DCHECK_NE(scope_type, SCRIPT_SCOPE); SetDefaults(); } @@ -420,13 +428,6 @@ void DeclarationScope::Analyze(ParseInfo* info, AnalyzeMode mode) { scope->outer_scope()->scope_type() == SCRIPT_SCOPE || scope->outer_scope()->already_resolved_); - // If there's a chance that there's a reference to global 'this', predeclare - // it as a dynamic global on the script scope. - if (scope->GetReceiverScope()->is_script_scope()) { - info->script_scope()->DeclareDynamicGlobal( - info->ast_value_factory()->this_string(), Variable::THIS); - } - scope->AllocateVariables(info, mode); #ifdef DEBUG diff --git a/src/ast/scopes.h b/src/ast/scopes.h index f93c6fa192..eacda64cc5 100644 --- a/src/ast/scopes.h +++ b/src/ast/scopes.h @@ -587,7 +587,7 @@ class DeclarationScope : public Scope { DeclarationScope(Zone* zone, ScopeType scope_type, Handle scope_info); // Creates a script scope. - explicit DeclarationScope(Zone* zone); + DeclarationScope(Zone* zone, AstValueFactory* ast_value_factory); bool IsDeclaredParameter(const AstRawString* name) { // If IsSimpleParameterList is false, duplicate parameters are not allowed, diff --git a/src/parsing/parser-base.h b/src/parsing/parser-base.h index f1f047c9dd..693d054df9 100644 --- a/src/parsing/parser-base.h +++ b/src/parsing/parser-base.h @@ -667,7 +667,7 @@ class ParserBase { }; DeclarationScope* NewScriptScope() const { - return new (zone()) DeclarationScope(zone()); + return new (zone()) DeclarationScope(zone(), ast_value_factory()); } DeclarationScope* NewVarblockScope() const { diff --git a/test/cctest/test-parsing.cc b/test/cctest/test-parsing.cc index 1c1c9683da..fa9e29d1c9 100644 --- a/test/cctest/test-parsing.cc +++ b/test/cctest/test-parsing.cc @@ -3321,7 +3321,8 @@ TEST(SerializationOfMaybeAssignmentFlag) { const i::AstRawString* name = avf.GetOneByteString("result"); i::Handle str = name->string(); CHECK(str->IsInternalizedString()); - i::DeclarationScope* script_scope = new (&zone) i::DeclarationScope(&zone); + i::DeclarationScope* script_scope = + new (&zone) i::DeclarationScope(&zone, &avf); i::Scope* s = i::Scope::DeserializeScopeChain( isolate, &zone, context, script_scope, &avf, i::Scope::DeserializationMode::kKeepScopeInfo); @@ -3368,7 +3369,8 @@ TEST(IfArgumentsArrayAccessedThenParametersMaybeAssigned) { i::AstValueFactory avf(&zone, isolate->heap()->HashSeed()); avf.Internalize(isolate); - i::DeclarationScope* script_scope = new (&zone) i::DeclarationScope(&zone); + i::DeclarationScope* script_scope = + new (&zone) i::DeclarationScope(&zone, &avf); i::Scope* s = i::Scope::DeserializeScopeChain( isolate, &zone, context, script_scope, &avf, i::Scope::DeserializationMode::kKeepScopeInfo);