OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 566 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
577 scanner_(isolate_->unicode_cache()), | 577 scanner_(isolate_->unicode_cache()), |
578 top_scope_(NULL), | 578 top_scope_(NULL), |
579 with_nesting_level_(0), | 579 with_nesting_level_(0), |
580 lexical_scope_(NULL), | 580 lexical_scope_(NULL), |
581 target_stack_(NULL), | 581 target_stack_(NULL), |
582 allow_natives_syntax_(allow_natives_syntax), | 582 allow_natives_syntax_(allow_natives_syntax), |
583 extension_(extension), | 583 extension_(extension), |
584 pre_data_(pre_data), | 584 pre_data_(pre_data), |
585 fni_(NULL), | 585 fni_(NULL), |
586 stack_overflow_(false), | 586 stack_overflow_(false), |
587 parenthesized_function_(false) { | 587 parenthesized_function_(false), |
| 588 harmony_block_scoping_(false) { |
588 AstNode::ResetIds(); | 589 AstNode::ResetIds(); |
589 } | 590 } |
590 | 591 |
591 | 592 |
592 FunctionLiteral* Parser::ParseProgram(Handle<String> source, | 593 FunctionLiteral* Parser::ParseProgram(Handle<String> source, |
593 bool in_global_context, | 594 bool in_global_context, |
594 StrictModeFlag strict_mode) { | 595 StrictModeFlag strict_mode) { |
595 ZoneScope zone_scope(isolate(), DONT_DELETE_ON_EXIT); | 596 ZoneScope zone_scope(isolate(), DONT_DELETE_ON_EXIT); |
596 | 597 |
597 HistogramTimerScope timer(isolate()->counters()->parse()); | 598 HistogramTimerScope timer(isolate()->counters()->parse()); |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
802 Factory* factory = isolate()->factory(); | 803 Factory* factory = isolate()->factory(); |
803 Handle<FixedArray> elements = factory->NewFixedArray(args.length()); | 804 Handle<FixedArray> elements = factory->NewFixedArray(args.length()); |
804 for (int i = 0; i < args.length(); i++) { | 805 for (int i = 0; i < args.length(); i++) { |
805 elements->set(i, *args[i]); | 806 elements->set(i, *args[i]); |
806 } | 807 } |
807 Handle<JSArray> array = factory->NewJSArrayWithElements(elements); | 808 Handle<JSArray> array = factory->NewJSArrayWithElements(elements); |
808 Handle<Object> result = factory->NewSyntaxError(type, array); | 809 Handle<Object> result = factory->NewSyntaxError(type, array); |
809 isolate()->Throw(*result, &location); | 810 isolate()->Throw(*result, &location); |
810 } | 811 } |
811 | 812 |
| 813 void Parser::SetHarmonyBlockScoping(bool block_scoping) { |
| 814 harmony_block_scoping_ = block_scoping; |
| 815 } |
812 | 816 |
813 // Base class containing common code for the different finder classes used by | 817 // Base class containing common code for the different finder classes used by |
814 // the parser. | 818 // the parser. |
815 class ParserFinder { | 819 class ParserFinder { |
816 protected: | 820 protected: |
817 ParserFinder() {} | 821 ParserFinder() {} |
818 static Assignment* AsAssignment(Statement* stat) { | 822 static Assignment* AsAssignment(Statement* stat) { |
819 if (stat == NULL) return NULL; | 823 if (stat == NULL) return NULL; |
820 ExpressionStatement* exp_stat = stat->AsExpressionStatement(); | 824 ExpressionStatement* exp_stat = stat->AsExpressionStatement(); |
821 if (exp_stat == NULL) return NULL; | 825 if (exp_stat == NULL) return NULL; |
(...skipping 658 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1480 CHECK_OK); | 1484 CHECK_OK); |
1481 // Even if we're not at the top-level of the global or a function | 1485 // Even if we're not at the top-level of the global or a function |
1482 // scope, we treat is as such and introduce the function with it's | 1486 // scope, we treat is as such and introduce the function with it's |
1483 // initial value upon entering the corresponding scope. | 1487 // initial value upon entering the corresponding scope. |
1484 Declare(name, Variable::VAR, fun, true, CHECK_OK); | 1488 Declare(name, Variable::VAR, fun, true, CHECK_OK); |
1485 return EmptyStatement(); | 1489 return EmptyStatement(); |
1486 } | 1490 } |
1487 | 1491 |
1488 | 1492 |
1489 Block* Parser::ParseBlock(ZoneStringList* labels, bool* ok) { | 1493 Block* Parser::ParseBlock(ZoneStringList* labels, bool* ok) { |
| 1494 if (harmony_block_scoping_) return ParseScopedBlock(labels, ok); |
| 1495 |
1490 // Block :: | 1496 // Block :: |
1491 // '{' Statement* '}' | 1497 // '{' Statement* '}' |
1492 | 1498 |
1493 // Note that a Block does not introduce a new execution scope! | 1499 // Note that a Block does not introduce a new execution scope! |
1494 // (ECMA-262, 3rd, 12.2) | 1500 // (ECMA-262, 3rd, 12.2) |
1495 // | 1501 // |
1496 // Construct block expecting 16 statements. | 1502 // Construct block expecting 16 statements. |
1497 Block* result = new(zone()) Block(isolate(), labels, 16, false); | 1503 Block* result = new(zone()) Block(isolate(), labels, 16, false); |
1498 Target target(&this->target_stack_, result); | 1504 Target target(&this->target_stack_, result); |
1499 Expect(Token::LBRACE, CHECK_OK); | 1505 Expect(Token::LBRACE, CHECK_OK); |
1500 InitializationBlockFinder block_finder(top_scope_, target_stack_); | 1506 InitializationBlockFinder block_finder(top_scope_, target_stack_); |
1501 while (peek() != Token::RBRACE) { | 1507 while (peek() != Token::RBRACE) { |
1502 Statement* stat = ParseStatement(NULL, CHECK_OK); | 1508 Statement* stat = ParseStatement(NULL, CHECK_OK); |
1503 if (stat && !stat->IsEmpty()) { | 1509 if (stat && !stat->IsEmpty()) { |
1504 result->AddStatement(stat); | 1510 result->AddStatement(stat); |
1505 block_finder.Update(stat); | 1511 block_finder.Update(stat); |
1506 } | 1512 } |
1507 } | 1513 } |
1508 Expect(Token::RBRACE, CHECK_OK); | 1514 Expect(Token::RBRACE, CHECK_OK); |
1509 return result; | 1515 return result; |
1510 } | 1516 } |
1511 | 1517 |
1512 | 1518 |
| 1519 Block* Parser::ParseScopedBlock(ZoneStringList* labels, bool* ok) { |
| 1520 // Construct block expecting 16 statements. |
| 1521 Block* body = new(zone()) Block(isolate(), labels, 16, false); |
| 1522 Scope* saved_scope = top_scope_; |
| 1523 Scope* block_scope = NewScope(top_scope_, |
| 1524 Scope::BLOCK_SCOPE, |
| 1525 inside_with()); |
| 1526 body->set_block_scope(block_scope); |
| 1527 block_scope->DeclareLocal(isolate()->factory()->block_scope_symbol(), |
| 1528 Variable::VAR); |
| 1529 if (top_scope_->is_strict_mode()) { |
| 1530 block_scope->EnableStrictMode(); |
| 1531 } |
| 1532 top_scope_ = block_scope; |
| 1533 |
| 1534 // Parse the statements and collect escaping labels. |
| 1535 TargetCollector collector; |
| 1536 Target target(&this->target_stack_, &collector); |
| 1537 Expect(Token::LBRACE, CHECK_OK); |
| 1538 { |
| 1539 Target target_body(&this->target_stack_, body); |
| 1540 InitializationBlockFinder block_finder(top_scope_, target_stack_); |
| 1541 |
| 1542 while (peek() != Token::RBRACE) { |
| 1543 Statement* stat = ParseStatement(NULL, CHECK_OK); |
| 1544 if (stat && !stat->IsEmpty()) { |
| 1545 body->AddStatement(stat); |
| 1546 block_finder.Update(stat); |
| 1547 } |
| 1548 } |
| 1549 } |
| 1550 Expect(Token::RBRACE, CHECK_OK); |
| 1551 |
| 1552 // Create exit block. |
| 1553 Block* exit = new(zone()) Block(isolate(), NULL, 1, false); |
| 1554 exit->AddStatement(new(zone()) ExitContextStatement()); |
| 1555 |
| 1556 // Create a try-finally statement. |
| 1557 TryFinallyStatement* try_finally = |
| 1558 new(zone()) TryFinallyStatement(body, exit); |
| 1559 try_finally->set_escaping_targets(collector.targets()); |
| 1560 top_scope_ = saved_scope; |
| 1561 |
| 1562 // Create a result block. |
| 1563 Block* result = new(zone()) Block(isolate(), NULL, 1, false); |
| 1564 result->AddStatement(try_finally); |
| 1565 return result; |
| 1566 } |
| 1567 |
| 1568 |
1513 Block* Parser::ParseVariableStatement(bool* ok) { | 1569 Block* Parser::ParseVariableStatement(bool* ok) { |
1514 // VariableStatement :: | 1570 // VariableStatement :: |
1515 // VariableDeclarations ';' | 1571 // VariableDeclarations ';' |
1516 | 1572 |
1517 Handle<String> ignore; | 1573 Handle<String> ignore; |
1518 Block* result = ParseVariableDeclarations(true, &ignore, CHECK_OK); | 1574 Block* result = ParseVariableDeclarations(true, &ignore, CHECK_OK); |
1519 ExpectSemicolon(CHECK_OK); | 1575 ExpectSemicolon(CHECK_OK); |
1520 return result; | 1576 return result; |
1521 } | 1577 } |
1522 | 1578 |
(...skipping 3575 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5098 return !parser.failed(); | 5154 return !parser.failed(); |
5099 } | 5155 } |
5100 | 5156 |
5101 | 5157 |
5102 bool ParserApi::Parse(CompilationInfo* info) { | 5158 bool ParserApi::Parse(CompilationInfo* info) { |
5103 ASSERT(info->function() == NULL); | 5159 ASSERT(info->function() == NULL); |
5104 FunctionLiteral* result = NULL; | 5160 FunctionLiteral* result = NULL; |
5105 Handle<Script> script = info->script(); | 5161 Handle<Script> script = info->script(); |
5106 if (info->is_lazy()) { | 5162 if (info->is_lazy()) { |
5107 Parser parser(script, true, NULL, NULL); | 5163 Parser parser(script, true, NULL, NULL); |
| 5164 parser.SetHarmonyBlockScoping(!info->is_native() && |
| 5165 FLAG_harmony_block_scoping); |
5108 result = parser.ParseLazy(info); | 5166 result = parser.ParseLazy(info); |
5109 } else { | 5167 } else { |
5110 // Whether we allow %identifier(..) syntax. | 5168 // Whether we allow %identifier(..) syntax. |
5111 bool allow_natives_syntax = | 5169 bool allow_natives_syntax = |
5112 info->allows_natives_syntax() || FLAG_allow_natives_syntax; | 5170 info->allows_natives_syntax() || FLAG_allow_natives_syntax; |
5113 ScriptDataImpl* pre_data = info->pre_parse_data(); | 5171 ScriptDataImpl* pre_data = info->pre_parse_data(); |
5114 Parser parser(script, allow_natives_syntax, info->extension(), pre_data); | 5172 Parser parser(script, allow_natives_syntax, info->extension(), pre_data); |
| 5173 parser.SetHarmonyBlockScoping(!info->is_native() && |
| 5174 FLAG_harmony_block_scoping); |
5115 if (pre_data != NULL && pre_data->has_error()) { | 5175 if (pre_data != NULL && pre_data->has_error()) { |
5116 Scanner::Location loc = pre_data->MessageLocation(); | 5176 Scanner::Location loc = pre_data->MessageLocation(); |
5117 const char* message = pre_data->BuildMessage(); | 5177 const char* message = pre_data->BuildMessage(); |
5118 Vector<const char*> args = pre_data->BuildArgs(); | 5178 Vector<const char*> args = pre_data->BuildArgs(); |
5119 parser.ReportMessageAt(loc, message, args); | 5179 parser.ReportMessageAt(loc, message, args); |
5120 DeleteArray(message); | 5180 DeleteArray(message); |
5121 for (int i = 0; i < args.length(); i++) { | 5181 for (int i = 0; i < args.length(); i++) { |
5122 DeleteArray(args[i]); | 5182 DeleteArray(args[i]); |
5123 } | 5183 } |
5124 DeleteArray(args.start()); | 5184 DeleteArray(args.start()); |
5125 ASSERT(info->isolate()->has_pending_exception()); | 5185 ASSERT(info->isolate()->has_pending_exception()); |
5126 } else { | 5186 } else { |
5127 Handle<String> source = Handle<String>(String::cast(script->source())); | 5187 Handle<String> source = Handle<String>(String::cast(script->source())); |
5128 result = parser.ParseProgram(source, | 5188 result = parser.ParseProgram(source, |
5129 info->is_global(), | 5189 info->is_global(), |
5130 info->StrictMode()); | 5190 info->StrictMode()); |
5131 } | 5191 } |
5132 } | 5192 } |
5133 | |
5134 info->SetFunction(result); | 5193 info->SetFunction(result); |
5135 return (result != NULL); | 5194 return (result != NULL); |
5136 } | 5195 } |
5137 | 5196 |
5138 } } // namespace v8::internal | 5197 } } // namespace v8::internal |
OLD | NEW |