OLD | NEW |
---|---|
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
479 ~BlockState() { parser_->top_scope_ = outer_scope_; } | 479 ~BlockState() { parser_->top_scope_ = outer_scope_; } |
480 | 480 |
481 private: | 481 private: |
482 Parser* parser_; | 482 Parser* parser_; |
483 Scope* outer_scope_; | 483 Scope* outer_scope_; |
484 }; | 484 }; |
485 | 485 |
486 | 486 |
487 Parser::FunctionState::FunctionState(Parser* parser, | 487 Parser::FunctionState::FunctionState(Parser* parser, |
488 Scope* scope, | 488 Scope* scope, |
489 bool is_generator, | |
490 Isolate* isolate) | 489 Isolate* isolate) |
491 : next_materialized_literal_index_(JSFunction::kLiteralsPrefixSize), | 490 : next_materialized_literal_index_(JSFunction::kLiteralsPrefixSize), |
492 next_handler_index_(0), | 491 next_handler_index_(0), |
493 expected_property_count_(0), | 492 expected_property_count_(0), |
494 is_generator_(is_generator), | |
495 only_simple_this_property_assignments_(false), | 493 only_simple_this_property_assignments_(false), |
496 this_property_assignments_(isolate->factory()->empty_fixed_array()), | 494 this_property_assignments_(isolate->factory()->empty_fixed_array()), |
497 parser_(parser), | 495 parser_(parser), |
498 outer_function_state_(parser->current_function_state_), | 496 outer_function_state_(parser->current_function_state_), |
499 outer_scope_(parser->top_scope_), | 497 outer_scope_(parser->top_scope_), |
498 generator_object_variable_(NULL), | |
500 saved_ast_node_id_(isolate->ast_node_id()), | 499 saved_ast_node_id_(isolate->ast_node_id()), |
501 factory_(isolate, parser->zone()) { | 500 factory_(isolate, parser->zone()) { |
502 parser->top_scope_ = scope; | 501 parser->top_scope_ = scope; |
503 parser->current_function_state_ = this; | 502 parser->current_function_state_ = this; |
504 isolate->set_ast_node_id(BailoutId::FirstUsable().ToInt()); | 503 isolate->set_ast_node_id(BailoutId::FirstUsable().ToInt()); |
505 } | 504 } |
506 | 505 |
507 | 506 |
508 Parser::FunctionState::~FunctionState() { | 507 Parser::FunctionState::~FunctionState() { |
509 parser_->top_scope_ = outer_scope_; | 508 parser_->top_scope_ = outer_scope_; |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
635 | 634 |
636 // Compute the parsing mode. | 635 // Compute the parsing mode. |
637 Mode mode = (FLAG_lazy && allow_lazy()) ? PARSE_LAZILY : PARSE_EAGERLY; | 636 Mode mode = (FLAG_lazy && allow_lazy()) ? PARSE_LAZILY : PARSE_EAGERLY; |
638 if (allow_natives_syntax() || | 637 if (allow_natives_syntax() || |
639 extension_ != NULL || | 638 extension_ != NULL || |
640 scope->is_eval_scope()) { | 639 scope->is_eval_scope()) { |
641 mode = PARSE_EAGERLY; | 640 mode = PARSE_EAGERLY; |
642 } | 641 } |
643 ParsingModeScope parsing_mode(this, mode); | 642 ParsingModeScope parsing_mode(this, mode); |
644 | 643 |
645 bool is_generator = false; | |
646 // Enters 'scope'. | 644 // Enters 'scope'. |
647 FunctionState function_state(this, scope, is_generator, isolate()); | 645 FunctionState function_state(this, scope, isolate()); |
648 | 646 |
649 top_scope_->SetLanguageMode(info->language_mode()); | 647 top_scope_->SetLanguageMode(info->language_mode()); |
650 ZoneList<Statement*>* body = new(zone()) ZoneList<Statement*>(16, zone()); | 648 ZoneList<Statement*>* body = new(zone()) ZoneList<Statement*>(16, zone()); |
651 bool ok = true; | 649 bool ok = true; |
652 int beg_loc = scanner().location().beg_pos; | 650 int beg_loc = scanner().location().beg_pos; |
653 ParseSourceElements(body, Token::EOS, info->is_eval(), true, &ok); | 651 ParseSourceElements(body, Token::EOS, info->is_eval(), true, &ok); |
654 if (ok && !top_scope_->is_classic_mode()) { | 652 if (ok && !top_scope_->is_classic_mode()) { |
655 CheckOctalLiteral(beg_loc, scanner().location().end_pos, &ok); | 653 CheckOctalLiteral(beg_loc, scanner().location().end_pos, &ok); |
656 } | 654 } |
657 | 655 |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
751 FunctionLiteral* result = NULL; | 749 FunctionLiteral* result = NULL; |
752 | 750 |
753 { | 751 { |
754 // Parse the function literal. | 752 // Parse the function literal. |
755 Scope* scope = NewScope(top_scope_, GLOBAL_SCOPE); | 753 Scope* scope = NewScope(top_scope_, GLOBAL_SCOPE); |
756 info()->SetGlobalScope(scope); | 754 info()->SetGlobalScope(scope); |
757 if (!info()->closure().is_null()) { | 755 if (!info()->closure().is_null()) { |
758 scope = Scope::DeserializeScopeChain(info()->closure()->context(), scope, | 756 scope = Scope::DeserializeScopeChain(info()->closure()->context(), scope, |
759 zone()); | 757 zone()); |
760 } | 758 } |
761 bool is_generator = false; // Top scope is not a generator. | 759 FunctionState function_state(this, scope, isolate()); |
762 FunctionState function_state(this, scope, is_generator, isolate()); | |
763 ASSERT(scope->language_mode() != STRICT_MODE || !info()->is_classic_mode()); | 760 ASSERT(scope->language_mode() != STRICT_MODE || !info()->is_classic_mode()); |
764 ASSERT(scope->language_mode() != EXTENDED_MODE || | 761 ASSERT(scope->language_mode() != EXTENDED_MODE || |
765 info()->is_extended_mode()); | 762 info()->is_extended_mode()); |
766 ASSERT(info()->language_mode() == shared_info->language_mode()); | 763 ASSERT(info()->language_mode() == shared_info->language_mode()); |
767 scope->SetLanguageMode(shared_info->language_mode()); | 764 scope->SetLanguageMode(shared_info->language_mode()); |
768 FunctionLiteral::Type type = shared_info->is_expression() | 765 FunctionLiteral::Type type = shared_info->is_expression() |
769 ? (shared_info->is_anonymous() | 766 ? (shared_info->is_anonymous() |
770 ? FunctionLiteral::ANONYMOUS_EXPRESSION | 767 ? FunctionLiteral::ANONYMOUS_EXPRESSION |
771 : FunctionLiteral::NAMED_EXPRESSION) | 768 : FunctionLiteral::NAMED_EXPRESSION) |
772 : FunctionLiteral::DECLARATION; | 769 : FunctionLiteral::DECLARATION; |
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1026 | 1023 |
1027 | 1024 |
1028 void* Parser::ParseSourceElements(ZoneList<Statement*>* processor, | 1025 void* Parser::ParseSourceElements(ZoneList<Statement*>* processor, |
1029 int end_token, | 1026 int end_token, |
1030 bool is_eval, | 1027 bool is_eval, |
1031 bool is_global, | 1028 bool is_global, |
1032 bool* ok) { | 1029 bool* ok) { |
1033 // SourceElements :: | 1030 // SourceElements :: |
1034 // (ModuleElement)* <end_token> | 1031 // (ModuleElement)* <end_token> |
1035 | 1032 |
1033 // For generators, allocate and yield an iterator on function entry. | |
Michael Starzinger
2013/04/11 19:17:39
Can we move this down into "ParseFunctionLiteral"
wingo
2013/04/12 10:50:17
Done.
| |
1034 if (is_generator()) { | |
1035 ZoneList<Expression*>* arguments = | |
1036 new(zone()) ZoneList<Expression*>(1, zone()); | |
1037 arguments->Add(factory()->NewThisFunction(), zone()); | |
1038 CallRuntime* allocation = factory()->NewCallRuntime( | |
1039 isolate()->factory()->empty_string(), | |
1040 Runtime::FunctionForId(Runtime::kCreateJSGeneratorObject), | |
1041 arguments); | |
1042 VariableProxy* init_proxy = factory()->NewVariableProxy( | |
1043 current_function_state_->generator_object_variable()); | |
1044 Assignment* assignment = factory()->NewAssignment( | |
1045 Token::INIT_VAR, init_proxy, allocation, RelocInfo::kNoPosition); | |
1046 VariableProxy* get_proxy = factory()->NewVariableProxy( | |
1047 current_function_state_->generator_object_variable()); | |
1048 Yield* yield = factory()->NewYield( | |
1049 get_proxy, assignment, false, RelocInfo::kNoPosition); | |
1050 processor->Add(factory()->NewExpressionStatement(yield), zone()); | |
1051 } | |
1052 | |
1036 // Allocate a target stack to use for this set of source | 1053 // Allocate a target stack to use for this set of source |
1037 // elements. This way, all scripts and functions get their own | 1054 // elements. This way, all scripts and functions get their own |
1038 // target stack thus avoiding illegal breaks and continues across | 1055 // target stack thus avoiding illegal breaks and continues across |
1039 // functions. | 1056 // functions. |
1040 TargetScope scope(&this->target_stack_); | 1057 TargetScope scope(&this->target_stack_); |
1041 | 1058 |
1042 ASSERT(processor != NULL); | 1059 ASSERT(processor != NULL); |
1043 ThisNamedPropertyAssignmentFinder this_property_assignment_finder(isolate(), | 1060 ThisNamedPropertyAssignmentFinder this_property_assignment_finder(isolate(), |
1044 zone()); | 1061 zone()); |
1045 bool directive_prologue = true; // Parsing directive prologue. | 1062 bool directive_prologue = true; // Parsing directive prologue. |
(...skipping 2050 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3096 return factory()->NewAssignment(op, expression, right, pos); | 3113 return factory()->NewAssignment(op, expression, right, pos); |
3097 } | 3114 } |
3098 | 3115 |
3099 | 3116 |
3100 Expression* Parser::ParseYieldExpression(bool* ok) { | 3117 Expression* Parser::ParseYieldExpression(bool* ok) { |
3101 // YieldExpression :: | 3118 // YieldExpression :: |
3102 // 'yield' '*'? AssignmentExpression | 3119 // 'yield' '*'? AssignmentExpression |
3103 int position = scanner().peek_location().beg_pos; | 3120 int position = scanner().peek_location().beg_pos; |
3104 Expect(Token::YIELD, CHECK_OK); | 3121 Expect(Token::YIELD, CHECK_OK); |
3105 bool is_yield_star = Check(Token::MUL); | 3122 bool is_yield_star = Check(Token::MUL); |
3123 Expression* generator_object = factory()->NewVariableProxy( | |
3124 current_function_state_->generator_object_variable()); | |
3106 Expression* expression = ParseAssignmentExpression(false, CHECK_OK); | 3125 Expression* expression = ParseAssignmentExpression(false, CHECK_OK); |
3107 return factory()->NewYield(expression, is_yield_star, position); | 3126 return factory()->NewYield(generator_object, expression, is_yield_star, |
3127 position); | |
3108 } | 3128 } |
3109 | 3129 |
3110 | 3130 |
3111 // Precedence = 3 | 3131 // Precedence = 3 |
3112 Expression* Parser::ParseConditionalExpression(bool accept_IN, bool* ok) { | 3132 Expression* Parser::ParseConditionalExpression(bool accept_IN, bool* ok) { |
3113 // ConditionalExpression :: | 3133 // ConditionalExpression :: |
3114 // LogicalOrExpression | 3134 // LogicalOrExpression |
3115 // LogicalOrExpression '?' AssignmentExpression ':' AssignmentExpression | 3135 // LogicalOrExpression '?' AssignmentExpression ':' AssignmentExpression |
3116 | 3136 |
3117 // We start using the binary expression parser for prec >= 4 only! | 3137 // We start using the binary expression parser for prec >= 4 only! |
(...skipping 1264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4382 FunctionLiteral::ParameterFlag duplicate_parameters = | 4402 FunctionLiteral::ParameterFlag duplicate_parameters = |
4383 FunctionLiteral::kNoDuplicateParameters; | 4403 FunctionLiteral::kNoDuplicateParameters; |
4384 FunctionLiteral::IsParenthesizedFlag parenthesized = parenthesized_function_ | 4404 FunctionLiteral::IsParenthesizedFlag parenthesized = parenthesized_function_ |
4385 ? FunctionLiteral::kIsParenthesized | 4405 ? FunctionLiteral::kIsParenthesized |
4386 : FunctionLiteral::kNotParenthesized; | 4406 : FunctionLiteral::kNotParenthesized; |
4387 FunctionLiteral::IsGeneratorFlag generator = is_generator | 4407 FunctionLiteral::IsGeneratorFlag generator = is_generator |
4388 ? FunctionLiteral::kIsGenerator | 4408 ? FunctionLiteral::kIsGenerator |
4389 : FunctionLiteral::kNotGenerator; | 4409 : FunctionLiteral::kNotGenerator; |
4390 AstProperties ast_properties; | 4410 AstProperties ast_properties; |
4391 // Parse function body. | 4411 // Parse function body. |
4392 { FunctionState function_state(this, scope, is_generator, isolate()); | 4412 { FunctionState function_state(this, scope, isolate()); |
4393 top_scope_->SetScopeName(function_name); | 4413 top_scope_->SetScopeName(function_name); |
4394 // For generators, allocating variables in contexts is currently a win | 4414 |
4395 // because it minimizes the work needed to suspend and resume an activation. | 4415 if (is_generator) { |
4396 if (is_generator) top_scope_->ForceContextAllocation(); | 4416 // For generators, allocating variables in contexts is currently a win |
4417 // because it minimizes the work needed to suspend and resume an | |
4418 // activation. | |
4419 top_scope_->ForceContextAllocation(); | |
4420 | |
4421 // Calling a generator returns a generator object. That object is stored | |
4422 // in a temporary variable, a definition that is used by "yield" | |
4423 // expressions. Presence of a variable for the generator object in the | |
4424 // FunctionState indicates that this function is a generator. | |
4425 Handle<String> tempname = isolate()->factory()->InternalizeOneByteString( | |
4426 STATIC_ASCII_VECTOR(".generator_object")); | |
4427 Variable* temp = top_scope_->DeclarationScope()->NewTemporary(tempname); | |
4428 function_state.set_generator_object_variable(temp); | |
4429 } | |
4397 | 4430 |
4398 // FormalParameterList :: | 4431 // FormalParameterList :: |
4399 // '(' (Identifier)*[','] ')' | 4432 // '(' (Identifier)*[','] ')' |
4400 Expect(Token::LPAREN, CHECK_OK); | 4433 Expect(Token::LPAREN, CHECK_OK); |
4401 scope->set_start_position(scanner().location().beg_pos); | 4434 scope->set_start_position(scanner().location().beg_pos); |
4402 Scanner::Location name_loc = Scanner::Location::invalid(); | 4435 Scanner::Location name_loc = Scanner::Location::invalid(); |
4403 Scanner::Location dupe_loc = Scanner::Location::invalid(); | 4436 Scanner::Location dupe_loc = Scanner::Location::invalid(); |
4404 Scanner::Location reserved_loc = Scanner::Location::invalid(); | 4437 Scanner::Location reserved_loc = Scanner::Location::invalid(); |
4405 | 4438 |
4406 bool done = (peek() == Token::RPAREN); | 4439 bool done = (peek() == Token::RPAREN); |
(...skipping 1567 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
5974 ASSERT(info()->isolate()->has_pending_exception()); | 6007 ASSERT(info()->isolate()->has_pending_exception()); |
5975 } else { | 6008 } else { |
5976 result = ParseProgram(); | 6009 result = ParseProgram(); |
5977 } | 6010 } |
5978 } | 6011 } |
5979 info()->SetFunction(result); | 6012 info()->SetFunction(result); |
5980 return (result != NULL); | 6013 return (result != NULL); |
5981 } | 6014 } |
5982 | 6015 |
5983 } } // namespace v8::internal | 6016 } } // namespace v8::internal |
OLD | NEW |