Chromium Code Reviews| 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 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 442 | 442 |
| 443 const ModuleScope* Scope::AsModuleScope() const { | 443 const ModuleScope* Scope::AsModuleScope() const { |
| 444 DCHECK(is_module_scope()); | 444 DCHECK(is_module_scope()); |
| 445 return static_cast<const ModuleScope*>(this); | 445 return static_cast<const ModuleScope*>(this); |
| 446 } | 446 } |
| 447 | 447 |
| 448 int Scope::num_parameters() const { | 448 int Scope::num_parameters() const { |
| 449 return is_declaration_scope() ? AsDeclarationScope()->num_parameters() : 0; | 449 return is_declaration_scope() ? AsDeclarationScope()->num_parameters() : 0; |
| 450 } | 450 } |
| 451 | 451 |
| 452 void DeclarationScope::HoistSloppyBlockFunctions(AstNodeFactory* factory, | |
|
adamk
2016/09/02 18:16:27
Can you add some DCHECKS at the start here?
DCHEC
marja
2016/09/05 09:23:43
Done.
| |
| 453 bool* ok) { | |
| 454 // For each variable which is used as a function declaration in a sloppy | |
| 455 // block, | |
| 456 SloppyBlockFunctionMap* map = sloppy_block_function_map(); | |
| 457 for (ZoneHashMap::Entry* p = map->Start(); p != nullptr; p = map->Next(p)) { | |
| 458 AstRawString* name = static_cast<AstRawString*>(p->key); | |
| 459 | |
| 460 // If the variable wouldn't conflict with a lexical declaration | |
| 461 // or parameter, | |
| 462 | |
| 463 // Check if there's a conflict with a parameter. | |
| 464 // This depends on the fact that functions always have a scope solely to | |
| 465 // hold complex parameters, and the names local to that scope are | |
| 466 // precisely the names of the parameters. IsDeclaredParameter(name) does | |
| 467 // not hold for names declared by complex parameters, nor are those | |
| 468 // bindings necessarily declared lexically, so we have to check for them | |
| 469 // explicitly. On the other hand, if there are not complex parameters, | |
| 470 // it is sufficient to just check IsDeclaredParameter. | |
| 471 if (!HasSimpleParameters()) { | |
|
adamk
2016/09/02 18:16:27
HasSimpleParameters() does a scope chain walk, see
marja
2016/09/05 09:23:43
Done.
| |
| 472 if (outer_scope_->LookupLocal(name) != nullptr) { | |
| 473 continue; | |
| 474 } | |
| 475 } else { | |
| 476 if (IsDeclaredParameter(name)) { | |
| 477 continue; | |
| 478 } | |
| 479 } | |
| 480 | |
| 481 bool var_created = false; | |
| 482 | |
| 483 // Write in assignments to var for each block-scoped function declaration | |
| 484 auto delegates = static_cast<SloppyBlockFunctionStatement*>(p->value); | |
| 485 | |
| 486 DeclarationScope* decl_scope = this; | |
| 487 while (decl_scope->is_eval_scope()) { | |
| 488 decl_scope = decl_scope->outer_scope()->GetDeclarationScope(); | |
| 489 } | |
| 490 Scope* outer_scope = decl_scope->outer_scope(); | |
| 491 | |
| 492 for (SloppyBlockFunctionStatement* delegate = delegates; | |
| 493 delegate != nullptr; delegate = delegate->next()) { | |
| 494 // Check if there's a conflict with a lexical declaration | |
| 495 Scope* query_scope = delegate->scope()->outer_scope(); | |
| 496 Variable* var = nullptr; | |
| 497 bool should_hoist = true; | |
| 498 | |
| 499 // Note that we perform this loop for each delegate named 'name', | |
| 500 // which may duplicate work if those delegates share scopes. | |
| 501 // It is not sufficient to just do a Lookup on query_scope: for | |
| 502 // example, that does not prevent hoisting of the function in | |
| 503 // `{ let e; try {} catch (e) { function e(){} } }` | |
| 504 do { | |
| 505 var = query_scope->LookupLocal(name); | |
| 506 if (var != nullptr && IsLexicalVariableMode(var->mode())) { | |
| 507 should_hoist = false; | |
| 508 break; | |
| 509 } | |
| 510 query_scope = query_scope->outer_scope(); | |
| 511 } while (query_scope != outer_scope); | |
| 512 | |
| 513 if (!should_hoist) continue; | |
| 514 | |
| 515 // Declare a var-style binding for the function in the outer scope | |
| 516 if (!var_created) { | |
| 517 var_created = true; | |
| 518 VariableProxy* proxy = NewUnresolved(factory, name); | |
| 519 Declaration* declaration = | |
| 520 factory->NewVariableDeclaration(proxy, this, kNoSourcePosition); | |
| 521 // Based on the preceding check, it doesn't matter what we pass as | |
| 522 // allow_harmony_restrictive_generators and | |
| 523 // sloppy_mode_block_scope_function_redefinition. | |
| 524 DeclareVariable(declaration, VAR, | |
| 525 Variable::DefaultInitializationFlag(VAR), false, | |
| 526 nullptr, ok); | |
| 527 DCHECK(*ok); // Based on the preceding check, this should not fail | |
| 528 if (!*ok) return; | |
| 529 } | |
| 530 | |
| 531 // Create VariableProxies for creating an assignment statement | |
| 532 // (later). Read from the local lexical scope and write to the function | |
| 533 // scope. | |
| 534 delegate->set_to(NewUnresolved(factory, name)); | |
| 535 delegate->set_from(delegate->scope()->NewUnresolved(factory, name)); | |
| 536 } | |
| 537 } | |
| 538 } | |
| 539 | |
| 452 void DeclarationScope::Analyze(ParseInfo* info, AnalyzeMode mode) { | 540 void DeclarationScope::Analyze(ParseInfo* info, AnalyzeMode mode) { |
| 453 DCHECK(info->literal() != NULL); | 541 DCHECK(info->literal() != NULL); |
| 454 DeclarationScope* scope = info->literal()->scope(); | 542 DeclarationScope* scope = info->literal()->scope(); |
| 455 | 543 |
| 456 // We are compiling one of three cases: | 544 // We are compiling one of three cases: |
| 457 // 1) top-level code, | 545 // 1) top-level code, |
| 458 // 2) a function/eval/module on the top-level | 546 // 2) a function/eval/module on the top-level |
| 459 // 3) a function/eval in a scope that was already resolved. | 547 // 3) a function/eval in a scope that was already resolved. |
| 460 DCHECK(scope->scope_type() == SCRIPT_SCOPE || | 548 DCHECK(scope->scope_type() == SCRIPT_SCOPE || |
| 461 scope->outer_scope()->scope_type() == SCRIPT_SCOPE || | 549 scope->outer_scope()->scope_type() == SCRIPT_SCOPE || |
| (...skipping 1225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1687 Variable* function = | 1775 Variable* function = |
| 1688 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; | 1776 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; |
| 1689 bool is_function_var_in_context = | 1777 bool is_function_var_in_context = |
| 1690 function != nullptr && function->IsContextSlot(); | 1778 function != nullptr && function->IsContextSlot(); |
| 1691 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - | 1779 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
| 1692 (is_function_var_in_context ? 1 : 0); | 1780 (is_function_var_in_context ? 1 : 0); |
| 1693 } | 1781 } |
| 1694 | 1782 |
| 1695 } // namespace internal | 1783 } // namespace internal |
| 1696 } // namespace v8 | 1784 } // namespace v8 |
| OLD | NEW |