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::kFull) { | |
246 current_scope->InternalizeScopeInfo(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::InternalizeScopeInfo(Isolate* isolate, | |
258 AstValueFactory* ast_value_factory) { | |
259 HandleScope scope(isolate); | |
260 // Internalize context local & globals variables. | |
261 for (int var = 0; var < scope_info_->ContextLocalCount() + | |
262 scope_info_->ContextGlobalCount(); | |
263 ++var) { | |
264 Handle<String> name_handle = | |
265 handle(scope_info_->ContextLocalName(var), isolate); | |
266 const AstRawString* name = ast_value_factory->GetString(name_handle); | |
267 int index = Context::MIN_CONTEXT_SLOTS + var; | |
268 VariableMode mode = scope_info_->ContextLocalMode(var); | |
269 InitializationFlag init_flag = scope_info_->ContextLocalInitFlag(var); | |
270 MaybeAssignedFlag maybe_assigned_flag = | |
271 scope_info_->ContextLocalMaybeAssignedFlag(var); | |
272 VariableLocation location = var < scope_info_->ContextLocalCount() | |
273 ? VariableLocation::CONTEXT | |
274 : VariableLocation::GLOBAL; | |
275 Variable::Kind kind = Variable::NORMAL; | |
276 if (index == scope_info_->ReceiverContextSlotIndex()) { | |
277 kind = Variable::THIS; | |
278 } | |
279 | |
280 Variable* result = variables_.Declare(this, name, mode, kind, init_flag, | |
281 maybe_assigned_flag); | |
282 result->AllocateTo(location, index); | |
283 } | |
284 | |
285 // We must read parameters from the end since for multiply declared | |
286 // parameters the value of the last declaration of that parameter is used | |
287 // inside a function (and thus we need to look at the last index). Was bug# | |
288 // 1110337. | |
289 std::set<const AstRawString*> parameters_seen; | |
jochen (gone - plz use gerrit)
2016/08/01 14:16:00
Not sure whether I can assume that the locals and
marja
2016/08/02 08:29:31
What?
| |
290 for (int index = scope_info_->ParameterCount() - 1; index >= 0; --index) { | |
291 Handle<String> name_handle = | |
292 handle(scope_info_->ParameterName(index), isolate); | |
293 const AstRawString* name = ast_value_factory->GetString(name_handle); | |
294 if (parameters_seen.find(name) != parameters_seen.end()) continue; | |
295 parameters_seen.insert(name); | |
296 | |
297 VariableMode mode = DYNAMIC; | |
298 InitializationFlag init_flag = kCreatedInitialized; | |
299 MaybeAssignedFlag maybe_assigned_flag = kMaybeAssigned; | |
300 VariableLocation location = VariableLocation::LOOKUP; | |
301 Variable::Kind kind = Variable::NORMAL; | |
302 | |
303 Variable* result = variables_.Declare(this, name, mode, kind, init_flag, | |
304 maybe_assigned_flag); | |
305 result->AllocateTo(location, index); | |
306 } | |
307 | |
308 // Internalize function proxy for this scope. | |
309 if (scope_info_->HasFunctionName()) { | |
310 AstNodeFactory factory(ast_value_factory); | |
jochen (gone - plz use gerrit)
2016/08/01 14:16:00
can I just create a node factory here, or do I nee
marja
2016/08/02 08:29:31
This should be fine, as long as you give it the sa
| |
311 VariableMode mode; | |
312 Handle<String> name_handle = handle(scope_info_->FunctionName(), isolate); | |
313 const AstRawString* name = ast_value_factory->GetString(name_handle); | |
314 int index = scope_info_->FunctionContextSlotIndex(*name_handle, &mode); | |
315 Variable* result = new (zone()) | |
316 Variable(this, name, mode, Variable::NORMAL, kCreatedInitialized); | |
317 VariableProxy* proxy = factory.NewVariableProxy(result); | |
318 VariableDeclaration* declaration = | |
319 factory.NewVariableDeclaration(proxy, mode, this, kNoSourcePosition); | |
320 DeclareFunctionVar(declaration); | |
321 result->AllocateTo(VariableLocation::CONTEXT, index); | |
322 } | |
323 | |
324 scope_info_ = Handle<ScopeInfo>::null(); | |
325 } | |
251 | 326 |
252 bool Scope::Analyze(ParseInfo* info) { | 327 bool Scope::Analyze(ParseInfo* info) { |
253 DCHECK(info->literal() != NULL); | 328 DCHECK(info->literal() != NULL); |
254 DCHECK(info->scope() == NULL); | 329 DCHECK(info->scope() == NULL); |
255 Scope* scope = info->literal()->scope(); | 330 Scope* scope = info->literal()->scope(); |
256 Scope* top = scope; | 331 Scope* top = scope; |
257 | 332 |
258 // Traverse the scope tree up to the first unresolved scope or the global | 333 // 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. | 334 // scope and start scope resolution and variable allocation from that scope. |
260 while (!top->is_script_scope() && | 335 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(); | 1651 function_ != NULL && function_->proxy()->var()->IsContextSlot(); |
1577 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - num_global_slots() - | 1652 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - num_global_slots() - |
1578 (is_function_var_in_context ? 1 : 0); | 1653 (is_function_var_in_context ? 1 : 0); |
1579 } | 1654 } |
1580 | 1655 |
1581 | 1656 |
1582 int Scope::ContextGlobalCount() const { return num_global_slots(); } | 1657 int Scope::ContextGlobalCount() const { return num_global_slots(); } |
1583 | 1658 |
1584 } // namespace internal | 1659 } // namespace internal |
1585 } // namespace v8 | 1660 } // namespace v8 |
OLD | NEW |