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> |
| 8 |
7 #include "src/accessors.h" | 9 #include "src/accessors.h" |
8 #include "src/bootstrapper.h" | 10 #include "src/bootstrapper.h" |
9 #include "src/messages.h" | 11 #include "src/messages.h" |
10 #include "src/parsing/parser.h" // for ParseInfo | 12 #include "src/parsing/parser.h" // for ParseInfo |
11 | 13 |
12 namespace v8 { | 14 namespace v8 { |
13 namespace internal { | 15 namespace internal { |
14 | 16 |
15 // ---------------------------------------------------------------------------- | 17 // ---------------------------------------------------------------------------- |
16 // Implementation of LocalsMap | 18 // Implementation of LocalsMap |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 has_simple_parameters_ = true; | 199 has_simple_parameters_ = true; |
198 rest_parameter_ = NULL; | 200 rest_parameter_ = NULL; |
199 rest_index_ = -1; | 201 rest_index_ = -1; |
200 start_position_ = kNoSourcePosition; | 202 start_position_ = kNoSourcePosition; |
201 end_position_ = kNoSourcePosition; | 203 end_position_ = kNoSourcePosition; |
202 is_hidden_ = false; | 204 is_hidden_ = false; |
203 } | 205 } |
204 | 206 |
205 Scope* Scope::DeserializeScopeChain(Isolate* isolate, Zone* zone, | 207 Scope* Scope::DeserializeScopeChain(Isolate* isolate, Zone* zone, |
206 Context* context, Scope* script_scope, | 208 Context* context, Scope* script_scope, |
207 AstValueFactory* ast_value_factory) { | 209 AstValueFactory* ast_value_factory, |
| 210 DeserializationMode deserialization_mode) { |
208 // Reconstruct the outer scope chain from a closure's context chain. | 211 // Reconstruct the outer scope chain from a closure's context chain. |
209 Scope* current_scope = NULL; | 212 Scope* current_scope = NULL; |
210 Scope* innermost_scope = NULL; | 213 Scope* innermost_scope = NULL; |
211 while (!context->IsNativeContext()) { | 214 while (!context->IsNativeContext()) { |
212 if (context->IsWithContext() || context->IsDebugEvaluateContext()) { | 215 if (context->IsWithContext() || context->IsDebugEvaluateContext()) { |
213 // For scope analysis, debug-evaluate is equivalent to a with scope. | 216 // For scope analysis, debug-evaluate is equivalent to a with scope. |
214 Scope* with_scope = new (zone) | 217 Scope* with_scope = new (zone) |
215 Scope(zone, current_scope, WITH_SCOPE, Handle<ScopeInfo>::null()); | 218 Scope(zone, current_scope, WITH_SCOPE, Handle<ScopeInfo>::null()); |
216 current_scope = with_scope; | 219 current_scope = with_scope; |
217 // All the inner scopes are inside a with. | 220 // All the inner scopes are inside a with. |
(...skipping 14 matching lines...) Expand all Loading... |
232 ScopeInfo* scope_info = context->scope_info(); | 235 ScopeInfo* scope_info = context->scope_info(); |
233 current_scope = new (zone) Scope(zone, current_scope, BLOCK_SCOPE, | 236 current_scope = new (zone) Scope(zone, current_scope, BLOCK_SCOPE, |
234 Handle<ScopeInfo>(scope_info)); | 237 Handle<ScopeInfo>(scope_info)); |
235 } else { | 238 } else { |
236 DCHECK(context->IsCatchContext()); | 239 DCHECK(context->IsCatchContext()); |
237 String* name = context->catch_name(); | 240 String* name = context->catch_name(); |
238 current_scope = | 241 current_scope = |
239 new (zone) Scope(zone, current_scope, | 242 new (zone) Scope(zone, current_scope, |
240 ast_value_factory->GetString(handle(name, isolate))); | 243 ast_value_factory->GetString(handle(name, isolate))); |
241 } | 244 } |
| 245 if (deserialization_mode == DeserializationMode::kDeserializeOffHeap) { |
| 246 current_scope->DeserializeScopeInfo(isolate, ast_value_factory); |
| 247 } |
242 if (innermost_scope == NULL) innermost_scope = current_scope; | 248 if (innermost_scope == NULL) innermost_scope = current_scope; |
243 context = context->previous(); | 249 context = context->previous(); |
244 } | 250 } |
245 | 251 |
246 script_scope->AddInnerScope(current_scope); | 252 script_scope->AddInnerScope(current_scope); |
247 script_scope->PropagateScopeInfo(false); | 253 script_scope->PropagateScopeInfo(false); |
248 return (innermost_scope == NULL) ? script_scope : innermost_scope; | 254 return (innermost_scope == NULL) ? script_scope : innermost_scope; |
249 } | 255 } |
250 | 256 |
| 257 void Scope::DeserializeScopeInfo(Isolate* isolate, |
| 258 AstValueFactory* ast_value_factory) { |
| 259 if (scope_info_.is_null()) return; |
| 260 |
| 261 DCHECK(ThreadId::Current().Equals(isolate->thread_id())); |
| 262 |
| 263 std::set<const AstRawString*> names_seen; |
| 264 // Internalize context local & globals variables. |
| 265 for (int var = 0; var < scope_info_->ContextLocalCount() + |
| 266 scope_info_->ContextGlobalCount(); |
| 267 ++var) { |
| 268 Handle<String> name_handle(scope_info_->ContextLocalName(var), isolate); |
| 269 const AstRawString* name = ast_value_factory->GetString(name_handle); |
| 270 if (!names_seen.insert(name).second) continue; |
| 271 int index = Context::MIN_CONTEXT_SLOTS + var; |
| 272 VariableMode mode = scope_info_->ContextLocalMode(var); |
| 273 InitializationFlag init_flag = scope_info_->ContextLocalInitFlag(var); |
| 274 MaybeAssignedFlag maybe_assigned_flag = |
| 275 scope_info_->ContextLocalMaybeAssignedFlag(var); |
| 276 VariableLocation location = var < scope_info_->ContextLocalCount() |
| 277 ? VariableLocation::CONTEXT |
| 278 : VariableLocation::GLOBAL; |
| 279 Variable::Kind kind = Variable::NORMAL; |
| 280 if (index == scope_info_->ReceiverContextSlotIndex()) { |
| 281 kind = Variable::THIS; |
| 282 } |
| 283 |
| 284 Variable* result = variables_.Declare(this, name, mode, kind, init_flag, |
| 285 maybe_assigned_flag); |
| 286 result->AllocateTo(location, index); |
| 287 } |
| 288 |
| 289 // We must read parameters from the end since for multiply declared |
| 290 // parameters the value of the last declaration of that parameter is used |
| 291 // inside a function (and thus we need to look at the last index). Was bug# |
| 292 // 1110337. |
| 293 for (int index = scope_info_->ParameterCount() - 1; index >= 0; --index) { |
| 294 Handle<String> name_handle(scope_info_->ParameterName(index), isolate); |
| 295 const AstRawString* name = ast_value_factory->GetString(name_handle); |
| 296 if (!names_seen.insert(name).second) continue; |
| 297 |
| 298 VariableMode mode = DYNAMIC; |
| 299 InitializationFlag init_flag = kCreatedInitialized; |
| 300 MaybeAssignedFlag maybe_assigned_flag = kMaybeAssigned; |
| 301 VariableLocation location = VariableLocation::LOOKUP; |
| 302 Variable::Kind kind = Variable::NORMAL; |
| 303 |
| 304 Variable* result = variables_.Declare(this, name, mode, kind, init_flag, |
| 305 maybe_assigned_flag); |
| 306 result->AllocateTo(location, index); |
| 307 } |
| 308 |
| 309 // Internalize function proxy for this scope. |
| 310 if (scope_info_->HasFunctionName()) { |
| 311 AstNodeFactory factory(ast_value_factory); |
| 312 Handle<String> name_handle(scope_info_->FunctionName(), isolate); |
| 313 const AstRawString* name = ast_value_factory->GetString(name_handle); |
| 314 VariableMode mode; |
| 315 int index = scope_info_->FunctionContextSlotIndex(*name_handle, &mode); |
| 316 if (index >= 0) { |
| 317 Variable* result = new (zone()) |
| 318 Variable(this, name, mode, Variable::NORMAL, kCreatedInitialized); |
| 319 VariableProxy* proxy = factory.NewVariableProxy(result); |
| 320 VariableDeclaration* declaration = |
| 321 factory.NewVariableDeclaration(proxy, mode, this, kNoSourcePosition); |
| 322 DeclareFunctionVar(declaration); |
| 323 result->AllocateTo(VariableLocation::CONTEXT, index); |
| 324 } |
| 325 } |
| 326 |
| 327 scope_info_ = Handle<ScopeInfo>::null(); |
| 328 } |
251 | 329 |
252 bool Scope::Analyze(ParseInfo* info) { | 330 bool Scope::Analyze(ParseInfo* info) { |
253 DCHECK(info->literal() != NULL); | 331 DCHECK(info->literal() != NULL); |
254 DCHECK(info->scope() == NULL); | 332 DCHECK(info->scope() == NULL); |
255 Scope* scope = info->literal()->scope(); | 333 Scope* scope = info->literal()->scope(); |
256 Scope* top = scope; | 334 Scope* top = scope; |
257 | 335 |
258 // Traverse the scope tree up to the first unresolved scope or the global | 336 // Traverse the scope tree up to the first unresolved scope or the global |
259 // scope and start scope resolution and variable allocation from that scope. | 337 // scope and start scope resolution and variable allocation from that scope. |
260 while (!top->is_script_scope() && | 338 while (!top->is_script_scope() && |
(...skipping 1315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1576 function_ != NULL && function_->proxy()->var()->IsContextSlot(); | 1654 function_ != NULL && function_->proxy()->var()->IsContextSlot(); |
1577 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - num_global_slots() - | 1655 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - num_global_slots() - |
1578 (is_function_var_in_context ? 1 : 0); | 1656 (is_function_var_in_context ? 1 : 0); |
1579 } | 1657 } |
1580 | 1658 |
1581 | 1659 |
1582 int Scope::ContextGlobalCount() const { return num_global_slots(); } | 1660 int Scope::ContextGlobalCount() const { return num_global_slots(); } |
1583 | 1661 |
1584 } // namespace internal | 1662 } // namespace internal |
1585 } // namespace v8 | 1663 } // namespace v8 |
OLD | NEW |