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

Side by Side Diff: src/scopes.cc

Issue 7549008: Preliminary code for block scopes and block contexts. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Small fix: set harmony flag properly Created 9 years, 4 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 | Annotate | Revision Log
« no previous file with comments | « src/scopes.h ('k') | src/serialize.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 2011 the V8 project authors. All rights reserved. 1 // Copyright 2011 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 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 already_resolved_(false) { 139 already_resolved_(false) {
140 SetDefaults(type, outer_scope, Handle<SerializedScopeInfo>::null()); 140 SetDefaults(type, outer_scope, Handle<SerializedScopeInfo>::null());
141 // At some point we might want to provide outer scopes to 141 // At some point we might want to provide outer scopes to
142 // eval scopes (by walking the stack and reading the scope info). 142 // eval scopes (by walking the stack and reading the scope info).
143 // In that case, the ASSERT below needs to be adjusted. 143 // In that case, the ASSERT below needs to be adjusted.
144 ASSERT((type == GLOBAL_SCOPE || type == EVAL_SCOPE) == (outer_scope == NULL)); 144 ASSERT((type == GLOBAL_SCOPE || type == EVAL_SCOPE) == (outer_scope == NULL));
145 ASSERT(!HasIllegalRedeclaration()); 145 ASSERT(!HasIllegalRedeclaration());
146 } 146 }
147 147
148 148
149 Scope::Scope(Scope* inner_scope, Handle<SerializedScopeInfo> scope_info) 149 Scope::Scope(Scope* inner_scope,
150 Type type,
151 Handle<SerializedScopeInfo> scope_info)
150 : isolate_(Isolate::Current()), 152 : isolate_(Isolate::Current()),
151 inner_scopes_(4), 153 inner_scopes_(4),
152 variables_(), 154 variables_(),
153 temps_(4), 155 temps_(4),
154 params_(4), 156 params_(4),
155 unresolved_(16), 157 unresolved_(16),
156 decls_(4), 158 decls_(4),
157 already_resolved_(true) { 159 already_resolved_(true) {
158 ASSERT(!scope_info.is_null()); 160 ASSERT(!scope_info.is_null());
159 SetDefaults(FUNCTION_SCOPE, NULL, scope_info); 161 SetDefaults(type, NULL, scope_info);
160 if (scope_info->HasHeapAllocatedLocals()) { 162 if (scope_info->HasHeapAllocatedLocals()) {
161 num_heap_slots_ = scope_info_->NumberOfContextSlots(); 163 num_heap_slots_ = scope_info_->NumberOfContextSlots();
162 } 164 }
163 AddInnerScope(inner_scope); 165 AddInnerScope(inner_scope);
164 } 166 }
165 167
166 168
167 Scope::Scope(Scope* inner_scope, Handle<String> catch_variable_name) 169 Scope::Scope(Scope* inner_scope, Handle<String> catch_variable_name)
168 : isolate_(Isolate::Current()), 170 : isolate_(Isolate::Current()),
169 inner_scopes_(1), 171 inner_scopes_(1),
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 if (context->IsWithContext()) { 227 if (context->IsWithContext()) {
226 // All the inner scopes are inside a with. 228 // All the inner scopes are inside a with.
227 contains_with = true; 229 contains_with = true;
228 for (Scope* s = innermost_scope; s != NULL; s = s->outer_scope()) { 230 for (Scope* s = innermost_scope; s != NULL; s = s->outer_scope()) {
229 s->scope_inside_with_ = true; 231 s->scope_inside_with_ = true;
230 } 232 }
231 } else { 233 } else {
232 if (context->IsFunctionContext()) { 234 if (context->IsFunctionContext()) {
233 SerializedScopeInfo* scope_info = 235 SerializedScopeInfo* scope_info =
234 context->closure()->shared()->scope_info(); 236 context->closure()->shared()->scope_info();
235 current_scope = 237 current_scope = new Scope(current_scope, FUNCTION_SCOPE,
236 new Scope(current_scope, Handle<SerializedScopeInfo>(scope_info)); 238 Handle<SerializedScopeInfo>(scope_info));
239 } else if (context->IsBlockContext()) {
240 SerializedScopeInfo* scope_info =
241 SerializedScopeInfo::cast(context->extension());
242 current_scope = new Scope(current_scope, BLOCK_SCOPE,
243 Handle<SerializedScopeInfo>(scope_info));
237 } else { 244 } else {
238 ASSERT(context->IsCatchContext()); 245 ASSERT(context->IsCatchContext());
239 String* name = String::cast(context->extension()); 246 String* name = String::cast(context->extension());
240 current_scope = new Scope(current_scope, Handle<String>(name)); 247 current_scope = new Scope(current_scope, Handle<String>(name));
241 } 248 }
242 if (contains_with) current_scope->RecordWithStatement(); 249 if (contains_with) current_scope->RecordWithStatement();
243 if (innermost_scope == NULL) innermost_scope = current_scope; 250 if (innermost_scope == NULL) innermost_scope = current_scope;
244 } 251 }
245 252
246 // Forget about a with when we move to a context for a different function. 253 // Forget about a with when we move to a context for a different function.
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 } 294 }
288 295
289 // Declare convenience variables. 296 // Declare convenience variables.
290 // Declare and allocate receiver (even for the global scope, and even 297 // Declare and allocate receiver (even for the global scope, and even
291 // if naccesses_ == 0). 298 // if naccesses_ == 0).
292 // NOTE: When loading parameters in the global scope, we must take 299 // NOTE: When loading parameters in the global scope, we must take
293 // care not to access them as properties of the global object, but 300 // care not to access them as properties of the global object, but
294 // instead load them directly from the stack. Currently, the only 301 // instead load them directly from the stack. Currently, the only
295 // such parameter is 'this' which is passed on the stack when 302 // such parameter is 'this' which is passed on the stack when
296 // invoking scripts 303 // invoking scripts
297 if (is_catch_scope()) { 304 if (is_catch_scope() || is_block_scope()) {
298 ASSERT(outer_scope() != NULL); 305 ASSERT(outer_scope() != NULL);
299 receiver_ = outer_scope()->receiver(); 306 receiver_ = outer_scope()->receiver();
300 } else { 307 } else {
308 ASSERT(is_function_scope() ||
309 is_global_scope() ||
310 is_eval_scope());
301 Variable* var = 311 Variable* var =
302 variables_.Declare(this, 312 variables_.Declare(this,
303 isolate_->factory()->this_symbol(), 313 isolate_->factory()->this_symbol(),
304 Variable::VAR, 314 Variable::VAR,
305 false, 315 false,
306 Variable::THIS); 316 Variable::THIS);
307 var->set_rewrite(NewSlot(var, Slot::PARAMETER, -1)); 317 var->set_rewrite(NewSlot(var, Slot::PARAMETER, -1));
308 receiver_ = var; 318 receiver_ = var;
309 } 319 }
310 320
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
552 for (Scope* s = this; s != scope; s = s->outer_scope_) { 562 for (Scope* s = this; s != scope; s = s->outer_scope_) {
553 ASSERT(s != NULL); // scope must be in the scope chain 563 ASSERT(s != NULL); // scope must be in the scope chain
554 if (s->num_heap_slots() > 0) n++; 564 if (s->num_heap_slots() > 0) n++;
555 } 565 }
556 return n; 566 return n;
557 } 567 }
558 568
559 569
560 Scope* Scope::DeclarationScope() { 570 Scope* Scope::DeclarationScope() {
561 Scope* scope = this; 571 Scope* scope = this;
562 while (scope->is_catch_scope()) { 572 while (scope->is_catch_scope() ||
573 scope->is_block_scope()) {
563 scope = scope->outer_scope(); 574 scope = scope->outer_scope();
564 } 575 }
565 return scope; 576 return scope;
566 } 577 }
567 578
568 579
580 Handle<SerializedScopeInfo> Scope::GetSerializedScopeInfo() {
581 if (scope_info_.is_null()) {
582 scope_info_ = SerializedScopeInfo::Create(this);
583 }
584 return scope_info_;
585 }
586
587
569 #ifdef DEBUG 588 #ifdef DEBUG
570 static const char* Header(Scope::Type type) { 589 static const char* Header(Scope::Type type) {
571 switch (type) { 590 switch (type) {
572 case Scope::EVAL_SCOPE: return "eval"; 591 case Scope::EVAL_SCOPE: return "eval";
573 case Scope::FUNCTION_SCOPE: return "function"; 592 case Scope::FUNCTION_SCOPE: return "function";
574 case Scope::GLOBAL_SCOPE: return "global"; 593 case Scope::GLOBAL_SCOPE: return "global";
575 case Scope::CATCH_SCOPE: return "catch"; 594 case Scope::CATCH_SCOPE: return "catch";
595 case Scope::BLOCK_SCOPE: return "block";
576 } 596 }
577 UNREACHABLE(); 597 UNREACHABLE();
578 return NULL; 598 return NULL;
579 } 599 }
580 600
581 601
582 static void Indent(int n, const char* str) { 602 static void Indent(int n, const char* str) {
583 PrintF("%*s%s", n, "", str); 603 PrintF("%*s%s", n, "", str);
584 } 604 }
585 605
586 606
587 static void PrintName(Handle<String> name) { 607 static void PrintName(Handle<String> name) {
588 SmartPointer<char> s = name->ToCString(DISALLOW_NULLS); 608 SmartPointer<char> s = name->ToCString(DISALLOW_NULLS);
589 PrintF("%s", *s); 609 PrintF("%s", *s);
590 } 610 }
591 611
592 612
593 static void PrintVar(PrettyPrinter* printer, int indent, Variable* var) { 613 static void PrintVar(PrettyPrinter* printer, int indent, Variable* var) {
594 if (var->is_used() || var->rewrite() != NULL) { 614 if (var->is_used() || var->rewrite() != NULL) {
595 Indent(indent, Variable::Mode2String(var->mode())); 615 Indent(indent, Variable::Mode2String(var->mode()));
596 PrintF(" "); 616 PrintF(" ");
597 PrintName(var->name()); 617 PrintName(var->name());
598 PrintF("; // "); 618 PrintF("; // ");
599 if (var->rewrite() != NULL) { 619 if (var->rewrite() != NULL) {
600 PrintF("%s, ", printer->Print(var->rewrite())); 620 PrintF("%s, ", printer->Print(var->rewrite()));
601 if (var->is_accessed_from_inner_scope()) PrintF(", "); 621 if (var->is_accessed_from_inner_function_scope()) PrintF(", ");
602 } 622 }
603 if (var->is_accessed_from_inner_scope()) PrintF("inner scope access"); 623 if (var->is_accessed_from_inner_function_scope()) {
624 PrintF("inner scope access");
625 }
604 PrintF("\n"); 626 PrintF("\n");
605 } 627 }
606 } 628 }
607 629
608 630
609 static void PrintMap(PrettyPrinter* printer, int indent, VariableMap* map) { 631 static void PrintMap(PrettyPrinter* printer, int indent, VariableMap* map) {
610 for (VariableMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) { 632 for (VariableMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) {
611 Variable* var = reinterpret_cast<Variable*>(p->value); 633 Variable* var = reinterpret_cast<Variable*>(p->value);
612 PrintVar(printer, indent, var); 634 PrintVar(printer, indent, var);
613 } 635 }
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
714 } 736 }
715 737
716 738
717 // Lookup a variable starting with this scope. The result is either 739 // Lookup a variable starting with this scope. The result is either
718 // the statically resolved variable belonging to an outer scope, or 740 // the statically resolved variable belonging to an outer scope, or
719 // NULL. It may be NULL because a) we couldn't find a variable, or b) 741 // NULL. It may be NULL because a) we couldn't find a variable, or b)
720 // because the variable is just a guess (and may be shadowed by 742 // because the variable is just a guess (and may be shadowed by
721 // another variable that is introduced dynamically via an 'eval' call 743 // another variable that is introduced dynamically via an 'eval' call
722 // or a 'with' statement). 744 // or a 'with' statement).
723 Variable* Scope::LookupRecursive(Handle<String> name, 745 Variable* Scope::LookupRecursive(Handle<String> name,
724 bool inner_lookup, 746 bool from_inner_function,
725 Variable** invalidated_local) { 747 Variable** invalidated_local) {
726 // If we find a variable, but the current scope calls 'eval', the found 748 // If we find a variable, but the current scope calls 'eval', the found
727 // variable may not be the correct one (the 'eval' may introduce a 749 // variable may not be the correct one (the 'eval' may introduce a
728 // property with the same name). In that case, remember that the variable 750 // property with the same name). In that case, remember that the variable
729 // found is just a guess. 751 // found is just a guess.
730 bool guess = scope_calls_eval_; 752 bool guess = scope_calls_eval_;
731 753
732 // Try to find the variable in this scope. 754 // Try to find the variable in this scope.
733 Variable* var = LocalLookup(name); 755 Variable* var = LocalLookup(name);
734 756
735 if (var != NULL) { 757 if (var != NULL) {
736 // We found a variable. If this is not an inner lookup, we are done. 758 // We found a variable. If this is not an inner lookup, we are done.
737 // (Even if there is an 'eval' in this scope which introduces the 759 // (Even if there is an 'eval' in this scope which introduces the
738 // same variable again, the resulting variable remains the same. 760 // same variable again, the resulting variable remains the same.
739 // Note that enclosing 'with' statements are handled at the call site.) 761 // Note that enclosing 'with' statements are handled at the call site.)
740 if (!inner_lookup) 762 if (!from_inner_function)
741 return var; 763 return var;
742 764
743 } else { 765 } else {
744 // We did not find a variable locally. Check against the function variable, 766 // We did not find a variable locally. Check against the function variable,
745 // if any. We can do this for all scopes, since the function variable is 767 // if any. We can do this for all scopes, since the function variable is
746 // only present - if at all - for function scopes. 768 // only present - if at all - for function scopes.
747 // 769 //
748 // This lookup corresponds to a lookup in the "intermediate" scope sitting 770 // This lookup corresponds to a lookup in the "intermediate" scope sitting
749 // between this scope and the outer scope. (ECMA-262, 3rd., requires that 771 // between this scope and the outer scope. (ECMA-262, 3rd., requires that
750 // the name of named function literal is kept in an intermediate scope 772 // the name of named function literal is kept in an intermediate scope
751 // in between this scope and the next outer scope.) 773 // in between this scope and the next outer scope.)
752 if (function_ != NULL && function_->name().is_identical_to(name)) { 774 if (function_ != NULL && function_->name().is_identical_to(name)) {
753 var = function_; 775 var = function_;
754 776
755 } else if (outer_scope_ != NULL) { 777 } else if (outer_scope_ != NULL) {
756 var = outer_scope_->LookupRecursive(name, true, invalidated_local); 778 var = outer_scope_->LookupRecursive(
779 name,
780 is_function_scope() || from_inner_function,
781 invalidated_local);
757 // We may have found a variable in an outer scope. However, if 782 // We may have found a variable in an outer scope. However, if
758 // the current scope is inside a 'with', the actual variable may 783 // the current scope is inside a 'with', the actual variable may
759 // be a property introduced via the 'with' statement. Then, the 784 // be a property introduced via the 'with' statement. Then, the
760 // variable we may have found is just a guess. 785 // variable we may have found is just a guess.
761 if (scope_inside_with_) 786 if (scope_inside_with_)
762 guess = true; 787 guess = true;
763 } 788 }
764 789
765 // If we did not find a variable, we are done. 790 // If we did not find a variable, we are done.
766 if (var == NULL) 791 if (var == NULL)
767 return NULL; 792 return NULL;
768 } 793 }
769 794
770 ASSERT(var != NULL); 795 ASSERT(var != NULL);
771 796
772 // If this is a lookup from an inner scope, mark the variable. 797 // If this is a lookup from an inner scope, mark the variable.
773 if (inner_lookup) { 798 if (from_inner_function) {
774 var->MarkAsAccessedFromInnerScope(); 799 var->MarkAsAccessedFromInnerFunctionScope();
775 } 800 }
776 801
777 // If the variable we have found is just a guess, invalidate the 802 // If the variable we have found is just a guess, invalidate the
778 // result. If the found variable is local, record that fact so we 803 // result. If the found variable is local, record that fact so we
779 // can generate fast code to get it if it is not shadowed by eval. 804 // can generate fast code to get it if it is not shadowed by eval.
780 if (guess) { 805 if (guess) {
781 if (!var->is_global()) *invalidated_local = var; 806 if (!var->is_global()) *invalidated_local = var;
782 var = NULL; 807 var = NULL;
783 } 808 }
784 809
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
915 940
916 return scope_calls_eval_ || inner_scope_calls_eval_; 941 return scope_calls_eval_ || inner_scope_calls_eval_;
917 } 942 }
918 943
919 944
920 bool Scope::MustAllocate(Variable* var) { 945 bool Scope::MustAllocate(Variable* var) {
921 // Give var a read/write use if there is a chance it might be accessed 946 // Give var a read/write use if there is a chance it might be accessed
922 // via an eval() call. This is only possible if the variable has a 947 // via an eval() call. This is only possible if the variable has a
923 // visible name. 948 // visible name.
924 if ((var->is_this() || var->name()->length() > 0) && 949 if ((var->is_this() || var->name()->length() > 0) &&
925 (var->is_accessed_from_inner_scope() || 950 (var->is_accessed_from_inner_function_scope() ||
926 scope_calls_eval_ || 951 scope_calls_eval_ ||
927 inner_scope_calls_eval_ || 952 inner_scope_calls_eval_ ||
928 scope_contains_with_ || 953 scope_contains_with_ ||
929 is_catch_scope())) { 954 is_catch_scope() ||
955 is_block_scope())) {
930 var->set_is_used(true); 956 var->set_is_used(true);
931 } 957 }
932 // Global variables do not need to be allocated. 958 // Global variables do not need to be allocated.
933 return !var->is_global() && var->is_used(); 959 return !var->is_global() && var->is_used();
934 } 960 }
935 961
936 962
937 bool Scope::MustAllocateInContext(Variable* var) { 963 bool Scope::MustAllocateInContext(Variable* var) {
938 // If var is accessed from an inner scope, or if there is a possibility 964 // If var is accessed from an inner scope, or if there is a possibility
939 // that it might be accessed from the current or an inner scope (through 965 // that it might be accessed from the current or an inner scope (through
940 // an eval() call or a runtime with lookup), it must be allocated in the 966 // an eval() call or a runtime with lookup), it must be allocated in the
941 // context. 967 // context.
942 // 968 //
943 // Exceptions: temporary variables are never allocated in a context; 969 // Exceptions: temporary variables are never allocated in a context;
944 // catch-bound variables are always allocated in a context. 970 // catch-bound variables are always allocated in a context.
945 if (var->mode() == Variable::TEMPORARY) return false; 971 if (var->mode() == Variable::TEMPORARY) return false;
946 if (is_catch_scope()) return true; 972 if (is_catch_scope() || is_block_scope()) return true;
947 return var->is_accessed_from_inner_scope() || 973 return var->is_accessed_from_inner_function_scope() ||
948 scope_calls_eval_ || 974 scope_calls_eval_ ||
949 inner_scope_calls_eval_ || 975 inner_scope_calls_eval_ ||
950 scope_contains_with_ || 976 scope_contains_with_ ||
951 var->is_global(); 977 var->is_global();
952 } 978 }
953 979
954 980
955 bool Scope::HasArgumentsParameter() { 981 bool Scope::HasArgumentsParameter() {
956 for (int i = 0; i < params_.length(); i++) { 982 for (int i = 0; i < params_.length(); i++) {
957 if (params_[i]->name().is_identical_to( 983 if (params_[i]->name().is_identical_to(
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
1003 // The same parameter may occur multiple times in the parameters_ list. 1029 // The same parameter may occur multiple times in the parameters_ list.
1004 // If it does, and if it is not copied into the context object, it must 1030 // If it does, and if it is not copied into the context object, it must
1005 // receive the highest parameter index for that parameter; thus iteration 1031 // receive the highest parameter index for that parameter; thus iteration
1006 // order is relevant! 1032 // order is relevant!
1007 for (int i = params_.length() - 1; i >= 0; --i) { 1033 for (int i = params_.length() - 1; i >= 0; --i) {
1008 Variable* var = params_[i]; 1034 Variable* var = params_[i];
1009 ASSERT(var->scope() == this); 1035 ASSERT(var->scope() == this);
1010 if (uses_nonstrict_arguments) { 1036 if (uses_nonstrict_arguments) {
1011 // Give the parameter a use from an inner scope, to force allocation 1037 // Give the parameter a use from an inner scope, to force allocation
1012 // to the context. 1038 // to the context.
1013 var->MarkAsAccessedFromInnerScope(); 1039 var->MarkAsAccessedFromInnerFunctionScope();
1014 } 1040 }
1015 1041
1016 if (MustAllocate(var)) { 1042 if (MustAllocate(var)) {
1017 if (MustAllocateInContext(var)) { 1043 if (MustAllocateInContext(var)) {
1018 ASSERT(var->rewrite() == NULL || var->IsContextSlot()); 1044 ASSERT(var->rewrite() == NULL || var->IsContextSlot());
1019 if (var->rewrite() == NULL) { 1045 if (var->rewrite() == NULL) {
1020 AllocateHeapSlot(var); 1046 AllocateHeapSlot(var);
1021 } 1047 }
1022 } else { 1048 } else {
1023 ASSERT(var->rewrite() == NULL || var->IsParameter()); 1049 ASSERT(var->rewrite() == NULL || var->IsParameter());
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
1103 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS && 1129 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS &&
1104 !must_have_local_context) { 1130 !must_have_local_context) {
1105 num_heap_slots_ = 0; 1131 num_heap_slots_ = 0;
1106 } 1132 }
1107 1133
1108 // Allocation done. 1134 // Allocation done.
1109 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS); 1135 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS);
1110 } 1136 }
1111 1137
1112 } } // namespace v8::internal 1138 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/scopes.h ('k') | src/serialize.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698