Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: src/parsing/parser.cc

Issue 2637403008: [async-iteration] add support for for-await-of loops in Async Functions (Closed)
Patch Set: rebase Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 535 matching lines...) Expand 10 before | Expand all | Expand 10 after
546 info->extension() == nullptr && can_compile_lazily; 546 info->extension() == nullptr && can_compile_lazily;
547 set_allow_natives(FLAG_allow_natives_syntax || info->is_native()); 547 set_allow_natives(FLAG_allow_natives_syntax || info->is_native());
548 set_allow_tailcalls(FLAG_harmony_tailcalls && !info->is_native() && 548 set_allow_tailcalls(FLAG_harmony_tailcalls && !info->is_native() &&
549 info->isolate()->is_tail_call_elimination_enabled()); 549 info->isolate()->is_tail_call_elimination_enabled());
550 set_allow_harmony_do_expressions(FLAG_harmony_do_expressions); 550 set_allow_harmony_do_expressions(FLAG_harmony_do_expressions);
551 set_allow_harmony_function_sent(FLAG_harmony_function_sent); 551 set_allow_harmony_function_sent(FLAG_harmony_function_sent);
552 set_allow_harmony_restrictive_generators(FLAG_harmony_restrictive_generators); 552 set_allow_harmony_restrictive_generators(FLAG_harmony_restrictive_generators);
553 set_allow_harmony_trailing_commas(FLAG_harmony_trailing_commas); 553 set_allow_harmony_trailing_commas(FLAG_harmony_trailing_commas);
554 set_allow_harmony_class_fields(FLAG_harmony_class_fields); 554 set_allow_harmony_class_fields(FLAG_harmony_class_fields);
555 set_allow_harmony_object_rest_spread(FLAG_harmony_object_rest_spread); 555 set_allow_harmony_object_rest_spread(FLAG_harmony_object_rest_spread);
556 set_allow_harmony_async_iteration(FLAG_harmony_async_iteration);
556 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount; 557 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount;
557 ++feature) { 558 ++feature) {
558 use_counts_[feature] = 0; 559 use_counts_[feature] = 0;
559 } 560 }
560 if (info->ast_value_factory() == NULL) { 561 if (info->ast_value_factory() == NULL) {
561 // info takes ownership of AstValueFactory. 562 // info takes ownership of AstValueFactory.
562 info->set_ast_value_factory(new AstValueFactory( 563 info->set_ast_value_factory(new AstValueFactory(
563 zone(), info->isolate()->ast_string_constants(), info->hash_seed())); 564 zone(), info->isolate()->ast_string_constants(), info->hash_seed()));
564 info->set_ast_value_factory_owned(); 565 info->set_ast_value_factory_owned();
565 ast_value_factory_ = info->ast_value_factory(); 566 ast_value_factory_ = info->ast_value_factory();
(...skipping 1245 matching lines...) Expand 10 before | Expand all | Expand 10 after
1811 statement = factory()->NewExpressionStatement( 1812 statement = factory()->NewExpressionStatement(
1812 factory()->NewAssignment(Token::INIT, fproxy, 1813 factory()->NewAssignment(Token::INIT, fproxy,
1813 factory()->NewThisFunction(pos), 1814 factory()->NewThisFunction(pos),
1814 kNoSourcePosition), 1815 kNoSourcePosition),
1815 kNoSourcePosition); 1816 kNoSourcePosition);
1816 } 1817 }
1817 result->Set(index, statement); 1818 result->Set(index, statement);
1818 } 1819 }
1819 } 1820 }
1820 1821
1821 // !%_IsJSReceiver(result = iterator.next()) && 1822 // [if (IteratorType == kNormal)]
1822 // %ThrowIteratorResultNotAnObject(result) 1823 // !%_IsJSReceiver(result = iterator.next()) &&
1824 // %ThrowIteratorResultNotAnObject(result)
1825 // [else if (IteratorType == kAsync)]
1826 // !%_IsJSReceiver(result = Await(iterator.next())) &&
1827 // %ThrowIteratorResultNotAnObject(result)
1828 // [endif]
1823 Expression* Parser::BuildIteratorNextResult(Expression* iterator, 1829 Expression* Parser::BuildIteratorNextResult(Expression* iterator,
1824 Variable* result, int pos) { 1830 Variable* result, IteratorType type,
1831 int pos) {
1825 Expression* next_literal = factory()->NewStringLiteral( 1832 Expression* next_literal = factory()->NewStringLiteral(
1826 ast_value_factory()->next_string(), kNoSourcePosition); 1833 ast_value_factory()->next_string(), kNoSourcePosition);
1827 Expression* next_property = 1834 Expression* next_property =
1828 factory()->NewProperty(iterator, next_literal, kNoSourcePosition); 1835 factory()->NewProperty(iterator, next_literal, kNoSourcePosition);
1829 ZoneList<Expression*>* next_arguments = 1836 ZoneList<Expression*>* next_arguments =
1830 new (zone()) ZoneList<Expression*>(0, zone()); 1837 new (zone()) ZoneList<Expression*>(0, zone());
1831 Expression* next_call = 1838 Expression* next_call =
1832 factory()->NewCall(next_property, next_arguments, pos); 1839 factory()->NewCall(next_property, next_arguments, pos);
1840 if (type == IteratorType::kAsync) {
1841 next_call = RewriteAwaitExpression(next_call, pos);
1842 }
1833 Expression* result_proxy = factory()->NewVariableProxy(result); 1843 Expression* result_proxy = factory()->NewVariableProxy(result);
1834 Expression* left = 1844 Expression* left =
1835 factory()->NewAssignment(Token::ASSIGN, result_proxy, next_call, pos); 1845 factory()->NewAssignment(Token::ASSIGN, result_proxy, next_call, pos);
1836 1846
1837 // %_IsJSReceiver(...) 1847 // %_IsJSReceiver(...)
1838 ZoneList<Expression*>* is_spec_object_args = 1848 ZoneList<Expression*>* is_spec_object_args =
1839 new (zone()) ZoneList<Expression*>(1, zone()); 1849 new (zone()) ZoneList<Expression*>(1, zone());
1840 is_spec_object_args->Add(left, zone()); 1850 is_spec_object_args->Add(left, zone());
1841 Expression* is_spec_object_call = factory()->NewCallRuntime( 1851 Expression* is_spec_object_call = factory()->NewCallRuntime(
1842 Runtime::kInlineIsJSReceiver, is_spec_object_args, pos); 1852 Runtime::kInlineIsJSReceiver, is_spec_object_args, pos);
(...skipping 14 matching lines...) Expand all
1857 1867
1858 Statement* Parser::InitializeForEachStatement(ForEachStatement* stmt, 1868 Statement* Parser::InitializeForEachStatement(ForEachStatement* stmt,
1859 Expression* each, 1869 Expression* each,
1860 Expression* subject, 1870 Expression* subject,
1861 Statement* body, 1871 Statement* body,
1862 int each_keyword_pos) { 1872 int each_keyword_pos) {
1863 ForOfStatement* for_of = stmt->AsForOfStatement(); 1873 ForOfStatement* for_of = stmt->AsForOfStatement();
1864 if (for_of != NULL) { 1874 if (for_of != NULL) {
1865 const bool finalize = true; 1875 const bool finalize = true;
1866 return InitializeForOfStatement(for_of, each, subject, body, finalize, 1876 return InitializeForOfStatement(for_of, each, subject, body, finalize,
1867 each_keyword_pos); 1877 IteratorType::kNormal, each_keyword_pos);
1868 } else { 1878 } else {
1869 if (each->IsArrayLiteral() || each->IsObjectLiteral()) { 1879 if (each->IsArrayLiteral() || each->IsObjectLiteral()) {
1870 Variable* temp = NewTemporary(ast_value_factory()->empty_string()); 1880 Variable* temp = NewTemporary(ast_value_factory()->empty_string());
1871 VariableProxy* temp_proxy = factory()->NewVariableProxy(temp); 1881 VariableProxy* temp_proxy = factory()->NewVariableProxy(temp);
1872 Expression* assign_each = PatternRewriter::RewriteDestructuringAssignment( 1882 Expression* assign_each = PatternRewriter::RewriteDestructuringAssignment(
1873 this, factory()->NewAssignment(Token::ASSIGN, each, temp_proxy, 1883 this, factory()->NewAssignment(Token::ASSIGN, each, temp_proxy,
1874 kNoSourcePosition), 1884 kNoSourcePosition),
1875 scope()); 1885 scope());
1876 auto block = factory()->NewBlock(nullptr, 2, false, kNoSourcePosition); 1886 auto block = factory()->NewBlock(nullptr, 2, false, kNoSourcePosition);
1877 block->statements()->Add( 1887 block->statements()->Add(
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
2011 // INTERNAL variable that's invisible to the debugger 2021 // INTERNAL variable that's invisible to the debugger
2012 // but visible to everything else. 2022 // but visible to everything else.
2013 Declaration* tdz_decl = DeclareVariable(for_info.bound_names[i], LET, 2023 Declaration* tdz_decl = DeclareVariable(for_info.bound_names[i], LET,
2014 kNoSourcePosition, CHECK_OK); 2024 kNoSourcePosition, CHECK_OK);
2015 tdz_decl->proxy()->var()->set_initializer_position(position()); 2025 tdz_decl->proxy()->var()->set_initializer_position(position());
2016 } 2026 }
2017 } 2027 }
2018 return init_block; 2028 return init_block;
2019 } 2029 }
2020 2030
2021 Statement* Parser::InitializeForOfStatement(ForOfStatement* for_of, 2031 Statement* Parser::InitializeForOfStatement(
2022 Expression* each, 2032 ForEachStatement* for_each, Expression* each, Expression* iterable,
2023 Expression* iterable, 2033 Statement* body, bool finalize, IteratorType type, int next_result_pos) {
2024 Statement* body, bool finalize,
2025 int next_result_pos) {
2026 // Create the auxiliary expressions needed for iterating over the iterable, 2034 // Create the auxiliary expressions needed for iterating over the iterable,
2027 // and initialize the given ForOfStatement with them. 2035 // and initialize the given ForOfStatement with them.
2028 // If finalize is true, also instrument the loop with code that performs the 2036 // If finalize is true, also instrument the loop with code that performs the
2029 // proper ES6 iterator finalization. In that case, the result is not 2037 // proper ES6 iterator finalization. In that case, the result is not
2030 // immediately a ForOfStatement. 2038 // immediately a ForOfStatement.
2039 DCHECK(for_each->IsForOfStatement());
2040 ForOfStatement* for_of = for_each->AsForOfStatement();
2031 2041
2032 const int nopos = kNoSourcePosition; 2042 const int nopos = kNoSourcePosition;
2033 auto avfactory = ast_value_factory(); 2043 auto avfactory = ast_value_factory();
2034 2044
2035 Variable* iterator = NewTemporary(avfactory->dot_iterator_string()); 2045 Variable* iterator = NewTemporary(avfactory->dot_iterator_string());
2036 Variable* result = NewTemporary(avfactory->dot_result_string()); 2046 Variable* result = NewTemporary(avfactory->dot_result_string());
2037 Variable* completion = NewTemporary(avfactory->empty_string()); 2047 Variable* completion = NewTemporary(avfactory->empty_string());
2038 2048
2039 // iterator = GetIterator(iterable) 2049 // iterator = GetIterator(iterable, type)
2040 Expression* assign_iterator; 2050 Expression* assign_iterator;
2041 { 2051 {
2042 assign_iterator = factory()->NewAssignment( 2052 assign_iterator = factory()->NewAssignment(
2043 Token::ASSIGN, factory()->NewVariableProxy(iterator), 2053 Token::ASSIGN, factory()->NewVariableProxy(iterator),
2044 factory()->NewGetIterator(iterable, iterable->position()), 2054 factory()->NewGetIterator(iterable, type, iterable->position()),
2045 iterable->position()); 2055 iterable->position());
2046 } 2056 }
2047 2057
2048 // !%_IsJSReceiver(result = iterator.next()) && 2058 // [if (IteratorType == kNormal)]
2049 // %ThrowIteratorResultNotAnObject(result) 2059 // !%_IsJSReceiver(result = iterator.next()) &&
2060 // %ThrowIteratorResultNotAnObject(result)
2061 // [else if (IteratorType == kAsync)]
2062 // !%_IsJSReceiver(result = Await(iterator.next())) &&
2063 // %ThrowIteratorResultNotAnObject(result)
2064 // [endif]
2050 Expression* next_result; 2065 Expression* next_result;
2051 { 2066 {
2052 Expression* iterator_proxy = factory()->NewVariableProxy(iterator); 2067 Expression* iterator_proxy = factory()->NewVariableProxy(iterator);
2053 next_result = 2068 next_result =
2054 BuildIteratorNextResult(iterator_proxy, result, next_result_pos); 2069 BuildIteratorNextResult(iterator_proxy, result, type, next_result_pos);
2055 } 2070 }
2056 2071
2057 // result.done 2072 // result.done
2058 Expression* result_done; 2073 Expression* result_done;
2059 { 2074 {
2060 Expression* done_literal = factory()->NewStringLiteral( 2075 Expression* done_literal = factory()->NewStringLiteral(
2061 ast_value_factory()->done_string(), kNoSourcePosition); 2076 ast_value_factory()->done_string(), kNoSourcePosition);
2062 Expression* result_proxy = factory()->NewVariableProxy(result); 2077 Expression* result_proxy = factory()->NewVariableProxy(result);
2063 result_done = 2078 result_done =
2064 factory()->NewProperty(result_proxy, done_literal, kNoSourcePosition); 2079 factory()->NewProperty(result_proxy, done_literal, kNoSourcePosition);
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
2132 // Statement* body (gets overwritten) 2147 // Statement* body (gets overwritten)
2133 if (finalize) { 2148 if (finalize) {
2134 Block* block = factory()->NewBlock(nullptr, 2, false, nopos); 2149 Block* block = factory()->NewBlock(nullptr, 2, false, nopos);
2135 block->statements()->Add(body, zone()); 2150 block->statements()->Add(body, zone());
2136 block->statements()->Add(set_completion_normal, zone()); 2151 block->statements()->Add(set_completion_normal, zone());
2137 body = block; 2152 body = block;
2138 } 2153 }
2139 2154
2140 for_of->Initialize(body, iterator, assign_iterator, next_result, result_done, 2155 for_of->Initialize(body, iterator, assign_iterator, next_result, result_done,
2141 assign_each); 2156 assign_each);
2142 return finalize ? FinalizeForOfStatement(for_of, completion, nopos) : for_of; 2157 return finalize ? FinalizeForOfStatement(for_of, completion, type, nopos)
2158 : for_of;
2143 } 2159 }
2144 2160
2145 Statement* Parser::DesugarLexicalBindingsInForStatement( 2161 Statement* Parser::DesugarLexicalBindingsInForStatement(
2146 ForStatement* loop, Statement* init, Expression* cond, Statement* next, 2162 ForStatement* loop, Statement* init, Expression* cond, Statement* next,
2147 Statement* body, Scope* inner_scope, const ForInfo& for_info, bool* ok) { 2163 Statement* body, Scope* inner_scope, const ForInfo& for_info, bool* ok) {
2148 // ES6 13.7.4.8 specifies that on each loop iteration the let variables are 2164 // ES6 13.7.4.8 specifies that on each loop iteration the let variables are
2149 // copied into a new environment. Moreover, the "next" statement must be 2165 // copied into a new environment. Moreover, the "next" statement must be
2150 // evaluated not in the environment of the just completed iteration but in 2166 // evaluated not in the environment of the just completed iteration but in
2151 // that of the upcoming one. We achieve this with the following desugaring. 2167 // that of the upcoming one. We achieve this with the following desugaring.
2152 // Extra care is needed to preserve the completion value of the original loop. 2168 // Extra care is needed to preserve the completion value of the original loop.
(...skipping 644 matching lines...) Expand 10 before | Expand all | Expand 10 after
2797 reusable_preparser_ = new PreParser( 2813 reusable_preparser_ = new PreParser(
2798 zone(), &scanner_, stack_limit_, ast_value_factory(), 2814 zone(), &scanner_, stack_limit_, ast_value_factory(),
2799 &pending_error_handler_, runtime_call_stats_, parsing_on_main_thread_); 2815 &pending_error_handler_, runtime_call_stats_, parsing_on_main_thread_);
2800 #define SET_ALLOW(name) reusable_preparser_->set_allow_##name(allow_##name()); 2816 #define SET_ALLOW(name) reusable_preparser_->set_allow_##name(allow_##name());
2801 SET_ALLOW(natives); 2817 SET_ALLOW(natives);
2802 SET_ALLOW(harmony_do_expressions); 2818 SET_ALLOW(harmony_do_expressions);
2803 SET_ALLOW(harmony_function_sent); 2819 SET_ALLOW(harmony_function_sent);
2804 SET_ALLOW(harmony_trailing_commas); 2820 SET_ALLOW(harmony_trailing_commas);
2805 SET_ALLOW(harmony_class_fields); 2821 SET_ALLOW(harmony_class_fields);
2806 SET_ALLOW(harmony_object_rest_spread); 2822 SET_ALLOW(harmony_object_rest_spread);
2823 SET_ALLOW(harmony_async_iteration);
2807 #undef SET_ALLOW 2824 #undef SET_ALLOW
2808 } 2825 }
2809 // Aborting inner function preparsing would leave scopes in an inconsistent 2826 // Aborting inner function preparsing would leave scopes in an inconsistent
2810 // state; we don't parse inner functions in the abortable mode anyway. 2827 // state; we don't parse inner functions in the abortable mode anyway.
2811 DCHECK(!is_inner_function || !may_abort); 2828 DCHECK(!is_inner_function || !may_abort);
2812 2829
2813 PreParser::PreParseResult result = reusable_preparser_->PreParseFunction( 2830 PreParser::PreParseResult result = reusable_preparser_->PreParseFunction(
2814 kind, function_scope, parsing_module_, is_inner_function, may_abort, 2831 kind, function_scope, parsing_module_, is_inner_function, may_abort,
2815 use_counts_); 2832 use_counts_);
2816 2833
(...skipping 1238 matching lines...) Expand 10 before | Expand all | Expand 10 after
4055 factory()->NewCallRuntime(Runtime::kAppendElement, 4072 factory()->NewCallRuntime(Runtime::kAppendElement,
4056 append_element_args, kNoSourcePosition), 4073 append_element_args, kNoSourcePosition),
4057 kNoSourcePosition); 4074 kNoSourcePosition);
4058 } 4075 }
4059 // for (each of spread) %AppendElement($R, each) 4076 // for (each of spread) %AppendElement($R, each)
4060 ForEachStatement* loop = factory()->NewForEachStatement( 4077 ForEachStatement* loop = factory()->NewForEachStatement(
4061 ForEachStatement::ITERATE, nullptr, kNoSourcePosition); 4078 ForEachStatement::ITERATE, nullptr, kNoSourcePosition);
4062 const bool finalize = false; 4079 const bool finalize = false;
4063 InitializeForOfStatement(loop->AsForOfStatement(), 4080 InitializeForOfStatement(loop->AsForOfStatement(),
4064 factory()->NewVariableProxy(each), subject, 4081 factory()->NewVariableProxy(each), subject,
4065 append_body, finalize); 4082 append_body, finalize, IteratorType::kNormal);
4066 do_block->statements()->Add(loop, zone()); 4083 do_block->statements()->Add(loop, zone());
4067 } 4084 }
4068 } 4085 }
4069 // Now, rewind the original array literal to truncate everything from the 4086 // Now, rewind the original array literal to truncate everything from the
4070 // first spread (included) until the end. This fixes $R's initialization. 4087 // first spread (included) until the end. This fixes $R's initialization.
4071 lit->RewindSpreads(); 4088 lit->RewindSpreads();
4072 return factory()->NewDoExpression(do_block, result, lit->position()); 4089 return factory()->NewDoExpression(do_block, result, lit->position());
4073 } 4090 }
4074 4091
4075 void Parser::QueueDestructuringAssignmentForRewriting(Expression* expr) { 4092 void Parser::QueueDestructuringAssignmentForRewriting(Expression* expr) {
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
4247 Expression* assignment = 4264 Expression* assignment =
4248 factory()->NewAssignment(Token::ASSIGN, output_proxy, 4265 factory()->NewAssignment(Token::ASSIGN, output_proxy,
4249 factory()->NewUndefinedLiteral(nopos), nopos); 4266 factory()->NewUndefinedLiteral(nopos), nopos);
4250 initialize_output = factory()->NewExpressionStatement(assignment, nopos); 4267 initialize_output = factory()->NewExpressionStatement(assignment, nopos);
4251 } 4268 }
4252 4269
4253 // let iterator = GetIterator(iterable); 4270 // let iterator = GetIterator(iterable);
4254 Variable* var_iterator = NewTemporary(ast_value_factory()->empty_string()); 4271 Variable* var_iterator = NewTemporary(ast_value_factory()->empty_string());
4255 Statement* get_iterator; 4272 Statement* get_iterator;
4256 { 4273 {
4257 Expression* iterator = factory()->NewGetIterator(iterable, nopos); 4274 Expression* iterator =
4275 factory()->NewGetIterator(iterable, GetIterator::Hint::kNormal, nopos);
4258 Expression* iterator_proxy = factory()->NewVariableProxy(var_iterator); 4276 Expression* iterator_proxy = factory()->NewVariableProxy(var_iterator);
4259 Expression* assignment = factory()->NewAssignment( 4277 Expression* assignment = factory()->NewAssignment(
4260 Token::ASSIGN, iterator_proxy, iterator, nopos); 4278 Token::ASSIGN, iterator_proxy, iterator, nopos);
4261 get_iterator = factory()->NewExpressionStatement(assignment, nopos); 4279 get_iterator = factory()->NewExpressionStatement(assignment, nopos);
4262 } 4280 }
4263 4281
4264 // output = iterator.next(input); 4282 // output = iterator.next(input);
4265 Statement* call_next; 4283 Statement* call_next;
4266 { 4284 {
4267 Expression* iterator_proxy = factory()->NewVariableProxy(var_iterator); 4285 Expression* iterator_proxy = factory()->NewVariableProxy(var_iterator);
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
4329 Token::EQ, factory()->NewVariableProxy(var_throw), 4347 Token::EQ, factory()->NewVariableProxy(var_throw),
4330 factory()->NewNullLiteral(nopos), nopos); 4348 factory()->NewNullLiteral(nopos), nopos);
4331 Expression* call = 4349 Expression* call =
4332 NewThrowTypeError(MessageTemplate::kThrowMethodMissing, 4350 NewThrowTypeError(MessageTemplate::kThrowMethodMissing,
4333 ast_value_factory()->empty_string(), nopos); 4351 ast_value_factory()->empty_string(), nopos);
4334 Statement* throw_call = factory()->NewExpressionStatement(call, nopos); 4352 Statement* throw_call = factory()->NewExpressionStatement(call, nopos);
4335 4353
4336 Block* then = factory()->NewBlock(nullptr, 4 + 1, false, nopos); 4354 Block* then = factory()->NewBlock(nullptr, 4 + 1, false, nopos);
4337 BuildIteratorCloseForCompletion( 4355 BuildIteratorCloseForCompletion(
4338 scope(), then->statements(), var_iterator, 4356 scope(), then->statements(), var_iterator,
4339 factory()->NewSmiLiteral(Parser::kNormalCompletion, nopos)); 4357 factory()->NewSmiLiteral(Parser::kNormalCompletion, nopos),
4358 IteratorType::kNormal);
4340 then->statements()->Add(throw_call, zone()); 4359 then->statements()->Add(throw_call, zone());
4341 check_throw = factory()->NewIfStatement( 4360 check_throw = factory()->NewIfStatement(
4342 condition, then, factory()->NewEmptyStatement(nopos), nopos); 4361 condition, then, factory()->NewEmptyStatement(nopos), nopos);
4343 } 4362 }
4344 4363
4345 // output = %_Call(iteratorThrow, iterator, input); 4364 // output = %_Call(iteratorThrow, iterator, input);
4346 Statement* call_throw; 4365 Statement* call_throw;
4347 { 4366 {
4348 auto args = new (zone()) ZoneList<Expression*>(3, zone()); 4367 auto args = new (zone()) ZoneList<Expression*>(3, zone());
4349 args->Add(factory()->NewVariableProxy(var_throw), zone()); 4368 args->Add(factory()->NewVariableProxy(var_throw), zone());
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
4608 Variable* iterator, Variable* input, 4627 Variable* iterator, Variable* input,
4609 Variable* var_output) { 4628 Variable* var_output) {
4610 // 4629 //
4611 // This function adds four statements to [statements], corresponding to the 4630 // This function adds four statements to [statements], corresponding to the
4612 // following code: 4631 // following code:
4613 // 4632 //
4614 // let iteratorReturn = iterator.return; 4633 // let iteratorReturn = iterator.return;
4615 // if (IS_NULL_OR_UNDEFINED(iteratorReturn) { 4634 // if (IS_NULL_OR_UNDEFINED(iteratorReturn) {
4616 // return {value: input, done: true}; 4635 // return {value: input, done: true};
4617 // } 4636 // }
4618 // output = %_Call(iteratorReturn, iterator, input); 4637 //
4638 // [if (IteratorType == kAsync)]
4639 // output = Await(%_Call(iteratorReturn, iterator, input));
4640 // [else]
4641 // output = %_Call(iteratorReturn, iterator, input);
4642 // [endif]
4643 //
4619 // if (!IS_RECEIVER(output)) %ThrowIterResultNotAnObject(output); 4644 // if (!IS_RECEIVER(output)) %ThrowIterResultNotAnObject(output);
4620 // 4645 //
4621 4646
4622 const int nopos = kNoSourcePosition; 4647 const int nopos = kNoSourcePosition;
4623 4648
4624 // let iteratorReturn = iterator.return; 4649 // let iteratorReturn = iterator.return;
4625 Variable* var_return = var_output; // Reusing the output variable. 4650 Variable* var_return = var_output; // Reusing the output variable.
4626 Statement* get_return; 4651 Statement* get_return;
4627 { 4652 {
4628 Expression* iterator_proxy = factory()->NewVariableProxy(iterator); 4653 Expression* iterator_proxy = factory()->NewVariableProxy(iterator);
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
4696 } 4721 }
4697 4722
4698 statements->Add(get_return, zone()); 4723 statements->Add(get_return, zone());
4699 statements->Add(check_return, zone()); 4724 statements->Add(check_return, zone());
4700 statements->Add(call_return, zone()); 4725 statements->Add(call_return, zone());
4701 statements->Add(validate_output, zone()); 4726 statements->Add(validate_output, zone());
4702 } 4727 }
4703 4728
4704 void Parser::FinalizeIteratorUse(Scope* use_scope, Variable* completion, 4729 void Parser::FinalizeIteratorUse(Scope* use_scope, Variable* completion,
4705 Expression* condition, Variable* iter, 4730 Expression* condition, Variable* iter,
4706 Block* iterator_use, Block* target) { 4731 Block* iterator_use, Block* target,
4732 IteratorType type) {
4707 // 4733 //
4708 // This function adds two statements to [target], corresponding to the 4734 // This function adds two statements to [target], corresponding to the
4709 // following code: 4735 // following code:
4710 // 4736 //
4711 // completion = kNormalCompletion; 4737 // completion = kNormalCompletion;
4712 // try { 4738 // try {
4713 // try { 4739 // try {
4714 // iterator_use 4740 // iterator_use
4715 // } catch(e) { 4741 // } catch(e) {
4716 // if (completion === kAbruptCompletion) completion = kThrowCompletion; 4742 // if (completion === kAbruptCompletion) completion = kThrowCompletion;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
4752 condition, statement, factory()->NewEmptyStatement(nopos), nopos); 4778 condition, statement, factory()->NewEmptyStatement(nopos), nopos);
4753 } 4779 }
4754 4780
4755 // if (condition) { 4781 // if (condition) {
4756 // #BuildIteratorCloseForCompletion(iter, completion) 4782 // #BuildIteratorCloseForCompletion(iter, completion)
4757 // } 4783 // }
4758 Block* maybe_close; 4784 Block* maybe_close;
4759 { 4785 {
4760 Block* block = factory()->NewBlock(nullptr, 2, true, nopos); 4786 Block* block = factory()->NewBlock(nullptr, 2, true, nopos);
4761 Expression* proxy = factory()->NewVariableProxy(completion); 4787 Expression* proxy = factory()->NewVariableProxy(completion);
4762 BuildIteratorCloseForCompletion(use_scope, block->statements(), iter, 4788 BuildIteratorCloseForCompletion(use_scope, block->statements(), iter, proxy,
4763 proxy); 4789 type);
4764 DCHECK(block->statements()->length() == 2); 4790 DCHECK(block->statements()->length() == 2);
4765 4791
4766 maybe_close = factory()->NewBlock(nullptr, 1, true, nopos); 4792 maybe_close = factory()->NewBlock(nullptr, 1, true, nopos);
4767 maybe_close->statements()->Add( 4793 maybe_close->statements()->Add(
4768 factory()->NewIfStatement(condition, block, 4794 factory()->NewIfStatement(condition, block,
4769 factory()->NewEmptyStatement(nopos), nopos), 4795 factory()->NewEmptyStatement(nopos), nopos),
4770 zone()); 4796 zone());
4771 } 4797 }
4772 4798
4773 // try { #try_block } 4799 // try { #try_block }
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
4812 factory()->NewTryFinallyStatement(try_block, maybe_close, nopos); 4838 factory()->NewTryFinallyStatement(try_block, maybe_close, nopos);
4813 } 4839 }
4814 4840
4815 target->statements()->Add(initialize_completion, zone()); 4841 target->statements()->Add(initialize_completion, zone());
4816 target->statements()->Add(try_finally, zone()); 4842 target->statements()->Add(try_finally, zone());
4817 } 4843 }
4818 4844
4819 void Parser::BuildIteratorCloseForCompletion(Scope* scope, 4845 void Parser::BuildIteratorCloseForCompletion(Scope* scope,
4820 ZoneList<Statement*>* statements, 4846 ZoneList<Statement*>* statements,
4821 Variable* iterator, 4847 Variable* iterator,
4822 Expression* completion) { 4848 Expression* completion,
4849 IteratorType type) {
4823 // 4850 //
4824 // This function adds two statements to [statements], corresponding to the 4851 // This function adds two statements to [statements], corresponding to the
4825 // following code: 4852 // following code:
4826 // 4853 //
4827 // let iteratorReturn = iterator.return; 4854 // let iteratorReturn = iterator.return;
4828 // if (!IS_NULL_OR_UNDEFINED(iteratorReturn)) { 4855 // if (!IS_NULL_OR_UNDEFINED(iteratorReturn)) {
4829 // if (completion === kThrowCompletion) { 4856 // if (completion === kThrowCompletion) {
4830 // if (!IS_CALLABLE(iteratorReturn)) { 4857 // if (!IS_CALLABLE(iteratorReturn)) {
4831 // throw MakeTypeError(kReturnMethodNotCallable); 4858 // throw MakeTypeError(kReturnMethodNotCallable);
4832 // } 4859 // }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
4870 // try { %_Call(iteratorReturn, iterator) } catch (_) { } 4897 // try { %_Call(iteratorReturn, iterator) } catch (_) { }
4871 Statement* try_call_return; 4898 Statement* try_call_return;
4872 { 4899 {
4873 auto args = new (zone()) ZoneList<Expression*>(2, zone()); 4900 auto args = new (zone()) ZoneList<Expression*>(2, zone());
4874 args->Add(factory()->NewVariableProxy(var_return), zone()); 4901 args->Add(factory()->NewVariableProxy(var_return), zone());
4875 args->Add(factory()->NewVariableProxy(iterator), zone()); 4902 args->Add(factory()->NewVariableProxy(iterator), zone());
4876 4903
4877 Expression* call = 4904 Expression* call =
4878 factory()->NewCallRuntime(Runtime::kInlineCall, args, nopos); 4905 factory()->NewCallRuntime(Runtime::kInlineCall, args, nopos);
4879 4906
4907 if (type == IteratorType::kAsync) {
4908 call = RewriteAwaitExpression(call, nopos);
4909 }
4910
4880 Block* try_block = factory()->NewBlock(nullptr, 1, false, nopos); 4911 Block* try_block = factory()->NewBlock(nullptr, 1, false, nopos);
4881 try_block->statements()->Add(factory()->NewExpressionStatement(call, nopos), 4912 try_block->statements()->Add(factory()->NewExpressionStatement(call, nopos),
4882 zone()); 4913 zone());
4883 4914
4884 Block* catch_block = factory()->NewBlock(nullptr, 0, false, nopos); 4915 Block* catch_block = factory()->NewBlock(nullptr, 0, false, nopos);
4885 4916
4886 Scope* catch_scope = NewScopeWithParent(scope, CATCH_SCOPE); 4917 Scope* catch_scope = NewScopeWithParent(scope, CATCH_SCOPE);
4887 Variable* catch_variable = 4918 Variable* catch_variable =
4888 catch_scope->DeclareLocal(ast_value_factory()->dot_catch_string(), VAR); 4919 catch_scope->DeclareLocal(ast_value_factory()->dot_catch_string(), VAR);
4889 catch_scope->set_is_hidden(); 4920 catch_scope->set_is_hidden();
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
4969 maybe_call_return = factory()->NewIfStatement( 5000 maybe_call_return = factory()->NewIfStatement(
4970 condition, factory()->NewEmptyStatement(nopos), call_return_carefully, 5001 condition, factory()->NewEmptyStatement(nopos), call_return_carefully,
4971 nopos); 5002 nopos);
4972 } 5003 }
4973 5004
4974 statements->Add(get_return, zone()); 5005 statements->Add(get_return, zone());
4975 statements->Add(maybe_call_return, zone()); 5006 statements->Add(maybe_call_return, zone());
4976 } 5007 }
4977 5008
4978 Statement* Parser::FinalizeForOfStatement(ForOfStatement* loop, 5009 Statement* Parser::FinalizeForOfStatement(ForOfStatement* loop,
4979 Variable* var_completion, int pos) { 5010 Variable* var_completion,
5011 IteratorType type, int pos) {
4980 // 5012 //
4981 // This function replaces the loop with the following wrapping: 5013 // This function replaces the loop with the following wrapping:
4982 // 5014 //
4983 // completion = kNormalCompletion; 5015 // completion = kNormalCompletion;
4984 // try { 5016 // try {
4985 // try { 5017 // try {
4986 // #loop; 5018 // #loop;
4987 // } catch(e) { 5019 // } catch(e) {
4988 // if (completion === kAbruptCompletion) completion = kThrowCompletion; 5020 // if (completion === kAbruptCompletion) completion = kThrowCompletion;
4989 // %ReThrow(e); 5021 // %ReThrow(e);
(...skipping 23 matching lines...) Expand all
5013 { 5045 {
5014 Block* try_block = factory()->NewBlock(nullptr, 1, false, nopos); 5046 Block* try_block = factory()->NewBlock(nullptr, 1, false, nopos);
5015 try_block->statements()->Add(loop, zone()); 5047 try_block->statements()->Add(loop, zone());
5016 5048
5017 // The scope in which the parser creates this loop. 5049 // The scope in which the parser creates this loop.
5018 Scope* loop_scope = scope()->outer_scope(); 5050 Scope* loop_scope = scope()->outer_scope();
5019 DCHECK_EQ(loop_scope->scope_type(), BLOCK_SCOPE); 5051 DCHECK_EQ(loop_scope->scope_type(), BLOCK_SCOPE);
5020 DCHECK_EQ(scope()->scope_type(), BLOCK_SCOPE); 5052 DCHECK_EQ(scope()->scope_type(), BLOCK_SCOPE);
5021 5053
5022 FinalizeIteratorUse(loop_scope, var_completion, closing_condition, 5054 FinalizeIteratorUse(loop_scope, var_completion, closing_condition,
5023 loop->iterator(), try_block, final_loop); 5055 loop->iterator(), try_block, final_loop, type);
5024 } 5056 }
5025 5057
5026 return final_loop; 5058 return final_loop;
5027 } 5059 }
5028 5060
5029 #undef CHECK_OK 5061 #undef CHECK_OK
5030 #undef CHECK_OK_VOID 5062 #undef CHECK_OK_VOID
5031 #undef CHECK_FAILED 5063 #undef CHECK_FAILED
5032 5064
5033 } // namespace internal 5065 } // namespace internal
5034 } // namespace v8 5066 } // namespace v8
OLDNEW
« no previous file with comments | « src/parsing/parser.h ('k') | src/parsing/parser-base.h » ('j') | src/parsing/parser-base.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698