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

Side by Side Diff: src/parser.cc

Issue 1309813007: [es6] implement destructuring assignment (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Cache te right scope in DeclareAndInitializeVariables() Created 5 years 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/parser.h" 5 #include "src/parser.h"
6 6
7 #include "src/api.h" 7 #include "src/api.h"
8 #include "src/ast.h" 8 #include "src/ast.h"
9 #include "src/ast-expression-visitor.h"
9 #include "src/ast-literal-reindexer.h" 10 #include "src/ast-literal-reindexer.h"
10 #include "src/bailout-reason.h" 11 #include "src/bailout-reason.h"
11 #include "src/base/platform/platform.h" 12 #include "src/base/platform/platform.h"
12 #include "src/bootstrapper.h" 13 #include "src/bootstrapper.h"
13 #include "src/char-predicates-inl.h" 14 #include "src/char-predicates-inl.h"
14 #include "src/codegen.h" 15 #include "src/codegen.h"
15 #include "src/compiler.h" 16 #include "src/compiler.h"
16 #include "src/messages.h" 17 #include "src/messages.h"
17 #include "src/parameter-initializer-rewriter.h" 18 #include "src/parameter-initializer-rewriter.h"
18 #include "src/preparser.h" 19 #include "src/preparser.h"
(...skipping 894 matching lines...) Expand 10 before | Expand all | Expand 10 after
913 // ParseInfo during background parsing. 914 // ParseInfo during background parsing.
914 DCHECK(!info->script().is_null() || info->source_stream() != NULL); 915 DCHECK(!info->script().is_null() || info->source_stream() != NULL);
915 set_allow_lazy(info->allow_lazy_parsing()); 916 set_allow_lazy(info->allow_lazy_parsing());
916 set_allow_natives(FLAG_allow_natives_syntax || info->is_native()); 917 set_allow_natives(FLAG_allow_natives_syntax || info->is_native());
917 set_allow_harmony_sloppy(FLAG_harmony_sloppy); 918 set_allow_harmony_sloppy(FLAG_harmony_sloppy);
918 set_allow_harmony_sloppy_function(FLAG_harmony_sloppy_function); 919 set_allow_harmony_sloppy_function(FLAG_harmony_sloppy_function);
919 set_allow_harmony_sloppy_let(FLAG_harmony_sloppy_let); 920 set_allow_harmony_sloppy_let(FLAG_harmony_sloppy_let);
920 set_allow_harmony_rest_parameters(FLAG_harmony_rest_parameters); 921 set_allow_harmony_rest_parameters(FLAG_harmony_rest_parameters);
921 set_allow_harmony_default_parameters(FLAG_harmony_default_parameters); 922 set_allow_harmony_default_parameters(FLAG_harmony_default_parameters);
922 set_allow_harmony_destructuring_bind(FLAG_harmony_destructuring_bind); 923 set_allow_harmony_destructuring_bind(FLAG_harmony_destructuring_bind);
924 set_allow_harmony_destructuring_assignment(
925 FLAG_harmony_destructuring_assignment);
923 set_allow_strong_mode(FLAG_strong_mode); 926 set_allow_strong_mode(FLAG_strong_mode);
924 set_allow_legacy_const(FLAG_legacy_const); 927 set_allow_legacy_const(FLAG_legacy_const);
925 set_allow_harmony_do_expressions(FLAG_harmony_do_expressions); 928 set_allow_harmony_do_expressions(FLAG_harmony_do_expressions);
926 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount; 929 for (int feature = 0; feature < v8::Isolate::kUseCounterFeatureCount;
927 ++feature) { 930 ++feature) {
928 use_counts_[feature] = 0; 931 use_counts_[feature] = 0;
929 } 932 }
930 if (info->ast_value_factory() == NULL) { 933 if (info->ast_value_factory() == NULL) {
931 // info takes ownership of AstValueFactory. 934 // info takes ownership of AstValueFactory.
932 info->set_ast_value_factory(new AstValueFactory(zone(), info->hash_seed())); 935 info->set_ast_value_factory(new AstValueFactory(zone(), info->hash_seed()));
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
1083 if (body->length() != 1 || 1086 if (body->length() != 1 ||
1084 !body->at(0)->IsExpressionStatement() || 1087 !body->at(0)->IsExpressionStatement() ||
1085 !body->at(0)->AsExpressionStatement()-> 1088 !body->at(0)->AsExpressionStatement()->
1086 expression()->IsFunctionLiteral()) { 1089 expression()->IsFunctionLiteral()) {
1087 ReportMessage(MessageTemplate::kSingleFunctionLiteral); 1090 ReportMessage(MessageTemplate::kSingleFunctionLiteral);
1088 ok = false; 1091 ok = false;
1089 } 1092 }
1090 } 1093 }
1091 1094
1092 if (ok) { 1095 if (ok) {
1096 ParserTraits::RewriteDestructuringAssignments();
1093 result = factory()->NewFunctionLiteral( 1097 result = factory()->NewFunctionLiteral(
1094 ast_value_factory()->empty_string(), ast_value_factory(), scope_, 1098 ast_value_factory()->empty_string(), ast_value_factory(), scope_,
1095 body, function_state.materialized_literal_count(), 1099 body, function_state.materialized_literal_count(),
1096 function_state.expected_property_count(), 0, 1100 function_state.expected_property_count(), 0,
1097 FunctionLiteral::kNoDuplicateParameters, 1101 FunctionLiteral::kNoDuplicateParameters,
1098 FunctionLiteral::ANONYMOUS_EXPRESSION, FunctionLiteral::kGlobalOrEval, 1102 FunctionLiteral::ANONYMOUS_EXPRESSION, FunctionLiteral::kGlobalOrEval,
1099 FunctionLiteral::kShouldLazyCompile, FunctionKind::kNormalFunction, 1103 FunctionLiteral::kShouldLazyCompile, FunctionKind::kNormalFunction,
1100 0); 1104 0);
1101 } 1105 }
1102 } 1106 }
(...skipping 3294 matching lines...) Expand 10 before | Expand all | Expand 10 after
4397 CheckStrictOctalLiteral(scope->start_position(), scope->end_position(), 4401 CheckStrictOctalLiteral(scope->start_position(), scope->end_position(),
4398 CHECK_OK); 4402 CHECK_OK);
4399 } 4403 }
4400 if (is_sloppy(language_mode) && allow_harmony_sloppy_function()) { 4404 if (is_sloppy(language_mode) && allow_harmony_sloppy_function()) {
4401 InsertSloppyBlockFunctionVarBindings(scope, CHECK_OK); 4405 InsertSloppyBlockFunctionVarBindings(scope, CHECK_OK);
4402 } 4406 }
4403 if (is_strict(language_mode) || allow_harmony_sloppy() || 4407 if (is_strict(language_mode) || allow_harmony_sloppy() ||
4404 allow_harmony_destructuring_bind()) { 4408 allow_harmony_destructuring_bind()) {
4405 CheckConflictingVarDeclarations(scope, CHECK_OK); 4409 CheckConflictingVarDeclarations(scope, CHECK_OK);
4406 } 4410 }
4411
4412 ParserTraits::RewriteDestructuringAssignments();
4407 } 4413 }
4408 4414
4409 bool has_duplicate_parameters = 4415 bool has_duplicate_parameters =
4410 !formals_classifier.is_valid_formal_parameter_list_without_duplicates(); 4416 !formals_classifier.is_valid_formal_parameter_list_without_duplicates();
4411 FunctionLiteral::ParameterFlag duplicate_parameters = 4417 FunctionLiteral::ParameterFlag duplicate_parameters =
4412 has_duplicate_parameters ? FunctionLiteral::kHasDuplicateParameters 4418 has_duplicate_parameters ? FunctionLiteral::kHasDuplicateParameters
4413 : FunctionLiteral::kNoDuplicateParameters; 4419 : FunctionLiteral::kNoDuplicateParameters;
4414 4420
4415 FunctionLiteral* function_literal = factory()->NewFunctionLiteral( 4421 FunctionLiteral* function_literal = factory()->NewFunctionLiteral(
4416 function_name, ast_value_factory(), scope, body, 4422 function_name, ast_value_factory(), scope, body,
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
4525 RelocInfo::kNoPosition); 4531 RelocInfo::kNoPosition);
4526 IfStatement* if_statement = factory()->NewIfStatement( 4532 IfStatement* if_statement = factory()->NewIfStatement(
4527 condition, factory()->NewExpressionStatement(throw_type_error, 4533 condition, factory()->NewExpressionStatement(throw_type_error,
4528 RelocInfo::kNoPosition), 4534 RelocInfo::kNoPosition),
4529 factory()->NewEmptyStatement(RelocInfo::kNoPosition), 4535 factory()->NewEmptyStatement(RelocInfo::kNoPosition),
4530 RelocInfo::kNoPosition); 4536 RelocInfo::kNoPosition);
4531 return if_statement; 4537 return if_statement;
4532 } 4538 }
4533 4539
4534 4540
4541 class InitializerRewriter : public AstExpressionVisitor {
4542 public:
4543 InitializerRewriter(uintptr_t stack_limit, Expression* root, Parser* parser,
4544 Scope* scope)
4545 : AstExpressionVisitor(stack_limit, root),
4546 parser_(parser),
4547 scope_(scope) {}
4548
4549 private:
4550 void VisitExpression(Expression* expr) {
4551 if (expr->IsAssignment()) {
4552 Assignment* node = expr->AsAssignment();
4553 if (node->op() == Token::ASSIGN && node->is_destructuring_assignment() &&
4554 !node->destructuring_assignment()) {
4555 bool ok = true;
4556 Parser::PatternRewriter::RewriteDestructuringAssignment(parser_, node,
4557 scope_, &ok);
4558 DCHECK(ok);
4559 }
4560 }
4561 }
4562
4563 private:
4564 Parser* parser_;
4565 Scope* scope_;
4566 };
4567
4568
4569 void Parser::RewriteParameterInitializer(Expression* expr, Scope* scope) {
4570 InitializerRewriter rewriter(stack_limit_, expr, this, scope);
4571 rewriter.Run();
4572 }
4573
4574
4535 Block* Parser::BuildParameterInitializationBlock( 4575 Block* Parser::BuildParameterInitializationBlock(
4536 const ParserFormalParameters& parameters, bool* ok) { 4576 const ParserFormalParameters& parameters, bool* ok) {
4537 DCHECK(!parameters.is_simple); 4577 DCHECK(!parameters.is_simple);
4538 DCHECK(scope_->is_function_scope()); 4578 DCHECK(scope_->is_function_scope());
4539 Block* init_block = 4579 Block* init_block =
4540 factory()->NewBlock(NULL, 1, true, RelocInfo::kNoPosition); 4580 factory()->NewBlock(NULL, 1, true, RelocInfo::kNoPosition);
4541 for (int i = 0; i < parameters.params.length(); ++i) { 4581 for (int i = 0; i < parameters.params.length(); ++i) {
4542 auto parameter = parameters.params[i]; 4582 auto parameter = parameters.params[i];
4543 DeclarationDescriptor descriptor; 4583 DeclarationDescriptor descriptor;
4544 descriptor.declaration_kind = DeclarationDescriptor::PARAMETER; 4584 descriptor.declaration_kind = DeclarationDescriptor::PARAMETER;
4545 descriptor.parser = this; 4585 descriptor.parser = this;
4546 descriptor.declaration_scope = scope_; 4586 descriptor.declaration_scope = scope_;
4547 descriptor.scope = scope_; 4587 descriptor.scope = scope_;
4548 descriptor.hoist_scope = nullptr; 4588 descriptor.hoist_scope = nullptr;
4549 descriptor.mode = LET; 4589 descriptor.mode = LET;
4550 descriptor.is_const = false; 4590 descriptor.is_const = false;
4551 descriptor.needs_init = true; 4591 descriptor.needs_init = true;
4552 descriptor.declaration_pos = parameter.pattern->position(); 4592 descriptor.declaration_pos = parameter.pattern->position();
4553 descriptor.initialization_pos = parameter.pattern->position(); 4593 descriptor.initialization_pos = parameter.pattern->position();
4554 Expression* initial_value = 4594 Expression* initial_value =
4555 factory()->NewVariableProxy(parameters.scope->parameter(i)); 4595 factory()->NewVariableProxy(parameters.scope->parameter(i));
4556 if (parameter.initializer != nullptr) { 4596 if (parameter.initializer != nullptr) {
4557 // IS_UNDEFINED($param) ? initializer : $param 4597 // IS_UNDEFINED($param) ? initializer : $param
4558 DCHECK(!parameter.is_rest); 4598 DCHECK(!parameter.is_rest);
4599
4600 // Ensure initializer is rewritten
4601 RewriteParameterInitializer(parameter.initializer, scope_);
4602
4559 auto condition = factory()->NewCompareOperation( 4603 auto condition = factory()->NewCompareOperation(
4560 Token::EQ_STRICT, 4604 Token::EQ_STRICT,
4561 factory()->NewVariableProxy(parameters.scope->parameter(i)), 4605 factory()->NewVariableProxy(parameters.scope->parameter(i)),
4562 factory()->NewUndefinedLiteral(RelocInfo::kNoPosition), 4606 factory()->NewUndefinedLiteral(RelocInfo::kNoPosition),
4563 RelocInfo::kNoPosition); 4607 RelocInfo::kNoPosition);
4564 initial_value = factory()->NewConditional( 4608 initial_value = factory()->NewConditional(
4565 condition, parameter.initializer, initial_value, 4609 condition, parameter.initializer, initial_value,
4566 RelocInfo::kNoPosition); 4610 RelocInfo::kNoPosition);
4567 descriptor.initialization_pos = parameter.initializer->position(); 4611 descriptor.initialization_pos = parameter.initializer->position();
4568 } else if (parameter.is_rest) { 4612 } else if (parameter.is_rest) {
(...skipping 1887 matching lines...) Expand 10 before | Expand all | Expand 10 after
6456 } 6500 }
6457 6501
6458 6502
6459 void Parser::RaiseLanguageMode(LanguageMode mode) { 6503 void Parser::RaiseLanguageMode(LanguageMode mode) {
6460 SetLanguageMode(scope_, 6504 SetLanguageMode(scope_,
6461 static_cast<LanguageMode>(scope_->language_mode() | mode)); 6505 static_cast<LanguageMode>(scope_->language_mode() | mode));
6462 } 6506 }
6463 6507
6464 } // namespace internal 6508 } // namespace internal
6465 } // namespace v8 6509 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698