| OLD | NEW |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 } | 297 } |
| 298 | 298 |
| 299 | 299 |
| 300 // Make sure the method gets instantiated by the template system. | 300 // Make sure the method gets instantiated by the template system. |
| 301 template void Scope::CollectUsedVariables( | 301 template void Scope::CollectUsedVariables( |
| 302 List<Variable*, FreeStoreAllocationPolicy>* locals); | 302 List<Variable*, FreeStoreAllocationPolicy>* locals); |
| 303 template void Scope::CollectUsedVariables( | 303 template void Scope::CollectUsedVariables( |
| 304 List<Variable*, PreallocatedStorage>* locals); | 304 List<Variable*, PreallocatedStorage>* locals); |
| 305 | 305 |
| 306 | 306 |
| 307 void Scope::AllocateVariables() { | 307 void Scope::AllocateVariables(Handle<Context> context) { |
| 308 ASSERT(outer_scope_ == NULL); // eval or global scopes only | 308 ASSERT(outer_scope_ == NULL); // eval or global scopes only |
| 309 | 309 |
| 310 // 1) Propagate scope information. | 310 // 1) Propagate scope information. |
| 311 // If we are in an eval scope, we may have other outer scopes about | 311 // If we are in an eval scope, we may have other outer scopes about |
| 312 // which we don't know anything at this point. Thus we must be conservative | 312 // which we don't know anything at this point. Thus we must be conservative |
| 313 // and assume they may invoke eval themselves. Eventually we could capture | 313 // and assume they may invoke eval themselves. Eventually we could capture |
| 314 // this information in the ScopeInfo and then use it here (by traversing | 314 // this information in the ScopeInfo and then use it here (by traversing |
| 315 // the call chain stack, at compile time). | 315 // the call chain stack, at compile time). |
| 316 bool eval_scope = is_eval_scope(); | 316 bool eval_scope = is_eval_scope(); |
| 317 PropagateScopeInfo(eval_scope, eval_scope); | 317 PropagateScopeInfo(eval_scope, eval_scope); |
| 318 | 318 |
| 319 // 2) Resolve variables. | 319 // 2) Resolve variables. |
| 320 Scope* global_scope = NULL; | 320 Scope* global_scope = NULL; |
| 321 if (is_global_scope()) global_scope = this; | 321 if (is_global_scope()) global_scope = this; |
| 322 ResolveVariablesRecursively(global_scope); | 322 ResolveVariablesRecursively(global_scope, context); |
| 323 | 323 |
| 324 // 3) Allocate variables. | 324 // 3) Allocate variables. |
| 325 AllocateVariablesRecursively(); | 325 AllocateVariablesRecursively(); |
| 326 } | 326 } |
| 327 | 327 |
| 328 | 328 |
| 329 bool Scope::AllowsLazyCompilation() const { | 329 bool Scope::AllowsLazyCompilation() const { |
| 330 return !force_eager_compilation_ && HasTrivialOuterContext(); | 330 return !force_eager_compilation_ && HasTrivialOuterContext(); |
| 331 } | 331 } |
| 332 | 332 |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 570 // If the variable we have found is just a guess, invalidate the result. | 570 // If the variable we have found is just a guess, invalidate the result. |
| 571 if (guess) { | 571 if (guess) { |
| 572 *invalidated_local = var; | 572 *invalidated_local = var; |
| 573 var = NULL; | 573 var = NULL; |
| 574 } | 574 } |
| 575 | 575 |
| 576 return var; | 576 return var; |
| 577 } | 577 } |
| 578 | 578 |
| 579 | 579 |
| 580 void Scope::ResolveVariable(Scope* global_scope, VariableProxy* proxy) { | 580 void Scope::ResolveVariable(Scope* global_scope, |
| 581 Handle<Context> context, |
| 582 VariableProxy* proxy) { |
| 581 ASSERT(global_scope == NULL || global_scope->is_global_scope()); | 583 ASSERT(global_scope == NULL || global_scope->is_global_scope()); |
| 582 | 584 |
| 583 // If the proxy is already resolved there's nothing to do | 585 // If the proxy is already resolved there's nothing to do |
| 584 // (functions and consts may be resolved by the parser). | 586 // (functions and consts may be resolved by the parser). |
| 585 if (proxy->var() != NULL) return; | 587 if (proxy->var() != NULL) return; |
| 586 | 588 |
| 587 // Otherwise, try to resolve the variable. | 589 // Otherwise, try to resolve the variable. |
| 588 Variable* invalidated_local = NULL; | 590 Variable* invalidated_local = NULL; |
| 589 Variable* var = LookupRecursive(proxy->name(), false, &invalidated_local); | 591 Variable* var = LookupRecursive(proxy->name(), false, &invalidated_local); |
| 590 | 592 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 631 | 633 |
| 632 } else if (invalidated_local != NULL) { | 634 } else if (invalidated_local != NULL) { |
| 633 // No with statements are involved and we found a local | 635 // No with statements are involved and we found a local |
| 634 // variable that might be shadowed by eval introduced | 636 // variable that might be shadowed by eval introduced |
| 635 // variables. | 637 // variables. |
| 636 var = NonLocal(proxy->name(), Variable::DYNAMIC_LOCAL); | 638 var = NonLocal(proxy->name(), Variable::DYNAMIC_LOCAL); |
| 637 var->set_local_if_not_shadowed(invalidated_local); | 639 var->set_local_if_not_shadowed(invalidated_local); |
| 638 | 640 |
| 639 } else if (outer_scope_is_eval_scope_) { | 641 } else if (outer_scope_is_eval_scope_) { |
| 640 // No with statements and we did not find a local and the code | 642 // No with statements and we did not find a local and the code |
| 641 // is executed with a call to eval. We don't know anything | 643 // is executed with a call to eval. The context contains |
| 642 // because we do not have information about the scopes | 644 // scope information that we can use to determine if the |
| 643 // surrounding the eval call. | 645 // variable is global if it is not shadowed by eval-introduced |
| 644 var = NonLocal(proxy->name(), Variable::DYNAMIC); | 646 // variables. |
| 647 if (context->GlobalIfNotShadowedByEval(proxy->name())) { |
| 648 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL); |
| 649 |
| 650 } else { |
| 651 var = NonLocal(proxy->name(), Variable::DYNAMIC); |
| 652 } |
| 645 | 653 |
| 646 } else { | 654 } else { |
| 647 // No with statements and we did not find a local and the code | 655 // No with statements and we did not find a local and the code |
| 648 // is not executed with a call to eval. We know that this | 656 // is not executed with a call to eval. We know that this |
| 649 // variable is global unless it is shadowed by eval-introduced | 657 // variable is global unless it is shadowed by eval-introduced |
| 650 // variables. | 658 // variables. |
| 651 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL); | 659 var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL); |
| 652 } | 660 } |
| 653 } | 661 } |
| 654 } | 662 } |
| 655 | 663 |
| 656 proxy->BindTo(var); | 664 proxy->BindTo(var); |
| 657 } | 665 } |
| 658 | 666 |
| 659 | 667 |
| 660 void Scope::ResolveVariablesRecursively(Scope* global_scope) { | 668 void Scope::ResolveVariablesRecursively(Scope* global_scope, |
| 669 Handle<Context> context) { |
| 661 ASSERT(global_scope == NULL || global_scope->is_global_scope()); | 670 ASSERT(global_scope == NULL || global_scope->is_global_scope()); |
| 662 | 671 |
| 663 // Resolve unresolved variables for this scope. | 672 // Resolve unresolved variables for this scope. |
| 664 for (int i = 0; i < unresolved_.length(); i++) { | 673 for (int i = 0; i < unresolved_.length(); i++) { |
| 665 ResolveVariable(global_scope, unresolved_[i]); | 674 ResolveVariable(global_scope, context, unresolved_[i]); |
| 666 } | 675 } |
| 667 | 676 |
| 668 // Resolve unresolved variables for inner scopes. | 677 // Resolve unresolved variables for inner scopes. |
| 669 for (int i = 0; i < inner_scopes_.length(); i++) { | 678 for (int i = 0; i < inner_scopes_.length(); i++) { |
| 670 inner_scopes_[i]->ResolveVariablesRecursively(global_scope); | 679 inner_scopes_[i]->ResolveVariablesRecursively(global_scope, context); |
| 671 } | 680 } |
| 672 } | 681 } |
| 673 | 682 |
| 674 | 683 |
| 675 bool Scope::PropagateScopeInfo(bool outer_scope_calls_eval, | 684 bool Scope::PropagateScopeInfo(bool outer_scope_calls_eval, |
| 676 bool outer_scope_is_eval_scope) { | 685 bool outer_scope_is_eval_scope) { |
| 677 if (outer_scope_calls_eval) { | 686 if (outer_scope_calls_eval) { |
| 678 outer_scope_calls_eval_ = true; | 687 outer_scope_calls_eval_ = true; |
| 679 } | 688 } |
| 680 | 689 |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 927 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS && | 936 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS && |
| 928 !must_have_local_context) { | 937 !must_have_local_context) { |
| 929 num_heap_slots_ = 0; | 938 num_heap_slots_ = 0; |
| 930 } | 939 } |
| 931 | 940 |
| 932 // Allocation done. | 941 // Allocation done. |
| 933 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS); | 942 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS); |
| 934 } | 943 } |
| 935 | 944 |
| 936 } } // namespace v8::internal | 945 } } // namespace v8::internal |
| OLD | NEW |