Index: src/parser.cc |
diff --git a/src/parser.cc b/src/parser.cc |
index 9e3d672b7100361b240d2d150c38d86d80e08344..03ac4399f0af56a33d716e6ee3009456a2b139d7 100644 |
--- a/src/parser.cc |
+++ b/src/parser.cc |
@@ -4173,11 +4173,6 @@ FunctionLiteral* Parser::ParseFunctionLiteral( |
if (should_be_used_once_hint) |
function_literal->set_should_be_used_once_hint(); |
- if (scope->has_rest_parameter()) { |
- // TODO(caitp): enable optimization of functions with rest params |
- function_literal->set_dont_optimize_reason(kRestParameter); |
- } |
- |
if (fni_ != NULL && should_infer_name) fni_->AddFunction(function_literal); |
return function_literal; |
} |
@@ -4315,8 +4310,6 @@ Block* Parser::BuildParameterInitializationBlock( |
factory()->NewBlock(NULL, 1, true, RelocInfo::kNoPosition); |
for (int i = 0; i < parameters.params.length(); ++i) { |
auto parameter = parameters.params[i]; |
- // TODO(caitp,rossberg): Remove special handling for rest once desugared. |
- if (parameter.is_rest) break; |
DeclarationDescriptor descriptor; |
descriptor.declaration_kind = DeclarationDescriptor::PARAMETER; |
descriptor.parser = this; |
@@ -4328,12 +4321,98 @@ Block* Parser::BuildParameterInitializationBlock( |
descriptor.declaration_pos = parameter.pattern->position(); |
descriptor.initialization_pos = parameter.pattern->position(); |
descriptor.init_op = Token::INIT_LET; |
+ |
+ auto parameter_var = parameters.scope->parameter(i); |
+ auto parameter_proxy = factory()->NewVariableProxy(parameter_var); |
adamk
2015/08/05 22:13:02
I'd move this down to where it's used to avoid acc
|
+ |
+ if (parameter.is_rest) { |
+ DCHECK(parameter.pattern->IsVariableProxy()); |
+ DCHECK_EQ(i, parameters.params.length() - 1); |
+ |
+ // tmp_var = []; |
+ // for (i = rest_index; i < %_ArgumentsLength(); ++i) { |
adamk
2015/08/05 22:13:02
This desugaring needs updating to match what's goi
|
+ // %AddElement(tmp_var, tmp_var.length, %_Arguments(i)); |
+ // } |
+ // let <$rest_array> = tmp_var; |
+ auto empty_values = new (zone()) ZoneList<Expression*>(0, zone()); |
+ auto empty_array = factory()->NewArrayLiteral( |
+ empty_values, parameters.rest_array_literal_index, |
+ is_strong(language_mode()), RelocInfo::kNoPosition); |
+ |
+ auto init_array = |
+ factory()->NewAssignment(Token::INIT_VAR, parameter_proxy, |
+ empty_array, RelocInfo::kNoPosition); |
+ |
+ auto loop = factory()->NewForStatement(NULL, RelocInfo::kNoPosition); |
+ |
+ // argument_index = rest_index |
+ auto argument_index = |
+ parameters.scope->NewTemporary(ast_value_factory()->dot_string()); |
+ auto init = factory()->NewExpressionStatement( |
+ factory()->NewAssignment( |
+ Token::INIT_VAR, factory()->NewVariableProxy(argument_index), |
+ factory()->NewSmiLiteral(i, RelocInfo::kNoPosition), |
+ RelocInfo::kNoPosition), |
+ RelocInfo::kNoPosition); |
+ |
+ auto arguments_length = Runtime::FunctionForId(Runtime::kArgumentsLength); |
adamk
2015/08/05 22:13:02
I think you want kInlineArgumentsLength here (if I
|
+ auto empty_arguments = new (zone()) ZoneList<Expression*>(0, zone()); |
+ |
+ // arguments_index < arguments.length |
+ auto cond = factory()->NewCompareOperation( |
+ Token::LT, factory()->NewVariableProxy(argument_index), |
+ factory()->NewCallRuntime(ast_value_factory()->empty_string(), |
+ arguments_length, empty_arguments, |
+ RelocInfo::kNoPosition), |
+ RelocInfo::kNoPosition); |
+ |
+ // ++argument_index |
+ auto next = factory()->NewExpressionStatement( |
+ factory()->NewCountOperation( |
+ Token::INC, true, factory()->NewVariableProxy(argument_index), |
+ RelocInfo::kNoPosition), |
+ RelocInfo::kNoPosition); |
+ |
+ // %_Arguments(arguments_index) / arguments[arguments_index] |
+ auto arguments = Runtime::FunctionForId(Runtime::kArguments); |
adamk
2015/08/05 22:13:02
kInlineArguments
|
+ |
+ auto arguments_args = new (zone()) ZoneList<Expression*>(1, zone()); |
+ arguments_args->Add(factory()->NewVariableProxy(argument_index), zone()); |
+ |
+ // %AppendElement($rest, $_Arguments(arguments_index)) |
adamk
2015/08/05 22:13:02
Should read:
%AppendElement($rest, %_Arguments(ar
|
+ auto append_element = Runtime::FunctionForId(Runtime::kAppendElement); |
+ auto append_element_args = new (zone()) ZoneList<Expression*>(2, zone()); |
+ |
+ append_element_args->Add(factory()->NewVariableProxy(parameter_var), |
+ zone()); |
+ append_element_args->Add( |
+ factory()->NewCallRuntime(ast_value_factory()->empty_string(), |
+ arguments, arguments_args, |
+ RelocInfo::kNoPosition), |
+ zone()); |
+ |
+ auto body = factory()->NewExpressionStatement( |
+ factory()->NewCallRuntime(ast_value_factory()->empty_string(), |
+ append_element, append_element_args, |
+ RelocInfo::kNoPosition), |
+ RelocInfo::kNoPosition); |
+ |
+ loop->Initialize(init, cond, next, body); |
+ |
+ init_block->AddStatement( |
+ factory()->NewExpressionStatement(init_array, RelocInfo::kNoPosition), |
+ zone()); |
+ |
+ init_block->AddStatement(loop, zone()); |
+ } |
+ |
DeclarationParsingResult::Declaration decl( |
parameter.pattern, parameter.pattern->position(), |
- factory()->NewVariableProxy(parameters.scope->parameter(i))); |
+ factory()->NewVariableProxy(parameter_var)); |
PatternRewriter::DeclareAndInitializeVariables(init_block, &descriptor, |
&decl, nullptr, CHECK_OK); |
} |
+ |
return init_block; |
} |