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 426 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1949 ExpectSemicolon(CHECK_OK); | 2005 ExpectSemicolon(CHECK_OK); |
1950 return new(zone()) ReturnStatement(GetLiteralUndefined()); | 2006 return new(zone()) ReturnStatement(GetLiteralUndefined()); |
1951 } | 2007 } |
1952 | 2008 |
1953 Expression* expr = ParseExpression(true, CHECK_OK); | 2009 Expression* expr = ParseExpression(true, CHECK_OK); |
1954 ExpectSemicolon(CHECK_OK); | 2010 ExpectSemicolon(CHECK_OK); |
1955 return new(zone()) ReturnStatement(expr); | 2011 return new(zone()) ReturnStatement(expr); |
1956 } | 2012 } |
1957 | 2013 |
1958 | 2014 |
1959 Block* Parser::WithHelper(Expression* obj, ZoneStringList* labels, bool* ok) { | |
1960 // Parse the statement and collect escaping labels. | |
1961 TargetCollector collector; | |
1962 Statement* stat; | |
1963 { Target target(&this->target_stack_, &collector); | |
1964 with_nesting_level_++; | |
1965 top_scope_->DeclarationScope()->RecordWithStatement(); | |
1966 stat = ParseStatement(labels, CHECK_OK); | |
1967 with_nesting_level_--; | |
1968 } | |
1969 // Create resulting block with two statements. | |
1970 // 1: Evaluate the with expression. | |
1971 // 2: The try-finally block evaluating the body. | |
1972 Block* result = new(zone()) Block(isolate(), NULL, 2, false); | |
1973 | |
1974 if (result != NULL) { | |
1975 result->AddStatement(new(zone()) EnterWithContextStatement(obj)); | |
1976 | |
1977 // Create body block. | |
1978 Block* body = new(zone()) Block(isolate(), NULL, 1, false); | |
1979 body->AddStatement(stat); | |
1980 | |
1981 // Create exit block. | |
1982 Block* exit = new(zone()) Block(isolate(), NULL, 1, false); | |
1983 exit->AddStatement(new(zone()) ExitContextStatement()); | |
1984 | |
1985 // Return a try-finally statement. | |
1986 TryFinallyStatement* wrapper = new(zone()) TryFinallyStatement(body, exit); | |
1987 wrapper->set_escaping_targets(collector.targets()); | |
1988 result->AddStatement(wrapper); | |
1989 } | |
1990 return result; | |
1991 } | |
1992 | |
1993 | |
1994 Statement* Parser::ParseWithStatement(ZoneStringList* labels, bool* ok) { | 2015 Statement* Parser::ParseWithStatement(ZoneStringList* labels, bool* ok) { |
1995 // WithStatement :: | 2016 // WithStatement :: |
1996 // 'with' '(' Expression ')' Statement | 2017 // 'with' '(' Expression ')' Statement |
1997 | 2018 |
1998 Expect(Token::WITH, CHECK_OK); | 2019 Expect(Token::WITH, CHECK_OK); |
1999 | 2020 |
2000 if (top_scope_->is_strict_mode()) { | 2021 if (top_scope_->is_strict_mode()) { |
2001 ReportMessage("strict_mode_with", Vector<const char*>::empty()); | 2022 ReportMessage("strict_mode_with", Vector<const char*>::empty()); |
2002 *ok = false; | 2023 *ok = false; |
2003 return NULL; | 2024 return NULL; |
2004 } | 2025 } |
2005 | 2026 |
2006 Expect(Token::LPAREN, CHECK_OK); | 2027 Expect(Token::LPAREN, CHECK_OK); |
2007 Expression* expr = ParseExpression(true, CHECK_OK); | 2028 Expression* expr = ParseExpression(true, CHECK_OK); |
2008 Expect(Token::RPAREN, CHECK_OK); | 2029 Expect(Token::RPAREN, CHECK_OK); |
2009 | 2030 |
2010 return WithHelper(expr, labels, CHECK_OK); | 2031 ++with_nesting_level_; |
| 2032 top_scope_->DeclarationScope()->RecordWithStatement(); |
| 2033 Statement* stmt = ParseStatement(labels, CHECK_OK); |
| 2034 --with_nesting_level_; |
| 2035 return new(zone()) WithStatement(expr, stmt); |
2011 } | 2036 } |
2012 | 2037 |
2013 | 2038 |
2014 CaseClause* Parser::ParseCaseClause(bool* default_seen_ptr, bool* ok) { | 2039 CaseClause* Parser::ParseCaseClause(bool* default_seen_ptr, bool* ok) { |
2015 // CaseClause :: | 2040 // CaseClause :: |
2016 // 'case' Expression ':' Statement* | 2041 // 'case' Expression ':' Statement* |
2017 // 'default' ':' Statement* | 2042 // 'default' ':' Statement* |
2018 | 2043 |
2019 Expression* label = NULL; // NULL expression indicates default case | 2044 Expression* label = NULL; // NULL expression indicates default case |
2020 if (peek() == Token::CASE) { | 2045 if (peek() == Token::CASE) { |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2135 | 2160 |
2136 if (top_scope_->is_strict_mode() && IsEvalOrArguments(name)) { | 2161 if (top_scope_->is_strict_mode() && IsEvalOrArguments(name)) { |
2137 ReportMessage("strict_catch_variable", Vector<const char*>::empty()); | 2162 ReportMessage("strict_catch_variable", Vector<const char*>::empty()); |
2138 *ok = false; | 2163 *ok = false; |
2139 return NULL; | 2164 return NULL; |
2140 } | 2165 } |
2141 | 2166 |
2142 Expect(Token::RPAREN, CHECK_OK); | 2167 Expect(Token::RPAREN, CHECK_OK); |
2143 | 2168 |
2144 if (peek() == Token::LBRACE) { | 2169 if (peek() == Token::LBRACE) { |
2145 // Rewrite the catch body B to a single statement block | 2170 // Rewrite the catch body { B } to a block: |
2146 // { try B finally { PopContext }}. | 2171 // { { B } ExitContext; }. |
2147 Block* inner_body; | 2172 Target target(&this->target_stack_, &catch_collector); |
2148 // We need to collect escapes from the body for both the inner | 2173 catch_scope = NewScope(top_scope_, Scope::CATCH_SCOPE, inside_with()); |
2149 // try/finally used to pop the catch context and any possible outer | 2174 if (top_scope_->is_strict_mode()) { |
2150 // try/finally. | 2175 catch_scope->EnableStrictMode(); |
2151 TargetCollector inner_collector; | 2176 } |
2152 { Target target(&this->target_stack_, &catch_collector); | 2177 catch_variable = catch_scope->DeclareLocal(name, Variable::VAR); |
2153 { Target target(&this->target_stack_, &inner_collector); | 2178 catch_block = new(zone()) Block(isolate(), NULL, 2, false); |
2154 catch_scope = NewScope(top_scope_, Scope::CATCH_SCOPE, inside_with()); | |
2155 if (top_scope_->is_strict_mode()) { | |
2156 catch_scope->EnableStrictMode(); | |
2157 } | |
2158 catch_variable = catch_scope->DeclareLocal(name, Variable::VAR); | |
2159 | 2179 |
2160 Scope* saved_scope = top_scope_; | 2180 Scope* saved_scope = top_scope_; |
2161 top_scope_ = catch_scope; | 2181 top_scope_ = catch_scope; |
2162 inner_body = ParseBlock(NULL, CHECK_OK); | 2182 Block* catch_body = ParseBlock(NULL, CHECK_OK); |
2163 top_scope_ = saved_scope; | 2183 top_scope_ = saved_scope; |
2164 } | 2184 catch_block->AddStatement(catch_body); |
2165 } | 2185 catch_block->AddStatement(new(zone()) ExitContextStatement()); |
2166 | |
2167 // Create exit block. | |
2168 Block* inner_finally = new(zone()) Block(isolate(), NULL, 1, false); | |
2169 inner_finally->AddStatement(new(zone()) ExitContextStatement()); | |
2170 | |
2171 // Create a try/finally statement. | |
2172 TryFinallyStatement* inner_try_finally = | |
2173 new(zone()) TryFinallyStatement(inner_body, inner_finally); | |
2174 inner_try_finally->set_escaping_targets(inner_collector.targets()); | |
2175 | |
2176 catch_block = new(zone()) Block(isolate(), NULL, 1, false); | |
2177 catch_block->AddStatement(inner_try_finally); | |
2178 } else { | 2186 } else { |
2179 Expect(Token::LBRACE, CHECK_OK); | 2187 Expect(Token::LBRACE, CHECK_OK); |
2180 } | 2188 } |
2181 | 2189 |
2182 tok = peek(); | 2190 tok = peek(); |
2183 } | 2191 } |
2184 | 2192 |
2185 Block* finally_block = NULL; | 2193 Block* finally_block = NULL; |
2186 if (tok == Token::FINALLY || catch_block == NULL) { | 2194 if (tok == Token::FINALLY || catch_block == NULL) { |
2187 Consume(Token::FINALLY); | 2195 Consume(Token::FINALLY); |
(...skipping 2910 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5098 return !parser.failed(); | 5106 return !parser.failed(); |
5099 } | 5107 } |
5100 | 5108 |
5101 | 5109 |
5102 bool ParserApi::Parse(CompilationInfo* info) { | 5110 bool ParserApi::Parse(CompilationInfo* info) { |
5103 ASSERT(info->function() == NULL); | 5111 ASSERT(info->function() == NULL); |
5104 FunctionLiteral* result = NULL; | 5112 FunctionLiteral* result = NULL; |
5105 Handle<Script> script = info->script(); | 5113 Handle<Script> script = info->script(); |
5106 if (info->is_lazy()) { | 5114 if (info->is_lazy()) { |
5107 Parser parser(script, true, NULL, NULL); | 5115 Parser parser(script, true, NULL, NULL); |
| 5116 parser.SetHarmonyBlockScoping(!info->is_native() && |
| 5117 FLAG_harmony_block_scoping); |
5108 result = parser.ParseLazy(info); | 5118 result = parser.ParseLazy(info); |
5109 } else { | 5119 } else { |
5110 // Whether we allow %identifier(..) syntax. | 5120 // Whether we allow %identifier(..) syntax. |
5111 bool allow_natives_syntax = | 5121 bool allow_natives_syntax = |
5112 info->allows_natives_syntax() || FLAG_allow_natives_syntax; | 5122 info->allows_natives_syntax() || FLAG_allow_natives_syntax; |
5113 ScriptDataImpl* pre_data = info->pre_parse_data(); | 5123 ScriptDataImpl* pre_data = info->pre_parse_data(); |
5114 Parser parser(script, allow_natives_syntax, info->extension(), pre_data); | 5124 Parser parser(script, allow_natives_syntax, info->extension(), pre_data); |
| 5125 parser.SetHarmonyBlockScoping(!info->is_native() && |
| 5126 FLAG_harmony_block_scoping); |
5115 if (pre_data != NULL && pre_data->has_error()) { | 5127 if (pre_data != NULL && pre_data->has_error()) { |
5116 Scanner::Location loc = pre_data->MessageLocation(); | 5128 Scanner::Location loc = pre_data->MessageLocation(); |
5117 const char* message = pre_data->BuildMessage(); | 5129 const char* message = pre_data->BuildMessage(); |
5118 Vector<const char*> args = pre_data->BuildArgs(); | 5130 Vector<const char*> args = pre_data->BuildArgs(); |
5119 parser.ReportMessageAt(loc, message, args); | 5131 parser.ReportMessageAt(loc, message, args); |
5120 DeleteArray(message); | 5132 DeleteArray(message); |
5121 for (int i = 0; i < args.length(); i++) { | 5133 for (int i = 0; i < args.length(); i++) { |
5122 DeleteArray(args[i]); | 5134 DeleteArray(args[i]); |
5123 } | 5135 } |
5124 DeleteArray(args.start()); | 5136 DeleteArray(args.start()); |
5125 ASSERT(info->isolate()->has_pending_exception()); | 5137 ASSERT(info->isolate()->has_pending_exception()); |
5126 } else { | 5138 } else { |
5127 Handle<String> source = Handle<String>(String::cast(script->source())); | 5139 Handle<String> source = Handle<String>(String::cast(script->source())); |
5128 result = parser.ParseProgram(source, | 5140 result = parser.ParseProgram(source, |
5129 info->is_global(), | 5141 info->is_global(), |
5130 info->StrictMode()); | 5142 info->StrictMode()); |
5131 } | 5143 } |
5132 } | 5144 } |
5133 | |
5134 info->SetFunction(result); | 5145 info->SetFunction(result); |
5135 return (result != NULL); | 5146 return (result != NULL); |
5136 } | 5147 } |
5137 | 5148 |
5138 } } // namespace v8::internal | 5149 } } // namespace v8::internal |
OLD | NEW |