Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(321)

Side by Side Diff: src/ast/scopes.cc

Issue 2352593002: Preparse inner functions (new try) (Closed)
Patch Set: add comment Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/ast/scopes.h ('k') | src/flag-definitions.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/ast/scopes.h" 5 #include "src/ast/scopes.h"
6 6
7 #include <set> 7 #include <set>
8 8
9 #include "src/accessors.h" 9 #include "src/accessors.h"
10 #include "src/ast/ast.h" 10 #include "src/ast/ast.h"
(...skipping 1055 matching lines...) Expand 10 before | Expand all | Expand 10 after
1066 AllocateScopeInfosRecursively(info->isolate(), mode, outer_scope); 1066 AllocateScopeInfosRecursively(info->isolate(), mode, outer_scope);
1067 // The debugger expects all shared function infos to contain a scope info. 1067 // The debugger expects all shared function infos to contain a scope info.
1068 // Since the top-most scope will end up in a shared function info, make sure 1068 // Since the top-most scope will end up in a shared function info, make sure
1069 // it has one, even if it doesn't need a scope info. 1069 // it has one, even if it doesn't need a scope info.
1070 // TODO(jochen|yangguo): Remove this requirement. 1070 // TODO(jochen|yangguo): Remove this requirement.
1071 if (scope_info_.is_null()) { 1071 if (scope_info_.is_null()) {
1072 scope_info_ = ScopeInfo::Create(info->isolate(), zone(), this, outer_scope); 1072 scope_info_ = ScopeInfo::Create(info->isolate(), zone(), this, outer_scope);
1073 } 1073 }
1074 } 1074 }
1075 1075
1076 bool Scope::AllowsLazyParsing() const { 1076 bool Scope::AllowsLazyParsingWithoutUnresolvedVariables() const {
1077 // If we are inside a block scope, we must parse eagerly to find out how 1077 // If we are inside a block scope, we must find unresolved variables in the
1078 // to allocate variables on the block scope. At this point, declarations may 1078 // inner scopes to find out how to allocate variables on the block scope. At
1079 // not have yet been parsed. 1079 // this point, declarations may not have yet been parsed.
1080 for (const Scope* s = this; s != nullptr; s = s->outer_scope_) { 1080 for (const Scope* s = this; s != nullptr; s = s->outer_scope_) {
1081 if (s->is_block_scope()) return false; 1081 if (s->is_block_scope()) return false;
1082 // TODO(marja): Refactor parsing modes: also add s->is_function_scope()
1083 // here.
1082 } 1084 }
1083 return true; 1085 return true;
1084 } 1086 }
1085 1087
1086 bool DeclarationScope::AllowsLazyCompilation() const { 1088 bool DeclarationScope::AllowsLazyCompilation() const {
1087 return !force_eager_compilation_; 1089 return !force_eager_compilation_;
1088 } 1090 }
1089 1091
1090 bool DeclarationScope::AllowsLazyCompilationWithoutContext() const { 1092 bool DeclarationScope::AllowsLazyCompilationWithoutContext() const {
1091 if (force_eager_compilation_) return false; 1093 if (force_eager_compilation_) return false;
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
1171 Scope* Scope::GetOuterScopeWithContext() { 1173 Scope* Scope::GetOuterScopeWithContext() {
1172 Scope* scope = outer_scope_; 1174 Scope* scope = outer_scope_;
1173 while (scope && !scope->NeedsContext()) { 1175 while (scope && !scope->NeedsContext()) {
1174 scope = scope->outer_scope(); 1176 scope = scope->outer_scope();
1175 } 1177 }
1176 return scope; 1178 return scope;
1177 } 1179 }
1178 1180
1179 Handle<StringSet> DeclarationScope::CollectNonLocals( 1181 Handle<StringSet> DeclarationScope::CollectNonLocals(
1180 ParseInfo* info, Handle<StringSet> non_locals) { 1182 ParseInfo* info, Handle<StringSet> non_locals) {
1181 VariableProxy* free_variables = FetchFreeVariables(this, info); 1183 VariableProxy* free_variables = FetchFreeVariables(this, true, info);
1182 for (VariableProxy* proxy = free_variables; proxy != nullptr; 1184 for (VariableProxy* proxy = free_variables; proxy != nullptr;
1183 proxy = proxy->next_unresolved()) { 1185 proxy = proxy->next_unresolved()) {
1184 non_locals = StringSet::Add(non_locals, proxy->name()); 1186 non_locals = StringSet::Add(non_locals, proxy->name());
1185 } 1187 }
1186 return non_locals; 1188 return non_locals;
1187 } 1189 }
1188 1190
1189 void DeclarationScope::AnalyzePartially(DeclarationScope* migrate_to, 1191 void DeclarationScope::AnalyzePartially(DeclarationScope* migrate_to,
1190 AstNodeFactory* ast_node_factory) { 1192 AstNodeFactory* ast_node_factory) {
1191 // Try to resolve unresolved variables for this Scope and migrate those which 1193 // Try to resolve unresolved variables for this Scope and migrate those which
1192 // cannot be resolved inside. It doesn't make sense to try to resolve them in 1194 // cannot be resolved inside. It doesn't make sense to try to resolve them in
1193 // the outer Scopes here, because they are incomplete. 1195 // the outer Scopes here, because they are incomplete.
1194 for (VariableProxy* proxy = FetchFreeVariables(this); proxy != nullptr; 1196 for (VariableProxy* proxy =
1195 proxy = proxy->next_unresolved()) { 1197 FetchFreeVariables(this, !FLAG_lazy_inner_functions);
1198 proxy != nullptr; proxy = proxy->next_unresolved()) {
1196 DCHECK(!proxy->is_resolved()); 1199 DCHECK(!proxy->is_resolved());
1197 VariableProxy* copy = ast_node_factory->CopyVariableProxy(proxy); 1200 VariableProxy* copy = ast_node_factory->CopyVariableProxy(proxy);
1198 migrate_to->AddUnresolved(copy); 1201 migrate_to->AddUnresolved(copy);
1199 } 1202 }
1200 1203
1201 // Push scope data up to migrate_to. Note that migrate_to and this Scope 1204 // Push scope data up to migrate_to. Note that migrate_to and this Scope
1202 // describe the same Scope, just in different Zones. 1205 // describe the same Scope, just in different Zones.
1203 PropagateUsageFlagsToScope(migrate_to); 1206 PropagateUsageFlagsToScope(migrate_to);
1204 if (scope_uses_super_property_) migrate_to->scope_uses_super_property_ = true; 1207 if (scope_uses_super_property_) migrate_to->scope_uses_super_property_ = true;
1205 if (inner_scope_calls_eval_) migrate_to->inner_scope_calls_eval_ = true; 1208 if (inner_scope_calls_eval_) migrate_to->inner_scope_calls_eval_ = true;
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after
1508 } 1511 }
1509 1512
1510 return var; 1513 return var;
1511 } 1514 }
1512 1515
1513 void Scope::ResolveVariable(ParseInfo* info, VariableProxy* proxy) { 1516 void Scope::ResolveVariable(ParseInfo* info, VariableProxy* proxy) {
1514 DCHECK(info->script_scope()->is_script_scope()); 1517 DCHECK(info->script_scope()->is_script_scope());
1515 DCHECK(!proxy->is_resolved()); 1518 DCHECK(!proxy->is_resolved());
1516 Variable* var = LookupRecursive(proxy, nullptr); 1519 Variable* var = LookupRecursive(proxy, nullptr);
1517 ResolveTo(info, proxy, var); 1520 ResolveTo(info, proxy, var);
1521
1522 if (FLAG_lazy_inner_functions) {
1523 if (info != nullptr && info->is_native()) return;
1524 // Pessimistically force context allocation for all variables to which inner
1525 // scope variables could potentially resolve to.
1526 Scope* scope = GetClosureScope()->outer_scope_;
1527 while (scope != nullptr && scope->scope_info_.is_null()) {
1528 var = scope->LookupLocal(proxy->raw_name());
1529 if (var != nullptr) {
1530 // Since we don't lazy parse inner arrow functions, inner functions
1531 // cannot refer to the outer "this".
1532 if (!var->is_dynamic() && !var->is_this() &&
1533 !var->has_forced_context_allocation()) {
1534 var->ForceContextAllocation();
1535 var->set_is_used();
1536 // We don't know what the (potentially lazy parsed) inner function
1537 // does with the variable; pessimistically assume that it's assigned.
1538 var->set_maybe_assigned();
1539 }
1540 }
1541 scope = scope->outer_scope_;
1542 }
1543 }
1518 } 1544 }
1519 1545
1520 void Scope::ResolveTo(ParseInfo* info, VariableProxy* proxy, Variable* var) { 1546 void Scope::ResolveTo(ParseInfo* info, VariableProxy* proxy, Variable* var) {
1521 #ifdef DEBUG 1547 #ifdef DEBUG
1522 if (info->script_is_native()) { 1548 if (info->script_is_native()) {
1523 // To avoid polluting the global object in native scripts 1549 // To avoid polluting the global object in native scripts
1524 // - Variables must not be allocated to the global scope. 1550 // - Variables must not be allocated to the global scope.
1525 CHECK_NOT_NULL(outer_scope()); 1551 CHECK_NOT_NULL(outer_scope());
1526 // - Variables must be bound locally or unallocated. 1552 // - Variables must be bound locally or unallocated.
1527 if (var->IsGlobalObjectProperty()) { 1553 if (var->IsGlobalObjectProperty()) {
(...skipping 25 matching lines...) Expand all
1553 ResolveVariable(info, proxy); 1579 ResolveVariable(info, proxy);
1554 } 1580 }
1555 1581
1556 // Resolve unresolved variables for inner scopes. 1582 // Resolve unresolved variables for inner scopes.
1557 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { 1583 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) {
1558 scope->ResolveVariablesRecursively(info); 1584 scope->ResolveVariablesRecursively(info);
1559 } 1585 }
1560 } 1586 }
1561 1587
1562 VariableProxy* Scope::FetchFreeVariables(DeclarationScope* max_outer_scope, 1588 VariableProxy* Scope::FetchFreeVariables(DeclarationScope* max_outer_scope,
1563 ParseInfo* info, 1589 bool try_to_resolve, ParseInfo* info,
1564 VariableProxy* stack) { 1590 VariableProxy* stack) {
1565 for (VariableProxy *proxy = unresolved_, *next = nullptr; proxy != nullptr; 1591 for (VariableProxy *proxy = unresolved_, *next = nullptr; proxy != nullptr;
1566 proxy = next) { 1592 proxy = next) {
1567 next = proxy->next_unresolved(); 1593 next = proxy->next_unresolved();
1568 DCHECK(!proxy->is_resolved()); 1594 DCHECK(!proxy->is_resolved());
1569 Variable* var = LookupRecursive(proxy, max_outer_scope->outer_scope()); 1595 Variable* var = nullptr;
1596 if (try_to_resolve) {
1597 var = LookupRecursive(proxy, max_outer_scope->outer_scope());
1598 }
1570 if (var == nullptr) { 1599 if (var == nullptr) {
1571 proxy->set_next_unresolved(stack); 1600 proxy->set_next_unresolved(stack);
1572 stack = proxy; 1601 stack = proxy;
1573 } else if (info != nullptr) { 1602 } else if (info != nullptr) {
1574 ResolveTo(info, proxy, var); 1603 ResolveTo(info, proxy, var);
1575 } 1604 }
1576 } 1605 }
1577 1606
1578 // Clear unresolved_ as it's in an inconsistent state. 1607 // Clear unresolved_ as it's in an inconsistent state.
1579 unresolved_ = nullptr; 1608 unresolved_ = nullptr;
1580 1609
1581 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { 1610 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) {
1582 stack = scope->FetchFreeVariables(max_outer_scope, info, stack); 1611 stack =
1612 scope->FetchFreeVariables(max_outer_scope, try_to_resolve, info, stack);
1583 } 1613 }
1584 1614
1585 return stack; 1615 return stack;
1586 } 1616 }
1587 1617
1588 bool Scope::MustAllocate(Variable* var) { 1618 bool Scope::MustAllocate(Variable* var) {
1589 DCHECK(var->location() != VariableLocation::MODULE); 1619 DCHECK(var->location() != VariableLocation::MODULE);
1590 // Give var a read/write use if there is a chance it might be accessed 1620 // Give var a read/write use if there is a chance it might be accessed
1591 // via an eval() call. This is only possible if the variable has a 1621 // via an eval() call. This is only possible if the variable has a
1592 // visible name. 1622 // visible name.
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
1828 Variable* function = 1858 Variable* function =
1829 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; 1859 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr;
1830 bool is_function_var_in_context = 1860 bool is_function_var_in_context =
1831 function != nullptr && function->IsContextSlot(); 1861 function != nullptr && function->IsContextSlot();
1832 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - 1862 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS -
1833 (is_function_var_in_context ? 1 : 0); 1863 (is_function_var_in_context ? 1 : 0);
1834 } 1864 }
1835 1865
1836 } // namespace internal 1866 } // namespace internal
1837 } // namespace v8 1867 } // namespace v8
OLDNEW
« no previous file with comments | « src/ast/scopes.h ('k') | src/flag-definitions.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698