| 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 681 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 692 auto name = ast_value_factory()->empty_string(); | 692 auto name = ast_value_factory()->empty_string(); |
| 693 bool is_duplicate; | 693 bool is_duplicate; |
| 694 bool is_rest = false; | 694 bool is_rest = false; |
| 695 bool is_optional = false; | 695 bool is_optional = false; |
| 696 auto var = scope->DeclareParameter(name, VAR, is_optional, is_rest, | 696 auto var = scope->DeclareParameter(name, VAR, is_optional, is_rest, |
| 697 &is_duplicate, ast_value_factory()); | 697 &is_duplicate, ast_value_factory()); |
| 698 DCHECK(!is_duplicate); | 698 DCHECK(!is_duplicate); |
| 699 var->AllocateTo(VariableLocation::PARAMETER, 0); | 699 var->AllocateTo(VariableLocation::PARAMETER, 0); |
| 700 | 700 |
| 701 PrepareGeneratorVariables(); | 701 PrepareGeneratorVariables(); |
| 702 Expression* initial_yield = | |
| 703 BuildInitialYield(kNoSourcePosition, kGeneratorFunction); | |
| 704 body->Add( | |
| 705 factory()->NewExpressionStatement(initial_yield, kNoSourcePosition), | |
| 706 zone()); | |
| 707 | 702 |
| 708 ParseModuleItemList(body, &ok); | 703 ParseModuleItemList(body, &ok); |
| 709 ok = ok && | 704 ok = ok && |
| 710 module()->Validate(this->scope()->AsModuleScope(), | 705 module()->Validate(this->scope()->AsModuleScope(), |
| 711 &pending_error_handler_, zone()); | 706 &pending_error_handler_, zone()); |
| 712 } else { | 707 } else { |
| 713 // Don't count the mode in the use counters--give the program a chance | 708 // Don't count the mode in the use counters--give the program a chance |
| 714 // to enable script-wide strict mode below. | 709 // to enable script-wide strict mode below. |
| 715 this->scope()->SetLanguageMode(info->language_mode()); | 710 this->scope()->SetLanguageMode(info->language_mode()); |
| 716 ParseStatementList(body, Token::EOS, &ok); | 711 ParseStatementList(body, Token::EOS, &ok); |
| (...skipping 1053 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1770 // | 1765 // |
| 1771 // try { InitialYield; ...body...; return {value: undefined, done: true} } | 1766 // try { InitialYield; ...body...; return {value: undefined, done: true} } |
| 1772 // finally { %_GeneratorClose(generator) } | 1767 // finally { %_GeneratorClose(generator) } |
| 1773 // | 1768 // |
| 1774 // - InitialYield yields the actual generator object. | 1769 // - InitialYield yields the actual generator object. |
| 1775 // - Any return statement inside the body will have its argument wrapped | 1770 // - Any return statement inside the body will have its argument wrapped |
| 1776 // in a "done" iterator result object. | 1771 // in a "done" iterator result object. |
| 1777 // - If the generator terminates for whatever reason, we must close it. | 1772 // - If the generator terminates for whatever reason, we must close it. |
| 1778 // Hence the finally clause. | 1773 // Hence the finally clause. |
| 1779 | 1774 |
| 1780 Block* try_block = factory()->NewBlock(nullptr, 3, false, kNoSourcePosition); | 1775 ParseStatementList(body, Token::RBRACE, ok); |
| 1781 Expression* initial_yield = BuildInitialYield(pos, kind); | |
| 1782 try_block->statements()->Add( | |
| 1783 factory()->NewExpressionStatement(initial_yield, kNoSourcePosition), | |
| 1784 zone()); | |
| 1785 ParseStatementList(try_block->statements(), Token::RBRACE, ok); | |
| 1786 if (!*ok) return; | 1776 if (!*ok) return; |
| 1787 | 1777 |
| 1788 Statement* final_return = factory()->NewReturnStatement( | 1778 Statement* final_return = factory()->NewReturnStatement( |
| 1789 BuildIteratorResult(nullptr, true), kNoSourcePosition); | 1779 BuildIteratorResult(nullptr, true), kNoSourcePosition); |
| 1790 try_block->statements()->Add(final_return, zone()); | 1780 body->Add(final_return, zone()); |
| 1791 | |
| 1792 Block* finally_block = | |
| 1793 factory()->NewBlock(nullptr, 1, false, kNoSourcePosition); | |
| 1794 ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(1, zone()); | |
| 1795 VariableProxy* call_proxy = | |
| 1796 factory()->NewVariableProxy(function_state_->generator_object_variable()); | |
| 1797 args->Add(call_proxy, zone()); | |
| 1798 Expression* call = factory()->NewCallRuntime(Runtime::kInlineGeneratorClose, | |
| 1799 args, kNoSourcePosition); | |
| 1800 finally_block->statements()->Add( | |
| 1801 factory()->NewExpressionStatement(call, kNoSourcePosition), zone()); | |
| 1802 | |
| 1803 body->Add(factory()->NewTryFinallyStatement(try_block, finally_block, | |
| 1804 kNoSourcePosition), | |
| 1805 zone()); | |
| 1806 } | 1781 } |
| 1807 | 1782 |
| 1808 void Parser::CreateFunctionNameAssignment( | 1783 void Parser::CreateFunctionNameVariable( |
| 1809 const AstRawString* function_name, int pos, | 1784 const AstRawString* function_name, |
| 1810 FunctionLiteral::FunctionType function_type, | 1785 FunctionLiteral::FunctionType function_type, |
| 1811 DeclarationScope* function_scope, ZoneList<Statement*>* result, int index) { | 1786 DeclarationScope* function_scope) { |
| 1812 if (function_type == FunctionLiteral::kNamedExpression) { | 1787 if (function_type == FunctionLiteral::kNamedExpression) { |
| 1813 StatementT statement = factory()->NewEmptyStatement(kNoSourcePosition); | |
| 1814 if (function_scope->LookupLocal(function_name) == nullptr) { | 1788 if (function_scope->LookupLocal(function_name) == nullptr) { |
| 1815 // Now that we know the language mode, we can create the const assignment | 1789 // Now that we know the language mode, we can create the const assignment |
| 1816 // in the previously reserved spot. | 1790 // in the previously reserved spot. |
| 1817 DCHECK_EQ(function_scope, scope()); | 1791 DCHECK_EQ(function_scope, scope()); |
| 1818 Variable* fvar = function_scope->DeclareFunctionVar(function_name); | 1792 Variable* fvar = function_scope->DeclareFunctionVar(function_name); |
| 1819 VariableProxy* fproxy = factory()->NewVariableProxy(fvar); | 1793 DCHECK_NOT_NULL(fvar); |
| 1820 statement = factory()->NewExpressionStatement( | 1794 |
| 1821 factory()->NewAssignment(Token::INIT, fproxy, | 1795 // Ensure the variable is allocated if any inner scope _may_ use it. |
| 1822 factory()->NewThisFunction(pos), | 1796 // Otherwise, lazily parsed |
| 1823 kNoSourcePosition), | 1797 if (function_scope->calls_eval() || |
| 1824 kNoSourcePosition); | 1798 function_scope->inner_scope_calls_eval() || |
| 1799 function_scope->HasLazilyParsedInnerFunctionScope()) { |
| 1800 fvar->set_is_used(); |
| 1801 return; |
| 1802 } |
| 1825 } | 1803 } |
| 1826 result->Set(index, statement); | |
| 1827 } | 1804 } |
| 1828 } | 1805 } |
| 1829 | 1806 |
| 1830 // !%_IsJSReceiver(result = iterator.next()) && | 1807 // !%_IsJSReceiver(result = iterator.next()) && |
| 1831 // %ThrowIteratorResultNotAnObject(result) | 1808 // %ThrowIteratorResultNotAnObject(result) |
| 1832 Expression* Parser::BuildIteratorNextResult(Expression* iterator, | 1809 Expression* Parser::BuildIteratorNextResult(Expression* iterator, |
| 1833 Variable* result, int pos) { | 1810 Variable* result, int pos) { |
| 1834 Expression* next_literal = factory()->NewStringLiteral( | 1811 Expression* next_literal = factory()->NewStringLiteral( |
| 1835 ast_value_factory()->next_string(), kNoSourcePosition); | 1812 ast_value_factory()->next_string(), kNoSourcePosition); |
| 1836 Expression* next_property = | 1813 Expression* next_property = |
| (...skipping 636 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2473 void Parser::PrepareGeneratorVariables() { | 2450 void Parser::PrepareGeneratorVariables() { |
| 2474 // For generators, allocating variables in contexts is currently a win because | 2451 // For generators, allocating variables in contexts is currently a win because |
| 2475 // it minimizes the work needed to suspend and resume an activation. The | 2452 // it minimizes the work needed to suspend and resume an activation. The |
| 2476 // code produced for generators relies on this forced context allocation (it | 2453 // code produced for generators relies on this forced context allocation (it |
| 2477 // does not restore the frame's parameters upon resume). | 2454 // does not restore the frame's parameters upon resume). |
| 2478 function_state_->scope()->ForceContextAllocation(); | 2455 function_state_->scope()->ForceContextAllocation(); |
| 2479 | 2456 |
| 2480 // Calling a generator returns a generator object. That object is stored | 2457 // Calling a generator returns a generator object. That object is stored |
| 2481 // in a temporary variable, a definition that is used by "yield" | 2458 // in a temporary variable, a definition that is used by "yield" |
| 2482 // expressions. | 2459 // expressions. |
| 2483 Variable* temp = | 2460 Variable* temp = function_state_->scope()->DeclareGeneratorObjectVar( |
| 2484 NewTemporary(ast_value_factory()->dot_generator_object_string()); | 2461 ast_value_factory()->dot_generator_object_string()); |
| 2485 function_state_->set_generator_object_variable(temp); | 2462 temp->set_is_used(); |
| 2486 } | 2463 } |
| 2487 | 2464 |
| 2488 FunctionLiteral* Parser::ParseFunctionLiteral( | 2465 FunctionLiteral* Parser::ParseFunctionLiteral( |
| 2489 const AstRawString* function_name, Scanner::Location function_name_location, | 2466 const AstRawString* function_name, Scanner::Location function_name_location, |
| 2490 FunctionNameValidity function_name_validity, FunctionKind kind, | 2467 FunctionNameValidity function_name_validity, FunctionKind kind, |
| 2491 int function_token_pos, FunctionLiteral::FunctionType function_type, | 2468 int function_token_pos, FunctionLiteral::FunctionType function_type, |
| 2492 LanguageMode language_mode, bool* ok) { | 2469 LanguageMode language_mode, bool* ok) { |
| 2493 // Function :: | 2470 // Function :: |
| 2494 // '(' FormalParameterList? ')' '{' FunctionBody '}' | 2471 // '(' FormalParameterList? ')' '{' FunctionBody '}' |
| 2495 // | 2472 // |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2592 | 2569 |
| 2593 DCHECK_IMPLIES( | 2570 DCHECK_IMPLIES( |
| 2594 (is_lazy_top_level_function || | 2571 (is_lazy_top_level_function || |
| 2595 (parse_lazily() && function_type == FunctionLiteral::kDeclaration && | 2572 (parse_lazily() && function_type == FunctionLiteral::kDeclaration && |
| 2596 eager_compile_hint == FunctionLiteral::kShouldLazyCompile)), | 2573 eager_compile_hint == FunctionLiteral::kShouldLazyCompile)), |
| 2597 can_preparse); | 2574 can_preparse); |
| 2598 bool is_lazy_inner_function = | 2575 bool is_lazy_inner_function = |
| 2599 use_temp_zone && FLAG_lazy_inner_functions && !is_lazy_top_level_function; | 2576 use_temp_zone && FLAG_lazy_inner_functions && !is_lazy_top_level_function; |
| 2600 | 2577 |
| 2601 ZoneList<Statement*>* body = nullptr; | 2578 ZoneList<Statement*>* body = nullptr; |
| 2579 Block* parameter_init_block = nullptr; |
| 2602 int materialized_literal_count = -1; | 2580 int materialized_literal_count = -1; |
| 2603 int expected_property_count = -1; | 2581 int expected_property_count = -1; |
| 2604 bool should_be_used_once_hint = false; | 2582 bool should_be_used_once_hint = false; |
| 2605 int num_parameters = -1; | 2583 int num_parameters = -1; |
| 2606 int function_length = -1; | 2584 int function_length = -1; |
| 2607 bool has_duplicate_parameters = false; | 2585 bool has_duplicate_parameters = false; |
| 2608 int function_literal_id = GetNextFunctionLiteralId(); | 2586 int function_literal_id = GetNextFunctionLiteralId(); |
| 2609 | 2587 |
| 2610 Zone* outer_zone = zone(); | 2588 Zone* outer_zone = zone(); |
| 2611 DeclarationScope* scope; | 2589 DeclarationScope* scope; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2663 should_be_used_once_hint = true; | 2641 should_be_used_once_hint = true; |
| 2664 scope->ResetAfterPreparsing(ast_value_factory(), true); | 2642 scope->ResetAfterPreparsing(ast_value_factory(), true); |
| 2665 zone_scope.Reset(); | 2643 zone_scope.Reset(); |
| 2666 use_temp_zone = false; | 2644 use_temp_zone = false; |
| 2667 } | 2645 } |
| 2668 } | 2646 } |
| 2669 | 2647 |
| 2670 if (!is_lazy_top_level_function && !is_lazy_inner_function) { | 2648 if (!is_lazy_top_level_function && !is_lazy_inner_function) { |
| 2671 body = ParseFunction( | 2649 body = ParseFunction( |
| 2672 function_name, pos, kind, function_type, scope, &num_parameters, | 2650 function_name, pos, kind, function_type, scope, &num_parameters, |
| 2673 &function_length, &has_duplicate_parameters, | 2651 ¶meter_init_block, &function_length, &has_duplicate_parameters, |
| 2674 &materialized_literal_count, &expected_property_count, CHECK_OK); | 2652 &materialized_literal_count, &expected_property_count, CHECK_OK); |
| 2675 } | 2653 } |
| 2676 | 2654 |
| 2677 DCHECK(use_temp_zone || !is_lazy_top_level_function); | 2655 DCHECK(use_temp_zone || !is_lazy_top_level_function); |
| 2678 if (use_temp_zone) { | 2656 if (use_temp_zone) { |
| 2679 // If the preconditions are correct the function body should never be | 2657 // If the preconditions are correct the function body should never be |
| 2680 // accessed, but do this anyway for better behaviour if they're wrong. | 2658 // accessed, but do this anyway for better behaviour if they're wrong. |
| 2681 body = nullptr; | 2659 body = nullptr; |
| 2682 scope->AnalyzePartially(&previous_zone_ast_node_factory, | 2660 scope->AnalyzePartially(&previous_zone_ast_node_factory, |
| 2683 preparsed_scope_data_); | 2661 preparsed_scope_data_); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2725 has_duplicate_parameters ? FunctionLiteral::kHasDuplicateParameters | 2703 has_duplicate_parameters ? FunctionLiteral::kHasDuplicateParameters |
| 2726 : FunctionLiteral::kNoDuplicateParameters; | 2704 : FunctionLiteral::kNoDuplicateParameters; |
| 2727 | 2705 |
| 2728 // Note that the FunctionLiteral needs to be created in the main Zone again. | 2706 // Note that the FunctionLiteral needs to be created in the main Zone again. |
| 2729 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( | 2707 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( |
| 2730 function_name, scope, body, materialized_literal_count, | 2708 function_name, scope, body, materialized_literal_count, |
| 2731 expected_property_count, num_parameters, function_length, | 2709 expected_property_count, num_parameters, function_length, |
| 2732 duplicate_parameters, function_type, eager_compile_hint, pos, true, | 2710 duplicate_parameters, function_type, eager_compile_hint, pos, true, |
| 2733 function_literal_id); | 2711 function_literal_id); |
| 2734 function_literal->set_function_token_position(function_token_pos); | 2712 function_literal->set_function_token_position(function_token_pos); |
| 2713 function_literal->set_parameter_init_block(parameter_init_block); |
| 2735 if (should_be_used_once_hint) | 2714 if (should_be_used_once_hint) |
| 2736 function_literal->set_should_be_used_once_hint(); | 2715 function_literal->set_should_be_used_once_hint(); |
| 2737 | 2716 |
| 2738 if (should_infer_name) { | 2717 if (should_infer_name) { |
| 2739 DCHECK_NOT_NULL(fni_); | 2718 DCHECK_NOT_NULL(fni_); |
| 2740 fni_->AddFunction(function_literal); | 2719 fni_->AddFunction(function_literal); |
| 2741 } | 2720 } |
| 2742 return function_literal; | 2721 return function_literal; |
| 2743 } | 2722 } |
| 2744 | 2723 |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2971 if (param_scope != nullptr) { | 2950 if (param_scope != nullptr) { |
| 2972 CheckConflictingVarDeclarations(param_scope, CHECK_OK); | 2951 CheckConflictingVarDeclarations(param_scope, CHECK_OK); |
| 2973 } | 2952 } |
| 2974 init_block->statements()->Add(param_block, zone()); | 2953 init_block->statements()->Add(param_block, zone()); |
| 2975 } | 2954 } |
| 2976 ++index; | 2955 ++index; |
| 2977 } | 2956 } |
| 2978 return init_block; | 2957 return init_block; |
| 2979 } | 2958 } |
| 2980 | 2959 |
| 2981 Block* Parser::BuildRejectPromiseOnException(Block* inner_block) { | |
| 2982 // .promise = %AsyncFunctionPromiseCreate(); | |
| 2983 // try { | |
| 2984 // <inner_block> | |
| 2985 // } catch (.catch) { | |
| 2986 // %RejectPromise(.promise, .catch); | |
| 2987 // return .promise; | |
| 2988 // } finally { | |
| 2989 // %AsyncFunctionPromiseRelease(.promise); | |
| 2990 // } | |
| 2991 Block* result = factory()->NewBlock(nullptr, 2, true, kNoSourcePosition); | |
| 2992 | |
| 2993 // .promise = %AsyncFunctionPromiseCreate(); | |
| 2994 Statement* set_promise; | |
| 2995 { | |
| 2996 Expression* create_promise = factory()->NewCallRuntime( | |
| 2997 Context::ASYNC_FUNCTION_PROMISE_CREATE_INDEX, | |
| 2998 new (zone()) ZoneList<Expression*>(0, zone()), kNoSourcePosition); | |
| 2999 Assignment* assign_promise = factory()->NewAssignment( | |
| 3000 Token::INIT, factory()->NewVariableProxy(PromiseVariable()), | |
| 3001 create_promise, kNoSourcePosition); | |
| 3002 set_promise = | |
| 3003 factory()->NewExpressionStatement(assign_promise, kNoSourcePosition); | |
| 3004 } | |
| 3005 result->statements()->Add(set_promise, zone()); | |
| 3006 | |
| 3007 // catch (.catch) { return %RejectPromise(.promise, .catch), .promise } | |
| 3008 Scope* catch_scope = NewScope(CATCH_SCOPE); | |
| 3009 catch_scope->set_is_hidden(); | |
| 3010 Variable* catch_variable = | |
| 3011 catch_scope->DeclareLocal(ast_value_factory()->dot_catch_string(), VAR); | |
| 3012 Block* catch_block = factory()->NewBlock(nullptr, 1, true, kNoSourcePosition); | |
| 3013 | |
| 3014 Expression* promise_reject = BuildRejectPromise( | |
| 3015 factory()->NewVariableProxy(catch_variable), kNoSourcePosition); | |
| 3016 ReturnStatement* return_promise_reject = | |
| 3017 factory()->NewReturnStatement(promise_reject, kNoSourcePosition); | |
| 3018 catch_block->statements()->Add(return_promise_reject, zone()); | |
| 3019 | |
| 3020 TryStatement* try_catch_statement = | |
| 3021 factory()->NewTryCatchStatementForAsyncAwait(inner_block, catch_scope, | |
| 3022 catch_variable, catch_block, | |
| 3023 kNoSourcePosition); | |
| 3024 | |
| 3025 // There is no TryCatchFinally node, so wrap it in an outer try/finally | |
| 3026 Block* outer_try_block = | |
| 3027 factory()->NewBlock(nullptr, 1, true, kNoSourcePosition); | |
| 3028 outer_try_block->statements()->Add(try_catch_statement, zone()); | |
| 3029 | |
| 3030 // finally { %AsyncFunctionPromiseRelease(.promise) } | |
| 3031 Block* finally_block = | |
| 3032 factory()->NewBlock(nullptr, 1, true, kNoSourcePosition); | |
| 3033 { | |
| 3034 ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(1, zone()); | |
| 3035 args->Add(factory()->NewVariableProxy(PromiseVariable()), zone()); | |
| 3036 Expression* call_promise_release = factory()->NewCallRuntime( | |
| 3037 Context::ASYNC_FUNCTION_PROMISE_RELEASE_INDEX, args, kNoSourcePosition); | |
| 3038 Statement* promise_release = factory()->NewExpressionStatement( | |
| 3039 call_promise_release, kNoSourcePosition); | |
| 3040 finally_block->statements()->Add(promise_release, zone()); | |
| 3041 } | |
| 3042 | |
| 3043 Statement* try_finally_statement = factory()->NewTryFinallyStatement( | |
| 3044 outer_try_block, finally_block, kNoSourcePosition); | |
| 3045 | |
| 3046 result->statements()->Add(try_finally_statement, zone()); | |
| 3047 return result; | |
| 3048 } | |
| 3049 | |
| 3050 Assignment* Parser::BuildCreateJSGeneratorObject(int pos, FunctionKind kind) { | |
| 3051 // .generator = %CreateJSGeneratorObject(...); | |
| 3052 DCHECK_NOT_NULL(function_state_->generator_object_variable()); | |
| 3053 ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(2, zone()); | |
| 3054 args->Add(factory()->NewThisFunction(pos), zone()); | |
| 3055 args->Add(IsArrowFunction(kind) ? GetLiteralUndefined(pos) | |
| 3056 : ThisExpression(kNoSourcePosition), | |
| 3057 zone()); | |
| 3058 Expression* allocation = | |
| 3059 factory()->NewCallRuntime(Runtime::kCreateJSGeneratorObject, args, pos); | |
| 3060 VariableProxy* proxy = | |
| 3061 factory()->NewVariableProxy(function_state_->generator_object_variable()); | |
| 3062 return factory()->NewAssignment(Token::INIT, proxy, allocation, | |
| 3063 kNoSourcePosition); | |
| 3064 } | |
| 3065 | |
| 3066 Expression* Parser::BuildResolvePromise(Expression* value, int pos) { | 2960 Expression* Parser::BuildResolvePromise(Expression* value, int pos) { |
| 3067 // %ResolvePromise(.promise, value), .promise | 2961 // %ResolvePromise(.promise, value), .promise |
| 3068 ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(2, zone()); | 2962 ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(2, zone()); |
| 3069 args->Add(factory()->NewVariableProxy(PromiseVariable()), zone()); | 2963 args->Add(factory()->NewVariableProxy(PromiseVariable()), zone()); |
| 3070 args->Add(value, zone()); | 2964 args->Add(value, zone()); |
| 3071 Expression* call_runtime = | 2965 Expression* call_runtime = |
| 3072 factory()->NewCallRuntime(Context::PROMISE_RESOLVE_INDEX, args, pos); | 2966 factory()->NewCallRuntime(Context::PROMISE_RESOLVE_INDEX, args, pos); |
| 3073 return factory()->NewBinaryOperation( | 2967 return factory()->NewBinaryOperation( |
| 3074 Token::COMMA, call_runtime, | 2968 Token::COMMA, call_runtime, |
| 3075 factory()->NewVariableProxy(PromiseVariable()), pos); | 2969 factory()->NewVariableProxy(PromiseVariable()), pos); |
| 3076 } | 2970 } |
| 3077 | 2971 |
| 3078 Expression* Parser::BuildRejectPromise(Expression* value, int pos) { | |
| 3079 // %promise_internal_reject(.promise, value, false), .promise | |
| 3080 // Disables the additional debug event for the rejection since a debug event | |
| 3081 // already happened for the exception that got us here. | |
| 3082 ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(3, zone()); | |
| 3083 args->Add(factory()->NewVariableProxy(PromiseVariable()), zone()); | |
| 3084 args->Add(value, zone()); | |
| 3085 args->Add(factory()->NewBooleanLiteral(false, pos), zone()); | |
| 3086 Expression* call_runtime = factory()->NewCallRuntime( | |
| 3087 Context::PROMISE_INTERNAL_REJECT_INDEX, args, pos); | |
| 3088 return factory()->NewBinaryOperation( | |
| 3089 Token::COMMA, call_runtime, | |
| 3090 factory()->NewVariableProxy(PromiseVariable()), pos); | |
| 3091 } | |
| 3092 | |
| 3093 Variable* Parser::PromiseVariable() { | 2972 Variable* Parser::PromiseVariable() { |
| 3094 // Based on the various compilation paths, there are many different code | 2973 // Based on the various compilation paths, there are many different code |
| 3095 // paths which may be the first to access the Promise temporary. Whichever | 2974 // paths which may be the first to access the Promise temporary. Whichever |
| 3096 // comes first should create it and stash it in the FunctionState. | 2975 // comes first should create it and stash it in the FunctionState. |
| 3097 Variable* promise = function_state_->promise_variable(); | 2976 Variable* promise = function_state_->promise_variable(); |
| 3098 if (function_state_->promise_variable() == nullptr) { | 2977 if (function_state_->promise_variable() == nullptr) { |
| 3099 promise = scope()->NewTemporary(ast_value_factory()->empty_string()); | 2978 promise = function_state_->scope()->DeclarePromiseVar( |
| 3100 function_state_->set_promise_variable(promise); | 2979 ast_value_factory()->empty_string()); |
| 2980 promise->set_is_used(); |
| 3101 } | 2981 } |
| 3102 return promise; | 2982 return promise; |
| 3103 } | 2983 } |
| 3104 | 2984 |
| 3105 Expression* Parser::BuildInitialYield(int pos, FunctionKind kind) { | |
| 3106 Assignment* assignment = BuildCreateJSGeneratorObject(pos, kind); | |
| 3107 VariableProxy* generator = | |
| 3108 factory()->NewVariableProxy(function_state_->generator_object_variable()); | |
| 3109 // The position of the yield is important for reporting the exception | |
| 3110 // caused by calling the .throw method on a generator suspended at the | |
| 3111 // initial yield (i.e. right after generator instantiation). | |
| 3112 return factory()->NewYield(generator, assignment, scope()->start_position(), | |
| 3113 Yield::kOnExceptionThrow); | |
| 3114 } | |
| 3115 | |
| 3116 ZoneList<Statement*>* Parser::ParseFunction( | 2985 ZoneList<Statement*>* Parser::ParseFunction( |
| 3117 const AstRawString* function_name, int pos, FunctionKind kind, | 2986 const AstRawString* function_name, int pos, FunctionKind kind, |
| 3118 FunctionLiteral::FunctionType function_type, | 2987 FunctionLiteral::FunctionType function_type, |
| 3119 DeclarationScope* function_scope, int* num_parameters, int* function_length, | 2988 DeclarationScope* function_scope, int* num_parameters, |
| 2989 Block** parameter_init_block, int* function_length, |
| 3120 bool* has_duplicate_parameters, int* materialized_literal_count, | 2990 bool* has_duplicate_parameters, int* materialized_literal_count, |
| 3121 int* expected_property_count, bool* ok) { | 2991 int* expected_property_count, bool* ok) { |
| 3122 ParsingModeScope mode(this, allow_lazy_ ? PARSE_LAZILY : PARSE_EAGERLY); | 2992 ParsingModeScope mode(this, allow_lazy_ ? PARSE_LAZILY : PARSE_EAGERLY); |
| 3123 | 2993 |
| 3124 FunctionState function_state(&function_state_, &scope_state_, function_scope); | 2994 FunctionState function_state(&function_state_, &scope_state_, function_scope); |
| 3125 | 2995 |
| 3126 DuplicateFinder duplicate_finder; | 2996 DuplicateFinder duplicate_finder; |
| 3127 ExpressionClassifier formals_classifier(this, &duplicate_finder); | 2997 ExpressionClassifier formals_classifier(this, &duplicate_finder); |
| 3128 | 2998 |
| 3129 if (IsResumableFunction(kind)) PrepareGeneratorVariables(); | 2999 if (IsResumableFunction(kind)) PrepareGeneratorVariables(); |
| 3130 | 3000 |
| 3131 ParserFormalParameters formals(function_scope); | 3001 ParserFormalParameters formals(function_scope); |
| 3132 ParseFormalParameterList(&formals, CHECK_OK); | 3002 ParseFormalParameterList(&formals, CHECK_OK); |
| 3133 Expect(Token::RPAREN, CHECK_OK); | 3003 Expect(Token::RPAREN, CHECK_OK); |
| 3134 int formals_end_position = scanner()->location().end_pos; | 3004 int formals_end_position = scanner()->location().end_pos; |
| 3135 *num_parameters = formals.num_parameters(); | 3005 *num_parameters = formals.num_parameters(); |
| 3136 *function_length = formals.function_length; | 3006 *function_length = formals.function_length; |
| 3137 | 3007 |
| 3138 CheckArityRestrictions(formals.arity, kind, formals.has_rest, | 3008 CheckArityRestrictions(formals.arity, kind, formals.has_rest, |
| 3139 function_scope->start_position(), formals_end_position, | 3009 function_scope->start_position(), formals_end_position, |
| 3140 CHECK_OK); | 3010 CHECK_OK); |
| 3141 Expect(Token::LBRACE, CHECK_OK); | 3011 Expect(Token::LBRACE, CHECK_OK); |
| 3142 | 3012 |
| 3143 ZoneList<Statement*>* body = new (zone()) ZoneList<Statement*>(8, zone()); | 3013 ZoneList<Statement*>* body = new (zone()) ZoneList<Statement*>(8, zone()); |
| 3144 ParseFunctionBody(body, function_name, pos, formals, kind, function_type, ok); | 3014 ParseFunctionBody(body, parameter_init_block, function_name, pos, formals, |
| 3015 kind, function_type, ok); |
| 3145 | 3016 |
| 3146 // Validate parameter names. We can do this only after parsing the function, | 3017 // Validate parameter names. We can do this only after parsing the function, |
| 3147 // since the function can declare itself strict. | 3018 // since the function can declare itself strict. |
| 3148 const bool allow_duplicate_parameters = | 3019 const bool allow_duplicate_parameters = |
| 3149 is_sloppy(function_scope->language_mode()) && formals.is_simple && | 3020 is_sloppy(function_scope->language_mode()) && formals.is_simple && |
| 3150 !IsConciseMethod(kind); | 3021 !IsConciseMethod(kind); |
| 3151 ValidateFormalParameters(function_scope->language_mode(), | 3022 ValidateFormalParameters(function_scope->language_mode(), |
| 3152 allow_duplicate_parameters, CHECK_OK); | 3023 allow_duplicate_parameters, CHECK_OK); |
| 3153 | 3024 |
| 3154 RewriteDestructuringAssignments(); | 3025 RewriteDestructuringAssignments(); |
| (...skipping 609 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3764 | 3635 |
| 3765 // This method intoduces the line initializing the generator object | 3636 // This method intoduces the line initializing the generator object |
| 3766 // when desugaring the body of async_function. | 3637 // when desugaring the body of async_function. |
| 3767 void Parser::PrepareAsyncFunctionBody(ZoneList<Statement*>* body, | 3638 void Parser::PrepareAsyncFunctionBody(ZoneList<Statement*>* body, |
| 3768 FunctionKind kind, int pos) { | 3639 FunctionKind kind, int pos) { |
| 3769 // When parsing an async arrow function, we get here without having called | 3640 // When parsing an async arrow function, we get here without having called |
| 3770 // PrepareGeneratorVariables yet, so do it now. | 3641 // PrepareGeneratorVariables yet, so do it now. |
| 3771 if (function_state_->generator_object_variable() == nullptr) { | 3642 if (function_state_->generator_object_variable() == nullptr) { |
| 3772 PrepareGeneratorVariables(); | 3643 PrepareGeneratorVariables(); |
| 3773 } | 3644 } |
| 3774 body->Add(factory()->NewExpressionStatement( | |
| 3775 BuildCreateJSGeneratorObject(pos, kind), kNoSourcePosition), | |
| 3776 zone()); | |
| 3777 } | 3645 } |
| 3778 | 3646 |
| 3779 // This method completes the desugaring of the body of async_function. | 3647 // This method completes the desugaring of the body of async_function. |
| 3780 void Parser::RewriteAsyncFunctionBody(ZoneList<Statement*>* body, Block* block, | 3648 void Parser::RewriteAsyncFunctionBody(ZoneList<Statement*>* body, |
| 3781 Expression* return_value, bool* ok) { | 3649 Expression* return_value, bool* ok) { |
| 3782 // function async_function() { | 3650 // function async_function() { |
| 3783 // .generator_object = %CreateJSGeneratorObject(); | 3651 // .generator_object = %CreateJSGeneratorObject(); |
| 3784 // BuildRejectPromiseOnException({ | 3652 // BuildRejectPromiseOnException({ |
| 3785 // ... block ... | 3653 // ... block ... |
| 3786 // return %ResolvePromise(.promise, expr), .promise; | 3654 // return %ResolvePromise(.promise, expr), .promise; |
| 3787 // }) | 3655 // }) |
| 3788 // } | 3656 // } |
| 3789 | 3657 |
| 3790 return_value = BuildResolvePromise(return_value, return_value->position()); | 3658 return_value = BuildResolvePromise(return_value, return_value->position()); |
| 3791 block->statements()->Add( | 3659 body->Add( |
| 3792 factory()->NewReturnStatement(return_value, return_value->position()), | 3660 factory()->NewReturnStatement(return_value, return_value->position()), |
| 3793 zone()); | 3661 zone()); |
| 3794 block = BuildRejectPromiseOnException(block); | |
| 3795 body->Add(block, zone()); | |
| 3796 } | 3662 } |
| 3797 | 3663 |
| 3798 Expression* Parser::RewriteAwaitExpression(Expression* value, int await_pos) { | 3664 Expression* Parser::RewriteAwaitExpression(Expression* value, int await_pos) { |
| 3799 // yield do { | 3665 // yield do { |
| 3800 // tmp = <operand>; | 3666 // tmp = <operand>; |
| 3801 // %AsyncFunctionAwait(.generator_object, tmp, .promise); | 3667 // %AsyncFunctionAwait(.generator_object, tmp, .promise); |
| 3802 // .promise | 3668 // .promise |
| 3803 // } | 3669 // } |
| 3804 // The value of the expression is returned to the caller of the async | 3670 // The value of the expression is returned to the caller of the async |
| 3805 // function for the first yield statement; for this, .promise is the | 3671 // function for the first yield statement; for this, .promise is the |
| (...skipping 1221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5027 | 4893 |
| 5028 return final_loop; | 4894 return final_loop; |
| 5029 } | 4895 } |
| 5030 | 4896 |
| 5031 #undef CHECK_OK | 4897 #undef CHECK_OK |
| 5032 #undef CHECK_OK_VOID | 4898 #undef CHECK_OK_VOID |
| 5033 #undef CHECK_FAILED | 4899 #undef CHECK_FAILED |
| 5034 | 4900 |
| 5035 } // namespace internal | 4901 } // namespace internal |
| 5036 } // namespace v8 | 4902 } // namespace v8 |
| OLD | NEW |