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

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

Issue 2334323006: Fix async/await memory leak (Closed)
Patch Set: Rebase on master, and changes for review Created 4 years, 3 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
« no previous file with comments | « src/js/harmony-async-await.js ('k') | test/mjsunit/harmony/async-await-loop.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 4361 matching lines...) Expand 10 before | Expand all | Expand 10 after
4372 Expression* expr = args->at(0); 4372 Expression* expr = args->at(0);
4373 for (int i = 1; i < args->length(); ++i) { 4373 for (int i = 1; i < args->length(); ++i) {
4374 expr = factory()->NewBinaryOperation(Token::COMMA, expr, args->at(i), 4374 expr = factory()->NewBinaryOperation(Token::COMMA, expr, args->at(i),
4375 expr->position()); 4375 expr->position());
4376 } 4376 }
4377 return expr; 4377 return expr;
4378 } 4378 }
4379 4379
4380 Expression* Parser::RewriteAwaitExpression(Expression* value, int await_pos) { 4380 Expression* Parser::RewriteAwaitExpression(Expression* value, int await_pos) {
4381 // yield do { 4381 // yield do {
4382 // promise_tmp = .promise;
4382 // tmp = <operand>; 4383 // tmp = <operand>;
4383 // tmp = %AsyncFunctionAwait(.generator_object, tmp); 4384 // %AsyncFunctionAwait(.generator_object, tmp);
4385 // promise_tmp
4384 // } 4386 // }
4387 // The operand needs to be evaluated on a separate statement in order to get
4388 // a break location, and the .promise needs to be read earlier so that it
adamk 2016/09/16 17:24:47 The ".promise needs to be read earlier" I don't un
Dan Ehrenberg 2016/09/16 17:42:14 This shows up in locations printed in the stack tr
4389 // doesn't insert a false break location.
4390 // The value of the expression is returned to the caller of the async
4391 // function for the first yield statement; for this, .promise is the
4392 // appropriate return value, being a Promise that will be fulfilled or
4393 // rejected with the appropriate value by the desugaring. Subsequent yield
4394 // occurrences will return to the AsyncFunctionNext call within the
4395 // implemementation of the intermediate throwaway Promise's then handler.
4396 // This handler has nothing useful to do with the value, as the Promise is
4397 // ignored. If we yielded the value of the throwawayPromise that
4398 // AsyncFunctionAwait creates as an intermediate, it would create a memory
4399 // leak; we must return .promise instead;
4385 Variable* generator_object_variable = 4400 Variable* generator_object_variable =
4386 function_state_->generator_object_variable(); 4401 function_state_->generator_object_variable();
4387 4402
4388 // If generator_object_variable is null, 4403 // If generator_object_variable is null,
4389 if (!generator_object_variable) return value; 4404 if (!generator_object_variable) return value;
4390 4405
4391 const int nopos = kNoSourcePosition; 4406 const int nopos = kNoSourcePosition;
4392 4407
4393 Variable* temp_var = NewTemporary(ast_value_factory()->empty_string()); 4408 Variable* temp_var = NewTemporary(ast_value_factory()->empty_string());
4394 Block* do_block = factory()->NewBlock(nullptr, 2, false, nopos); 4409 Block* do_block = factory()->NewBlock(nullptr, 2, false, nopos);
4395 4410
4396 // Wrap value evaluation to provide a break location. 4411 // Wrap value evaluation to provide a break location.
4412 Variable* promise_temp_var =
4413 NewTemporary(ast_value_factory()->empty_string());
4414 Expression* promise_assignment = factory()->NewAssignment(
4415 Token::ASSIGN, factory()->NewVariableProxy(promise_temp_var),
4416 BuildDotPromise(), nopos);
4417 do_block->statements()->Add(
4418 factory()->NewExpressionStatement(promise_assignment, value->position()),
adamk 2016/09/16 17:24:47 This looks weird to me, why does this have a non-k
Dan Ehrenberg 2016/09/16 17:42:13 Oops, somehow lost the change to make it nopos in
4419 zone());
4420
4397 Expression* value_assignment = factory()->NewAssignment( 4421 Expression* value_assignment = factory()->NewAssignment(
4398 Token::ASSIGN, factory()->NewVariableProxy(temp_var), value, nopos); 4422 Token::ASSIGN, factory()->NewVariableProxy(temp_var), value, nopos);
4399 do_block->statements()->Add( 4423 do_block->statements()->Add(
4400 factory()->NewExpressionStatement(value_assignment, value->position()), 4424 factory()->NewExpressionStatement(value_assignment, value->position()),
4401 zone()); 4425 zone());
4402 4426
4403 ZoneList<Expression*>* async_function_await_args = 4427 ZoneList<Expression*>* async_function_await_args =
4404 new (zone()) ZoneList<Expression*>(2, zone()); 4428 new (zone()) ZoneList<Expression*>(2, zone());
4405 Expression* generator_object = 4429 Expression* generator_object =
4406 factory()->NewVariableProxy(generator_object_variable); 4430 factory()->NewVariableProxy(generator_object_variable);
4407 async_function_await_args->Add(generator_object, zone()); 4431 async_function_await_args->Add(generator_object, zone());
4408 async_function_await_args->Add(factory()->NewVariableProxy(temp_var), zone()); 4432 async_function_await_args->Add(factory()->NewVariableProxy(temp_var), zone());
4409 4433
4410 // The parser emits calls to AsyncFunctionAwaitCaught, but the 4434 // The parser emits calls to AsyncFunctionAwaitCaught, but the
4411 // AstNumberingVisitor will rewrite this to AsyncFunctionAwaitUncaught 4435 // AstNumberingVisitor will rewrite this to AsyncFunctionAwaitUncaught
4412 // if there is no local enclosing try/catch block. 4436 // if there is no local enclosing try/catch block.
4413 Expression* async_function_await = 4437 Expression* async_function_await =
4414 factory()->NewCallRuntime(Context::ASYNC_FUNCTION_AWAIT_CAUGHT_INDEX, 4438 factory()->NewCallRuntime(Context::ASYNC_FUNCTION_AWAIT_CAUGHT_INDEX,
4415 async_function_await_args, nopos); 4439 async_function_await_args, nopos);
4440 do_block->statements()->Add(
4441 factory()->NewExpressionStatement(async_function_await, await_pos),
4442 zone());
4416 4443
4417 // Wrap await to provide a break location between value evaluation and yield. 4444 // Wrap await to provide a break location between value evaluation and yield.
4418 Expression* await_assignment = factory()->NewAssignment( 4445 Expression* do_expr =
4419 Token::ASSIGN, factory()->NewVariableProxy(temp_var), 4446 factory()->NewDoExpression(do_block, promise_temp_var, nopos);
4420 async_function_await, nopos);
4421 do_block->statements()->Add(
4422 factory()->NewExpressionStatement(await_assignment, await_pos), zone());
4423 Expression* do_expr = factory()->NewDoExpression(do_block, temp_var, nopos);
4424 4447
4425 generator_object = factory()->NewVariableProxy(generator_object_variable); 4448 generator_object = factory()->NewVariableProxy(generator_object_variable);
4426 return factory()->NewYield(generator_object, do_expr, nopos, 4449 return factory()->NewYield(generator_object, do_expr, nopos,
4427 Yield::kOnExceptionRethrow); 4450 Yield::kOnExceptionRethrow);
4428 } 4451 }
4429 4452
4430 class NonPatternRewriter : public AstExpressionRewriter { 4453 class NonPatternRewriter : public AstExpressionRewriter {
4431 public: 4454 public:
4432 NonPatternRewriter(uintptr_t stack_limit, Parser* parser) 4455 NonPatternRewriter(uintptr_t stack_limit, Parser* parser)
4433 : AstExpressionRewriter(stack_limit), parser_(parser) {} 4456 : AstExpressionRewriter(stack_limit), parser_(parser) {}
(...skipping 1193 matching lines...) Expand 10 before | Expand all | Expand 10 after
5627 node->Print(Isolate::Current()); 5650 node->Print(Isolate::Current());
5628 } 5651 }
5629 #endif // DEBUG 5652 #endif // DEBUG
5630 5653
5631 #undef CHECK_OK 5654 #undef CHECK_OK
5632 #undef CHECK_OK_VOID 5655 #undef CHECK_OK_VOID
5633 #undef CHECK_FAILED 5656 #undef CHECK_FAILED
5634 5657
5635 } // namespace internal 5658 } // namespace internal
5636 } // namespace v8 5659 } // namespace v8
OLDNEW
« no previous file with comments | « src/js/harmony-async-await.js ('k') | test/mjsunit/harmony/async-await-loop.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698