Chromium Code Reviews| 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 = | |
|
Toon Verwaest
2016/08/03 11:54:33
You can just write Handle<String> name_handle(scop
| |
| 269 handle(scope_info_->ContextLocalName(var), isolate); | |
| 270 const AstRawString* name = ast_value_factory->GetString(name_handle); | |
| 271 if (!names_seen.insert(name).second) continue; | |
| 272 int index = Context::MIN_CONTEXT_SLOTS + var; | |
| 273 VariableMode mode = scope_info_->ContextLocalMode(var); | |
| 274 InitializationFlag init_flag = scope_info_->ContextLocalInitFlag(var); | |
| 275 MaybeAssignedFlag maybe_assigned_flag = | |
| 276 scope_info_->ContextLocalMaybeAssignedFlag(var); | |
| 277 VariableLocation location = var < scope_info_->ContextLocalCount() | |
| 278 ? VariableLocation::CONTEXT | |
| 279 : VariableLocation::GLOBAL; | |
| 280 Variable::Kind kind = Variable::NORMAL; | |
| 281 if (index == scope_info_->ReceiverContextSlotIndex()) { | |
| 282 kind = Variable::THIS; | |
| 283 } | |
| 284 | |
| 285 Variable* result = variables_.Declare(this, name, mode, kind, init_flag, | |
| 286 maybe_assigned_flag); | |
| 287 result->AllocateTo(location, index); | |
| 288 } | |
| 289 | |
| 290 // We must read parameters from the end since for multiply declared | |
| 291 // parameters the value of the last declaration of that parameter is used | |
| 292 // inside a function (and thus we need to look at the last index). Was bug# | |
| 293 // 1110337. | |
| 294 for (int index = scope_info_->ParameterCount() - 1; index >= 0; --index) { | |
| 295 Handle<String> name_handle = | |
| 296 handle(scope_info_->ParameterName(index), isolate); | |
| 297 const AstRawString* name = ast_value_factory->GetString(name_handle); | |
| 298 if (!names_seen.insert(name).second) continue; | |
| 299 | |
| 300 VariableMode mode = DYNAMIC; | |
| 301 InitializationFlag init_flag = kCreatedInitialized; | |
| 302 MaybeAssignedFlag maybe_assigned_flag = kMaybeAssigned; | |
| 303 VariableLocation location = VariableLocation::LOOKUP; | |
| 304 Variable::Kind kind = Variable::NORMAL; | |
| 305 | |
| 306 Variable* result = variables_.Declare(this, name, mode, kind, init_flag, | |
| 307 maybe_assigned_flag); | |
| 308 result->AllocateTo(location, index); | |
| 309 } | |
| 310 | |
| 311 // Internalize function proxy for this scope. | |
| 312 if (scope_info_->HasFunctionName()) { | |
| 313 AstNodeFactory factory(ast_value_factory); | |
| 314 Handle<String> name_handle = handle(scope_info_->FunctionName(), isolate); | |
| 315 const AstRawString* name = ast_value_factory->GetString(name_handle); | |
| 316 VariableMode mode; | |
| 317 int index = scope_info_->FunctionContextSlotIndex(*name_handle, &mode); | |
| 318 if (index >= 0) { | |
| 319 Variable* result = new (zone()) | |
| 320 Variable(this, name, mode, Variable::NORMAL, kCreatedInitialized); | |
| 321 VariableProxy* proxy = factory.NewVariableProxy(result); | |
| 322 VariableDeclaration* declaration = | |
| 323 factory.NewVariableDeclaration(proxy, mode, this, kNoSourcePosition); | |
| 324 DeclareFunctionVar(declaration); | |
| 325 result->AllocateTo(VariableLocation::CONTEXT, index); | |
| 326 } | |
| 327 } | |
| 328 | |
| 329 scope_info_ = Handle<ScopeInfo>::null(); | |
| 330 } | |
| 251 | 331 |
| 252 bool Scope::Analyze(ParseInfo* info) { | 332 bool Scope::Analyze(ParseInfo* info) { |
| 253 DCHECK(info->literal() != NULL); | 333 DCHECK(info->literal() != NULL); |
| 254 DCHECK(info->scope() == NULL); | 334 DCHECK(info->scope() == NULL); |
| 255 Scope* scope = info->literal()->scope(); | 335 Scope* scope = info->literal()->scope(); |
| 256 Scope* top = scope; | 336 Scope* top = scope; |
| 257 | 337 |
| 258 // Traverse the scope tree up to the first unresolved scope or the global | 338 // 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. | 339 // scope and start scope resolution and variable allocation from that scope. |
| 260 while (!top->is_script_scope() && | 340 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(); | 1656 function_ != NULL && function_->proxy()->var()->IsContextSlot(); |
| 1577 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - num_global_slots() - | 1657 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - num_global_slots() - |
| 1578 (is_function_var_in_context ? 1 : 0); | 1658 (is_function_var_in_context ? 1 : 0); |
| 1579 } | 1659 } |
| 1580 | 1660 |
| 1581 | 1661 |
| 1582 int Scope::ContextGlobalCount() const { return num_global_slots(); } | 1662 int Scope::ContextGlobalCount() const { return num_global_slots(); } |
| 1583 | 1663 |
| 1584 } // namespace internal | 1664 } // namespace internal |
| 1585 } // namespace v8 | 1665 } // namespace v8 |
| OLD | NEW |