OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 641 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
652 no_name, | 652 no_name, |
653 top_scope_, | 653 top_scope_, |
654 body, | 654 body, |
655 lexical_scope.materialized_literal_count(), | 655 lexical_scope.materialized_literal_count(), |
656 lexical_scope.expected_property_count(), | 656 lexical_scope.expected_property_count(), |
657 lexical_scope.only_simple_this_property_assignments(), | 657 lexical_scope.only_simple_this_property_assignments(), |
658 lexical_scope.this_property_assignments(), | 658 lexical_scope.this_property_assignments(), |
659 0, | 659 0, |
660 0, | 660 0, |
661 source->length(), | 661 source->length(), |
662 false, | 662 FunctionLiteral::ANONYMOUS_EXPRESSION, |
663 false); | 663 false); // Does not have duplicate parameters. |
664 } else if (stack_overflow_) { | 664 } else if (stack_overflow_) { |
665 isolate()->StackOverflow(); | 665 isolate()->StackOverflow(); |
666 } | 666 } |
667 } | 667 } |
668 | 668 |
669 // Make sure the target stack is empty. | 669 // Make sure the target stack is empty. |
670 ASSERT(target_stack_ == NULL); | 670 ASSERT(target_stack_ == NULL); |
671 | 671 |
672 // If there was a syntax error we have to get rid of the AST | 672 // If there was a syntax error we have to get rid of the AST |
673 // and it is not safe to do so before the scope has been deleted. | 673 // and it is not safe to do so before the scope has been deleted. |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
722 Scope* scope = NewScope(top_scope_, Scope::GLOBAL_SCOPE, inside_with()); | 722 Scope* scope = NewScope(top_scope_, Scope::GLOBAL_SCOPE, inside_with()); |
723 if (!info->closure().is_null()) { | 723 if (!info->closure().is_null()) { |
724 scope = Scope::DeserializeScopeChain(info, scope); | 724 scope = Scope::DeserializeScopeChain(info, scope); |
725 } | 725 } |
726 LexicalScope lexical_scope(this, scope, isolate()); | 726 LexicalScope lexical_scope(this, scope, isolate()); |
727 | 727 |
728 if (shared_info->strict_mode()) { | 728 if (shared_info->strict_mode()) { |
729 top_scope_->EnableStrictMode(); | 729 top_scope_->EnableStrictMode(); |
730 } | 730 } |
731 | 731 |
732 FunctionLiteralType type = | 732 FunctionLiteral::Type type = shared_info->is_expression() |
733 shared_info->is_expression() ? EXPRESSION : DECLARATION; | 733 ? (shared_info->is_anonymous() |
734 Handle<String> function_name = | 734 ? FunctionLiteral::ANONYMOUS_EXPRESSION |
735 shared_info->is_anonymous() ? Handle<String>::null() : name; | 735 : FunctionLiteral::NAMED_EXPRESSION) |
| 736 : FunctionLiteral::DECLARATION; |
736 bool ok = true; | 737 bool ok = true; |
737 result = ParseFunctionLiteral(function_name, | 738 result = ParseFunctionLiteral(name, |
738 false, // Strict mode name already checked. | 739 false, // Strict mode name already checked. |
739 RelocInfo::kNoPosition, | 740 RelocInfo::kNoPosition, |
740 type, | 741 type, |
741 &ok); | 742 &ok); |
742 // Make sure the results agree. | 743 // Make sure the results agree. |
743 ASSERT(ok == (result != NULL)); | 744 ASSERT(ok == (result != NULL)); |
744 } | 745 } |
745 | 746 |
746 // Make sure the target stack is empty. | 747 // Make sure the target stack is empty. |
747 ASSERT(target_stack_ == NULL); | 748 ASSERT(target_stack_ == NULL); |
(...skipping 720 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1468 // FunctionDeclaration :: | 1469 // FunctionDeclaration :: |
1469 // 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}' | 1470 // 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}' |
1470 Expect(Token::FUNCTION, CHECK_OK); | 1471 Expect(Token::FUNCTION, CHECK_OK); |
1471 int function_token_position = scanner().location().beg_pos; | 1472 int function_token_position = scanner().location().beg_pos; |
1472 bool is_strict_reserved = false; | 1473 bool is_strict_reserved = false; |
1473 Handle<String> name = ParseIdentifierOrStrictReservedWord( | 1474 Handle<String> name = ParseIdentifierOrStrictReservedWord( |
1474 &is_strict_reserved, CHECK_OK); | 1475 &is_strict_reserved, CHECK_OK); |
1475 FunctionLiteral* fun = ParseFunctionLiteral(name, | 1476 FunctionLiteral* fun = ParseFunctionLiteral(name, |
1476 is_strict_reserved, | 1477 is_strict_reserved, |
1477 function_token_position, | 1478 function_token_position, |
1478 DECLARATION, | 1479 FunctionLiteral::DECLARATION, |
1479 CHECK_OK); | 1480 CHECK_OK); |
1480 // Even if we're not at the top-level of the global or a function | 1481 // Even if we're not at the top-level of the global or a function |
1481 // scope, we treat is as such and introduce the function with it's | 1482 // scope, we treat is as such and introduce the function with it's |
1482 // initial value upon entering the corresponding scope. | 1483 // initial value upon entering the corresponding scope. |
1483 Declare(name, Variable::VAR, fun, true, CHECK_OK); | 1484 Declare(name, Variable::VAR, fun, true, CHECK_OK); |
1484 return EmptyStatement(); | 1485 return EmptyStatement(); |
1485 } | 1486 } |
1486 | 1487 |
1487 | 1488 |
1488 Block* Parser::ParseBlock(ZoneStringList* labels, bool* ok) { | 1489 Block* Parser::ParseBlock(ZoneStringList* labels, bool* ok) { |
(...skipping 1350 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2839 Expression* result = NULL; | 2840 Expression* result = NULL; |
2840 if (peek() == Token::FUNCTION) { | 2841 if (peek() == Token::FUNCTION) { |
2841 Expect(Token::FUNCTION, CHECK_OK); | 2842 Expect(Token::FUNCTION, CHECK_OK); |
2842 int function_token_position = scanner().location().beg_pos; | 2843 int function_token_position = scanner().location().beg_pos; |
2843 Handle<String> name; | 2844 Handle<String> name; |
2844 bool is_strict_reserved_name = false; | 2845 bool is_strict_reserved_name = false; |
2845 if (peek_any_identifier()) { | 2846 if (peek_any_identifier()) { |
2846 name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved_name, | 2847 name = ParseIdentifierOrStrictReservedWord(&is_strict_reserved_name, |
2847 CHECK_OK); | 2848 CHECK_OK); |
2848 } | 2849 } |
| 2850 FunctionLiteral::Type type = name.is_null() |
| 2851 ? FunctionLiteral::ANONYMOUS_EXPRESSION |
| 2852 : FunctionLiteral::NAMED_EXPRESSION; |
2849 result = ParseFunctionLiteral(name, | 2853 result = ParseFunctionLiteral(name, |
2850 is_strict_reserved_name, | 2854 is_strict_reserved_name, |
2851 function_token_position, | 2855 function_token_position, |
2852 EXPRESSION, | 2856 type, |
2853 CHECK_OK); | 2857 CHECK_OK); |
2854 } else { | 2858 } else { |
2855 result = ParsePrimaryExpression(CHECK_OK); | 2859 result = ParsePrimaryExpression(CHECK_OK); |
2856 } | 2860 } |
2857 | 2861 |
2858 while (true) { | 2862 while (true) { |
2859 switch (peek()) { | 2863 switch (peek()) { |
2860 case Token::LBRACK: { | 2864 case Token::LBRACK: { |
2861 Consume(Token::LBRACK); | 2865 Consume(Token::LBRACK); |
2862 int pos = scanner().location().beg_pos; | 2866 int pos = scanner().location().beg_pos; |
(...skipping 549 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3412 Handle<String> name; | 3416 Handle<String> name; |
3413 if (is_keyword) { | 3417 if (is_keyword) { |
3414 name = isolate_->factory()->LookupAsciiSymbol(Token::String(next)); | 3418 name = isolate_->factory()->LookupAsciiSymbol(Token::String(next)); |
3415 } else { | 3419 } else { |
3416 name = GetSymbol(CHECK_OK); | 3420 name = GetSymbol(CHECK_OK); |
3417 } | 3421 } |
3418 FunctionLiteral* value = | 3422 FunctionLiteral* value = |
3419 ParseFunctionLiteral(name, | 3423 ParseFunctionLiteral(name, |
3420 false, // reserved words are allowed here | 3424 false, // reserved words are allowed here |
3421 RelocInfo::kNoPosition, | 3425 RelocInfo::kNoPosition, |
3422 EXPRESSION, | 3426 FunctionLiteral::ANONYMOUS_EXPRESSION, |
3423 CHECK_OK); | 3427 CHECK_OK); |
3424 // Allow any number of parameters for compatiabilty with JSC. | 3428 // Allow any number of parameters for compatiabilty with JSC. |
3425 // Specification only allows zero parameters for get and one for set. | 3429 // Specification only allows zero parameters for get and one for set. |
3426 ObjectLiteral::Property* property = | 3430 ObjectLiteral::Property* property = |
3427 new(zone()) ObjectLiteral::Property(is_getter, value); | 3431 new(zone()) ObjectLiteral::Property(is_getter, value); |
3428 return property; | 3432 return property; |
3429 } else { | 3433 } else { |
3430 ReportUnexpectedToken(next); | 3434 ReportUnexpectedToken(next); |
3431 *ok = false; | 3435 *ok = false; |
3432 return NULL; | 3436 return NULL; |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3622 if (!done) Expect(Token::COMMA, CHECK_OK); | 3626 if (!done) Expect(Token::COMMA, CHECK_OK); |
3623 } | 3627 } |
3624 Expect(Token::RPAREN, CHECK_OK); | 3628 Expect(Token::RPAREN, CHECK_OK); |
3625 return result; | 3629 return result; |
3626 } | 3630 } |
3627 | 3631 |
3628 | 3632 |
3629 FunctionLiteral* Parser::ParseFunctionLiteral(Handle<String> function_name, | 3633 FunctionLiteral* Parser::ParseFunctionLiteral(Handle<String> function_name, |
3630 bool name_is_strict_reserved, | 3634 bool name_is_strict_reserved, |
3631 int function_token_position, | 3635 int function_token_position, |
3632 FunctionLiteralType type, | 3636 FunctionLiteral::Type type, |
3633 bool* ok) { | 3637 bool* ok) { |
3634 // Function :: | 3638 // Function :: |
3635 // '(' FormalParameterList? ')' '{' FunctionBody '}' | 3639 // '(' FormalParameterList? ')' '{' FunctionBody '}' |
3636 | 3640 |
3637 // Anonymous functions were passed either the empty symbol or a null | 3641 // Anonymous functions were passed either the empty symbol or a null |
3638 // handle as the function name. Remember if we were passed a non-empty | 3642 // handle as the function name. Remember if we were passed a non-empty |
3639 // handle to decide whether to invoke function name inference. | 3643 // handle to decide whether to invoke function name inference. |
3640 bool should_infer_name = function_name.is_null(); | 3644 bool should_infer_name = function_name.is_null(); |
3641 | 3645 |
3642 // We want a non-null handle as the function name. | 3646 // We want a non-null handle as the function name. |
3643 if (should_infer_name) { | 3647 if (should_infer_name) { |
3644 function_name = isolate()->factory()->empty_symbol(); | 3648 function_name = isolate()->factory()->empty_symbol(); |
3645 } | 3649 } |
3646 | 3650 |
3647 int num_parameters = 0; | 3651 int num_parameters = 0; |
3648 // Function declarations are hoisted. | 3652 // Function declarations are hoisted. |
3649 Scope* scope = (type == DECLARATION) | 3653 Scope* scope = (type == FunctionLiteral::DECLARATION) |
3650 ? NewScope(top_scope_->DeclarationScope(), Scope::FUNCTION_SCOPE, false) | 3654 ? NewScope(top_scope_->DeclarationScope(), Scope::FUNCTION_SCOPE, false) |
3651 : NewScope(top_scope_, Scope::FUNCTION_SCOPE, inside_with()); | 3655 : NewScope(top_scope_, Scope::FUNCTION_SCOPE, inside_with()); |
3652 ZoneList<Statement*>* body = new(zone()) ZoneList<Statement*>(8); | 3656 ZoneList<Statement*>* body = new(zone()) ZoneList<Statement*>(8); |
3653 int materialized_literal_count; | 3657 int materialized_literal_count; |
3654 int expected_property_count; | 3658 int expected_property_count; |
3655 int start_pos; | 3659 int start_pos; |
3656 int end_pos; | 3660 int end_pos; |
3657 bool only_simple_this_property_assignments; | 3661 bool only_simple_this_property_assignments; |
3658 Handle<FixedArray> this_property_assignments; | 3662 Handle<FixedArray> this_property_assignments; |
3659 bool has_duplicate_parameters = false; | 3663 bool has_duplicate_parameters = false; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3702 Expect(Token::RPAREN, CHECK_OK); | 3706 Expect(Token::RPAREN, CHECK_OK); |
3703 | 3707 |
3704 Expect(Token::LBRACE, CHECK_OK); | 3708 Expect(Token::LBRACE, CHECK_OK); |
3705 | 3709 |
3706 // If we have a named function expression, we add a local variable | 3710 // If we have a named function expression, we add a local variable |
3707 // declaration to the body of the function with the name of the | 3711 // declaration to the body of the function with the name of the |
3708 // function and let it refer to the function itself (closure). | 3712 // function and let it refer to the function itself (closure). |
3709 // NOTE: We create a proxy and resolve it here so that in the | 3713 // NOTE: We create a proxy and resolve it here so that in the |
3710 // future we can change the AST to only refer to VariableProxies | 3714 // future we can change the AST to only refer to VariableProxies |
3711 // instead of Variables and Proxis as is the case now. | 3715 // instead of Variables and Proxis as is the case now. |
3712 if (type == EXPRESSION && function_name->length() > 0) { | 3716 if (type == FunctionLiteral::NAMED_EXPRESSION) { |
3713 Variable* fvar = top_scope_->DeclareFunctionVar(function_name); | 3717 Variable* fvar = top_scope_->DeclareFunctionVar(function_name); |
3714 VariableProxy* fproxy = | 3718 VariableProxy* fproxy = |
3715 top_scope_->NewUnresolved(function_name, inside_with()); | 3719 top_scope_->NewUnresolved(function_name, inside_with()); |
3716 fproxy->BindTo(fvar); | 3720 fproxy->BindTo(fvar); |
3717 body->Add(new(zone()) ExpressionStatement( | 3721 body->Add(new(zone()) ExpressionStatement( |
3718 new(zone()) Assignment(isolate(), | 3722 new(zone()) Assignment(isolate(), |
3719 Token::INIT_CONST, | 3723 Token::INIT_CONST, |
3720 fproxy, | 3724 fproxy, |
3721 new(zone()) ThisFunction(isolate()), | 3725 new(zone()) ThisFunction(isolate()), |
3722 RelocInfo::kNoPosition))); | 3726 RelocInfo::kNoPosition))); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3820 function_name, | 3824 function_name, |
3821 scope, | 3825 scope, |
3822 body, | 3826 body, |
3823 materialized_literal_count, | 3827 materialized_literal_count, |
3824 expected_property_count, | 3828 expected_property_count, |
3825 only_simple_this_property_assignments, | 3829 only_simple_this_property_assignments, |
3826 this_property_assignments, | 3830 this_property_assignments, |
3827 num_parameters, | 3831 num_parameters, |
3828 start_pos, | 3832 start_pos, |
3829 end_pos, | 3833 end_pos, |
3830 type == EXPRESSION, | 3834 type, |
3831 has_duplicate_parameters); | 3835 has_duplicate_parameters); |
3832 function_literal->set_function_token_position(function_token_position); | 3836 function_literal->set_function_token_position(function_token_position); |
3833 | 3837 |
3834 if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal); | 3838 if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal); |
3835 return function_literal; | 3839 return function_literal; |
3836 } | 3840 } |
3837 | 3841 |
3838 | 3842 |
3839 Expression* Parser::ParseV8Intrinsic(bool* ok) { | 3843 Expression* Parser::ParseV8Intrinsic(bool* ok) { |
3840 // CallRuntime :: | 3844 // CallRuntime :: |
(...skipping 1284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5125 info->is_global(), | 5129 info->is_global(), |
5126 info->StrictMode()); | 5130 info->StrictMode()); |
5127 } | 5131 } |
5128 } | 5132 } |
5129 | 5133 |
5130 info->SetFunction(result); | 5134 info->SetFunction(result); |
5131 return (result != NULL); | 5135 return (result != NULL); |
5132 } | 5136 } |
5133 | 5137 |
5134 } } // namespace v8::internal | 5138 } } // namespace v8::internal |
OLD | NEW |