| OLD | NEW |
| 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/bootstrapper.h" | 10 #include "src/bootstrapper.h" |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 | 150 |
| 151 ModuleScope::ModuleScope(DeclarationScope* script_scope, | 151 ModuleScope::ModuleScope(DeclarationScope* script_scope, |
| 152 AstValueFactory* ast_value_factory) | 152 AstValueFactory* ast_value_factory) |
| 153 : DeclarationScope(ast_value_factory->zone(), script_scope, MODULE_SCOPE) { | 153 : DeclarationScope(ast_value_factory->zone(), script_scope, MODULE_SCOPE) { |
| 154 Zone* zone = ast_value_factory->zone(); | 154 Zone* zone = ast_value_factory->zone(); |
| 155 module_descriptor_ = new (zone) ModuleDescriptor(zone); | 155 module_descriptor_ = new (zone) ModuleDescriptor(zone); |
| 156 set_language_mode(STRICT); | 156 set_language_mode(STRICT); |
| 157 DeclareThis(ast_value_factory); | 157 DeclareThis(ast_value_factory); |
| 158 } | 158 } |
| 159 | 159 |
| 160 ModuleScope::ModuleScope(Isolate* isolate, Handle<ScopeInfo> scope_info, |
| 161 AstValueFactory* avfactory) |
| 162 : DeclarationScope(avfactory->zone(), MODULE_SCOPE, scope_info) { |
| 163 Zone* zone = avfactory->zone(); |
| 164 ModuleInfo* module_info = scope_info->ModuleDescriptorInfo(); |
| 165 |
| 166 set_language_mode(STRICT); |
| 167 module_descriptor_ = new (zone) ModuleDescriptor(zone); |
| 168 |
| 169 // Deserialize special exports. |
| 170 Handle<FixedArray> special_exports = handle(module_info->special_exports()); |
| 171 for (int i = 0, n = special_exports->length(); i < n; ++i) { |
| 172 Handle<FixedArray> serialized_entry( |
| 173 FixedArray::cast(special_exports->get(i)), isolate); |
| 174 module_descriptor_->AddSpecialExport( |
| 175 ModuleDescriptor::Entry::Deserialize(isolate, avfactory, |
| 176 serialized_entry), |
| 177 avfactory->zone()); |
| 178 } |
| 179 |
| 180 // Deserialize regular exports. |
| 181 Handle<FixedArray> regular_exports = handle(module_info->regular_exports()); |
| 182 for (int i = 0, n = regular_exports->length(); i < n; ++i) { |
| 183 Handle<FixedArray> serialized_entry( |
| 184 FixedArray::cast(regular_exports->get(i)), isolate); |
| 185 module_descriptor_->AddRegularExport(ModuleDescriptor::Entry::Deserialize( |
| 186 isolate, avfactory, serialized_entry)); |
| 187 } |
| 188 } |
| 189 |
| 160 Scope::Scope(Zone* zone, ScopeType scope_type, Handle<ScopeInfo> scope_info) | 190 Scope::Scope(Zone* zone, ScopeType scope_type, Handle<ScopeInfo> scope_info) |
| 161 : zone_(zone), | 191 : zone_(zone), |
| 162 outer_scope_(nullptr), | 192 outer_scope_(nullptr), |
| 163 variables_(zone), | 193 variables_(zone), |
| 164 locals_(0, zone), | 194 locals_(0, zone), |
| 165 decls_(0, zone), | 195 decls_(0, zone), |
| 166 scope_info_(scope_info), | 196 scope_info_(scope_info), |
| 167 scope_type_(scope_type) { | 197 scope_type_(scope_type) { |
| 168 DCHECK(!scope_info.is_null()); | 198 DCHECK(!scope_info.is_null()); |
| 169 SetDefaults(); | 199 SetDefaults(); |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 outer_scope->AsDeclarationScope()->set_asm_module(); | 333 outer_scope->AsDeclarationScope()->set_asm_module(); |
| 304 } else if (context->IsBlockContext()) { | 334 } else if (context->IsBlockContext()) { |
| 305 Handle<ScopeInfo> scope_info(context->scope_info(), isolate); | 335 Handle<ScopeInfo> scope_info(context->scope_info(), isolate); |
| 306 DCHECK_EQ(scope_info->scope_type(), BLOCK_SCOPE); | 336 DCHECK_EQ(scope_info->scope_type(), BLOCK_SCOPE); |
| 307 if (scope_info->is_declaration_scope()) { | 337 if (scope_info->is_declaration_scope()) { |
| 308 outer_scope = | 338 outer_scope = |
| 309 new (zone) DeclarationScope(zone, BLOCK_SCOPE, scope_info); | 339 new (zone) DeclarationScope(zone, BLOCK_SCOPE, scope_info); |
| 310 } else { | 340 } else { |
| 311 outer_scope = new (zone) Scope(zone, BLOCK_SCOPE, scope_info); | 341 outer_scope = new (zone) Scope(zone, BLOCK_SCOPE, scope_info); |
| 312 } | 342 } |
| 343 } else if (context->IsModuleContext()) { |
| 344 ScopeInfo* scope_info = context->closure()->shared()->scope_info(); |
| 345 DCHECK_EQ(scope_info->scope_type(), MODULE_SCOPE); |
| 346 outer_scope = new (zone) ModuleScope( |
| 347 isolate, Handle<ScopeInfo>(scope_info), ast_value_factory); |
| 313 } else { | 348 } else { |
| 314 DCHECK(context->IsCatchContext()); | 349 DCHECK(context->IsCatchContext()); |
| 315 String* name = context->catch_name(); | 350 String* name = context->catch_name(); |
| 316 outer_scope = new (zone) | 351 outer_scope = new (zone) |
| 317 Scope(zone, ast_value_factory->GetString(handle(name, isolate))); | 352 Scope(zone, ast_value_factory->GetString(handle(name, isolate))); |
| 318 } | 353 } |
| 319 if (current_scope != nullptr) { | 354 if (current_scope != nullptr) { |
| 320 outer_scope->AddInnerScope(current_scope); | 355 outer_scope->AddInnerScope(current_scope); |
| 321 } | 356 } |
| 322 current_scope = outer_scope; | 357 current_scope = outer_scope; |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 404 DeclarationScope* scope = info->literal()->scope(); | 439 DeclarationScope* scope = info->literal()->scope(); |
| 405 | 440 |
| 406 // We are compiling one of three cases: | 441 // We are compiling one of three cases: |
| 407 // 1) top-level code, | 442 // 1) top-level code, |
| 408 // 2) a function/eval/module on the top-level | 443 // 2) a function/eval/module on the top-level |
| 409 // 3) a function/eval in a scope that was already resolved. | 444 // 3) a function/eval in a scope that was already resolved. |
| 410 DCHECK(scope->scope_type() == SCRIPT_SCOPE || | 445 DCHECK(scope->scope_type() == SCRIPT_SCOPE || |
| 411 scope->outer_scope()->scope_type() == SCRIPT_SCOPE || | 446 scope->outer_scope()->scope_type() == SCRIPT_SCOPE || |
| 412 scope->outer_scope()->already_resolved_); | 447 scope->outer_scope()->already_resolved_); |
| 413 | 448 |
| 449 // For modules, we want to start variable allocation at the surrounding script |
| 450 // scope. |
| 451 if (scope->is_module_scope()) { |
| 452 scope = scope->outer_scope()->AsDeclarationScope(); |
| 453 } |
| 454 |
| 414 scope->AllocateVariables(info); | 455 scope->AllocateVariables(info); |
| 415 | 456 |
| 416 #ifdef DEBUG | 457 #ifdef DEBUG |
| 417 if (info->script_is_native() ? FLAG_print_builtin_scopes | 458 if (info->script_is_native() ? FLAG_print_builtin_scopes |
| 418 : FLAG_print_scopes) { | 459 : FLAG_print_scopes) { |
| 419 scope->Print(); | 460 scope->Print(); |
| 420 } | 461 } |
| 421 scope->CheckScopePositions(); | 462 scope->CheckScopePositions(); |
| 422 scope->CheckZones(); | 463 scope->CheckZones(); |
| 423 #endif | 464 #endif |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 593 | 634 |
| 594 VariableMode mode; | 635 VariableMode mode; |
| 595 InitializationFlag init_flag; | 636 InitializationFlag init_flag; |
| 596 MaybeAssignedFlag maybe_assigned_flag; | 637 MaybeAssignedFlag maybe_assigned_flag; |
| 597 | 638 |
| 598 VariableLocation location = VariableLocation::CONTEXT; | 639 VariableLocation location = VariableLocation::CONTEXT; |
| 599 int index = ScopeInfo::ContextSlotIndex(scope_info_, name_handle, &mode, | 640 int index = ScopeInfo::ContextSlotIndex(scope_info_, name_handle, &mode, |
| 600 &init_flag, &maybe_assigned_flag); | 641 &init_flag, &maybe_assigned_flag); |
| 601 if (index < 0 && scope_type() == MODULE_SCOPE) { | 642 if (index < 0 && scope_type() == MODULE_SCOPE) { |
| 602 location = VariableLocation::MODULE; | 643 location = VariableLocation::MODULE; |
| 603 index = -1; // TODO(neis): Find module variables in scope info. | 644 index = scope_info_->ModuleIndex(name_handle, &mode, &init_flag, |
| 645 &maybe_assigned_flag); |
| 604 } | 646 } |
| 605 if (index < 0) return nullptr; // Nowhere found. | 647 if (index < 0) return nullptr; // Nowhere found. |
| 606 | 648 |
| 607 Variable::Kind kind = Variable::NORMAL; | 649 Variable::Kind kind = Variable::NORMAL; |
| 608 if (location == VariableLocation::CONTEXT && | 650 if (location == VariableLocation::CONTEXT && |
| 609 index == scope_info_->ReceiverContextSlotIndex()) { | 651 index == scope_info_->ReceiverContextSlotIndex()) { |
| 610 kind = Variable::THIS; | 652 kind = Variable::THIS; |
| 611 } | 653 } |
| 612 // TODO(marja, rossberg): Correctly declare FUNCTION, CLASS, NEW_TARGET, and | 654 // TODO(marja, rossberg): Correctly declare FUNCTION, CLASS, NEW_TARGET, and |
| 613 // ARGUMENTS bindings as their corresponding Variable::Kind. | 655 // ARGUMENTS bindings as their corresponding Variable::Kind. |
| (...skipping 1042 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1656 Variable* function = | 1698 Variable* function = |
| 1657 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; | 1699 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; |
| 1658 bool is_function_var_in_context = | 1700 bool is_function_var_in_context = |
| 1659 function != nullptr && function->IsContextSlot(); | 1701 function != nullptr && function->IsContextSlot(); |
| 1660 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - | 1702 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
| 1661 (is_function_var_in_context ? 1 : 0); | 1703 (is_function_var_in_context ? 1 : 0); |
| 1662 } | 1704 } |
| 1663 | 1705 |
| 1664 } // namespace internal | 1706 } // namespace internal |
| 1665 } // namespace v8 | 1707 } // namespace v8 |
| OLD | NEW |