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 "src/accessors.h" | 7 #include "src/accessors.h" |
8 #include "src/ast/scopeinfo.h" | 8 #include "src/ast/scopeinfo.h" |
9 #include "src/bootstrapper.h" | 9 #include "src/bootstrapper.h" |
10 #include "src/messages.h" | 10 #include "src/messages.h" |
(...skipping 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 this, ast_value_factory_->this_string(), | 313 this, ast_value_factory_->this_string(), |
314 subclass_constructor ? CONST : VAR, Variable::THIS, | 314 subclass_constructor ? CONST : VAR, Variable::THIS, |
315 subclass_constructor ? kNeedsInitialization : kCreatedInitialized); | 315 subclass_constructor ? kNeedsInitialization : kCreatedInitialized); |
316 receiver_ = var; | 316 receiver_ = var; |
317 } | 317 } |
318 | 318 |
319 if (is_function_scope() && !is_arrow_scope()) { | 319 if (is_function_scope() && !is_arrow_scope()) { |
320 // Declare 'arguments' variable which exists in all non arrow functions. | 320 // Declare 'arguments' variable which exists in all non arrow functions. |
321 // Note that it might never be accessed, in which case it won't be | 321 // Note that it might never be accessed, in which case it won't be |
322 // allocated during variable allocation. | 322 // allocated during variable allocation. |
323 variables_.Declare(this, ast_value_factory_->arguments_string(), VAR, | 323 arguments_ = |
324 Variable::ARGUMENTS, kCreatedInitialized); | 324 variables_.Declare(this, ast_value_factory_->arguments_string(), VAR, |
| 325 Variable::ARGUMENTS, kCreatedInitialized); |
325 | 326 |
326 variables_.Declare(this, ast_value_factory_->new_target_string(), CONST, | 327 new_target_ = |
327 Variable::NORMAL, kCreatedInitialized); | 328 variables_.Declare(this, ast_value_factory_->new_target_string(), CONST, |
| 329 Variable::NORMAL, kCreatedInitialized); |
328 | 330 |
329 if (IsConciseMethod(function_kind_) || IsClassConstructor(function_kind_) || | 331 if (IsConciseMethod(function_kind_) || IsClassConstructor(function_kind_) || |
330 IsAccessorFunction(function_kind_)) { | 332 IsAccessorFunction(function_kind_)) { |
331 variables_.Declare(this, ast_value_factory_->this_function_string(), | 333 this_function_ = |
332 CONST, Variable::NORMAL, kCreatedInitialized); | 334 variables_.Declare(this, ast_value_factory_->this_function_string(), |
| 335 CONST, Variable::NORMAL, kCreatedInitialized); |
333 } | 336 } |
334 } | 337 } |
335 } | 338 } |
336 | 339 |
337 | 340 |
338 Scope* Scope::FinalizeBlockScope() { | 341 Scope* Scope::FinalizeBlockScope() { |
339 DCHECK(is_block_scope()); | 342 DCHECK(is_block_scope()); |
340 DCHECK(temps_.is_empty()); | 343 DCHECK(temps_.is_empty()); |
341 DCHECK(params_.is_empty()); | 344 DCHECK(params_.is_empty()); |
342 | 345 |
(...skipping 982 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1325 } | 1328 } |
1326 | 1329 |
1327 | 1330 |
1328 void Scope::AllocateHeapSlot(Variable* var) { | 1331 void Scope::AllocateHeapSlot(Variable* var) { |
1329 var->AllocateTo(VariableLocation::CONTEXT, num_heap_slots_++); | 1332 var->AllocateTo(VariableLocation::CONTEXT, num_heap_slots_++); |
1330 } | 1333 } |
1331 | 1334 |
1332 | 1335 |
1333 void Scope::AllocateParameterLocals(Isolate* isolate) { | 1336 void Scope::AllocateParameterLocals(Isolate* isolate) { |
1334 DCHECK(is_function_scope()); | 1337 DCHECK(is_function_scope()); |
1335 Variable* arguments = LookupLocal(ast_value_factory_->arguments_string()); | |
1336 // Functions have 'arguments' declared implicitly in all non arrow functions. | |
1337 DCHECK(arguments != nullptr || is_arrow_scope()); | |
1338 | 1338 |
1339 bool uses_sloppy_arguments = false; | 1339 bool uses_sloppy_arguments = false; |
1340 | 1340 |
1341 if (arguments != nullptr && MustAllocate(arguments) && | 1341 // Functions have 'arguments' declared implicitly in all non arrow functions. |
1342 !HasArgumentsParameter(isolate)) { | 1342 if (arguments_ != nullptr) { |
1343 // 'arguments' is used. Unless there is also a parameter called | 1343 // 'arguments' is used. Unless there is also a parameter called |
1344 // 'arguments', we must be conservative and allocate all parameters to | 1344 // 'arguments', we must be conservative and allocate all parameters to |
1345 // the context assuming they will be captured by the arguments object. | 1345 // the context assuming they will be captured by the arguments object. |
1346 // If we have a parameter named 'arguments', a (new) value is always | 1346 // If we have a parameter named 'arguments', a (new) value is always |
1347 // assigned to it via the function invocation. Then 'arguments' denotes | 1347 // assigned to it via the function invocation. Then 'arguments' denotes |
1348 // that specific parameter value and cannot be used to access the | 1348 // that specific parameter value and cannot be used to access the |
1349 // parameters, which is why we don't need to allocate an arguments | 1349 // parameters, which is why we don't need to allocate an arguments |
1350 // object in that case. | 1350 // object in that case. |
| 1351 if (MustAllocate(arguments_) && !HasArgumentsParameter(isolate)) { |
| 1352 // In strict mode 'arguments' does not alias formal parameters. |
| 1353 // Therefore in strict mode we allocate parameters as if 'arguments' |
| 1354 // were not used. |
| 1355 // If the parameter list is not simple, arguments isn't sloppy either. |
| 1356 uses_sloppy_arguments = |
| 1357 is_sloppy(language_mode()) && has_simple_parameters(); |
| 1358 } else { |
| 1359 // 'arguments' is unused. Tell the code generator that it does not need to |
| 1360 // allocate the arguments object by nulling out arguments_. |
| 1361 arguments_ = nullptr; |
| 1362 } |
1351 | 1363 |
1352 // We are using 'arguments'. Tell the code generator that is needs to | 1364 } else { |
1353 // allocate the arguments object by setting 'arguments_'. | 1365 DCHECK(is_arrow_scope()); |
1354 arguments_ = arguments; | |
1355 | |
1356 // In strict mode 'arguments' does not alias formal parameters. | |
1357 // Therefore in strict mode we allocate parameters as if 'arguments' | |
1358 // were not used. | |
1359 // If the parameter list is not simple, arguments isn't sloppy either. | |
1360 uses_sloppy_arguments = | |
1361 is_sloppy(language_mode()) && has_simple_parameters(); | |
1362 } | 1366 } |
1363 | 1367 |
1364 if (rest_parameter_ && !MustAllocate(rest_parameter_)) { | 1368 if (rest_parameter_ && !MustAllocate(rest_parameter_)) { |
1365 rest_parameter_ = NULL; | 1369 rest_parameter_ = nullptr; |
1366 } | 1370 } |
1367 | 1371 |
1368 // The same parameter may occur multiple times in the parameters_ list. | 1372 // The same parameter may occur multiple times in the parameters_ list. |
1369 // If it does, and if it is not copied into the context object, it must | 1373 // If it does, and if it is not copied into the context object, it must |
1370 // receive the highest parameter index for that parameter; thus iteration | 1374 // receive the highest parameter index for that parameter; thus iteration |
1371 // order is relevant! | 1375 // order is relevant! |
1372 for (int i = params_.length() - 1; i >= 0; --i) { | 1376 for (int i = params_.length() - 1; i >= 0; --i) { |
1373 Variable* var = params_[i]; | 1377 Variable* var = params_[i]; |
1374 if (var == rest_parameter_) continue; | 1378 if (var == rest_parameter_) continue; |
1375 | 1379 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1477 // because of the current ScopeInfo implementation (see | 1481 // because of the current ScopeInfo implementation (see |
1478 // ScopeInfo::ScopeInfo(FunctionScope* scope) constructor). | 1482 // ScopeInfo::ScopeInfo(FunctionScope* scope) constructor). |
1479 if (function_ != nullptr) { | 1483 if (function_ != nullptr) { |
1480 AllocateNonParameterLocal(isolate, function_->proxy()->var()); | 1484 AllocateNonParameterLocal(isolate, function_->proxy()->var()); |
1481 } | 1485 } |
1482 | 1486 |
1483 if (rest_parameter_ != nullptr) { | 1487 if (rest_parameter_ != nullptr) { |
1484 AllocateNonParameterLocal(isolate, rest_parameter_); | 1488 AllocateNonParameterLocal(isolate, rest_parameter_); |
1485 } | 1489 } |
1486 | 1490 |
1487 Variable* new_target_var = | 1491 if (new_target_ != nullptr && !MustAllocate(new_target_)) { |
1488 LookupLocal(ast_value_factory_->new_target_string()); | 1492 new_target_ = nullptr; |
1489 if (new_target_var != nullptr && MustAllocate(new_target_var)) { | |
1490 new_target_ = new_target_var; | |
1491 } | 1493 } |
1492 | 1494 |
1493 Variable* this_function_var = | 1495 if (this_function_ != nullptr && !MustAllocate(this_function_)) { |
1494 LookupLocal(ast_value_factory_->this_function_string()); | 1496 this_function_ = nullptr; |
1495 if (this_function_var != nullptr && MustAllocate(this_function_var)) { | |
1496 this_function_ = this_function_var; | |
1497 } | 1497 } |
1498 } | 1498 } |
1499 | 1499 |
1500 | 1500 |
1501 void Scope::AllocateVariablesRecursively(Isolate* isolate) { | 1501 void Scope::AllocateVariablesRecursively(Isolate* isolate) { |
1502 if (!already_resolved()) { | 1502 if (!already_resolved()) { |
1503 num_stack_slots_ = 0; | 1503 num_stack_slots_ = 0; |
1504 } | 1504 } |
1505 // Allocate variables for inner scopes. | 1505 // Allocate variables for inner scopes. |
1506 for (int i = 0; i < inner_scopes_.length(); i++) { | 1506 for (int i = 0; i < inner_scopes_.length(); i++) { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1551 function_ != NULL && function_->proxy()->var()->IsContextSlot(); | 1551 function_ != NULL && function_->proxy()->var()->IsContextSlot(); |
1552 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - num_global_slots() - | 1552 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - num_global_slots() - |
1553 (is_function_var_in_context ? 1 : 0); | 1553 (is_function_var_in_context ? 1 : 0); |
1554 } | 1554 } |
1555 | 1555 |
1556 | 1556 |
1557 int Scope::ContextGlobalCount() const { return num_global_slots(); } | 1557 int Scope::ContextGlobalCount() const { return num_global_slots(); } |
1558 | 1558 |
1559 } // namespace internal | 1559 } // namespace internal |
1560 } // namespace v8 | 1560 } // namespace v8 |
OLD | NEW |