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/parsing/parser.h" | 5 #include "src/parsing/parser.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "src/api.h" | 9 #include "src/api.h" |
10 #include "src/ast/ast-expression-rewriter.h" | 10 #include "src/ast/ast-expression-rewriter.h" |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 public: | 115 public: |
116 DiscardableZoneScope(Parser* parser, Zone* temp_zone, bool use_temp_zone) | 116 DiscardableZoneScope(Parser* parser, Zone* temp_zone, bool use_temp_zone) |
117 : ast_node_factory_scope_(parser->factory(), temp_zone, use_temp_zone), | 117 : ast_node_factory_scope_(parser->factory(), temp_zone, use_temp_zone), |
118 fni_(parser->ast_value_factory_, temp_zone), | 118 fni_(parser->ast_value_factory_, temp_zone), |
119 parser_(parser), | 119 parser_(parser), |
120 prev_fni_(parser->fni_), | 120 prev_fni_(parser->fni_), |
121 prev_zone_(parser->zone_) { | 121 prev_zone_(parser->zone_) { |
122 if (use_temp_zone) { | 122 if (use_temp_zone) { |
123 parser_->fni_ = &fni_; | 123 parser_->fni_ = &fni_; |
124 parser_->zone_ = temp_zone; | 124 parser_->zone_ = temp_zone; |
| 125 if (parser_->reusable_preparser_ != nullptr) { |
| 126 parser_->reusable_preparser_->zone_ = temp_zone; |
| 127 } |
125 } | 128 } |
126 } | 129 } |
127 ~DiscardableZoneScope() { | 130 ~DiscardableZoneScope() { |
128 parser_->fni_ = prev_fni_; | 131 parser_->fni_ = prev_fni_; |
129 parser_->zone_ = prev_zone_; | 132 parser_->zone_ = prev_zone_; |
| 133 if (parser_->reusable_preparser_ != nullptr) { |
| 134 parser_->reusable_preparser_->zone_ = prev_zone_; |
| 135 } |
130 } | 136 } |
131 | 137 |
132 private: | 138 private: |
133 AstNodeFactory::BodyScope ast_node_factory_scope_; | 139 AstNodeFactory::BodyScope ast_node_factory_scope_; |
134 FuncNameInferrer fni_; | 140 FuncNameInferrer fni_; |
135 Parser* parser_; | 141 Parser* parser_; |
136 FuncNameInferrer* prev_fni_; | 142 FuncNameInferrer* prev_fni_; |
137 Zone* prev_zone_; | 143 Zone* prev_zone_; |
138 | 144 |
139 DISALLOW_COPY_AND_ASSIGN(DiscardableZoneScope); | 145 DISALLOW_COPY_AND_ASSIGN(DiscardableZoneScope); |
(...skipping 2535 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2675 // - It must not have been prohibited by the caller to Parse (some callers | 2681 // - It must not have been prohibited by the caller to Parse (some callers |
2676 // need a full AST). | 2682 // need a full AST). |
2677 // - The outer scope must allow lazy compilation of inner functions. | 2683 // - The outer scope must allow lazy compilation of inner functions. |
2678 // - The function mustn't be a function expression with an open parenthesis | 2684 // - The function mustn't be a function expression with an open parenthesis |
2679 // before; we consider that a hint that the function will be called | 2685 // before; we consider that a hint that the function will be called |
2680 // immediately, and it would be a waste of time to make it lazily | 2686 // immediately, and it would be a waste of time to make it lazily |
2681 // compiled. | 2687 // compiled. |
2682 // These are all things we can know at this point, without looking at the | 2688 // These are all things we can know at this point, without looking at the |
2683 // function itself. | 2689 // function itself. |
2684 | 2690 |
2685 // In addition, we need to distinguish between these cases: | 2691 // We separate between lazy parsing top level functions and lazy parsing inner |
| 2692 // functions, because the latter needs to do more work. In particular, we need |
| 2693 // to track unresolved variables to distinguish between these cases: |
2686 // (function foo() { | 2694 // (function foo() { |
2687 // bar = function() { return 1; } | 2695 // bar = function() { return 1; } |
2688 // })(); | 2696 // })(); |
2689 // and | 2697 // and |
2690 // (function foo() { | 2698 // (function foo() { |
2691 // var a = 1; | 2699 // var a = 1; |
2692 // bar = function() { return a; } | 2700 // bar = function() { return a; } |
2693 // })(); | 2701 // })(); |
2694 | 2702 |
2695 // Now foo will be parsed eagerly and compiled eagerly (optimization: assume | 2703 // Now foo will be parsed eagerly and compiled eagerly (optimization: assume |
2696 // parenthesis before the function means that it will be called | 2704 // parenthesis before the function means that it will be called |
2697 // immediately). The inner function *must* be parsed eagerly to resolve the | 2705 // immediately). bar can be parsed lazily, but we need to parse it in a mode |
2698 // possible reference to the variable in foo's scope. However, it's possible | 2706 // that tracks unresolved variables. |
2699 // that it will be compiled lazily. | 2707 DCHECK_IMPLIES(mode() == PARSE_LAZILY, FLAG_lazy); |
| 2708 DCHECK_IMPLIES(mode() == PARSE_LAZILY, allow_lazy()); |
| 2709 DCHECK_IMPLIES(mode() == PARSE_LAZILY, extension_ == nullptr); |
2700 | 2710 |
2701 // To make this additional case work, both Parser and PreParser implement a | 2711 bool is_lazy_top_level_function = |
2702 // logic where only top-level functions will be parsed lazily. | 2712 mode() == PARSE_LAZILY && |
2703 bool is_lazily_parsed = mode() == PARSE_LAZILY && | 2713 eager_compile_hint == FunctionLiteral::kShouldLazyCompile && |
2704 scope()->AllowsLazyParsing() && | 2714 scope()->AllowsLazyParsingWithoutUnresolvedVariables(); |
2705 !function_state_->next_function_is_parenthesized(); | |
2706 | 2715 |
2707 // Determine whether the function body can be discarded after parsing. | 2716 // Determine whether we can still lazy parse the inner function. |
2708 // The preconditions are: | 2717 // The preconditions are: |
2709 // - Lazy compilation has to be enabled. | 2718 // - Lazy compilation has to be enabled. |
2710 // - Neither V8 natives nor native function declarations can be allowed, | 2719 // - Neither V8 natives nor native function declarations can be allowed, |
2711 // since parsing one would retroactively force the function to be | 2720 // since parsing one would retroactively force the function to be |
2712 // eagerly compiled. | 2721 // eagerly compiled. |
2713 // - The invoker of this parser can't depend on the AST being eagerly | 2722 // - The invoker of this parser can't depend on the AST being eagerly |
2714 // built (either because the function is about to be compiled, or | 2723 // built (either because the function is about to be compiled, or |
2715 // because the AST is going to be inspected for some reason). | 2724 // because the AST is going to be inspected for some reason). |
2716 // - Because of the above, we can't be attempting to parse a | 2725 // - Because of the above, we can't be attempting to parse a |
2717 // FunctionExpression; even without enclosing parentheses it might be | 2726 // FunctionExpression; even without enclosing parentheses it might be |
2718 // immediately invoked. | 2727 // immediately invoked. |
2719 // - The function literal shouldn't be hinted to eagerly compile. | 2728 // - The function literal shouldn't be hinted to eagerly compile. |
2720 // - For asm.js functions the body needs to be available when module | 2729 // - For asm.js functions the body needs to be available when module |
2721 // validation is active, because we examine the entire module at once. | 2730 // validation is active, because we examine the entire module at once. |
2722 bool use_temp_zone = | 2731 |
2723 !is_lazily_parsed && allow_lazy() && | 2732 // Inner functions will be parsed using a temporary Zone. After parsing, we |
| 2733 // will migrate unresolved variable into a Scope in the main Zone. |
| 2734 // TODO(marja): Refactor parsing modes: simplify this. |
| 2735 bool is_lazy_inner_function = |
| 2736 !is_lazy_top_level_function && allow_lazy() && |
2724 function_type == FunctionLiteral::kDeclaration && | 2737 function_type == FunctionLiteral::kDeclaration && |
2725 eager_compile_hint != FunctionLiteral::kShouldEagerCompile && | 2738 eager_compile_hint != FunctionLiteral::kShouldEagerCompile && |
2726 !(FLAG_validate_asm && scope()->IsAsmModule()); | 2739 !(FLAG_validate_asm && scope()->IsAsmModule()); |
2727 | 2740 |
2728 DeclarationScope* main_scope = nullptr; | 2741 DeclarationScope* main_scope = nullptr; |
2729 if (use_temp_zone) { | 2742 if (is_lazy_inner_function) { |
2730 // This Scope lives in the main Zone; we'll migrate data into it later. | 2743 // This Scope lives in the main Zone; we'll migrate data into it later. |
2731 main_scope = NewFunctionScope(kind); | 2744 main_scope = NewFunctionScope(kind); |
2732 } | 2745 } |
2733 | 2746 |
2734 ZoneList<Statement*>* body = nullptr; | 2747 ZoneList<Statement*>* body = nullptr; |
2735 int arity = -1; | 2748 int arity = -1; |
2736 int materialized_literal_count = -1; | 2749 int materialized_literal_count = -1; |
2737 int expected_property_count = -1; | 2750 int expected_property_count = -1; |
2738 DuplicateFinder duplicate_finder(scanner()->unicode_cache()); | 2751 DuplicateFinder duplicate_finder(scanner()->unicode_cache()); |
2739 bool should_be_used_once_hint = false; | 2752 bool should_be_used_once_hint = false; |
2740 bool has_duplicate_parameters; | 2753 bool has_duplicate_parameters; |
2741 | 2754 |
2742 { | 2755 { |
2743 // Temporary zones can nest. When we migrate free variables (see below), we | 2756 // Temporary zones can nest. When we migrate free variables (see below), we |
2744 // need to recreate them in the previous Zone. | 2757 // need to recreate them in the previous Zone. |
2745 AstNodeFactory previous_zone_ast_node_factory(ast_value_factory()); | 2758 AstNodeFactory previous_zone_ast_node_factory(ast_value_factory()); |
2746 previous_zone_ast_node_factory.set_zone(zone()); | 2759 previous_zone_ast_node_factory.set_zone(zone()); |
2747 | 2760 |
2748 // Open a new zone scope, which sets our AstNodeFactory to allocate in the | 2761 // Open a new zone scope, which sets our AstNodeFactory to allocate in the |
2749 // new temporary zone if the preconditions are satisfied, and ensures that | 2762 // new temporary zone if the preconditions are satisfied, and ensures that |
2750 // the previous zone is always restored after parsing the body. To be able | 2763 // the previous zone is always restored after parsing the body. To be able |
2751 // to do scope analysis correctly after full parsing, we migrate needed | 2764 // to do scope analysis correctly after full parsing, we migrate needed |
2752 // information from scope into main_scope when the function has been parsed. | 2765 // information from scope into main_scope when the function has been parsed. |
2753 Zone temp_zone(zone()->allocator()); | 2766 Zone temp_zone(zone()->allocator()); |
2754 DiscardableZoneScope zone_scope(this, &temp_zone, use_temp_zone); | 2767 DiscardableZoneScope zone_scope(this, &temp_zone, is_lazy_inner_function); |
2755 | 2768 |
2756 DeclarationScope* scope = NewFunctionScope(kind); | 2769 DeclarationScope* scope = NewFunctionScope(kind); |
2757 SetLanguageMode(scope, language_mode); | 2770 SetLanguageMode(scope, language_mode); |
2758 if (!use_temp_zone) { | 2771 if (!is_lazy_inner_function) { |
2759 main_scope = scope; | 2772 main_scope = scope; |
2760 } else { | 2773 } else { |
2761 DCHECK(main_scope->zone() != scope->zone()); | 2774 DCHECK(main_scope->zone() != scope->zone()); |
2762 } | 2775 } |
2763 | 2776 |
2764 FunctionState function_state(&function_state_, &scope_state_, scope, kind); | 2777 FunctionState function_state(&function_state_, &scope_state_, scope, kind); |
2765 #ifdef DEBUG | 2778 #ifdef DEBUG |
2766 scope->SetScopeName(function_name); | 2779 scope->SetScopeName(function_name); |
2767 #endif | 2780 #endif |
2768 ExpressionClassifier formals_classifier(this, &duplicate_finder); | 2781 ExpressionClassifier formals_classifier(this, &duplicate_finder); |
(...skipping 23 matching lines...) Expand all Loading... |
2792 int formals_end_position = scanner()->location().end_pos; | 2805 int formals_end_position = scanner()->location().end_pos; |
2793 | 2806 |
2794 CheckArityRestrictions(arity, kind, formals.has_rest, start_position, | 2807 CheckArityRestrictions(arity, kind, formals.has_rest, start_position, |
2795 formals_end_position, CHECK_OK); | 2808 formals_end_position, CHECK_OK); |
2796 Expect(Token::LBRACE, CHECK_OK); | 2809 Expect(Token::LBRACE, CHECK_OK); |
2797 // Don't include the rest parameter into the function's formal parameter | 2810 // Don't include the rest parameter into the function's formal parameter |
2798 // count (esp. the SharedFunctionInfo::internal_formal_parameter_count, | 2811 // count (esp. the SharedFunctionInfo::internal_formal_parameter_count, |
2799 // which says whether we need to create an arguments adaptor frame). | 2812 // which says whether we need to create an arguments adaptor frame). |
2800 if (formals.has_rest) arity--; | 2813 if (formals.has_rest) arity--; |
2801 | 2814 |
2802 // Eager or lazy parse? | 2815 // Eager or lazy parse? If is_lazy_top_level_function, we'll parse |
2803 // If is_lazily_parsed, we'll parse lazily. We'll call SkipLazyFunctionBody, | 2816 // lazily. We'll call SkipLazyFunctionBody, which may decide to abort lazy |
2804 // which may decide to abort lazy parsing if it suspects that wasn't a good | 2817 // parsing if it suspects that wasn't a good idea. If so (in which case the |
2805 // idea. If so (in which case the parser is expected to have backtracked), | 2818 // parser is expected to have backtracked), or if we didn't try to lazy |
2806 // or if we didn't try to lazy parse in the first place, we'll have to parse | 2819 // parse in the first place, we'll have to parse eagerly. |
2807 // eagerly. | 2820 if (is_lazy_top_level_function) { |
2808 if (is_lazily_parsed) { | |
2809 Scanner::BookmarkScope bookmark(scanner()); | 2821 Scanner::BookmarkScope bookmark(scanner()); |
2810 bookmark.Set(); | 2822 bookmark.Set(); |
2811 LazyParsingResult result = | 2823 LazyParsingResult result = |
2812 SkipLazyFunctionBody(&materialized_literal_count, | 2824 SkipLazyFunctionBody(&materialized_literal_count, |
2813 &expected_property_count, true, CHECK_OK); | 2825 &expected_property_count, false, true, CHECK_OK); |
2814 | 2826 |
2815 materialized_literal_count += formals.materialized_literals_count + | 2827 materialized_literal_count += formals.materialized_literals_count + |
2816 function_state.materialized_literal_count(); | 2828 function_state.materialized_literal_count(); |
2817 | 2829 |
2818 if (result == kLazyParsingAborted) { | 2830 if (result == kLazyParsingAborted) { |
2819 bookmark.Apply(); | 2831 bookmark.Apply(); |
2820 // Trigger eager (re-)parsing, just below this block. | 2832 // Trigger eager (re-)parsing, just below this block. |
2821 is_lazily_parsed = false; | 2833 is_lazy_top_level_function = false; |
2822 | 2834 |
2823 // This is probably an initialization function. Inform the compiler it | 2835 // This is probably an initialization function. Inform the compiler it |
2824 // should also eager-compile this function, and that we expect it to be | 2836 // should also eager-compile this function, and that we expect it to be |
2825 // used once. | 2837 // used once. |
2826 eager_compile_hint = FunctionLiteral::kShouldEagerCompile; | 2838 eager_compile_hint = FunctionLiteral::kShouldEagerCompile; |
2827 should_be_used_once_hint = true; | 2839 should_be_used_once_hint = true; |
2828 } | 2840 } |
2829 } | 2841 } |
2830 if (!is_lazily_parsed) { | 2842 if (is_lazy_inner_function) { |
| 2843 if (FLAG_lazy_inner_functions) { |
| 2844 LazyParsingResult result = SkipLazyFunctionBody( |
| 2845 &materialized_literal_count, &expected_property_count, true, false, |
| 2846 CHECK_OK); |
| 2847 materialized_literal_count += |
| 2848 formals.materialized_literals_count + |
| 2849 function_state.materialized_literal_count(); |
| 2850 DCHECK(result != kLazyParsingAborted); |
| 2851 USE(result); |
| 2852 } else { |
| 2853 ParseEagerFunctionBody(function_name, pos, formals, kind, function_type, |
| 2854 CHECK_OK); |
| 2855 materialized_literal_count = |
| 2856 function_state.materialized_literal_count(); |
| 2857 expected_property_count = function_state.expected_property_count(); |
| 2858 } |
| 2859 } else if (!is_lazy_top_level_function) { |
2831 body = ParseEagerFunctionBody(function_name, pos, formals, kind, | 2860 body = ParseEagerFunctionBody(function_name, pos, formals, kind, |
2832 function_type, CHECK_OK); | 2861 function_type, CHECK_OK); |
2833 | 2862 |
2834 materialized_literal_count = function_state.materialized_literal_count(); | 2863 materialized_literal_count = function_state.materialized_literal_count(); |
2835 expected_property_count = function_state.expected_property_count(); | 2864 expected_property_count = function_state.expected_property_count(); |
2836 if (use_temp_zone) { | |
2837 // If the preconditions are correct the function body should never be | |
2838 // accessed, but do this anyway for better behaviour if they're wrong. | |
2839 body = nullptr; | |
2840 } | |
2841 } | 2865 } |
2842 | 2866 |
2843 // Parsing the body may change the language mode in our scope. | 2867 // Parsing the body may change the language mode in our scope. |
2844 language_mode = scope->language_mode(); | 2868 language_mode = scope->language_mode(); |
2845 | 2869 |
2846 // Validate name and parameter names. We can do this only after parsing the | 2870 // Validate name and parameter names. We can do this only after parsing the |
2847 // function, since the function can declare itself strict. | 2871 // function, since the function can declare itself strict. |
2848 CheckFunctionName(language_mode, function_name, function_name_validity, | 2872 CheckFunctionName(language_mode, function_name, function_name_validity, |
2849 function_name_location, CHECK_OK); | 2873 function_name_location, CHECK_OK); |
2850 const bool allow_duplicate_parameters = | 2874 const bool allow_duplicate_parameters = |
2851 is_sloppy(language_mode) && formals.is_simple && !IsConciseMethod(kind); | 2875 is_sloppy(language_mode) && formals.is_simple && !IsConciseMethod(kind); |
2852 ValidateFormalParameters(language_mode, allow_duplicate_parameters, | 2876 ValidateFormalParameters(language_mode, allow_duplicate_parameters, |
2853 CHECK_OK); | 2877 CHECK_OK); |
2854 | 2878 |
2855 if (is_strict(language_mode)) { | 2879 if (is_strict(language_mode)) { |
2856 CheckStrictOctalLiteral(scope->start_position(), scope->end_position(), | 2880 CheckStrictOctalLiteral(scope->start_position(), scope->end_position(), |
2857 CHECK_OK); | 2881 CHECK_OK); |
2858 CheckDecimalLiteralWithLeadingZero(scope->start_position(), | 2882 CheckDecimalLiteralWithLeadingZero(scope->start_position(), |
2859 scope->end_position()); | 2883 scope->end_position()); |
2860 } | 2884 } |
2861 CheckConflictingVarDeclarations(scope, CHECK_OK); | 2885 CheckConflictingVarDeclarations(scope, CHECK_OK); |
2862 | 2886 |
2863 if (body) { | 2887 if (body) { |
2864 // If body can be inspected, rewrite queued destructuring assignments | 2888 // If body can be inspected, rewrite queued destructuring assignments |
2865 RewriteDestructuringAssignments(); | 2889 RewriteDestructuringAssignments(); |
2866 } | 2890 } |
2867 has_duplicate_parameters = | 2891 has_duplicate_parameters = |
2868 !classifier()->is_valid_formal_parameter_list_without_duplicates(); | 2892 !classifier()->is_valid_formal_parameter_list_without_duplicates(); |
2869 | 2893 |
2870 if (use_temp_zone) { | 2894 if (is_lazy_inner_function) { |
2871 DCHECK(main_scope != scope); | 2895 DCHECK(main_scope != scope); |
2872 scope->AnalyzePartially(main_scope, &previous_zone_ast_node_factory); | 2896 scope->AnalyzePartially(main_scope, &previous_zone_ast_node_factory); |
2873 } | 2897 } |
2874 } // DiscardableZoneScope goes out of scope. | 2898 } // DiscardableZoneScope goes out of scope. |
2875 | 2899 |
2876 FunctionLiteral::ParameterFlag duplicate_parameters = | 2900 FunctionLiteral::ParameterFlag duplicate_parameters = |
2877 has_duplicate_parameters ? FunctionLiteral::kHasDuplicateParameters | 2901 has_duplicate_parameters ? FunctionLiteral::kHasDuplicateParameters |
2878 : FunctionLiteral::kNoDuplicateParameters; | 2902 : FunctionLiteral::kNoDuplicateParameters; |
2879 | 2903 |
2880 // Note that the FunctionLiteral needs to be created in the main Zone again. | 2904 // Note that the FunctionLiteral needs to be created in the main Zone again. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2914 } | 2938 } |
2915 return ParseFunctionLiteral(name, scanner()->location(), | 2939 return ParseFunctionLiteral(name, scanner()->location(), |
2916 is_strict_reserved ? kFunctionNameIsStrictReserved | 2940 is_strict_reserved ? kFunctionNameIsStrictReserved |
2917 : kFunctionNameValidityUnknown, | 2941 : kFunctionNameValidityUnknown, |
2918 FunctionKind::kAsyncFunction, pos, type, | 2942 FunctionKind::kAsyncFunction, pos, type, |
2919 language_mode(), CHECK_OK); | 2943 language_mode(), CHECK_OK); |
2920 } | 2944 } |
2921 | 2945 |
2922 Parser::LazyParsingResult Parser::SkipLazyFunctionBody( | 2946 Parser::LazyParsingResult Parser::SkipLazyFunctionBody( |
2923 int* materialized_literal_count, int* expected_property_count, | 2947 int* materialized_literal_count, int* expected_property_count, |
2924 bool may_abort, bool* ok) { | 2948 bool is_inner_function, bool may_abort, bool* ok) { |
2925 if (produce_cached_parse_data()) CHECK(log_); | 2949 if (produce_cached_parse_data()) CHECK(log_); |
2926 | 2950 |
2927 int function_block_pos = position(); | 2951 int function_block_pos = position(); |
2928 DeclarationScope* scope = this->scope()->AsDeclarationScope(); | 2952 DeclarationScope* scope = this->scope()->AsDeclarationScope(); |
2929 DCHECK(scope->is_function_scope()); | 2953 DCHECK(scope->is_function_scope()); |
2930 scope->set_is_lazily_parsed(true); | 2954 scope->set_is_lazily_parsed(true); |
2931 if (consume_cached_parse_data() && !cached_parse_data_->rejected()) { | 2955 // Inner functions are not part of the cached data. |
| 2956 if (!is_inner_function && consume_cached_parse_data() && |
| 2957 !cached_parse_data_->rejected()) { |
2932 // If we have cached data, we use it to skip parsing the function body. The | 2958 // If we have cached data, we use it to skip parsing the function body. The |
2933 // data contains the information we need to construct the lazy function. | 2959 // data contains the information we need to construct the lazy function. |
2934 FunctionEntry entry = | 2960 FunctionEntry entry = |
2935 cached_parse_data_->GetFunctionEntry(function_block_pos); | 2961 cached_parse_data_->GetFunctionEntry(function_block_pos); |
2936 // Check that cached data is valid. If not, mark it as invalid (the embedder | 2962 // Check that cached data is valid. If not, mark it as invalid (the embedder |
2937 // handles it). Note that end position greater than end of stream is safe, | 2963 // handles it). Note that end position greater than end of stream is safe, |
2938 // and hard to check. | 2964 // and hard to check. |
2939 if (entry.is_valid() && entry.end_pos() > function_block_pos) { | 2965 if (entry.is_valid() && entry.end_pos() > function_block_pos) { |
2940 scanner()->SeekForward(entry.end_pos() - 1); | 2966 scanner()->SeekForward(entry.end_pos() - 1); |
2941 | 2967 |
2942 scope->set_end_position(entry.end_pos()); | 2968 scope->set_end_position(entry.end_pos()); |
2943 Expect(Token::RBRACE, CHECK_OK_VALUE(kLazyParsingComplete)); | 2969 Expect(Token::RBRACE, CHECK_OK_VALUE(kLazyParsingComplete)); |
2944 total_preparse_skipped_ += scope->end_position() - function_block_pos; | 2970 total_preparse_skipped_ += scope->end_position() - function_block_pos; |
2945 *materialized_literal_count = entry.literal_count(); | 2971 *materialized_literal_count = entry.literal_count(); |
2946 *expected_property_count = entry.property_count(); | 2972 *expected_property_count = entry.property_count(); |
2947 SetLanguageMode(scope, entry.language_mode()); | 2973 SetLanguageMode(scope, entry.language_mode()); |
2948 if (entry.uses_super_property()) scope->RecordSuperPropertyUsage(); | 2974 if (entry.uses_super_property()) scope->RecordSuperPropertyUsage(); |
2949 if (entry.calls_eval()) scope->RecordEvalCall(); | 2975 if (entry.calls_eval()) scope->RecordEvalCall(); |
2950 return kLazyParsingComplete; | 2976 return kLazyParsingComplete; |
2951 } | 2977 } |
2952 cached_parse_data_->Reject(); | 2978 cached_parse_data_->Reject(); |
2953 } | 2979 } |
2954 // With no cached data, we partially parse the function, without building an | 2980 // With no cached data, we partially parse the function, without building an |
2955 // AST. This gathers the data needed to build a lazy function. | 2981 // AST. This gathers the data needed to build a lazy function. |
2956 SingletonLogger logger; | 2982 SingletonLogger logger; |
2957 PreParser::PreParseResult result = | 2983 PreParser::PreParseResult result = |
2958 ParseLazyFunctionBodyWithPreParser(&logger, may_abort); | 2984 ParseLazyFunctionBodyWithPreParser(&logger, is_inner_function, may_abort); |
2959 // Return immediately if pre-parser decided to abort parsing. | 2985 // Return immediately if pre-parser decided to abort parsing. |
2960 if (result == PreParser::kPreParseAbort) { | 2986 if (result == PreParser::kPreParseAbort) { |
2961 scope->set_is_lazily_parsed(false); | 2987 scope->set_is_lazily_parsed(false); |
2962 return kLazyParsingAborted; | 2988 return kLazyParsingAborted; |
2963 } | 2989 } |
2964 if (result == PreParser::kPreParseStackOverflow) { | 2990 if (result == PreParser::kPreParseStackOverflow) { |
2965 // Propagate stack overflow. | 2991 // Propagate stack overflow. |
2966 set_stack_overflow(); | 2992 set_stack_overflow(); |
2967 *ok = false; | 2993 *ok = false; |
2968 return kLazyParsingComplete; | 2994 return kLazyParsingComplete; |
2969 } | 2995 } |
2970 if (logger.has_error()) { | 2996 if (logger.has_error()) { |
2971 ReportMessageAt(Scanner::Location(logger.start(), logger.end()), | 2997 ReportMessageAt(Scanner::Location(logger.start(), logger.end()), |
2972 logger.message(), logger.argument_opt(), | 2998 logger.message(), logger.argument_opt(), |
2973 logger.error_type()); | 2999 logger.error_type()); |
2974 *ok = false; | 3000 *ok = false; |
2975 return kLazyParsingComplete; | 3001 return kLazyParsingComplete; |
2976 } | 3002 } |
2977 scope->set_end_position(logger.end()); | 3003 scope->set_end_position(logger.end()); |
2978 Expect(Token::RBRACE, CHECK_OK_VALUE(kLazyParsingComplete)); | 3004 Expect(Token::RBRACE, CHECK_OK_VALUE(kLazyParsingComplete)); |
2979 total_preparse_skipped_ += scope->end_position() - function_block_pos; | 3005 total_preparse_skipped_ += scope->end_position() - function_block_pos; |
2980 *materialized_literal_count = logger.literals(); | 3006 *materialized_literal_count = logger.literals(); |
2981 *expected_property_count = logger.properties(); | 3007 *expected_property_count = logger.properties(); |
2982 SetLanguageMode(scope, logger.language_mode()); | 3008 SetLanguageMode(scope, logger.language_mode()); |
2983 if (logger.uses_super_property()) scope->RecordSuperPropertyUsage(); | 3009 if (logger.uses_super_property()) scope->RecordSuperPropertyUsage(); |
2984 if (logger.calls_eval()) scope->RecordEvalCall(); | 3010 if (logger.calls_eval()) scope->RecordEvalCall(); |
2985 if (produce_cached_parse_data()) { | 3011 if (!is_inner_function && produce_cached_parse_data()) { |
2986 DCHECK(log_); | 3012 DCHECK(log_); |
2987 // Position right after terminal '}'. | 3013 // Position right after terminal '}'. |
2988 int body_end = scanner()->location().end_pos; | 3014 int body_end = scanner()->location().end_pos; |
2989 log_->LogFunction(function_block_pos, body_end, *materialized_literal_count, | 3015 log_->LogFunction(function_block_pos, body_end, *materialized_literal_count, |
2990 *expected_property_count, language_mode(), | 3016 *expected_property_count, language_mode(), |
2991 scope->uses_super_property(), scope->calls_eval()); | 3017 scope->uses_super_property(), scope->calls_eval()); |
2992 } | 3018 } |
2993 return kLazyParsingComplete; | 3019 return kLazyParsingComplete; |
2994 } | 3020 } |
2995 | 3021 |
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3282 promise = scope()->NewTemporary(ast_value_factory()->empty_string()); | 3308 promise = scope()->NewTemporary(ast_value_factory()->empty_string()); |
3283 function_state_->set_promise_variable(promise); | 3309 function_state_->set_promise_variable(promise); |
3284 } | 3310 } |
3285 return promise; | 3311 return promise; |
3286 } | 3312 } |
3287 | 3313 |
3288 ZoneList<Statement*>* Parser::ParseEagerFunctionBody( | 3314 ZoneList<Statement*>* Parser::ParseEagerFunctionBody( |
3289 const AstRawString* function_name, int pos, | 3315 const AstRawString* function_name, int pos, |
3290 const ParserFormalParameters& parameters, FunctionKind kind, | 3316 const ParserFormalParameters& parameters, FunctionKind kind, |
3291 FunctionLiteral::FunctionType function_type, bool* ok) { | 3317 FunctionLiteral::FunctionType function_type, bool* ok) { |
3292 // Everything inside an eagerly parsed function will be parsed eagerly | 3318 // Everything inside an eagerly parsed function will be parsed eagerly (see |
3293 // (see comment above). | 3319 // comment above). Lazy inner functions are handled separately and they won't |
| 3320 // require the mode to be PARSE_LAZILY (see ParseFunctionLiteral). |
| 3321 // TODO(marja): Refactor parsing modes: remove this. |
3294 ParsingModeScope parsing_mode(this, PARSE_EAGERLY); | 3322 ParsingModeScope parsing_mode(this, PARSE_EAGERLY); |
3295 ZoneList<Statement*>* result = new(zone()) ZoneList<Statement*>(8, zone()); | 3323 ZoneList<Statement*>* result = new(zone()) ZoneList<Statement*>(8, zone()); |
3296 | 3324 |
3297 static const int kFunctionNameAssignmentIndex = 0; | 3325 static const int kFunctionNameAssignmentIndex = 0; |
3298 if (function_type == FunctionLiteral::kNamedExpression) { | 3326 if (function_type == FunctionLiteral::kNamedExpression) { |
3299 DCHECK(function_name != NULL); | 3327 DCHECK(function_name != NULL); |
3300 // If we have a named function expression, we add a local variable | 3328 // If we have a named function expression, we add a local variable |
3301 // declaration to the body of the function with the name of the | 3329 // declaration to the body of the function with the name of the |
3302 // function and let it refer to the function itself (closure). | 3330 // function and let it refer to the function itself (closure). |
3303 // Not having parsed the function body, the language mode may still change, | 3331 // Not having parsed the function body, the language mode may still change, |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3454 statement = factory()->NewEmptyStatement(kNoSourcePosition); | 3482 statement = factory()->NewEmptyStatement(kNoSourcePosition); |
3455 } | 3483 } |
3456 result->Set(kFunctionNameAssignmentIndex, statement); | 3484 result->Set(kFunctionNameAssignmentIndex, statement); |
3457 } | 3485 } |
3458 | 3486 |
3459 MarkCollectedTailCallExpressions(); | 3487 MarkCollectedTailCallExpressions(); |
3460 return result; | 3488 return result; |
3461 } | 3489 } |
3462 | 3490 |
3463 PreParser::PreParseResult Parser::ParseLazyFunctionBodyWithPreParser( | 3491 PreParser::PreParseResult Parser::ParseLazyFunctionBodyWithPreParser( |
3464 SingletonLogger* logger, bool may_abort) { | 3492 SingletonLogger* logger, bool is_inner_function, bool may_abort) { |
3465 // This function may be called on a background thread too; record only the | 3493 // This function may be called on a background thread too; record only the |
3466 // main thread preparse times. | 3494 // main thread preparse times. |
3467 if (pre_parse_timer_ != NULL) { | 3495 if (pre_parse_timer_ != NULL) { |
3468 pre_parse_timer_->Start(); | 3496 pre_parse_timer_->Start(); |
3469 } | 3497 } |
3470 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"), "V8.PreParse"); | 3498 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"), "V8.PreParse"); |
3471 | 3499 |
3472 DCHECK_EQ(Token::LBRACE, scanner()->current_token()); | 3500 DCHECK_EQ(Token::LBRACE, scanner()->current_token()); |
3473 | 3501 |
3474 if (reusable_preparser_ == NULL) { | 3502 if (reusable_preparser_ == NULL) { |
3475 reusable_preparser_ = new PreParser(zone(), &scanner_, ast_value_factory(), | 3503 reusable_preparser_ = new PreParser(zone(), &scanner_, ast_value_factory(), |
3476 NULL, stack_limit_); | 3504 NULL, stack_limit_); |
3477 reusable_preparser_->set_allow_lazy(true); | 3505 reusable_preparser_->set_allow_lazy(true); |
3478 #define SET_ALLOW(name) reusable_preparser_->set_allow_##name(allow_##name()); | 3506 #define SET_ALLOW(name) reusable_preparser_->set_allow_##name(allow_##name()); |
3479 SET_ALLOW(natives); | 3507 SET_ALLOW(natives); |
3480 SET_ALLOW(harmony_do_expressions); | 3508 SET_ALLOW(harmony_do_expressions); |
3481 SET_ALLOW(harmony_for_in); | 3509 SET_ALLOW(harmony_for_in); |
3482 SET_ALLOW(harmony_function_sent); | 3510 SET_ALLOW(harmony_function_sent); |
3483 SET_ALLOW(harmony_restrictive_declarations); | 3511 SET_ALLOW(harmony_restrictive_declarations); |
3484 SET_ALLOW(harmony_async_await); | 3512 SET_ALLOW(harmony_async_await); |
3485 SET_ALLOW(harmony_trailing_commas); | 3513 SET_ALLOW(harmony_trailing_commas); |
3486 SET_ALLOW(harmony_class_fields); | 3514 SET_ALLOW(harmony_class_fields); |
3487 #undef SET_ALLOW | 3515 #undef SET_ALLOW |
3488 } | 3516 } |
3489 PreParser::PreParseResult result = reusable_preparser_->PreParseLazyFunction( | 3517 // Aborting inner function preparsing would leave scopes in an inconsistent |
3490 language_mode(), function_state_->kind(), | 3518 // state; we don't parse inner functions in the abortable mode anyway. |
3491 scope()->AsDeclarationScope()->has_simple_parameters(), parsing_module_, | 3519 DCHECK(!is_inner_function || !may_abort); |
3492 logger, may_abort, use_counts_); | 3520 |
| 3521 FunctionKind kind = function_state_->kind(); |
| 3522 PreParser::PreParseResult result; |
| 3523 if (!is_inner_function) { |
| 3524 // If we don't need to look at the scope, construct a dummy scope chain |
| 3525 // which is not connected to the real scope chain. |
| 3526 LanguageMode mode = language_mode(); |
| 3527 bool has_simple_parameters = |
| 3528 scope()->AsDeclarationScope()->has_simple_parameters(); |
| 3529 DeclarationScope* top_scope = NewScriptScope(); |
| 3530 top_scope->SetLanguageMode(mode); |
| 3531 FunctionState top_state(&function_state_, &scope_state_, top_scope, |
| 3532 kNormalFunction); |
| 3533 DeclarationScope* function_scope = NewFunctionScope(kind); |
| 3534 if (!has_simple_parameters) { |
| 3535 function_scope->SetHasNonSimpleParameters(); |
| 3536 } |
| 3537 result = reusable_preparser_->PreParseLazyFunction( |
| 3538 kind, function_scope, parsing_module_, logger, is_inner_function, |
| 3539 may_abort, use_counts_); |
| 3540 } else { |
| 3541 // Detaching the scopes created by PreParser from the Scope chain must be |
| 3542 // done above (see ParseFunctionLiteral & AnalyzePartially). |
| 3543 result = reusable_preparser_->PreParseLazyFunction( |
| 3544 kind, scope()->AsDeclarationScope(), parsing_module_, logger, |
| 3545 is_inner_function, may_abort, use_counts_); |
| 3546 } |
3493 if (pre_parse_timer_ != NULL) { | 3547 if (pre_parse_timer_ != NULL) { |
3494 pre_parse_timer_->Stop(); | 3548 pre_parse_timer_->Stop(); |
3495 } | 3549 } |
3496 return result; | 3550 return result; |
3497 } | 3551 } |
3498 | 3552 |
3499 Expression* Parser::InstallHomeObject(Expression* function_literal, | 3553 Expression* Parser::InstallHomeObject(Expression* function_literal, |
3500 Expression* home_object) { | 3554 Expression* home_object) { |
3501 Block* do_block = factory()->NewBlock(nullptr, 1, false, kNoSourcePosition); | 3555 Block* do_block = factory()->NewBlock(nullptr, 1, false, kNoSourcePosition); |
3502 Variable* result_var = | 3556 Variable* result_var = |
(...skipping 2103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5606 | 5660 |
5607 return final_loop; | 5661 return final_loop; |
5608 } | 5662 } |
5609 | 5663 |
5610 #undef CHECK_OK | 5664 #undef CHECK_OK |
5611 #undef CHECK_OK_VOID | 5665 #undef CHECK_OK_VOID |
5612 #undef CHECK_FAILED | 5666 #undef CHECK_FAILED |
5613 | 5667 |
5614 } // namespace internal | 5668 } // namespace internal |
5615 } // namespace v8 | 5669 } // namespace v8 |
OLD | NEW |