OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 <stdlib.h> | 5 #include <stdlib.h> |
6 | 6 |
7 #include "src/v8.h" | 7 #include "src/v8.h" |
8 | 8 |
9 #include "src/bootstrapper.h" | 9 #include "src/bootstrapper.h" |
10 #include "src/scopeinfo.h" | 10 #include "src/scopeinfo.h" |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 } | 142 } |
143 | 143 |
144 // Add context globals' names. | 144 // Add context globals' names. |
145 DCHECK(index == scope_info->ContextGlobalNameEntriesIndex()); | 145 DCHECK(index == scope_info->ContextGlobalNameEntriesIndex()); |
146 for (int i = 0; i < context_global_count; ++i) { | 146 for (int i = 0; i < context_global_count; ++i) { |
147 scope_info->set(index++, *context_globals[i]->name()); | 147 scope_info->set(index++, *context_globals[i]->name()); |
148 } | 148 } |
149 | 149 |
150 // Add context locals' info. | 150 // Add context locals' info. |
151 DCHECK(index == scope_info->ContextLocalInfoEntriesIndex()); | 151 DCHECK(index == scope_info->ContextLocalInfoEntriesIndex()); |
| 152 bool encountered_lexical = false; |
| 153 int lexical_context_local_count = 0; |
152 for (int i = 0; i < context_local_count; ++i) { | 154 for (int i = 0; i < context_local_count; ++i) { |
153 Variable* var = context_locals[i]; | 155 Variable* var = context_locals[i]; |
154 uint32_t value = | 156 uint32_t value = |
155 ContextLocalMode::encode(var->mode()) | | 157 ContextLocalMode::encode(var->mode()) | |
156 ContextLocalInitFlag::encode(var->initialization_flag()) | | 158 ContextLocalInitFlag::encode(var->initialization_flag()) | |
157 ContextLocalMaybeAssignedFlag::encode(var->maybe_assigned()); | 159 ContextLocalMaybeAssignedFlag::encode(var->maybe_assigned()); |
158 scope_info->set(index++, Smi::FromInt(value)); | 160 scope_info->set(index++, Smi::FromInt(value)); |
| 161 if (encountered_lexical) { |
| 162 // Check that context locals are sorted so that lexicals are at the end. |
| 163 DCHECK(IsLexicalVariableMode(var->mode())); |
| 164 } else if (IsLexicalVariableMode(var->mode())) { |
| 165 lexical_context_local_count = context_local_count - i; |
| 166 } |
159 } | 167 } |
160 | 168 |
| 169 scope_info->SetLexicalContextLocalCount(lexical_context_local_count); |
| 170 |
161 // Add context globals' info. | 171 // Add context globals' info. |
162 DCHECK(index == scope_info->ContextGlobalInfoEntriesIndex()); | 172 DCHECK(index == scope_info->ContextGlobalInfoEntriesIndex()); |
163 for (int i = 0; i < context_global_count; ++i) { | 173 for (int i = 0; i < context_global_count; ++i) { |
164 Variable* var = context_globals[i]; | 174 Variable* var = context_globals[i]; |
165 // TODO(ishell): do we need this kind of info for globals here? | 175 // TODO(ishell): do we need this kind of info for globals here? |
166 uint32_t value = | 176 uint32_t value = |
167 ContextLocalMode::encode(var->mode()) | | 177 ContextLocalMode::encode(var->mode()) | |
168 ContextLocalInitFlag::encode(var->initialization_flag()) | | 178 ContextLocalInitFlag::encode(var->initialization_flag()) | |
169 ContextLocalMaybeAssignedFlag::encode(var->maybe_assigned()); | 179 ContextLocalMaybeAssignedFlag::encode(var->maybe_assigned()); |
170 scope_info->set(index++, Smi::FromInt(value)); | 180 scope_info->set(index++, Smi::FromInt(value)); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 scope_info->ContextLength() == 0)); | 225 scope_info->ContextLength() == 0)); |
216 return scope_info; | 226 return scope_info; |
217 } | 227 } |
218 | 228 |
219 | 229 |
220 Handle<ScopeInfo> ScopeInfo::CreateGlobalThisBinding(Isolate* isolate) { | 230 Handle<ScopeInfo> ScopeInfo::CreateGlobalThisBinding(Isolate* isolate) { |
221 DCHECK(isolate->bootstrapper()->IsActive()); | 231 DCHECK(isolate->bootstrapper()->IsActive()); |
222 | 232 |
223 const int stack_local_count = 0; | 233 const int stack_local_count = 0; |
224 const int context_local_count = 1; | 234 const int context_local_count = 1; |
| 235 const int lexical_context_local_count = 1; |
225 const int context_global_count = 0; | 236 const int context_global_count = 0; |
226 const int strong_mode_free_variable_count = 0; | 237 const int strong_mode_free_variable_count = 0; |
227 const bool simple_parameter_list = true; | 238 const bool simple_parameter_list = true; |
228 const VariableAllocationInfo receiver_info = CONTEXT; | 239 const VariableAllocationInfo receiver_info = CONTEXT; |
229 const VariableAllocationInfo function_name_info = NONE; | 240 const VariableAllocationInfo function_name_info = NONE; |
230 const VariableMode function_variable_mode = VAR; | 241 const VariableMode function_variable_mode = VAR; |
231 const bool has_function_name = false; | 242 const bool has_function_name = false; |
232 const bool has_receiver = true; | 243 const bool has_receiver = true; |
233 const int parameter_count = 0; | 244 const int parameter_count = 0; |
234 const int length = kVariablePartIndex + parameter_count + | 245 const int length = kVariablePartIndex + parameter_count + |
(...skipping 12 matching lines...) Expand all Loading... |
247 ReceiverVariableField::encode(receiver_info) | | 258 ReceiverVariableField::encode(receiver_info) | |
248 FunctionVariableField::encode(function_name_info) | | 259 FunctionVariableField::encode(function_name_info) | |
249 FunctionVariableMode::encode(function_variable_mode) | | 260 FunctionVariableMode::encode(function_variable_mode) | |
250 AsmModuleField::encode(false) | AsmFunctionField::encode(false) | | 261 AsmModuleField::encode(false) | AsmFunctionField::encode(false) | |
251 IsSimpleParameterListField::encode(simple_parameter_list) | | 262 IsSimpleParameterListField::encode(simple_parameter_list) | |
252 FunctionKindField::encode(FunctionKind::kNormalFunction); | 263 FunctionKindField::encode(FunctionKind::kNormalFunction); |
253 scope_info->SetFlags(flags); | 264 scope_info->SetFlags(flags); |
254 scope_info->SetParameterCount(parameter_count); | 265 scope_info->SetParameterCount(parameter_count); |
255 scope_info->SetStackLocalCount(stack_local_count); | 266 scope_info->SetStackLocalCount(stack_local_count); |
256 scope_info->SetContextLocalCount(context_local_count); | 267 scope_info->SetContextLocalCount(context_local_count); |
| 268 scope_info->SetLexicalContextLocalCount(lexical_context_local_count); |
257 scope_info->SetContextGlobalCount(context_global_count); | 269 scope_info->SetContextGlobalCount(context_global_count); |
258 scope_info->SetStrongModeFreeVariableCount(strong_mode_free_variable_count); | 270 scope_info->SetStrongModeFreeVariableCount(strong_mode_free_variable_count); |
259 | 271 |
260 int index = kVariablePartIndex; | 272 int index = kVariablePartIndex; |
261 const int first_slot_index = 0; | 273 const int first_slot_index = 0; |
262 DCHECK(index == scope_info->StackLocalFirstSlotIndex()); | 274 DCHECK(index == scope_info->StackLocalFirstSlotIndex()); |
263 scope_info->set(index++, Smi::FromInt(first_slot_index)); | 275 scope_info->set(index++, Smi::FromInt(first_slot_index)); |
264 DCHECK(index == scope_info->StackLocalEntriesIndex()); | 276 DCHECK(index == scope_info->StackLocalEntriesIndex()); |
265 | 277 |
266 // Here we add info for context-allocated "this". | 278 // Here we add info for context-allocated "this". |
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
565 // Cache as not found. Mode, location, init flag and maybe assigned flag | 577 // Cache as not found. Mode, location, init flag and maybe assigned flag |
566 // don't matter. | 578 // don't matter. |
567 context_slot_cache->Update(scope_info, name, TEMPORARY, | 579 context_slot_cache->Update(scope_info, name, TEMPORARY, |
568 VariableLocation::CONTEXT, kNeedsInitialization, | 580 VariableLocation::CONTEXT, kNeedsInitialization, |
569 kNotAssigned, -1); | 581 kNotAssigned, -1); |
570 } | 582 } |
571 return -1; | 583 return -1; |
572 } | 584 } |
573 | 585 |
574 | 586 |
| 587 int ScopeInfo::LexicalContextSlotIndex(Handle<ScopeInfo> scope_info, |
| 588 Handle<String> name) { |
| 589 DCHECK(name->IsInternalizedString()); |
| 590 if (scope_info->length() > 0) { |
| 591 // TODO(yangguo): consider using the context slot cache here. |
| 592 int total_count = scope_info->ContextLocalCount(); |
| 593 int lexical_count = scope_info->LexicalContextLocalCount(); |
| 594 int non_lexical_count = total_count - lexical_count; |
| 595 |
| 596 int start = scope_info->ContextLocalNameEntriesIndex(); |
| 597 int end = start + total_count; |
| 598 int lexical_start = start + non_lexical_count; |
| 599 |
| 600 for (int i = lexical_start; i < end; ++i) { |
| 601 if (*name == scope_info->get(i)) { |
| 602 int var = i - start; |
| 603 DCHECK(IsLexicalVariableMode(scope_info->ContextLocalMode(var))); |
| 604 return Context::MIN_CONTEXT_SLOTS + var; |
| 605 } |
| 606 } |
| 607 } |
| 608 return -1; |
| 609 } |
| 610 |
| 611 |
575 String* ScopeInfo::ContextSlotName(int slot_index) { | 612 String* ScopeInfo::ContextSlotName(int slot_index) { |
576 int const var = slot_index - Context::MIN_CONTEXT_SLOTS; | 613 int const var = slot_index - Context::MIN_CONTEXT_SLOTS; |
577 DCHECK_LE(0, var); | 614 DCHECK_LE(0, var); |
578 DCHECK_LT(var, ContextLocalCount() + ContextGlobalCount()); | 615 DCHECK_LT(var, ContextLocalCount() + ContextGlobalCount()); |
579 return ContextLocalName(var); | 616 return ContextLocalName(var); |
580 } | 617 } |
581 | 618 |
582 | 619 |
583 int ScopeInfo::ParameterIndex(String* name) { | 620 int ScopeInfo::ParameterIndex(String* name) { |
584 DCHECK(name->IsInternalizedString()); | 621 DCHECK(name->IsInternalizedString()); |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
830 info->set_mode(i, var->mode()); | 867 info->set_mode(i, var->mode()); |
831 DCHECK(var->index() >= 0); | 868 DCHECK(var->index() >= 0); |
832 info->set_index(i, var->index()); | 869 info->set_index(i, var->index()); |
833 } | 870 } |
834 DCHECK(i == info->length()); | 871 DCHECK(i == info->length()); |
835 return info; | 872 return info; |
836 } | 873 } |
837 | 874 |
838 } // namespace internal | 875 } // namespace internal |
839 } // namespace v8 | 876 } // namespace v8 |
OLD | NEW |