Index: src/parser.cc |
diff --git a/src/parser.cc b/src/parser.cc |
index 7c2f8feb94bcafc8f2da3dbff5961bce23e37aa0..7d3d8f5967f3e02c6f3c12842ef56230566c6f05 100644 |
--- a/src/parser.cc |
+++ b/src/parser.cc |
@@ -878,6 +878,7 @@ Parser::Parser(ParseInfo* info) |
set_allow_harmony_computed_property_names( |
FLAG_harmony_computed_property_names); |
set_allow_harmony_rest_params(FLAG_harmony_rest_parameters); |
+ set_allow_harmony_spreadcalls(FLAG_harmony_spreadcalls); |
set_allow_strong_mode(FLAG_strong_mode); |
for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount; |
++feature) { |
@@ -4176,6 +4177,8 @@ PreParser::PreParseResult Parser::ParseLazyFunctionBodyWithPreParser( |
allow_harmony_computed_property_names()); |
reusable_preparser_->set_allow_harmony_rest_params( |
allow_harmony_rest_params()); |
+ reusable_preparser_->set_allow_harmony_spreadcalls( |
+ allow_harmony_spreadcalls()); |
reusable_preparser_->set_allow_strong_mode(allow_strong_mode()); |
} |
PreParser::PreParseResult result = reusable_preparser_->PreParseLazyFunction( |
@@ -4294,7 +4297,11 @@ Expression* Parser::ParseV8Intrinsic(bool* ok) { |
Expect(Token::MOD, CHECK_OK); |
// Allow "eval" or "arguments" for backward compatibility. |
const AstRawString* name = ParseIdentifier(kAllowEvalOrArguments, CHECK_OK); |
- ZoneList<Expression*>* args = ParseArguments(CHECK_OK); |
+ Scanner::Location spread_pos; |
+ ZoneList<Expression*>* args = ParseArguments(&spread_pos, CHECK_OK); |
+ |
+ // TODO(caitp): support intrinsics |
+ DCHECK(!spread_pos.IsValid()); |
if (extension_ != NULL) { |
// The extension structures are only accessible while parsing the |
@@ -5562,4 +5569,107 @@ uint32_t Parser::ComputeTemplateLiteralHash(const TemplateLiteral* lit) { |
return running_hash; |
} |
+ |
+ |
+ZoneList<v8::internal::Expression*>* Parser::PrepareSpreadArguments( |
+ ZoneList<v8::internal::Expression*>* list) { |
+ ZoneList<v8::internal::Expression*>* args = |
+ new (zone()) ZoneList<v8::internal::Expression*>(1, zone()); |
+ if (list->length() == 1) { |
+ // Spread-call with single spread argument produces an InternalArray |
+ // containing the values from the array. |
+ // |
+ // Function is called or constructed with the produced array of arguments |
+ // |
+ // EG: Apply(Func, Spread(spread0)) |
+ args->Add( |
+ factory()->NewCallRuntime(ast_value_factory()->spread_iterable_string(), |
+ NULL, list, RelocInfo::kNoPosition), |
+ zone()); |
+ return args; |
+ } else { |
+ // Spread-call with multiple arguments produces array literals for each |
+ // sequences of unspread arguments, and converts each spread iterable to |
+ // an Internal array. Finally, all of these produced arrays are flattened |
+ // into a single InternalArray, containing the arguments for the call. |
+ // |
+ // EG: Apply(Func, Flatten([unspread0, unspread1], Spread(spread0), |
+ // Spread(spread1), [unspread2, unspread3])) |
+ int i = 0; |
+ int n = list->length(); |
+ while (i < n) { |
+ if (!list->at(i)->IsSpreadOperation()) { |
+ ZoneList<v8::internal::Expression*>* unspread = |
+ new (zone()) ZoneList<v8::internal::Expression*>(1, zone()); |
+ |
+ // Push array of unspread parameters |
+ while (i < n && !list->at(i)->IsSpreadOperation()) { |
+ unspread->Add(list->at(i++), zone()); |
+ } |
+ int literal_index = function_state_->NextMaterializedLiteralIndex(); |
+ args->Add(factory()->NewArrayLiteral(unspread, literal_index, |
+ RelocInfo::kNoPosition), |
+ zone()); |
+ |
+ if (i == n) break; |
+ } |
+ |
+ // Push eagerly spread argument |
+ ZoneList<v8::internal::Expression*>* spread = |
+ new (zone()) ZoneList<v8::internal::Expression*>(1, zone()); |
+ spread->Add(list->at(i++), zone()); |
+ args->Add(factory()->NewCallRuntime( |
+ ast_value_factory()->spread_iterable_string(), NULL, spread, |
+ RelocInfo::kNoPosition), |
+ zone()); |
+ } |
+ |
+ list = new (zone()) ZoneList<v8::internal::Expression*>(1, zone()); |
+ list->Add(factory()->NewCallRuntime( |
+ ast_value_factory()->spread_arguments_string(), NULL, args, |
+ RelocInfo::kNoPosition), |
+ zone()); |
+ return list; |
+ } |
+ UNREACHABLE(); |
+} |
+ |
+ |
+Expression* Parser::SpreadCall(Expression* function, |
+ ZoneList<v8::internal::Expression*>* args, |
+ int pos) { |
+ Expression* receiver; |
+ bool is_super = function->IsSuperReference(); |
+ |
+ const AstRawString* fn; |
+ args->InsertAt(0, function, zone()); |
+ if (is_super) { |
+ fn = ast_value_factory()->reflect_construct_string(); |
+ args->Add(factory()->NewVariableProxy(scope_->new_target_var()), zone()); |
+ } else { |
+ fn = ast_value_factory()->reflect_apply_string(); |
+ if (function->IsProperty()) { |
+ receiver = function->AsProperty()->obj(); |
+ } else { |
+ receiver = factory()->NewUndefinedLiteral(pos); |
+ } |
+ args->InsertAt(1, receiver, zone()); |
+ } |
+ |
+ Expression* result = factory()->NewCallRuntime(fn, NULL, args, pos); |
+ if (is_super) { |
+ result->AsCallRuntime()->SetIsSuperCall(); |
+ } |
+ return result; |
+} |
+ |
+ |
+Expression* Parser::SpreadCallNew(Expression* function, |
+ ZoneList<v8::internal::Expression*>* args, |
+ int pos) { |
+ args->InsertAt(0, function, zone()); |
+ |
+ return factory()->NewCallRuntime( |
+ ast_value_factory()->reflect_construct_string(), NULL, args, pos); |
+} |
} } // namespace v8::internal |