OLD | NEW |
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 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 | 111 |
112 Scope::Scope(Scope* outer_scope, ScopeType type) | 112 Scope::Scope(Scope* outer_scope, ScopeType type) |
113 : isolate_(Isolate::Current()), | 113 : isolate_(Isolate::Current()), |
114 inner_scopes_(4), | 114 inner_scopes_(4), |
115 variables_(), | 115 variables_(), |
116 temps_(4), | 116 temps_(4), |
117 params_(4), | 117 params_(4), |
118 unresolved_(16), | 118 unresolved_(16), |
119 decls_(4), | 119 decls_(4), |
120 already_resolved_(false) { | 120 already_resolved_(false) { |
121 SetDefaults(type, outer_scope, Handle<SerializedScopeInfo>::null()); | 121 SetDefaults(type, outer_scope, Handle<ScopeInfo>::null()); |
122 // At some point we might want to provide outer scopes to | 122 // At some point we might want to provide outer scopes to |
123 // eval scopes (by walking the stack and reading the scope info). | 123 // eval scopes (by walking the stack and reading the scope info). |
124 // In that case, the ASSERT below needs to be adjusted. | 124 // In that case, the ASSERT below needs to be adjusted. |
125 ASSERT((type == GLOBAL_SCOPE || type == EVAL_SCOPE) == (outer_scope == NULL)); | 125 ASSERT((type == GLOBAL_SCOPE || type == EVAL_SCOPE) == (outer_scope == NULL)); |
126 ASSERT(!HasIllegalRedeclaration()); | 126 ASSERT(!HasIllegalRedeclaration()); |
127 } | 127 } |
128 | 128 |
129 | 129 |
130 Scope::Scope(Scope* inner_scope, | 130 Scope::Scope(Scope* inner_scope, |
131 ScopeType type, | 131 ScopeType type, |
132 Handle<SerializedScopeInfo> scope_info) | 132 Handle<ScopeInfo> scope_info) |
133 : isolate_(Isolate::Current()), | 133 : isolate_(Isolate::Current()), |
134 inner_scopes_(4), | 134 inner_scopes_(4), |
135 variables_(), | 135 variables_(), |
136 temps_(4), | 136 temps_(4), |
137 params_(4), | 137 params_(4), |
138 unresolved_(16), | 138 unresolved_(16), |
139 decls_(4), | 139 decls_(4), |
140 already_resolved_(true) { | 140 already_resolved_(true) { |
141 SetDefaults(type, NULL, scope_info); | 141 SetDefaults(type, NULL, scope_info); |
142 if (!scope_info.is_null() && scope_info->HasHeapAllocatedLocals()) { | 142 if (!scope_info.is_null()) { |
143 num_heap_slots_ = scope_info_->NumberOfContextSlots(); | 143 num_heap_slots_ = scope_info_->NumberOfContextSlots(); |
144 } | 144 } |
145 AddInnerScope(inner_scope); | 145 AddInnerScope(inner_scope); |
146 } | 146 } |
147 | 147 |
148 | 148 |
149 Scope::Scope(Scope* inner_scope, Handle<String> catch_variable_name) | 149 Scope::Scope(Scope* inner_scope, Handle<String> catch_variable_name) |
150 : isolate_(Isolate::Current()), | 150 : isolate_(Isolate::Current()), |
151 inner_scopes_(1), | 151 inner_scopes_(1), |
152 variables_(), | 152 variables_(), |
153 temps_(0), | 153 temps_(0), |
154 params_(0), | 154 params_(0), |
155 unresolved_(0), | 155 unresolved_(0), |
156 decls_(0), | 156 decls_(0), |
157 already_resolved_(true) { | 157 already_resolved_(true) { |
158 SetDefaults(CATCH_SCOPE, NULL, Handle<SerializedScopeInfo>::null()); | 158 SetDefaults(CATCH_SCOPE, NULL, Handle<ScopeInfo>::null()); |
159 AddInnerScope(inner_scope); | 159 AddInnerScope(inner_scope); |
160 ++num_var_or_const_; | 160 ++num_var_or_const_; |
161 Variable* variable = variables_.Declare(this, | 161 Variable* variable = variables_.Declare(this, |
162 catch_variable_name, | 162 catch_variable_name, |
163 VAR, | 163 VAR, |
164 true, // Valid left-hand side. | 164 true, // Valid left-hand side. |
165 Variable::NORMAL); | 165 Variable::NORMAL); |
166 AllocateHeapSlot(variable); | 166 AllocateHeapSlot(variable); |
167 } | 167 } |
168 | 168 |
169 | 169 |
170 void Scope::SetDefaults(ScopeType type, | 170 void Scope::SetDefaults(ScopeType type, |
171 Scope* outer_scope, | 171 Scope* outer_scope, |
172 Handle<SerializedScopeInfo> scope_info) { | 172 Handle<ScopeInfo> scope_info) { |
173 outer_scope_ = outer_scope; | 173 outer_scope_ = outer_scope; |
174 type_ = type; | 174 type_ = type; |
175 scope_name_ = isolate_->factory()->empty_symbol(); | 175 scope_name_ = isolate_->factory()->empty_symbol(); |
176 dynamics_ = NULL; | 176 dynamics_ = NULL; |
177 receiver_ = NULL; | 177 receiver_ = NULL; |
178 function_ = NULL; | 178 function_ = NULL; |
179 arguments_ = NULL; | 179 arguments_ = NULL; |
180 illegal_redecl_ = NULL; | 180 illegal_redecl_ = NULL; |
181 scope_inside_with_ = false; | 181 scope_inside_with_ = false; |
182 scope_contains_with_ = false; | 182 scope_contains_with_ = false; |
(...skipping 16 matching lines...) Expand all Loading... |
199 Scope* Scope::DeserializeScopeChain(CompilationInfo* info, | 199 Scope* Scope::DeserializeScopeChain(CompilationInfo* info, |
200 Scope* global_scope) { | 200 Scope* global_scope) { |
201 // Reconstruct the outer scope chain from a closure's context chain. | 201 // Reconstruct the outer scope chain from a closure's context chain. |
202 ASSERT(!info->closure().is_null()); | 202 ASSERT(!info->closure().is_null()); |
203 Context* context = info->closure()->context(); | 203 Context* context = info->closure()->context(); |
204 Scope* current_scope = NULL; | 204 Scope* current_scope = NULL; |
205 Scope* innermost_scope = NULL; | 205 Scope* innermost_scope = NULL; |
206 bool contains_with = false; | 206 bool contains_with = false; |
207 while (!context->IsGlobalContext()) { | 207 while (!context->IsGlobalContext()) { |
208 if (context->IsWithContext()) { | 208 if (context->IsWithContext()) { |
209 Scope* with_scope = new Scope(current_scope, WITH_SCOPE, | 209 Scope* with_scope = new Scope(current_scope, |
210 Handle<SerializedScopeInfo>::null()); | 210 WITH_SCOPE, |
| 211 Handle<ScopeInfo>::null()); |
211 current_scope = with_scope; | 212 current_scope = with_scope; |
212 // All the inner scopes are inside a with. | 213 // All the inner scopes are inside a with. |
213 contains_with = true; | 214 contains_with = true; |
214 for (Scope* s = innermost_scope; s != NULL; s = s->outer_scope()) { | 215 for (Scope* s = innermost_scope; s != NULL; s = s->outer_scope()) { |
215 s->scope_inside_with_ = true; | 216 s->scope_inside_with_ = true; |
216 } | 217 } |
217 } else if (context->IsFunctionContext()) { | 218 } else if (context->IsFunctionContext()) { |
218 SerializedScopeInfo* scope_info = | 219 ScopeInfo* scope_info = context->closure()->shared()->scope_info(); |
219 context->closure()->shared()->scope_info(); | 220 current_scope = new Scope(current_scope, |
220 current_scope = new Scope(current_scope, FUNCTION_SCOPE, | 221 FUNCTION_SCOPE, |
221 Handle<SerializedScopeInfo>(scope_info)); | 222 Handle<ScopeInfo>(scope_info)); |
222 } else if (context->IsBlockContext()) { | 223 } else if (context->IsBlockContext()) { |
223 SerializedScopeInfo* scope_info = | 224 ScopeInfo* scope_info = ScopeInfo::cast(context->extension()); |
224 SerializedScopeInfo::cast(context->extension()); | 225 current_scope = new Scope(current_scope, |
225 current_scope = new Scope(current_scope, BLOCK_SCOPE, | 226 BLOCK_SCOPE, |
226 Handle<SerializedScopeInfo>(scope_info)); | 227 Handle<ScopeInfo>(scope_info)); |
227 } else { | 228 } else { |
228 ASSERT(context->IsCatchContext()); | 229 ASSERT(context->IsCatchContext()); |
229 String* name = String::cast(context->extension()); | 230 String* name = String::cast(context->extension()); |
230 current_scope = new Scope(current_scope, Handle<String>(name)); | 231 current_scope = new Scope(current_scope, Handle<String>(name)); |
231 } | 232 } |
232 if (contains_with) current_scope->RecordWithStatement(); | 233 if (contains_with) current_scope->RecordWithStatement(); |
233 if (innermost_scope == NULL) innermost_scope = current_scope; | 234 if (innermost_scope == NULL) innermost_scope = current_scope; |
234 | 235 |
235 // Forget about a with when we move to a context for a different function. | 236 // Forget about a with when we move to a context for a different function. |
236 if (context->previous()->closure() != context->closure()) { | 237 if (context->previous()->closure() != context->closure()) { |
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
603 | 604 |
604 Scope* Scope::DeclarationScope() { | 605 Scope* Scope::DeclarationScope() { |
605 Scope* scope = this; | 606 Scope* scope = this; |
606 while (!scope->is_declaration_scope()) { | 607 while (!scope->is_declaration_scope()) { |
607 scope = scope->outer_scope(); | 608 scope = scope->outer_scope(); |
608 } | 609 } |
609 return scope; | 610 return scope; |
610 } | 611 } |
611 | 612 |
612 | 613 |
613 Handle<SerializedScopeInfo> Scope::GetSerializedScopeInfo() { | 614 Handle<ScopeInfo> Scope::GetScopeInfo() { |
614 if (scope_info_.is_null()) { | 615 if (scope_info_.is_null()) { |
615 scope_info_ = SerializedScopeInfo::Create(this); | 616 scope_info_ = ScopeInfo::Create(this); |
616 } | 617 } |
617 return scope_info_; | 618 return scope_info_; |
618 } | 619 } |
619 | 620 |
620 | 621 |
621 void Scope::GetNestedScopeChain( | 622 void Scope::GetNestedScopeChain( |
622 List<Handle<SerializedScopeInfo> >* chain, | 623 List<Handle<ScopeInfo> >* chain, |
623 int position) { | 624 int position) { |
624 chain->Add(Handle<SerializedScopeInfo>(GetSerializedScopeInfo())); | 625 chain->Add(Handle<ScopeInfo>(GetScopeInfo())); |
625 | 626 |
626 for (int i = 0; i < inner_scopes_.length(); i++) { | 627 for (int i = 0; i < inner_scopes_.length(); i++) { |
627 Scope* scope = inner_scopes_[i]; | 628 Scope* scope = inner_scopes_[i]; |
628 int beg_pos = scope->start_position(); | 629 int beg_pos = scope->start_position(); |
629 int end_pos = scope->end_position(); | 630 int end_pos = scope->end_position(); |
630 ASSERT(beg_pos >= 0 && end_pos >= 0); | 631 ASSERT(beg_pos >= 0 && end_pos >= 0); |
631 if (beg_pos <= position && position <= end_pos) { | 632 if (beg_pos <= position && position <= end_pos) { |
632 scope->GetNestedScopeChain(chain, position); | 633 scope->GetNestedScopeChain(chain, position); |
633 return; | 634 return; |
634 } | 635 } |
(...skipping 484 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1119 if (already_resolved()) return; | 1120 if (already_resolved()) return; |
1120 // The number of slots required for variables. | 1121 // The number of slots required for variables. |
1121 num_stack_slots_ = 0; | 1122 num_stack_slots_ = 0; |
1122 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS; | 1123 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS; |
1123 | 1124 |
1124 // Allocate variables for this scope. | 1125 // Allocate variables for this scope. |
1125 // Parameters must be allocated first, if any. | 1126 // Parameters must be allocated first, if any. |
1126 if (is_function_scope()) AllocateParameterLocals(); | 1127 if (is_function_scope()) AllocateParameterLocals(); |
1127 AllocateNonParameterLocals(); | 1128 AllocateNonParameterLocals(); |
1128 | 1129 |
1129 // Allocate context if necessary. | 1130 // Force allocation of a context for this scope if necessary. For a 'with' |
1130 bool must_have_local_context = false; | 1131 // scope and for a function scope that makes an 'eval' call we need a context, |
1131 if (scope_calls_eval_ || scope_contains_with_) { | 1132 // even if no local variables were statically allocated in the scope. |
1132 // The context for the eval() call or 'with' statement in this scope. | 1133 bool must_have_context = is_with_scope() || |
1133 // Unless we are in the global or an eval scope, we need a local | 1134 (is_function_scope() && calls_eval()); |
1134 // context even if we didn't statically allocate any locals in it, | |
1135 // and the compiler will access the context variable. If we are | |
1136 // not in an inner scope, the scope is provided from the outside. | |
1137 must_have_local_context = is_function_scope(); | |
1138 } | |
1139 | 1135 |
1140 // If we didn't allocate any locals in the local context, then we only | 1136 // If we didn't allocate any locals in the local context, then we only |
1141 // need the minimal number of slots if we must have a local context. | 1137 // need the minimal number of slots if we must have a context. |
1142 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS && | 1138 if (num_heap_slots_ == Context::MIN_CONTEXT_SLOTS && !must_have_context) { |
1143 !must_have_local_context) { | |
1144 num_heap_slots_ = 0; | 1139 num_heap_slots_ = 0; |
1145 } | 1140 } |
1146 | 1141 |
1147 // Allocation done. | 1142 // Allocation done. |
1148 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS); | 1143 ASSERT(num_heap_slots_ == 0 || num_heap_slots_ >= Context::MIN_CONTEXT_SLOTS); |
1149 } | 1144 } |
1150 | 1145 |
1151 } } // namespace v8::internal | 1146 } } // namespace v8::internal |
OLD | NEW |