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

Unified Diff: src/parsing/parser.cc

Issue 2664083002: [ignition] desugar async functions/generators/modules in BytecodeGenerator
Patch Set: get rid of lambdas, for better or worse.. Created 3 years, 10 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 side-by-side diff with in-line comments
Download patch
Index: src/parsing/parser.cc
diff --git a/src/parsing/parser.cc b/src/parsing/parser.cc
index efb76fd5d8ac0eccf8b14e223ab4e3b0bae84ae1..b3e702964ebab20480794156f590f30b84cdbfbd 100644
--- a/src/parsing/parser.cc
+++ b/src/parsing/parser.cc
@@ -699,11 +699,6 @@ FunctionLiteral* Parser::DoParseProgram(ParseInfo* info) {
var->AllocateTo(VariableLocation::PARAMETER, 0);
PrepareGeneratorVariables();
- Expression* initial_yield =
- BuildInitialYield(kNoSourcePosition, kGeneratorFunction);
- body->Add(
- factory()->NewExpressionStatement(initial_yield, kNoSourcePosition),
- zone());
ParseModuleItemList(body, &ok);
ok = ok &&
@@ -1777,53 +1772,35 @@ void Parser::ParseAndRewriteGeneratorFunctionBody(int pos, FunctionKind kind,
// - If the generator terminates for whatever reason, we must close it.
// Hence the finally clause.
- Block* try_block = factory()->NewBlock(nullptr, 3, false, kNoSourcePosition);
- Expression* initial_yield = BuildInitialYield(pos, kind);
- try_block->statements()->Add(
- factory()->NewExpressionStatement(initial_yield, kNoSourcePosition),
- zone());
- ParseStatementList(try_block->statements(), Token::RBRACE, ok);
+ ParseStatementList(body, Token::RBRACE, ok);
if (!*ok) return;
Statement* final_return = factory()->NewReturnStatement(
BuildIteratorResult(nullptr, true), kNoSourcePosition);
- try_block->statements()->Add(final_return, zone());
-
- Block* finally_block =
- factory()->NewBlock(nullptr, 1, false, kNoSourcePosition);
- ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(1, zone());
- VariableProxy* call_proxy =
- factory()->NewVariableProxy(function_state_->generator_object_variable());
- args->Add(call_proxy, zone());
- Expression* call = factory()->NewCallRuntime(Runtime::kInlineGeneratorClose,
- args, kNoSourcePosition);
- finally_block->statements()->Add(
- factory()->NewExpressionStatement(call, kNoSourcePosition), zone());
-
- body->Add(factory()->NewTryFinallyStatement(try_block, finally_block,
- kNoSourcePosition),
- zone());
+ body->Add(final_return, zone());
}
-void Parser::CreateFunctionNameAssignment(
- const AstRawString* function_name, int pos,
+void Parser::CreateFunctionNameVariable(
+ const AstRawString* function_name,
FunctionLiteral::FunctionType function_type,
- DeclarationScope* function_scope, ZoneList<Statement*>* result, int index) {
+ DeclarationScope* function_scope) {
if (function_type == FunctionLiteral::kNamedExpression) {
- StatementT statement = factory()->NewEmptyStatement(kNoSourcePosition);
if (function_scope->LookupLocal(function_name) == nullptr) {
// Now that we know the language mode, we can create the const assignment
// in the previously reserved spot.
DCHECK_EQ(function_scope, scope());
Variable* fvar = function_scope->DeclareFunctionVar(function_name);
- VariableProxy* fproxy = factory()->NewVariableProxy(fvar);
- statement = factory()->NewExpressionStatement(
- factory()->NewAssignment(Token::INIT, fproxy,
- factory()->NewThisFunction(pos),
- kNoSourcePosition),
- kNoSourcePosition);
+ DCHECK_NOT_NULL(fvar);
+
+ // Ensure the variable is allocated if any inner scope _may_ use it.
+ // Otherwise, lazily parsed
+ if (function_scope->calls_eval() ||
+ function_scope->inner_scope_calls_eval() ||
+ function_scope->HasLazilyParsedInnerFunctionScope()) {
+ fvar->set_is_used();
+ return;
+ }
}
- result->Set(index, statement);
}
}
@@ -2480,9 +2457,9 @@ void Parser::PrepareGeneratorVariables() {
// Calling a generator returns a generator object. That object is stored
// in a temporary variable, a definition that is used by "yield"
// expressions.
- Variable* temp =
- NewTemporary(ast_value_factory()->dot_generator_object_string());
- function_state_->set_generator_object_variable(temp);
+ Variable* temp = function_state_->scope()->DeclareGeneratorObjectVar(
+ ast_value_factory()->dot_generator_object_string());
+ temp->set_is_used();
}
FunctionLiteral* Parser::ParseFunctionLiteral(
@@ -2599,6 +2576,7 @@ FunctionLiteral* Parser::ParseFunctionLiteral(
use_temp_zone && FLAG_lazy_inner_functions && !is_lazy_top_level_function;
ZoneList<Statement*>* body = nullptr;
+ Block* parameter_init_block = nullptr;
int materialized_literal_count = -1;
int expected_property_count = -1;
bool should_be_used_once_hint = false;
@@ -2670,7 +2648,7 @@ FunctionLiteral* Parser::ParseFunctionLiteral(
if (!is_lazy_top_level_function && !is_lazy_inner_function) {
body = ParseFunction(
function_name, pos, kind, function_type, scope, &num_parameters,
- &function_length, &has_duplicate_parameters,
+ &parameter_init_block, &function_length, &has_duplicate_parameters,
&materialized_literal_count, &expected_property_count, CHECK_OK);
}
@@ -2732,6 +2710,7 @@ FunctionLiteral* Parser::ParseFunctionLiteral(
duplicate_parameters, function_type, eager_compile_hint, pos, true,
function_literal_id);
function_literal->set_function_token_position(function_token_pos);
+ function_literal->set_parameter_init_block(parameter_init_block);
if (should_be_used_once_hint)
function_literal->set_should_be_used_once_hint();
@@ -2978,91 +2957,6 @@ Block* Parser::BuildParameterInitializationBlock(
return init_block;
}
-Block* Parser::BuildRejectPromiseOnException(Block* inner_block) {
- // .promise = %AsyncFunctionPromiseCreate();
- // try {
- // <inner_block>
- // } catch (.catch) {
- // %RejectPromise(.promise, .catch);
- // return .promise;
- // } finally {
- // %AsyncFunctionPromiseRelease(.promise);
- // }
- Block* result = factory()->NewBlock(nullptr, 2, true, kNoSourcePosition);
-
- // .promise = %AsyncFunctionPromiseCreate();
- Statement* set_promise;
- {
- Expression* create_promise = factory()->NewCallRuntime(
- Context::ASYNC_FUNCTION_PROMISE_CREATE_INDEX,
- new (zone()) ZoneList<Expression*>(0, zone()), kNoSourcePosition);
- Assignment* assign_promise = factory()->NewAssignment(
- Token::INIT, factory()->NewVariableProxy(PromiseVariable()),
- create_promise, kNoSourcePosition);
- set_promise =
- factory()->NewExpressionStatement(assign_promise, kNoSourcePosition);
- }
- result->statements()->Add(set_promise, zone());
-
- // catch (.catch) { return %RejectPromise(.promise, .catch), .promise }
- Scope* catch_scope = NewScope(CATCH_SCOPE);
- catch_scope->set_is_hidden();
- Variable* catch_variable =
- catch_scope->DeclareLocal(ast_value_factory()->dot_catch_string(), VAR);
- Block* catch_block = factory()->NewBlock(nullptr, 1, true, kNoSourcePosition);
-
- Expression* promise_reject = BuildRejectPromise(
- factory()->NewVariableProxy(catch_variable), kNoSourcePosition);
- ReturnStatement* return_promise_reject =
- factory()->NewReturnStatement(promise_reject, kNoSourcePosition);
- catch_block->statements()->Add(return_promise_reject, zone());
-
- TryStatement* try_catch_statement =
- factory()->NewTryCatchStatementForAsyncAwait(inner_block, catch_scope,
- catch_variable, catch_block,
- kNoSourcePosition);
-
- // There is no TryCatchFinally node, so wrap it in an outer try/finally
- Block* outer_try_block =
- factory()->NewBlock(nullptr, 1, true, kNoSourcePosition);
- outer_try_block->statements()->Add(try_catch_statement, zone());
-
- // finally { %AsyncFunctionPromiseRelease(.promise) }
- Block* finally_block =
- factory()->NewBlock(nullptr, 1, true, kNoSourcePosition);
- {
- ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(1, zone());
- args->Add(factory()->NewVariableProxy(PromiseVariable()), zone());
- Expression* call_promise_release = factory()->NewCallRuntime(
- Context::ASYNC_FUNCTION_PROMISE_RELEASE_INDEX, args, kNoSourcePosition);
- Statement* promise_release = factory()->NewExpressionStatement(
- call_promise_release, kNoSourcePosition);
- finally_block->statements()->Add(promise_release, zone());
- }
-
- Statement* try_finally_statement = factory()->NewTryFinallyStatement(
- outer_try_block, finally_block, kNoSourcePosition);
-
- result->statements()->Add(try_finally_statement, zone());
- return result;
-}
-
-Assignment* Parser::BuildCreateJSGeneratorObject(int pos, FunctionKind kind) {
- // .generator = %CreateJSGeneratorObject(...);
- DCHECK_NOT_NULL(function_state_->generator_object_variable());
- ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(2, zone());
- args->Add(factory()->NewThisFunction(pos), zone());
- args->Add(IsArrowFunction(kind) ? GetLiteralUndefined(pos)
- : ThisExpression(kNoSourcePosition),
- zone());
- Expression* allocation =
- factory()->NewCallRuntime(Runtime::kCreateJSGeneratorObject, args, pos);
- VariableProxy* proxy =
- factory()->NewVariableProxy(function_state_->generator_object_variable());
- return factory()->NewAssignment(Token::INIT, proxy, allocation,
- kNoSourcePosition);
-}
-
Expression* Parser::BuildResolvePromise(Expression* value, int pos) {
// %ResolvePromise(.promise, value), .promise
ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(2, zone());
@@ -3075,48 +2969,24 @@ Expression* Parser::BuildResolvePromise(Expression* value, int pos) {
factory()->NewVariableProxy(PromiseVariable()), pos);
}
-Expression* Parser::BuildRejectPromise(Expression* value, int pos) {
- // %promise_internal_reject(.promise, value, false), .promise
- // Disables the additional debug event for the rejection since a debug event
- // already happened for the exception that got us here.
- ZoneList<Expression*>* args = new (zone()) ZoneList<Expression*>(3, zone());
- args->Add(factory()->NewVariableProxy(PromiseVariable()), zone());
- args->Add(value, zone());
- args->Add(factory()->NewBooleanLiteral(false, pos), zone());
- Expression* call_runtime = factory()->NewCallRuntime(
- Context::PROMISE_INTERNAL_REJECT_INDEX, args, pos);
- return factory()->NewBinaryOperation(
- Token::COMMA, call_runtime,
- factory()->NewVariableProxy(PromiseVariable()), pos);
-}
-
Variable* Parser::PromiseVariable() {
// Based on the various compilation paths, there are many different code
// paths which may be the first to access the Promise temporary. Whichever
// comes first should create it and stash it in the FunctionState.
Variable* promise = function_state_->promise_variable();
if (function_state_->promise_variable() == nullptr) {
- promise = scope()->NewTemporary(ast_value_factory()->empty_string());
- function_state_->set_promise_variable(promise);
+ promise = function_state_->scope()->DeclarePromiseVar(
+ ast_value_factory()->empty_string());
+ promise->set_is_used();
}
return promise;
}
-Expression* Parser::BuildInitialYield(int pos, FunctionKind kind) {
- Assignment* assignment = BuildCreateJSGeneratorObject(pos, kind);
- VariableProxy* generator =
- factory()->NewVariableProxy(function_state_->generator_object_variable());
- // The position of the yield is important for reporting the exception
- // caused by calling the .throw method on a generator suspended at the
- // initial yield (i.e. right after generator instantiation).
- return factory()->NewYield(generator, assignment, scope()->start_position(),
- Yield::kOnExceptionThrow);
-}
-
ZoneList<Statement*>* Parser::ParseFunction(
const AstRawString* function_name, int pos, FunctionKind kind,
FunctionLiteral::FunctionType function_type,
- DeclarationScope* function_scope, int* num_parameters, int* function_length,
+ DeclarationScope* function_scope, int* num_parameters,
+ Block** parameter_init_block, int* function_length,
bool* has_duplicate_parameters, int* materialized_literal_count,
int* expected_property_count, bool* ok) {
ParsingModeScope mode(this, allow_lazy_ ? PARSE_LAZILY : PARSE_EAGERLY);
@@ -3141,7 +3011,8 @@ ZoneList<Statement*>* Parser::ParseFunction(
Expect(Token::LBRACE, CHECK_OK);
ZoneList<Statement*>* body = new (zone()) ZoneList<Statement*>(8, zone());
- ParseFunctionBody(body, function_name, pos, formals, kind, function_type, ok);
+ ParseFunctionBody(body, parameter_init_block, function_name, pos, formals,
+ kind, function_type, ok);
// Validate parameter names. We can do this only after parsing the function,
// since the function can declare itself strict.
@@ -3771,13 +3642,10 @@ void Parser::PrepareAsyncFunctionBody(ZoneList<Statement*>* body,
if (function_state_->generator_object_variable() == nullptr) {
PrepareGeneratorVariables();
}
- body->Add(factory()->NewExpressionStatement(
- BuildCreateJSGeneratorObject(pos, kind), kNoSourcePosition),
- zone());
}
// This method completes the desugaring of the body of async_function.
-void Parser::RewriteAsyncFunctionBody(ZoneList<Statement*>* body, Block* block,
+void Parser::RewriteAsyncFunctionBody(ZoneList<Statement*>* body,
Expression* return_value, bool* ok) {
// function async_function() {
// .generator_object = %CreateJSGeneratorObject();
@@ -3788,11 +3656,9 @@ void Parser::RewriteAsyncFunctionBody(ZoneList<Statement*>* body, Block* block,
// }
return_value = BuildResolvePromise(return_value, return_value->position());
- block->statements()->Add(
+ body->Add(
factory()->NewReturnStatement(return_value, return_value->position()),
zone());
- block = BuildRejectPromiseOnException(block);
- body->Add(block, zone());
}
Expression* Parser::RewriteAwaitExpression(Expression* value, int await_pos) {

Powered by Google App Engine
This is Rietveld 408576698