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> | 7 #include <set> |
8 | 8 |
9 #include "src/accessors.h" | 9 #include "src/accessors.h" |
10 #include "src/ast/ast.h" | 10 #include "src/ast/ast.h" |
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 scope_nonlinear_ = false; | 296 scope_nonlinear_ = false; |
297 is_hidden_ = false; | 297 is_hidden_ = false; |
298 is_debug_evaluate_scope_ = false; | 298 is_debug_evaluate_scope_ = false; |
299 | 299 |
300 inner_scope_calls_eval_ = false; | 300 inner_scope_calls_eval_ = false; |
301 force_context_allocation_ = false; | 301 force_context_allocation_ = false; |
302 | 302 |
303 is_declaration_scope_ = false; | 303 is_declaration_scope_ = false; |
304 | 304 |
305 is_lazily_parsed_ = false; | 305 is_lazily_parsed_ = false; |
| 306 should_eager_compile_ = false; |
306 } | 307 } |
307 | 308 |
308 bool Scope::HasSimpleParameters() { | 309 bool Scope::HasSimpleParameters() { |
309 DeclarationScope* scope = GetClosureScope(); | 310 DeclarationScope* scope = GetClosureScope(); |
310 return !scope->is_function_scope() || scope->has_simple_parameters(); | 311 return !scope->is_function_scope() || scope->has_simple_parameters(); |
311 } | 312 } |
312 | 313 |
| 314 bool Scope::ShouldEagerCompile() const { |
| 315 if (is_declaration_scope() && |
| 316 !AsDeclarationScope()->AllowsLazyCompilation()) { |
| 317 return true; |
| 318 } |
| 319 return !is_lazily_parsed_ && should_eager_compile_; |
| 320 } |
| 321 |
| 322 void Scope::SetShouldEagerCompile() { |
| 323 should_eager_compile_ = true; |
| 324 for (Scope* inner = inner_scope_; inner != nullptr; inner = inner->sibling_) { |
| 325 if (inner->is_function_scope()) continue; |
| 326 inner->SetShouldEagerCompile(); |
| 327 } |
| 328 } |
| 329 |
313 void DeclarationScope::set_asm_module() { | 330 void DeclarationScope::set_asm_module() { |
314 asm_module_ = true; | 331 asm_module_ = true; |
315 // Mark any existing inner function scopes as asm function scopes. | 332 // Mark any existing inner function scopes as asm function scopes. |
316 for (Scope* inner = inner_scope_; inner != nullptr; inner = inner->sibling_) { | 333 for (Scope* inner = inner_scope_; inner != nullptr; inner = inner->sibling_) { |
317 if (inner->is_function_scope()) { | 334 if (inner->is_function_scope()) { |
318 inner->AsDeclarationScope()->set_asm_function(); | 335 inner->AsDeclarationScope()->set_asm_function(); |
319 } | 336 } |
320 } | 337 } |
321 } | 338 } |
322 | 339 |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
545 } | 562 } |
546 | 563 |
547 // We are compiling one of three cases: | 564 // We are compiling one of three cases: |
548 // 1) top-level code, | 565 // 1) top-level code, |
549 // 2) a function/eval/module on the top-level | 566 // 2) a function/eval/module on the top-level |
550 // 3) a function/eval in a scope that was already resolved. | 567 // 3) a function/eval in a scope that was already resolved. |
551 DCHECK(scope->scope_type() == SCRIPT_SCOPE || | 568 DCHECK(scope->scope_type() == SCRIPT_SCOPE || |
552 scope->outer_scope()->scope_type() == SCRIPT_SCOPE || | 569 scope->outer_scope()->scope_type() == SCRIPT_SCOPE || |
553 scope->outer_scope()->already_resolved_); | 570 scope->outer_scope()->already_resolved_); |
554 | 571 |
| 572 // The outer scope is never lazy. |
| 573 scope->SetShouldEagerCompile(); |
| 574 |
555 scope->AllocateVariables(info, mode); | 575 scope->AllocateVariables(info, mode); |
556 | 576 |
557 // Ensuring that the outer script scope has a scope info avoids having | 577 // Ensuring that the outer script scope has a scope info avoids having |
558 // special case for native contexts vs other contexts. | 578 // special case for native contexts vs other contexts. |
559 if (info->script_scope()->scope_info_.is_null()) { | 579 if (info->script_scope()->scope_info_.is_null()) { |
560 info->script_scope()->scope_info_ = | 580 info->script_scope()->scope_info_ = |
561 handle(ScopeInfo::Empty(info->isolate())); | 581 handle(ScopeInfo::Empty(info->isolate())); |
562 } | 582 } |
563 | 583 |
564 #ifdef DEBUG | 584 #ifdef DEBUG |
(...skipping 843 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1408 Indent(n1, "// strict mode scope\n"); | 1428 Indent(n1, "// strict mode scope\n"); |
1409 } | 1429 } |
1410 if (IsAsmModule()) Indent(n1, "// scope is an asm module\n"); | 1430 if (IsAsmModule()) Indent(n1, "// scope is an asm module\n"); |
1411 if (IsAsmFunction()) Indent(n1, "// scope is an asm function\n"); | 1431 if (IsAsmFunction()) Indent(n1, "// scope is an asm function\n"); |
1412 if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n"); | 1432 if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n"); |
1413 if (is_declaration_scope() && AsDeclarationScope()->uses_super_property()) { | 1433 if (is_declaration_scope() && AsDeclarationScope()->uses_super_property()) { |
1414 Indent(n1, "// scope uses 'super' property\n"); | 1434 Indent(n1, "// scope uses 'super' property\n"); |
1415 } | 1435 } |
1416 if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n"); | 1436 if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n"); |
1417 if (is_lazily_parsed_) Indent(n1, "// lazily parsed\n"); | 1437 if (is_lazily_parsed_) Indent(n1, "// lazily parsed\n"); |
| 1438 if (should_eager_compile_) Indent(n1, "// will be compiled\n"); |
1418 if (num_stack_slots_ > 0) { | 1439 if (num_stack_slots_ > 0) { |
1419 Indent(n1, "// "); | 1440 Indent(n1, "// "); |
1420 PrintF("%d stack slots\n", num_stack_slots_); | 1441 PrintF("%d stack slots\n", num_stack_slots_); |
1421 } | 1442 } |
1422 if (num_heap_slots_ > 0) { | 1443 if (num_heap_slots_ > 0) { |
1423 Indent(n1, "// "); | 1444 Indent(n1, "// "); |
1424 PrintF("%d heap slots\n", num_heap_slots_); | 1445 PrintF("%d heap slots\n", num_heap_slots_); |
1425 } | 1446 } |
1426 | 1447 |
1427 // Print locals. | 1448 // Print locals. |
(...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1887 scope_info_ = ScopeInfo::Create(isolate, zone(), this, outer_scope); | 1908 scope_info_ = ScopeInfo::Create(isolate, zone(), this, outer_scope); |
1888 } | 1909 } |
1889 | 1910 |
1890 // The ScopeInfo chain should mirror the context chain, so we only link to | 1911 // The ScopeInfo chain should mirror the context chain, so we only link to |
1891 // the next outer scope that needs a context. | 1912 // the next outer scope that needs a context. |
1892 MaybeHandle<ScopeInfo> next_outer_scope = outer_scope; | 1913 MaybeHandle<ScopeInfo> next_outer_scope = outer_scope; |
1893 if (NeedsContext()) next_outer_scope = scope_info_; | 1914 if (NeedsContext()) next_outer_scope = scope_info_; |
1894 | 1915 |
1895 // Allocate ScopeInfos for inner scopes. | 1916 // Allocate ScopeInfos for inner scopes. |
1896 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { | 1917 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { |
1897 scope->AllocateScopeInfosRecursively(isolate, mode, next_outer_scope); | 1918 AnalyzeMode next_mode = mode; |
| 1919 bool next_eager = should_eager_compile_; |
| 1920 if (scope->is_function_scope()) { |
| 1921 // Make sure all inner scopes have are consistently marked: we can't |
| 1922 // eager compile inner functions of lazy functions, but if a function |
| 1923 // should be eagerly compiled, all its inner scopes are compiled as well. |
| 1924 next_eager = should_eager_compile_ ? scope->ShouldEagerCompile() : false; |
| 1925 |
| 1926 // The ScopeIterator which uses the AnalyzeMode::kDebugger only expects |
| 1927 // to find ScopeInfos for the current function and all its inner |
| 1928 // non-function scopes (see ScopeIterator::GetNestedScopeChain). |
| 1929 next_mode = AnalyzeMode::kRegular; |
| 1930 } |
| 1931 scope->should_eager_compile_ = next_eager; |
| 1932 scope->AllocateScopeInfosRecursively(isolate, next_mode, next_outer_scope); |
1898 } | 1933 } |
1899 } | 1934 } |
1900 | 1935 |
1901 int Scope::StackLocalCount() const { | 1936 int Scope::StackLocalCount() const { |
1902 Variable* function = | 1937 Variable* function = |
1903 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; | 1938 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; |
1904 return num_stack_slots() - | 1939 return num_stack_slots() - |
1905 (function != nullptr && function->IsStackLocal() ? 1 : 0); | 1940 (function != nullptr && function->IsStackLocal() ? 1 : 0); |
1906 } | 1941 } |
1907 | 1942 |
1908 | 1943 |
1909 int Scope::ContextLocalCount() const { | 1944 int Scope::ContextLocalCount() const { |
1910 if (num_heap_slots() == 0) return 0; | 1945 if (num_heap_slots() == 0) return 0; |
1911 Variable* function = | 1946 Variable* function = |
1912 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; | 1947 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; |
1913 bool is_function_var_in_context = | 1948 bool is_function_var_in_context = |
1914 function != nullptr && function->IsContextSlot(); | 1949 function != nullptr && function->IsContextSlot(); |
1915 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - | 1950 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
1916 (is_function_var_in_context ? 1 : 0); | 1951 (is_function_var_in_context ? 1 : 0); |
1917 } | 1952 } |
1918 | 1953 |
1919 } // namespace internal | 1954 } // namespace internal |
1920 } // namespace v8 | 1955 } // namespace v8 |
OLD | NEW |