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/v8.h" | 5 #include "src/v8.h" |
6 | 6 |
7 #include "src/accessors.h" | 7 #include "src/accessors.h" |
8 #include "src/bootstrapper.h" | 8 #include "src/bootstrapper.h" |
9 #include "src/messages.h" | 9 #include "src/messages.h" |
10 #include "src/parser.h" | 10 #include "src/parser.h" |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 receiver_ = NULL; | 158 receiver_ = NULL; |
159 new_target_ = nullptr; | 159 new_target_ = nullptr; |
160 function_ = NULL; | 160 function_ = NULL; |
161 arguments_ = NULL; | 161 arguments_ = NULL; |
162 illegal_redecl_ = NULL; | 162 illegal_redecl_ = NULL; |
163 scope_inside_with_ = false; | 163 scope_inside_with_ = false; |
164 scope_contains_with_ = false; | 164 scope_contains_with_ = false; |
165 scope_calls_eval_ = false; | 165 scope_calls_eval_ = false; |
166 scope_uses_arguments_ = false; | 166 scope_uses_arguments_ = false; |
167 scope_uses_super_property_ = false; | 167 scope_uses_super_property_ = false; |
| 168 scope_uses_this_ = false; |
168 asm_module_ = false; | 169 asm_module_ = false; |
169 asm_function_ = outer_scope != NULL && outer_scope->asm_module_; | 170 asm_function_ = outer_scope != NULL && outer_scope->asm_module_; |
170 // Inherit the language mode from the parent scope. | 171 // Inherit the language mode from the parent scope. |
171 language_mode_ = outer_scope != NULL ? outer_scope->language_mode_ : SLOPPY; | 172 language_mode_ = outer_scope != NULL ? outer_scope->language_mode_ : SLOPPY; |
172 outer_scope_calls_sloppy_eval_ = false; | 173 outer_scope_calls_sloppy_eval_ = false; |
173 inner_scope_calls_eval_ = false; | 174 inner_scope_calls_eval_ = false; |
174 inner_scope_uses_arguments_ = false; | 175 inner_scope_uses_arguments_ = false; |
| 176 inner_scope_uses_this_ = false; |
175 inner_scope_uses_super_property_ = false; | 177 inner_scope_uses_super_property_ = false; |
176 force_eager_compilation_ = false; | 178 force_eager_compilation_ = false; |
177 force_context_allocation_ = (outer_scope != NULL && !is_function_scope()) | 179 force_context_allocation_ = (outer_scope != NULL && !is_function_scope()) |
178 ? outer_scope->has_forced_context_allocation() : false; | 180 ? outer_scope->has_forced_context_allocation() : false; |
179 num_var_or_const_ = 0; | 181 num_var_or_const_ = 0; |
180 num_stack_slots_ = 0; | 182 num_stack_slots_ = 0; |
181 num_heap_slots_ = 0; | 183 num_heap_slots_ = 0; |
182 num_modules_ = 0; | 184 num_modules_ = 0; |
183 module_var_ = NULL, | 185 module_var_ = NULL, |
184 rest_parameter_ = NULL; | 186 rest_parameter_ = NULL; |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 DCHECK(!already_resolved()); | 302 DCHECK(!already_resolved()); |
301 | 303 |
302 // Add this scope as a new inner scope of the outer scope. | 304 // Add this scope as a new inner scope of the outer scope. |
303 if (outer_scope_ != NULL) { | 305 if (outer_scope_ != NULL) { |
304 outer_scope_->inner_scopes_.Add(this, zone()); | 306 outer_scope_->inner_scopes_.Add(this, zone()); |
305 scope_inside_with_ = outer_scope_->scope_inside_with_ || is_with_scope(); | 307 scope_inside_with_ = outer_scope_->scope_inside_with_ || is_with_scope(); |
306 } else { | 308 } else { |
307 scope_inside_with_ = is_with_scope(); | 309 scope_inside_with_ = is_with_scope(); |
308 } | 310 } |
309 | 311 |
310 // Declare convenience variables and the receiver. | 312 // Declare convenience variables. |
| 313 // Declare and allocate receiver (even for the script scope, and even |
| 314 // if naccesses_ == 0). |
| 315 // NOTE: When loading parameters in the script scope, we must take |
| 316 // care not to access them as properties of the global object, but |
| 317 // instead load them directly from the stack. Currently, the only |
| 318 // such parameter is 'this' which is passed on the stack when |
| 319 // invoking scripts |
311 if (is_declaration_scope()) { | 320 if (is_declaration_scope()) { |
312 DCHECK(!subclass_constructor || is_function_scope()); | 321 DCHECK(!subclass_constructor || is_function_scope()); |
313 if (has_this_declaration()) { | 322 Variable* var = variables_.Declare( |
314 Variable* var = variables_.Declare( | 323 this, ast_value_factory_->this_string(), |
315 this, ast_value_factory_->this_string(), | 324 subclass_constructor ? CONST : VAR, Variable::THIS, |
316 subclass_constructor ? CONST : VAR, Variable::THIS, | 325 subclass_constructor ? kNeedsInitialization : kCreatedInitialized); |
317 subclass_constructor ? kNeedsInitialization : kCreatedInitialized); | 326 var->AllocateTo(Variable::PARAMETER, -1); |
318 receiver_ = var; | 327 receiver_ = var; |
319 } | |
320 | 328 |
321 if (subclass_constructor) { | 329 if (subclass_constructor) { |
322 new_target_ = | 330 new_target_ = |
323 variables_.Declare(this, ast_value_factory_->new_target_string(), | 331 variables_.Declare(this, ast_value_factory_->new_target_string(), |
324 CONST, Variable::NEW_TARGET, kCreatedInitialized); | 332 CONST, Variable::NEW_TARGET, kCreatedInitialized); |
325 new_target_->AllocateTo(Variable::PARAMETER, -2); | 333 new_target_->AllocateTo(Variable::PARAMETER, -2); |
326 new_target_->set_is_used(); | 334 new_target_->set_is_used(); |
327 } | 335 } |
| 336 } else { |
| 337 DCHECK(outer_scope() != NULL); |
| 338 receiver_ = outer_scope()->receiver(); |
328 } | 339 } |
329 | 340 |
330 if (is_function_scope() && !is_arrow_scope()) { | 341 if (is_function_scope() && !is_arrow_scope()) { |
331 // Declare 'arguments' variable which exists in all non arrow functions. | 342 // Declare 'arguments' variable which exists in all non arrow functions. |
332 // Note that it might never be accessed, in which case it won't be | 343 // Note that it might never be accessed, in which case it won't be |
333 // allocated during variable allocation. | 344 // allocated during variable allocation. |
334 variables_.Declare(this, | 345 variables_.Declare(this, |
335 ast_value_factory_->arguments_string(), | 346 ast_value_factory_->arguments_string(), |
336 VAR, | 347 VAR, |
337 Variable::ARGUMENTS, | 348 Variable::ARGUMENTS, |
(...skipping 24 matching lines...) Expand all Loading... |
362 } | 373 } |
363 | 374 |
364 // Move unresolved variables | 375 // Move unresolved variables |
365 for (int i = 0; i < unresolved_.length(); i++) { | 376 for (int i = 0; i < unresolved_.length(); i++) { |
366 outer_scope()->unresolved_.Add(unresolved_[i], zone()); | 377 outer_scope()->unresolved_.Add(unresolved_[i], zone()); |
367 } | 378 } |
368 | 379 |
369 // Propagate usage flags to outer scope. | 380 // Propagate usage flags to outer scope. |
370 if (uses_arguments()) outer_scope_->RecordArgumentsUsage(); | 381 if (uses_arguments()) outer_scope_->RecordArgumentsUsage(); |
371 if (uses_super_property()) outer_scope_->RecordSuperPropertyUsage(); | 382 if (uses_super_property()) outer_scope_->RecordSuperPropertyUsage(); |
| 383 if (uses_this()) outer_scope_->RecordThisUsage(); |
372 if (scope_calls_eval_) outer_scope_->RecordEvalCall(); | 384 if (scope_calls_eval_) outer_scope_->RecordEvalCall(); |
373 | 385 |
374 return NULL; | 386 return NULL; |
375 } | 387 } |
376 | 388 |
377 | 389 |
378 Variable* Scope::LookupLocal(const AstRawString* name) { | 390 Variable* Scope::LookupLocal(const AstRawString* name) { |
379 Variable* result = variables_.Lookup(name); | 391 Variable* result = variables_.Lookup(name); |
380 if (result != NULL || scope_info_.is_null()) { | 392 if (result != NULL || scope_info_.is_null()) { |
381 return result; | 393 return result; |
(...skipping 529 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
911 Indent(n1, "// strong mode scope\n"); | 923 Indent(n1, "// strong mode scope\n"); |
912 } else if (is_strict(language_mode())) { | 924 } else if (is_strict(language_mode())) { |
913 Indent(n1, "// strict mode scope\n"); | 925 Indent(n1, "// strict mode scope\n"); |
914 } | 926 } |
915 if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n"); | 927 if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n"); |
916 if (scope_contains_with_) Indent(n1, "// scope contains 'with'\n"); | 928 if (scope_contains_with_) Indent(n1, "// scope contains 'with'\n"); |
917 if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n"); | 929 if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n"); |
918 if (scope_uses_arguments_) Indent(n1, "// scope uses 'arguments'\n"); | 930 if (scope_uses_arguments_) Indent(n1, "// scope uses 'arguments'\n"); |
919 if (scope_uses_super_property_) | 931 if (scope_uses_super_property_) |
920 Indent(n1, "// scope uses 'super' property\n"); | 932 Indent(n1, "// scope uses 'super' property\n"); |
| 933 if (scope_uses_this_) Indent(n1, "// scope uses 'this'\n"); |
921 if (inner_scope_uses_arguments_) { | 934 if (inner_scope_uses_arguments_) { |
922 Indent(n1, "// inner scope uses 'arguments'\n"); | 935 Indent(n1, "// inner scope uses 'arguments'\n"); |
923 } | 936 } |
924 if (inner_scope_uses_super_property_) | 937 if (inner_scope_uses_super_property_) |
925 Indent(n1, "// inner scope uses 'super' property\n"); | 938 Indent(n1, "// inner scope uses 'super' property\n"); |
| 939 if (inner_scope_uses_this_) Indent(n1, "// inner scope uses 'this'\n"); |
926 if (outer_scope_calls_sloppy_eval_) { | 940 if (outer_scope_calls_sloppy_eval_) { |
927 Indent(n1, "// outer scope calls 'eval' in sloppy context\n"); | 941 Indent(n1, "// outer scope calls 'eval' in sloppy context\n"); |
928 } | 942 } |
929 if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n"); | 943 if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n"); |
930 if (num_stack_slots_ > 0) { Indent(n1, "// "); | 944 if (num_stack_slots_ > 0) { Indent(n1, "// "); |
931 PrintF("%d stack slots\n", num_stack_slots_); } | 945 PrintF("%d stack slots\n", num_stack_slots_); } |
932 if (num_heap_slots_ > 0) { Indent(n1, "// "); | 946 if (num_heap_slots_ > 0) { Indent(n1, "// "); |
933 PrintF("%d heap slots\n", num_heap_slots_); } | 947 PrintF("%d heap slots\n", num_heap_slots_); } |
934 | 948 |
935 // Print locals. | 949 // Print locals. |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1028 *binding_kind = BOUND; | 1042 *binding_kind = BOUND; |
1029 } else if (outer_scope_ != NULL) { | 1043 } else if (outer_scope_ != NULL) { |
1030 var = outer_scope_->LookupRecursive(proxy, binding_kind, factory); | 1044 var = outer_scope_->LookupRecursive(proxy, binding_kind, factory); |
1031 if (*binding_kind == BOUND && (is_function_scope() || is_with_scope())) { | 1045 if (*binding_kind == BOUND && (is_function_scope() || is_with_scope())) { |
1032 var->ForceContextAllocation(); | 1046 var->ForceContextAllocation(); |
1033 } | 1047 } |
1034 } else { | 1048 } else { |
1035 DCHECK(is_script_scope()); | 1049 DCHECK(is_script_scope()); |
1036 } | 1050 } |
1037 | 1051 |
1038 // "this" can't be shadowed by "eval"-introduced bindings or by "with" scopes. | 1052 if (is_with_scope()) { |
1039 // TODO(wingo): There are other variables in this category; add them. | |
1040 bool name_can_be_shadowed = var == nullptr || !var->is_this(); | |
1041 | |
1042 if (is_with_scope() && name_can_be_shadowed) { | |
1043 DCHECK(!already_resolved()); | 1053 DCHECK(!already_resolved()); |
1044 // The current scope is a with scope, so the variable binding can not be | 1054 // The current scope is a with scope, so the variable binding can not be |
1045 // statically resolved. However, note that it was necessary to do a lookup | 1055 // statically resolved. However, note that it was necessary to do a lookup |
1046 // in the outer scope anyway, because if a binding exists in an outer scope, | 1056 // in the outer scope anyway, because if a binding exists in an outer scope, |
1047 // the associated variable has to be marked as potentially being accessed | 1057 // the associated variable has to be marked as potentially being accessed |
1048 // from inside of an inner with scope (the property may not be in the 'with' | 1058 // from inside of an inner with scope (the property may not be in the 'with' |
1049 // object). | 1059 // object). |
1050 if (var != NULL && proxy->is_assigned()) var->set_maybe_assigned(); | 1060 if (var != NULL && proxy->is_assigned()) var->set_maybe_assigned(); |
1051 *binding_kind = DYNAMIC_LOOKUP; | 1061 *binding_kind = DYNAMIC_LOOKUP; |
1052 return NULL; | 1062 return NULL; |
1053 } else if (calls_sloppy_eval() && name_can_be_shadowed) { | 1063 } else if (calls_sloppy_eval()) { |
1054 // A variable binding may have been found in an outer scope, but the current | 1064 // A variable binding may have been found in an outer scope, but the current |
1055 // scope makes a sloppy 'eval' call, so the found variable may not be | 1065 // scope makes a sloppy 'eval' call, so the found variable may not be |
1056 // the correct one (the 'eval' may introduce a binding with the same name). | 1066 // the correct one (the 'eval' may introduce a binding with the same name). |
1057 // In that case, change the lookup result to reflect this situation. | 1067 // In that case, change the lookup result to reflect this situation. |
1058 if (*binding_kind == BOUND) { | 1068 if (*binding_kind == BOUND) { |
1059 *binding_kind = BOUND_EVAL_SHADOWED; | 1069 *binding_kind = BOUND_EVAL_SHADOWED; |
1060 } else if (*binding_kind == UNBOUND) { | 1070 } else if (*binding_kind == UNBOUND) { |
1061 *binding_kind = UNBOUND_EVAL_SHADOWED; | 1071 *binding_kind = UNBOUND_EVAL_SHADOWED; |
1062 } | 1072 } |
1063 } | 1073 } |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1275 // usage of arguments/super/this, but do not propagate them out from normal | 1285 // usage of arguments/super/this, but do not propagate them out from normal |
1276 // functions. | 1286 // functions. |
1277 if (!inner->is_function_scope() || inner->is_arrow_scope()) { | 1287 if (!inner->is_function_scope() || inner->is_arrow_scope()) { |
1278 if (inner->scope_uses_arguments_ || inner->inner_scope_uses_arguments_) { | 1288 if (inner->scope_uses_arguments_ || inner->inner_scope_uses_arguments_) { |
1279 inner_scope_uses_arguments_ = true; | 1289 inner_scope_uses_arguments_ = true; |
1280 } | 1290 } |
1281 if (inner->scope_uses_super_property_ || | 1291 if (inner->scope_uses_super_property_ || |
1282 inner->inner_scope_uses_super_property_) { | 1292 inner->inner_scope_uses_super_property_) { |
1283 inner_scope_uses_super_property_ = true; | 1293 inner_scope_uses_super_property_ = true; |
1284 } | 1294 } |
| 1295 if (inner->scope_uses_this_ || inner->inner_scope_uses_this_) { |
| 1296 inner_scope_uses_this_ = true; |
| 1297 } |
1285 } | 1298 } |
1286 if (inner->force_eager_compilation_) { | 1299 if (inner->force_eager_compilation_) { |
1287 force_eager_compilation_ = true; | 1300 force_eager_compilation_ = true; |
1288 } | 1301 } |
1289 if (asm_module_ && inner->scope_type() == FUNCTION_SCOPE) { | 1302 if (asm_module_ && inner->scope_type() == FUNCTION_SCOPE) { |
1290 inner->asm_function_ = true; | 1303 inner->asm_function_ = true; |
1291 } | 1304 } |
1292 } | 1305 } |
1293 } | 1306 } |
1294 | 1307 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1395 // order is relevant! | 1408 // order is relevant! |
1396 for (int i = params_.length() - 1; i >= 0; --i) { | 1409 for (int i = params_.length() - 1; i >= 0; --i) { |
1397 Variable* var = params_[i]; | 1410 Variable* var = params_[i]; |
1398 if (var == rest_parameter_) continue; | 1411 if (var == rest_parameter_) continue; |
1399 | 1412 |
1400 DCHECK(var->scope() == this); | 1413 DCHECK(var->scope() == this); |
1401 if (uses_sloppy_arguments || has_forced_context_allocation()) { | 1414 if (uses_sloppy_arguments || has_forced_context_allocation()) { |
1402 // Force context allocation of the parameter. | 1415 // Force context allocation of the parameter. |
1403 var->ForceContextAllocation(); | 1416 var->ForceContextAllocation(); |
1404 } | 1417 } |
1405 AllocateParameter(var, i); | |
1406 } | |
1407 } | |
1408 | 1418 |
1409 | 1419 if (MustAllocate(var)) { |
1410 void Scope::AllocateParameter(Variable* var, int index) { | 1420 if (MustAllocateInContext(var)) { |
1411 if (MustAllocate(var)) { | 1421 DCHECK(var->IsUnallocated() || var->IsContextSlot()); |
1412 if (MustAllocateInContext(var)) { | 1422 if (var->IsUnallocated()) { |
1413 DCHECK(var->IsUnallocated() || var->IsContextSlot()); | 1423 AllocateHeapSlot(var); |
1414 if (var->IsUnallocated()) { | 1424 } |
1415 AllocateHeapSlot(var); | 1425 } else { |
1416 } | 1426 DCHECK(var->IsUnallocated() || var->IsParameter()); |
1417 } else { | 1427 if (var->IsUnallocated()) { |
1418 DCHECK(var->IsUnallocated() || var->IsParameter()); | 1428 var->AllocateTo(Variable::PARAMETER, i); |
1419 if (var->IsUnallocated()) { | 1429 } |
1420 var->AllocateTo(Variable::PARAMETER, index); | |
1421 } | 1430 } |
1422 } | 1431 } |
1423 } | 1432 } |
1424 } | 1433 } |
1425 | 1434 |
1426 | 1435 |
1427 void Scope::AllocateReceiver() { | |
1428 DCHECK_NOT_NULL(receiver()); | |
1429 DCHECK_EQ(receiver()->scope(), this); | |
1430 | |
1431 if (has_forced_context_allocation()) { | |
1432 // Force context allocation of the receiver. | |
1433 receiver()->ForceContextAllocation(); | |
1434 } | |
1435 AllocateParameter(receiver(), -1); | |
1436 } | |
1437 | |
1438 | |
1439 void Scope::AllocateNonParameterLocal(Isolate* isolate, Variable* var) { | 1436 void Scope::AllocateNonParameterLocal(Isolate* isolate, Variable* var) { |
1440 DCHECK(var->scope() == this); | 1437 DCHECK(var->scope() == this); |
1441 DCHECK(!var->IsVariable(isolate->factory()->dot_result_string()) || | 1438 DCHECK(!var->IsVariable(isolate->factory()->dot_result_string()) || |
1442 !var->IsStackLocal()); | 1439 !var->IsStackLocal()); |
1443 if (var->IsUnallocated() && MustAllocate(var)) { | 1440 if (var->IsUnallocated() && MustAllocate(var)) { |
1444 if (MustAllocateInContext(var)) { | 1441 if (MustAllocateInContext(var)) { |
1445 AllocateHeapSlot(var); | 1442 AllocateHeapSlot(var); |
1446 } else { | 1443 } else { |
1447 AllocateStackSlot(var); | 1444 AllocateStackSlot(var); |
1448 } | 1445 } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1498 | 1495 |
1499 // If scope is already resolved, we still need to allocate | 1496 // If scope is already resolved, we still need to allocate |
1500 // variables in inner scopes which might not had been resolved yet. | 1497 // variables in inner scopes which might not had been resolved yet. |
1501 if (already_resolved()) return; | 1498 if (already_resolved()) return; |
1502 // The number of slots required for variables. | 1499 // The number of slots required for variables. |
1503 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS; | 1500 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS; |
1504 | 1501 |
1505 // Allocate variables for this scope. | 1502 // Allocate variables for this scope. |
1506 // Parameters must be allocated first, if any. | 1503 // Parameters must be allocated first, if any. |
1507 if (is_function_scope()) AllocateParameterLocals(isolate); | 1504 if (is_function_scope()) AllocateParameterLocals(isolate); |
1508 if (has_this_declaration()) AllocateReceiver(); | |
1509 AllocateNonParameterLocals(isolate); | 1505 AllocateNonParameterLocals(isolate); |
1510 | 1506 |
1511 // Force allocation of a context for this scope if necessary. For a 'with' | 1507 // Force allocation of a context for this scope if necessary. For a 'with' |
1512 // scope and for a function scope that makes an 'eval' call we need a context, | 1508 // scope and for a function scope that makes an 'eval' call we need a context, |
1513 // even if no local variables were statically allocated in the scope. | 1509 // even if no local variables were statically allocated in the scope. |
1514 // Likewise for modules. | 1510 // Likewise for modules. |
1515 bool must_have_context = is_with_scope() || is_module_scope() || | 1511 bool must_have_context = is_with_scope() || is_module_scope() || |
1516 (is_function_scope() && calls_sloppy_eval()); | 1512 (is_function_scope() && calls_sloppy_eval()); |
1517 | 1513 |
1518 // If we didn't allocate any locals in the local context, then we only | 1514 // If we didn't allocate any locals in the local context, then we only |
(...skipping 28 matching lines...) Expand all Loading... |
1547 (function_ != NULL && function_->proxy()->var()->IsStackLocal() ? 1 : 0); | 1543 (function_ != NULL && function_->proxy()->var()->IsStackLocal() ? 1 : 0); |
1548 } | 1544 } |
1549 | 1545 |
1550 | 1546 |
1551 int Scope::ContextLocalCount() const { | 1547 int Scope::ContextLocalCount() const { |
1552 if (num_heap_slots() == 0) return 0; | 1548 if (num_heap_slots() == 0) return 0; |
1553 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - | 1549 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
1554 (function_ != NULL && function_->proxy()->var()->IsContextSlot() ? 1 : 0); | 1550 (function_ != NULL && function_->proxy()->var()->IsContextSlot() ? 1 : 0); |
1555 } | 1551 } |
1556 } } // namespace v8::internal | 1552 } } // namespace v8::internal |
OLD | NEW |