| 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/api.h" | 7 #include "src/api.h" |
| 8 #include "src/ast.h" | 8 #include "src/ast.h" |
| 9 #include "src/base/platform/platform.h" | 9 #include "src/base/platform/platform.h" |
| 10 #include "src/bootstrapper.h" | 10 #include "src/bootstrapper.h" |
| (...skipping 678 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 689 return factory->NewTheHoleLiteral(RelocInfo::kNoPosition); | 689 return factory->NewTheHoleLiteral(RelocInfo::kNoPosition); |
| 690 } | 690 } |
| 691 | 691 |
| 692 | 692 |
| 693 Expression* ParserTraits::ParseV8Intrinsic(bool* ok) { | 693 Expression* ParserTraits::ParseV8Intrinsic(bool* ok) { |
| 694 return parser_->ParseV8Intrinsic(ok); | 694 return parser_->ParseV8Intrinsic(ok); |
| 695 } | 695 } |
| 696 | 696 |
| 697 | 697 |
| 698 FunctionLiteral* ParserTraits::ParseFunctionLiteral( | 698 FunctionLiteral* ParserTraits::ParseFunctionLiteral( |
| 699 const AstRawString* name, | 699 const AstRawString* name, Scanner::Location function_name_location, |
| 700 Scanner::Location function_name_location, | 700 bool name_is_strict_reserved, FunctionLiteral::IsGeneratorFlag is_generator, |
| 701 bool name_is_strict_reserved, | 701 FunctionLiteral::IsArrowFlag is_arrow, |
| 702 bool is_generator, | 702 FunctionLiteral::IsConciseMethodFlag is_concise_method, |
| 703 int function_token_position, | 703 int function_token_position, FunctionLiteral::FunctionType type, |
| 704 FunctionLiteral::FunctionType type, | 704 FunctionLiteral::ArityRestriction arity_restriction, bool* ok) { |
| 705 FunctionLiteral::ArityRestriction arity_restriction, | 705 return parser_->ParseFunctionLiteral( |
| 706 bool* ok) { | 706 name, function_name_location, name_is_strict_reserved, is_generator, |
| 707 return parser_->ParseFunctionLiteral(name, function_name_location, | 707 is_arrow, is_concise_method, function_token_position, type, |
| 708 name_is_strict_reserved, is_generator, | 708 arity_restriction, ok); |
| 709 function_token_position, type, | |
| 710 arity_restriction, ok); | |
| 711 } | 709 } |
| 712 | 710 |
| 713 | 711 |
| 714 Parser::Parser(CompilationInfo* info) | 712 Parser::Parser(CompilationInfo* info) |
| 715 : ParserBase<ParserTraits>(&scanner_, | 713 : ParserBase<ParserTraits>(&scanner_, |
| 716 info->isolate()->stack_guard()->real_climit(), | 714 info->isolate()->stack_guard()->real_climit(), |
| 717 info->extension(), NULL, info->zone(), this), | 715 info->extension(), NULL, info->zone(), this), |
| 718 isolate_(info->isolate()), | 716 isolate_(info->isolate()), |
| 719 script_(info->script()), | 717 script_(info->script()), |
| 720 scanner_(isolate_->unicode_cache()), | 718 scanner_(isolate_->unicode_cache()), |
| (...skipping 10 matching lines...) Expand all Loading... |
| 731 DCHECK(!script_.is_null()); | 729 DCHECK(!script_.is_null()); |
| 732 isolate_->set_ast_node_id(0); | 730 isolate_->set_ast_node_id(0); |
| 733 set_allow_harmony_scoping(!info->is_native() && FLAG_harmony_scoping); | 731 set_allow_harmony_scoping(!info->is_native() && FLAG_harmony_scoping); |
| 734 set_allow_modules(!info->is_native() && FLAG_harmony_modules); | 732 set_allow_modules(!info->is_native() && FLAG_harmony_modules); |
| 735 set_allow_natives_syntax(FLAG_allow_natives_syntax || info->is_native()); | 733 set_allow_natives_syntax(FLAG_allow_natives_syntax || info->is_native()); |
| 736 set_allow_lazy(false); // Must be explicitly enabled. | 734 set_allow_lazy(false); // Must be explicitly enabled. |
| 737 set_allow_generators(FLAG_harmony_generators); | 735 set_allow_generators(FLAG_harmony_generators); |
| 738 set_allow_arrow_functions(FLAG_harmony_arrow_functions); | 736 set_allow_arrow_functions(FLAG_harmony_arrow_functions); |
| 739 set_allow_harmony_numeric_literals(FLAG_harmony_numeric_literals); | 737 set_allow_harmony_numeric_literals(FLAG_harmony_numeric_literals); |
| 740 set_allow_classes(FLAG_harmony_classes); | 738 set_allow_classes(FLAG_harmony_classes); |
| 739 set_allow_harmony_object_literals(FLAG_harmony_object_literals); |
| 741 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount; | 740 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount; |
| 742 ++feature) { | 741 ++feature) { |
| 743 use_counts_[feature] = 0; | 742 use_counts_[feature] = 0; |
| 744 } | 743 } |
| 745 } | 744 } |
| 746 | 745 |
| 747 | 746 |
| 748 FunctionLiteral* Parser::ParseProgram() { | 747 FunctionLiteral* Parser::ParseProgram() { |
| 749 // TODO(bmeurer): We temporarily need to pass allow_nesting = true here, | 748 // TODO(bmeurer): We temporarily need to pass allow_nesting = true here, |
| 750 // see comment for HistogramTimerScope class. | 749 // see comment for HistogramTimerScope class. |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 871 | 870 |
| 872 ast_value_factory_->Internalize(isolate()); | 871 ast_value_factory_->Internalize(isolate()); |
| 873 if (ok) { | 872 if (ok) { |
| 874 result = factory()->NewFunctionLiteral( | 873 result = factory()->NewFunctionLiteral( |
| 875 ast_value_factory_->empty_string(), ast_value_factory_, scope_, body, | 874 ast_value_factory_->empty_string(), ast_value_factory_, scope_, body, |
| 876 function_state.materialized_literal_count(), | 875 function_state.materialized_literal_count(), |
| 877 function_state.expected_property_count(), | 876 function_state.expected_property_count(), |
| 878 function_state.handler_count(), 0, | 877 function_state.handler_count(), 0, |
| 879 FunctionLiteral::kNoDuplicateParameters, | 878 FunctionLiteral::kNoDuplicateParameters, |
| 880 FunctionLiteral::ANONYMOUS_EXPRESSION, FunctionLiteral::kGlobalOrEval, | 879 FunctionLiteral::ANONYMOUS_EXPRESSION, FunctionLiteral::kGlobalOrEval, |
| 881 FunctionLiteral::kNotParenthesized, FunctionLiteral::kNormalFunction, | 880 FunctionLiteral::kNotParenthesized, FunctionLiteral::kNotGenerator, |
| 882 0); | 881 FunctionLiteral::kNotArrow, FunctionLiteral::kNotConciseMethod, 0); |
| 883 result->set_ast_properties(factory()->visitor()->ast_properties()); | 882 result->set_ast_properties(factory()->visitor()->ast_properties()); |
| 884 result->set_dont_optimize_reason( | 883 result->set_dont_optimize_reason( |
| 885 factory()->visitor()->dont_optimize_reason()); | 884 factory()->visitor()->dont_optimize_reason()); |
| 886 } else if (stack_overflow()) { | 885 } else if (stack_overflow()) { |
| 887 isolate()->StackOverflow(); | 886 isolate()->StackOverflow(); |
| 888 } else { | 887 } else { |
| 889 ThrowPendingError(); | 888 ThrowPendingError(); |
| 890 } | 889 } |
| 891 } | 890 } |
| 892 | 891 |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 961 FunctionState function_state(&function_state_, &scope_, scope, zone(), | 960 FunctionState function_state(&function_state_, &scope_, scope, zone(), |
| 962 ast_value_factory_); | 961 ast_value_factory_); |
| 963 DCHECK(scope->strict_mode() == SLOPPY || info()->strict_mode() == STRICT); | 962 DCHECK(scope->strict_mode() == SLOPPY || info()->strict_mode() == STRICT); |
| 964 DCHECK(info()->strict_mode() == shared_info->strict_mode()); | 963 DCHECK(info()->strict_mode() == shared_info->strict_mode()); |
| 965 scope->SetStrictMode(shared_info->strict_mode()); | 964 scope->SetStrictMode(shared_info->strict_mode()); |
| 966 FunctionLiteral::FunctionType function_type = shared_info->is_expression() | 965 FunctionLiteral::FunctionType function_type = shared_info->is_expression() |
| 967 ? (shared_info->is_anonymous() | 966 ? (shared_info->is_anonymous() |
| 968 ? FunctionLiteral::ANONYMOUS_EXPRESSION | 967 ? FunctionLiteral::ANONYMOUS_EXPRESSION |
| 969 : FunctionLiteral::NAMED_EXPRESSION) | 968 : FunctionLiteral::NAMED_EXPRESSION) |
| 970 : FunctionLiteral::DECLARATION; | 969 : FunctionLiteral::DECLARATION; |
| 971 bool is_generator = shared_info->is_generator(); | 970 |
| 972 bool ok = true; | 971 bool ok = true; |
| 973 | 972 |
| 974 if (shared_info->is_arrow()) { | 973 if (shared_info->is_arrow()) { |
| 975 DCHECK(!is_generator); | 974 DCHECK(!shared_info->is_generator()); |
| 976 Expression* expression = ParseExpression(false, &ok); | 975 Expression* expression = ParseExpression(false, &ok); |
| 977 DCHECK(expression->IsFunctionLiteral()); | 976 DCHECK(expression->IsFunctionLiteral()); |
| 978 result = expression->AsFunctionLiteral(); | 977 result = expression->AsFunctionLiteral(); |
| 979 } else { | 978 } else { |
| 980 result = ParseFunctionLiteral(raw_name, Scanner::Location::invalid(), | 979 result = ParseFunctionLiteral( |
| 981 false, // Strict mode name already checked. | 980 raw_name, Scanner::Location::invalid(), |
| 982 is_generator, RelocInfo::kNoPosition, | 981 false, // Strict mode name already checked. |
| 983 function_type, | 982 shared_info->is_generator() ? FunctionLiteral::kIsGenerator |
| 984 FunctionLiteral::NORMAL_ARITY, &ok); | 983 : FunctionLiteral::kNotGenerator, |
| 984 FunctionLiteral::kNotArrow, |
| 985 shared_info->is_concise_method() ? FunctionLiteral::kIsConciseMethod |
| 986 : FunctionLiteral::kNotConciseMethod, |
| 987 RelocInfo::kNoPosition, function_type, FunctionLiteral::NORMAL_ARITY, |
| 988 &ok); |
| 985 } | 989 } |
| 986 // Make sure the results agree. | 990 // Make sure the results agree. |
| 987 DCHECK(ok == (result != NULL)); | 991 DCHECK(ok == (result != NULL)); |
| 988 } | 992 } |
| 989 | 993 |
| 990 // Make sure the target stack is empty. | 994 // Make sure the target stack is empty. |
| 991 DCHECK(target_stack_ == NULL); | 995 DCHECK(target_stack_ == NULL); |
| 992 | 996 |
| 993 ast_value_factory_->Internalize(isolate()); | 997 ast_value_factory_->Internalize(isolate()); |
| 994 if (result == NULL) { | 998 if (result == NULL) { |
| (...skipping 868 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1863 // 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}' | 1867 // 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}' |
| 1864 // GeneratorDeclaration :: | 1868 // GeneratorDeclaration :: |
| 1865 // 'function' '*' Identifier '(' FormalParameterListopt ')' | 1869 // 'function' '*' Identifier '(' FormalParameterListopt ')' |
| 1866 // '{' FunctionBody '}' | 1870 // '{' FunctionBody '}' |
| 1867 Expect(Token::FUNCTION, CHECK_OK); | 1871 Expect(Token::FUNCTION, CHECK_OK); |
| 1868 int pos = position(); | 1872 int pos = position(); |
| 1869 bool is_generator = allow_generators() && Check(Token::MUL); | 1873 bool is_generator = allow_generators() && Check(Token::MUL); |
| 1870 bool is_strict_reserved = false; | 1874 bool is_strict_reserved = false; |
| 1871 const AstRawString* name = ParseIdentifierOrStrictReservedWord( | 1875 const AstRawString* name = ParseIdentifierOrStrictReservedWord( |
| 1872 &is_strict_reserved, CHECK_OK); | 1876 &is_strict_reserved, CHECK_OK); |
| 1873 FunctionLiteral* fun = ParseFunctionLiteral(name, | 1877 FunctionLiteral* fun = ParseFunctionLiteral( |
| 1874 scanner()->location(), | 1878 name, scanner()->location(), is_strict_reserved, |
| 1875 is_strict_reserved, | 1879 is_generator ? FunctionLiteral::kIsGenerator |
| 1876 is_generator, | 1880 : FunctionLiteral::kNotGenerator, |
| 1877 pos, | 1881 FunctionLiteral::kNotArrow, FunctionLiteral::kNotConciseMethod, pos, |
| 1878 FunctionLiteral::DECLARATION, | 1882 FunctionLiteral::DECLARATION, FunctionLiteral::NORMAL_ARITY, CHECK_OK); |
| 1879 FunctionLiteral::NORMAL_ARITY, | |
| 1880 CHECK_OK); | |
| 1881 // Even if we're not at the top-level of the global or a function | 1883 // Even if we're not at the top-level of the global or a function |
| 1882 // scope, we treat it as such and introduce the function with its | 1884 // scope, we treat it as such and introduce the function with its |
| 1883 // initial value upon entering the corresponding scope. | 1885 // initial value upon entering the corresponding scope. |
| 1884 // In ES6, a function behaves as a lexical binding, except in the | 1886 // In ES6, a function behaves as a lexical binding, except in the |
| 1885 // global scope, or the initial scope of eval or another function. | 1887 // global scope, or the initial scope of eval or another function. |
| 1886 VariableMode mode = | 1888 VariableMode mode = |
| 1887 allow_harmony_scoping() && strict_mode() == STRICT && | 1889 allow_harmony_scoping() && strict_mode() == STRICT && |
| 1888 !(scope_->is_global_scope() || scope_->is_eval_scope() || | 1890 !(scope_->is_global_scope() || scope_->is_eval_scope() || |
| 1889 scope_->is_function_scope()) ? LET : VAR; | 1891 scope_->is_function_scope()) ? LET : VAR; |
| 1890 VariableProxy* proxy = NewUnresolved(name, mode, Interface::NewValue()); | 1892 VariableProxy* proxy = NewUnresolved(name, mode, Interface::NewValue()); |
| (...skipping 1443 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3334 Expression* expression, Scope* scope, Scanner::Location* dupe_loc, | 3336 Expression* expression, Scope* scope, Scanner::Location* dupe_loc, |
| 3335 bool* ok) { | 3337 bool* ok) { |
| 3336 int num_params = 0; | 3338 int num_params = 0; |
| 3337 *ok = CheckAndDeclareArrowParameter(this, expression, scope, &num_params, | 3339 *ok = CheckAndDeclareArrowParameter(this, expression, scope, &num_params, |
| 3338 dupe_loc); | 3340 dupe_loc); |
| 3339 return num_params; | 3341 return num_params; |
| 3340 } | 3342 } |
| 3341 | 3343 |
| 3342 | 3344 |
| 3343 FunctionLiteral* Parser::ParseFunctionLiteral( | 3345 FunctionLiteral* Parser::ParseFunctionLiteral( |
| 3344 const AstRawString* function_name, | 3346 const AstRawString* function_name, Scanner::Location function_name_location, |
| 3345 Scanner::Location function_name_location, | 3347 bool name_is_strict_reserved, FunctionLiteral::IsGeneratorFlag is_generator, |
| 3346 bool name_is_strict_reserved, | 3348 FunctionLiteral::IsArrowFlag is_arrow, |
| 3347 bool is_generator, | 3349 FunctionLiteral::IsConciseMethodFlag is_concise_method, |
| 3348 int function_token_pos, | 3350 int function_token_pos, FunctionLiteral::FunctionType function_type, |
| 3349 FunctionLiteral::FunctionType function_type, | 3351 FunctionLiteral::ArityRestriction arity_restriction, bool* ok) { |
| 3350 FunctionLiteral::ArityRestriction arity_restriction, | |
| 3351 bool* ok) { | |
| 3352 // Function :: | 3352 // Function :: |
| 3353 // '(' FormalParameterList? ')' '{' FunctionBody '}' | 3353 // '(' FormalParameterList? ')' '{' FunctionBody '}' |
| 3354 // | 3354 // |
| 3355 // Getter :: | 3355 // Getter :: |
| 3356 // '(' ')' '{' FunctionBody '}' | 3356 // '(' ')' '{' FunctionBody '}' |
| 3357 // | 3357 // |
| 3358 // Setter :: | 3358 // Setter :: |
| 3359 // '(' PropertySetParameterList ')' '{' FunctionBody '}' | 3359 // '(' PropertySetParameterList ')' '{' FunctionBody '}' |
| 3360 | 3360 |
| 3361 int pos = function_token_pos == RelocInfo::kNoPosition | 3361 int pos = function_token_pos == RelocInfo::kNoPosition |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3582 CHECK_OK); | 3582 CHECK_OK); |
| 3583 } | 3583 } |
| 3584 ast_properties = *factory()->visitor()->ast_properties(); | 3584 ast_properties = *factory()->visitor()->ast_properties(); |
| 3585 dont_optimize_reason = factory()->visitor()->dont_optimize_reason(); | 3585 dont_optimize_reason = factory()->visitor()->dont_optimize_reason(); |
| 3586 | 3586 |
| 3587 if (allow_harmony_scoping() && strict_mode() == STRICT) { | 3587 if (allow_harmony_scoping() && strict_mode() == STRICT) { |
| 3588 CheckConflictingVarDeclarations(scope, CHECK_OK); | 3588 CheckConflictingVarDeclarations(scope, CHECK_OK); |
| 3589 } | 3589 } |
| 3590 } | 3590 } |
| 3591 | 3591 |
| 3592 FunctionLiteral::KindFlag kind = is_generator | |
| 3593 ? FunctionLiteral::kGeneratorFunction | |
| 3594 : FunctionLiteral::kNormalFunction; | |
| 3595 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( | 3592 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( |
| 3596 function_name, ast_value_factory_, scope, body, | 3593 function_name, ast_value_factory_, scope, body, |
| 3597 materialized_literal_count, expected_property_count, handler_count, | 3594 materialized_literal_count, expected_property_count, handler_count, |
| 3598 num_parameters, duplicate_parameters, function_type, | 3595 num_parameters, duplicate_parameters, function_type, |
| 3599 FunctionLiteral::kIsFunction, parenthesized, kind, pos); | 3596 FunctionLiteral::kIsFunction, parenthesized, is_generator, is_arrow, |
| 3597 is_concise_method, pos); |
| 3600 function_literal->set_function_token_position(function_token_pos); | 3598 function_literal->set_function_token_position(function_token_pos); |
| 3601 function_literal->set_ast_properties(&ast_properties); | 3599 function_literal->set_ast_properties(&ast_properties); |
| 3602 function_literal->set_dont_optimize_reason(dont_optimize_reason); | 3600 function_literal->set_dont_optimize_reason(dont_optimize_reason); |
| 3603 | 3601 |
| 3604 if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal); | 3602 if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal); |
| 3605 return function_literal; | 3603 return function_literal; |
| 3606 } | 3604 } |
| 3607 | 3605 |
| 3608 | 3606 |
| 3609 void Parser::SkipLazyFunctionBody(const AstRawString* function_name, | 3607 void Parser::SkipLazyFunctionBody(const AstRawString* function_name, |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3743 reusable_preparser_ = new PreParser(&scanner_, NULL, stack_limit); | 3741 reusable_preparser_ = new PreParser(&scanner_, NULL, stack_limit); |
| 3744 reusable_preparser_->set_allow_harmony_scoping(allow_harmony_scoping()); | 3742 reusable_preparser_->set_allow_harmony_scoping(allow_harmony_scoping()); |
| 3745 reusable_preparser_->set_allow_modules(allow_modules()); | 3743 reusable_preparser_->set_allow_modules(allow_modules()); |
| 3746 reusable_preparser_->set_allow_natives_syntax(allow_natives_syntax()); | 3744 reusable_preparser_->set_allow_natives_syntax(allow_natives_syntax()); |
| 3747 reusable_preparser_->set_allow_lazy(true); | 3745 reusable_preparser_->set_allow_lazy(true); |
| 3748 reusable_preparser_->set_allow_generators(allow_generators()); | 3746 reusable_preparser_->set_allow_generators(allow_generators()); |
| 3749 reusable_preparser_->set_allow_arrow_functions(allow_arrow_functions()); | 3747 reusable_preparser_->set_allow_arrow_functions(allow_arrow_functions()); |
| 3750 reusable_preparser_->set_allow_harmony_numeric_literals( | 3748 reusable_preparser_->set_allow_harmony_numeric_literals( |
| 3751 allow_harmony_numeric_literals()); | 3749 allow_harmony_numeric_literals()); |
| 3752 reusable_preparser_->set_allow_classes(allow_classes()); | 3750 reusable_preparser_->set_allow_classes(allow_classes()); |
| 3751 reusable_preparser_->set_allow_harmony_object_literals( |
| 3752 allow_harmony_object_literals()); |
| 3753 } | 3753 } |
| 3754 PreParser::PreParseResult result = | 3754 PreParser::PreParseResult result = |
| 3755 reusable_preparser_->PreParseLazyFunction(strict_mode(), | 3755 reusable_preparser_->PreParseLazyFunction(strict_mode(), |
| 3756 is_generator(), | 3756 is_generator(), |
| 3757 logger); | 3757 logger); |
| 3758 return result; | 3758 return result; |
| 3759 } | 3759 } |
| 3760 | 3760 |
| 3761 | 3761 |
| 3762 Expression* Parser::ParseV8Intrinsic(bool* ok) { | 3762 Expression* Parser::ParseV8Intrinsic(bool* ok) { |
| (...skipping 1047 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4810 info()->SetAstValueFactory(ast_value_factory_); | 4810 info()->SetAstValueFactory(ast_value_factory_); |
| 4811 } | 4811 } |
| 4812 ast_value_factory_ = NULL; | 4812 ast_value_factory_ = NULL; |
| 4813 | 4813 |
| 4814 InternalizeUseCounts(); | 4814 InternalizeUseCounts(); |
| 4815 | 4815 |
| 4816 return (result != NULL); | 4816 return (result != NULL); |
| 4817 } | 4817 } |
| 4818 | 4818 |
| 4819 } } // namespace v8::internal | 4819 } } // namespace v8::internal |
| OLD | NEW |