| 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 1203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1214 } | 1214 } |
| 1215 | 1215 |
| 1216 | 1216 |
| 1217 Block* Parser::ParseModuleDeclaration(bool* ok) { | 1217 Block* Parser::ParseModuleDeclaration(bool* ok) { |
| 1218 // ModuleDeclaration: | 1218 // ModuleDeclaration: |
| 1219 // 'module' Identifier Module | 1219 // 'module' Identifier Module |
| 1220 | 1220 |
| 1221 // Create new block with one expected declaration. | 1221 // Create new block with one expected declaration. |
| 1222 Block* block = factory()->NewBlock(NULL, 1, true); | 1222 Block* block = factory()->NewBlock(NULL, 1, true); |
| 1223 Handle<String> name = ParseIdentifier(CHECK_OK); | 1223 Handle<String> name = ParseIdentifier(CHECK_OK); |
| 1224 // top_scope_->AddDeclaration( | 1224 Module* module = ParseModule(CHECK_OK); |
| 1225 // factory()->NewModuleDeclaration(proxy, module, top_scope_)); | 1225 VariableProxy* proxy = NewUnresolved(name, LET); |
| 1226 VariableProxy* proxy = Declare(name, LET, NULL, true, CHECK_OK); | 1226 Declaration* declaration = |
| 1227 Module* module = ParseModule(ok); | 1227 factory()->NewModuleDeclaration(proxy, module, top_scope_); |
| 1228 Declare(declaration, true, CHECK_OK); |
| 1229 |
| 1228 // TODO(rossberg): Add initialization statement to block. | 1230 // TODO(rossberg): Add initialization statement to block. |
| 1229 USE(proxy); | 1231 |
| 1230 USE(module); | |
| 1231 return block; | 1232 return block; |
| 1232 } | 1233 } |
| 1233 | 1234 |
| 1234 | 1235 |
| 1235 Module* Parser::ParseModule(bool* ok) { | 1236 Module* Parser::ParseModule(bool* ok) { |
| 1236 // Module: | 1237 // Module: |
| 1237 // '{' ModuleElement '}' | 1238 // '{' ModuleElement '}' |
| 1238 // '=' ModulePath | 1239 // '=' ModulePath |
| 1239 // 'at' String | 1240 // 'at' String |
| 1240 | 1241 |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1490 default: | 1491 default: |
| 1491 stmt = ParseExpressionOrLabelledStatement(labels, ok); | 1492 stmt = ParseExpressionOrLabelledStatement(labels, ok); |
| 1492 } | 1493 } |
| 1493 | 1494 |
| 1494 // Store the source position of the statement | 1495 // Store the source position of the statement |
| 1495 if (stmt != NULL) stmt->set_statement_pos(statement_pos); | 1496 if (stmt != NULL) stmt->set_statement_pos(statement_pos); |
| 1496 return stmt; | 1497 return stmt; |
| 1497 } | 1498 } |
| 1498 | 1499 |
| 1499 | 1500 |
| 1500 VariableProxy* Parser::Declare(Handle<String> name, | 1501 VariableProxy* Parser::NewUnresolved(Handle<String> name, VariableMode mode) { |
| 1501 VariableMode mode, | |
| 1502 FunctionLiteral* fun, | |
| 1503 bool resolve, | |
| 1504 bool* ok) { | |
| 1505 Variable* var = NULL; | |
| 1506 // If we are inside a function, a declaration of a var/const variable is a | 1502 // If we are inside a function, a declaration of a var/const variable is a |
| 1507 // truly local variable, and the scope of the variable is always the function | 1503 // truly local variable, and the scope of the variable is always the function |
| 1508 // scope. | 1504 // scope. |
| 1509 // Let/const variables in harmony mode are always added to the immediately | 1505 // Let/const variables in harmony mode are always added to the immediately |
| 1510 // enclosing scope. | 1506 // enclosing scope. |
| 1511 Scope* declaration_scope = (mode == LET || mode == CONST_HARMONY) | 1507 return DeclarationScope(mode)->NewUnresolved( |
| 1512 ? top_scope_ : top_scope_->DeclarationScope(); | 1508 factory(), name, scanner().location().beg_pos); |
| 1513 InitializationFlag init_flag = (fun != NULL || mode == VAR) | 1509 } |
| 1514 ? kCreatedInitialized : kNeedsInitialization; | 1510 |
| 1511 |
| 1512 void Parser::Declare(Declaration* declaration, bool resolve, bool* ok) { |
| 1513 Handle<String> name = declaration->proxy()->name(); |
| 1514 VariableMode mode = declaration->mode(); |
| 1515 Scope* declaration_scope = DeclarationScope(mode); |
| 1516 Variable* var = NULL; |
| 1515 | 1517 |
| 1516 // If a function scope exists, then we can statically declare this | 1518 // If a function scope exists, then we can statically declare this |
| 1517 // variable and also set its mode. In any case, a Declaration node | 1519 // variable and also set its mode. In any case, a Declaration node |
| 1518 // will be added to the scope so that the declaration can be added | 1520 // will be added to the scope so that the declaration can be added |
| 1519 // to the corresponding activation frame at runtime if necessary. | 1521 // to the corresponding activation frame at runtime if necessary. |
| 1520 // For instance declarations inside an eval scope need to be added | 1522 // For instance declarations inside an eval scope need to be added |
| 1521 // to the calling function context. | 1523 // to the calling function context. |
| 1522 // Similarly, strict mode eval scope does not leak variable declarations to | 1524 // Similarly, strict mode eval scope does not leak variable declarations to |
| 1523 // the caller's scope so we declare all locals, too. | 1525 // the caller's scope so we declare all locals, too. |
| 1524 // Also for block scoped let/const bindings the variable can be | 1526 // Also for block scoped let/const bindings the variable can be |
| 1525 // statically declared. | 1527 // statically declared. |
| 1526 if (declaration_scope->is_function_scope() || | 1528 if (declaration_scope->is_function_scope() || |
| 1527 declaration_scope->is_strict_or_extended_eval_scope() || | 1529 declaration_scope->is_strict_or_extended_eval_scope() || |
| 1528 declaration_scope->is_block_scope() || | 1530 declaration_scope->is_block_scope() || |
| 1529 declaration_scope->is_module_scope()) { | 1531 declaration_scope->is_module_scope()) { |
| 1530 // Declare the variable in the function scope. | 1532 // Declare the variable in the function scope. |
| 1531 var = declaration_scope->LocalLookup(name); | 1533 var = declaration_scope->LocalLookup(name); |
| 1532 if (var == NULL) { | 1534 if (var == NULL) { |
| 1533 // Declare the name. | 1535 // Declare the name. |
| 1534 var = declaration_scope->DeclareLocal(name, mode, init_flag); | 1536 var = declaration_scope->DeclareLocal( |
| 1537 name, mode, declaration->initialization()); |
| 1535 } else { | 1538 } else { |
| 1536 // The name was declared in this scope before; check for conflicting | 1539 // The name was declared in this scope before; check for conflicting |
| 1537 // re-declarations. We have a conflict if either of the declarations is | 1540 // re-declarations. We have a conflict if either of the declarations is |
| 1538 // not a var. There is similar code in runtime.cc in the Declare | 1541 // not a var. There is similar code in runtime.cc in the Declare |
| 1539 // functions. The function CheckNonConflictingScope checks for conflicting | 1542 // functions. The function CheckNonConflictingScope checks for conflicting |
| 1540 // var and let bindings from different scopes whereas this is a check for | 1543 // var and let bindings from different scopes whereas this is a check for |
| 1541 // conflicting declarations within the same scope. This check also covers | 1544 // conflicting declarations within the same scope. This check also covers |
| 1542 // | 1545 // |
| 1543 // function () { let x; { var x; } } | 1546 // function () { let x; { var x; } } |
| 1544 // | 1547 // |
| 1545 // because the var declaration is hoisted to the function scope where 'x' | 1548 // because the var declaration is hoisted to the function scope where 'x' |
| 1546 // is already bound. | 1549 // is already bound. |
| 1547 if ((mode != VAR) || (var->mode() != VAR)) { | 1550 if ((mode != VAR) || (var->mode() != VAR)) { |
| 1548 // We only have vars, consts and lets in declarations. | 1551 // We only have vars, consts and lets in declarations. |
| 1549 ASSERT(var->mode() == VAR || | 1552 ASSERT(var->mode() == VAR || |
| 1550 var->mode() == CONST || | 1553 var->mode() == CONST || |
| 1551 var->mode() == CONST_HARMONY || | 1554 var->mode() == CONST_HARMONY || |
| 1552 var->mode() == LET); | 1555 var->mode() == LET); |
| 1553 if (is_extended_mode()) { | 1556 if (is_extended_mode()) { |
| 1554 // In harmony mode we treat re-declarations as early errors. See | 1557 // In harmony mode we treat re-declarations as early errors. See |
| 1555 // ES5 16 for a definition of early errors. | 1558 // ES5 16 for a definition of early errors. |
| 1556 SmartArrayPointer<char> c_string = name->ToCString(DISALLOW_NULLS); | 1559 SmartArrayPointer<char> c_string = name->ToCString(DISALLOW_NULLS); |
| 1557 const char* elms[2] = { "Variable", *c_string }; | 1560 const char* elms[2] = { "Variable", *c_string }; |
| 1558 Vector<const char*> args(elms, 2); | 1561 Vector<const char*> args(elms, 2); |
| 1559 ReportMessage("redeclaration", args); | 1562 ReportMessage("redeclaration", args); |
| 1560 *ok = false; | 1563 *ok = false; |
| 1561 return NULL; | 1564 return; |
| 1562 } | 1565 } |
| 1563 const char* type = (var->mode() == VAR) | 1566 const char* type = (var->mode() == VAR) |
| 1564 ? "var" : var->is_const_mode() ? "const" : "let"; | 1567 ? "var" : var->is_const_mode() ? "const" : "let"; |
| 1565 Handle<String> type_string = | 1568 Handle<String> type_string = |
| 1566 isolate()->factory()->NewStringFromUtf8(CStrVector(type), TENURED); | 1569 isolate()->factory()->NewStringFromUtf8(CStrVector(type), TENURED); |
| 1567 Expression* expression = | 1570 Expression* expression = |
| 1568 NewThrowTypeError(isolate()->factory()->redeclaration_symbol(), | 1571 NewThrowTypeError(isolate()->factory()->redeclaration_symbol(), |
| 1569 type_string, name); | 1572 type_string, name); |
| 1570 declaration_scope->SetIllegalRedeclaration(expression); | 1573 declaration_scope->SetIllegalRedeclaration(expression); |
| 1571 } | 1574 } |
| 1572 } | 1575 } |
| 1573 } | 1576 } |
| 1574 | 1577 |
| 1575 // We add a declaration node for every declaration. The compiler | 1578 // We add a declaration node for every declaration. The compiler |
| 1576 // will only generate code if necessary. In particular, declarations | 1579 // will only generate code if necessary. In particular, declarations |
| 1577 // for inner local variables that do not represent functions won't | 1580 // for inner local variables that do not represent functions won't |
| 1578 // result in any generated code. | 1581 // result in any generated code. |
| 1579 // | 1582 // |
| 1580 // Note that we always add an unresolved proxy even if it's not | 1583 // Note that we always add an unresolved proxy even if it's not |
| 1581 // used, simply because we don't know in this method (w/o extra | 1584 // used, simply because we don't know in this method (w/o extra |
| 1582 // parameters) if the proxy is needed or not. The proxy will be | 1585 // parameters) if the proxy is needed or not. The proxy will be |
| 1583 // bound during variable resolution time unless it was pre-bound | 1586 // bound during variable resolution time unless it was pre-bound |
| 1584 // below. | 1587 // below. |
| 1585 // | 1588 // |
| 1586 // WARNING: This will lead to multiple declaration nodes for the | 1589 // WARNING: This will lead to multiple declaration nodes for the |
| 1587 // same variable if it is declared several times. This is not a | 1590 // same variable if it is declared several times. This is not a |
| 1588 // semantic issue as long as we keep the source order, but it may be | 1591 // semantic issue as long as we keep the source order, but it may be |
| 1589 // a performance issue since it may lead to repeated | 1592 // a performance issue since it may lead to repeated |
| 1590 // Runtime::DeclareContextSlot() calls. | 1593 // Runtime::DeclareContextSlot() calls. |
| 1591 VariableProxy* proxy = declaration_scope->NewUnresolved( | 1594 declaration_scope->AddDeclaration(declaration); |
| 1592 factory(), name, scanner().location().beg_pos); | |
| 1593 declaration_scope->AddDeclaration( | |
| 1594 factory()->NewVariableDeclaration(proxy, mode, fun, top_scope_)); | |
| 1595 | 1595 |
| 1596 if ((mode == CONST || mode == CONST_HARMONY) && | 1596 if ((mode == CONST || mode == CONST_HARMONY) && |
| 1597 declaration_scope->is_global_scope()) { | 1597 declaration_scope->is_global_scope()) { |
| 1598 // For global const variables we bind the proxy to a variable. | 1598 // For global const variables we bind the proxy to a variable. |
| 1599 ASSERT(resolve); // should be set by all callers | 1599 ASSERT(resolve); // should be set by all callers |
| 1600 Variable::Kind kind = Variable::NORMAL; | 1600 Variable::Kind kind = Variable::NORMAL; |
| 1601 var = new(zone()) Variable(declaration_scope, | 1601 var = new(zone()) Variable(declaration_scope, |
| 1602 name, | 1602 name, |
| 1603 mode, | 1603 mode, |
| 1604 true, | 1604 true, |
| 1605 kind, | 1605 kind, |
| 1606 kNeedsInitialization); | 1606 kNeedsInitialization); |
| 1607 } else if (declaration_scope->is_eval_scope() && | 1607 } else if (declaration_scope->is_eval_scope() && |
| 1608 declaration_scope->is_classic_mode()) { | 1608 declaration_scope->is_classic_mode()) { |
| 1609 // For variable declarations in a non-strict eval scope the proxy is bound | 1609 // For variable declarations in a non-strict eval scope the proxy is bound |
| 1610 // to a lookup variable to force a dynamic declaration using the | 1610 // to a lookup variable to force a dynamic declaration using the |
| 1611 // DeclareContextSlot runtime function. | 1611 // DeclareContextSlot runtime function. |
| 1612 Variable::Kind kind = Variable::NORMAL; | 1612 Variable::Kind kind = Variable::NORMAL; |
| 1613 var = new(zone()) Variable(declaration_scope, | 1613 var = new(zone()) Variable(declaration_scope, |
| 1614 name, | 1614 name, |
| 1615 mode, | 1615 mode, |
| 1616 true, | 1616 true, |
| 1617 kind, | 1617 kind, |
| 1618 init_flag); | 1618 declaration->initialization()); |
| 1619 var->AllocateTo(Variable::LOOKUP, -1); | 1619 var->AllocateTo(Variable::LOOKUP, -1); |
| 1620 resolve = true; | 1620 resolve = true; |
| 1621 } | 1621 } |
| 1622 | 1622 |
| 1623 // If requested and we have a local variable, bind the proxy to the variable | 1623 // If requested and we have a local variable, bind the proxy to the variable |
| 1624 // at parse-time. This is used for functions (and consts) declared inside | 1624 // at parse-time. This is used for functions (and consts) declared inside |
| 1625 // statements: the corresponding function (or const) variable must be in the | 1625 // statements: the corresponding function (or const) variable must be in the |
| 1626 // function scope and not a statement-local scope, e.g. as provided with a | 1626 // function scope and not a statement-local scope, e.g. as provided with a |
| 1627 // 'with' statement: | 1627 // 'with' statement: |
| 1628 // | 1628 // |
| 1629 // with (obj) { | 1629 // with (obj) { |
| 1630 // function f() {} | 1630 // function f() {} |
| 1631 // } | 1631 // } |
| 1632 // | 1632 // |
| 1633 // which is translated into: | 1633 // which is translated into: |
| 1634 // | 1634 // |
| 1635 // with (obj) { | 1635 // with (obj) { |
| 1636 // // in this case this is not: 'var f; f = function () {};' | 1636 // // in this case this is not: 'var f; f = function () {};' |
| 1637 // var f = function () {}; | 1637 // var f = function () {}; |
| 1638 // } | 1638 // } |
| 1639 // | 1639 // |
| 1640 // Note that if 'f' is accessed from inside the 'with' statement, it | 1640 // Note that if 'f' is accessed from inside the 'with' statement, it |
| 1641 // will be allocated in the context (because we must be able to look | 1641 // will be allocated in the context (because we must be able to look |
| 1642 // it up dynamically) but it will also be accessed statically, i.e., | 1642 // it up dynamically) but it will also be accessed statically, i.e., |
| 1643 // with a context slot index and a context chain length for this | 1643 // with a context slot index and a context chain length for this |
| 1644 // initialization code. Thus, inside the 'with' statement, we need | 1644 // initialization code. Thus, inside the 'with' statement, we need |
| 1645 // both access to the static and the dynamic context chain; the | 1645 // both access to the static and the dynamic context chain; the |
| 1646 // runtime needs to provide both. | 1646 // runtime needs to provide both. |
| 1647 if (resolve && var != NULL) proxy->BindTo(var); | 1647 if (resolve && var != NULL) declaration->proxy()->BindTo(var); |
| 1648 | |
| 1649 return proxy; | |
| 1650 } | 1648 } |
| 1651 | 1649 |
| 1652 | 1650 |
| 1653 // Language extension which is only enabled for source files loaded | 1651 // Language extension which is only enabled for source files loaded |
| 1654 // through the API's extension mechanism. A native function | 1652 // through the API's extension mechanism. A native function |
| 1655 // declaration is resolved by looking up the function through a | 1653 // declaration is resolved by looking up the function through a |
| 1656 // callback provided by the extension. | 1654 // callback provided by the extension. |
| 1657 Statement* Parser::ParseNativeDeclaration(bool* ok) { | 1655 Statement* Parser::ParseNativeDeclaration(bool* ok) { |
| 1658 Expect(Token::FUNCTION, CHECK_OK); | 1656 Expect(Token::FUNCTION, CHECK_OK); |
| 1659 Handle<String> name = ParseIdentifier(CHECK_OK); | 1657 Handle<String> name = ParseIdentifier(CHECK_OK); |
| 1660 Expect(Token::LPAREN, CHECK_OK); | 1658 Expect(Token::LPAREN, CHECK_OK); |
| 1661 bool done = (peek() == Token::RPAREN); | 1659 bool done = (peek() == Token::RPAREN); |
| 1662 while (!done) { | 1660 while (!done) { |
| 1663 ParseIdentifier(CHECK_OK); | 1661 ParseIdentifier(CHECK_OK); |
| 1664 done = (peek() == Token::RPAREN); | 1662 done = (peek() == Token::RPAREN); |
| 1665 if (!done) { | 1663 if (!done) { |
| 1666 Expect(Token::COMMA, CHECK_OK); | 1664 Expect(Token::COMMA, CHECK_OK); |
| 1667 } | 1665 } |
| 1668 } | 1666 } |
| 1669 Expect(Token::RPAREN, CHECK_OK); | 1667 Expect(Token::RPAREN, CHECK_OK); |
| 1670 Expect(Token::SEMICOLON, CHECK_OK); | 1668 Expect(Token::SEMICOLON, CHECK_OK); |
| 1671 | 1669 |
| 1672 // Make sure that the function containing the native declaration | 1670 // Make sure that the function containing the native declaration |
| 1673 // isn't lazily compiled. The extension structures are only | 1671 // isn't lazily compiled. The extension structures are only |
| 1674 // accessible while parsing the first time not when reparsing | 1672 // accessible while parsing the first time not when reparsing |
| 1675 // because of lazy compilation. | 1673 // because of lazy compilation. |
| 1676 top_scope_->DeclarationScope()->ForceEagerCompilation(); | 1674 DeclarationScope(VAR)->ForceEagerCompilation(); |
| 1677 | 1675 |
| 1678 // Compute the function template for the native function. | 1676 // Compute the function template for the native function. |
| 1679 v8::Handle<v8::FunctionTemplate> fun_template = | 1677 v8::Handle<v8::FunctionTemplate> fun_template = |
| 1680 extension_->GetNativeFunction(v8::Utils::ToLocal(name)); | 1678 extension_->GetNativeFunction(v8::Utils::ToLocal(name)); |
| 1681 ASSERT(!fun_template.IsEmpty()); | 1679 ASSERT(!fun_template.IsEmpty()); |
| 1682 | 1680 |
| 1683 // Instantiate the function and create a shared function info from it. | 1681 // Instantiate the function and create a shared function info from it. |
| 1684 Handle<JSFunction> fun = Utils::OpenHandle(*fun_template->GetFunction()); | 1682 Handle<JSFunction> fun = Utils::OpenHandle(*fun_template->GetFunction()); |
| 1685 const int literals = fun->NumberOfLiterals(); | 1683 const int literals = fun->NumberOfLiterals(); |
| 1686 Handle<Code> code = Handle<Code>(fun->shared()->code()); | 1684 Handle<Code> code = Handle<Code>(fun->shared()->code()); |
| 1687 Handle<Code> construct_stub = Handle<Code>(fun->shared()->construct_stub()); | 1685 Handle<Code> construct_stub = Handle<Code>(fun->shared()->construct_stub()); |
| 1688 Handle<SharedFunctionInfo> shared = | 1686 Handle<SharedFunctionInfo> shared = |
| 1689 isolate()->factory()->NewSharedFunctionInfo(name, literals, code, | 1687 isolate()->factory()->NewSharedFunctionInfo(name, literals, code, |
| 1690 Handle<ScopeInfo>(fun->shared()->scope_info())); | 1688 Handle<ScopeInfo>(fun->shared()->scope_info())); |
| 1691 shared->set_construct_stub(*construct_stub); | 1689 shared->set_construct_stub(*construct_stub); |
| 1692 | 1690 |
| 1693 // Copy the function data to the shared function info. | 1691 // Copy the function data to the shared function info. |
| 1694 shared->set_function_data(fun->shared()->function_data()); | 1692 shared->set_function_data(fun->shared()->function_data()); |
| 1695 int parameters = fun->shared()->formal_parameter_count(); | 1693 int parameters = fun->shared()->formal_parameter_count(); |
| 1696 shared->set_formal_parameter_count(parameters); | 1694 shared->set_formal_parameter_count(parameters); |
| 1697 | 1695 |
| 1698 // TODO(1240846): It's weird that native function declarations are | 1696 // TODO(1240846): It's weird that native function declarations are |
| 1699 // introduced dynamically when we meet their declarations, whereas | 1697 // introduced dynamically when we meet their declarations, whereas |
| 1700 // other functions are set up when entering the surrounding scope. | 1698 // other functions are set up when entering the surrounding scope. |
| 1699 VariableProxy* proxy = NewUnresolved(name, VAR); |
| 1700 Declaration* declaration = |
| 1701 factory()->NewVariableDeclaration(proxy, VAR, top_scope_); |
| 1702 Declare(declaration, true, CHECK_OK); |
| 1701 SharedFunctionInfoLiteral* lit = | 1703 SharedFunctionInfoLiteral* lit = |
| 1702 factory()->NewSharedFunctionInfoLiteral(shared); | 1704 factory()->NewSharedFunctionInfoLiteral(shared); |
| 1703 VariableProxy* var = Declare(name, VAR, NULL, true, CHECK_OK); | |
| 1704 return factory()->NewExpressionStatement( | 1705 return factory()->NewExpressionStatement( |
| 1705 factory()->NewAssignment( | 1706 factory()->NewAssignment( |
| 1706 Token::INIT_VAR, var, lit, RelocInfo::kNoPosition)); | 1707 Token::INIT_VAR, proxy, lit, RelocInfo::kNoPosition)); |
| 1707 } | 1708 } |
| 1708 | 1709 |
| 1709 | 1710 |
| 1710 Statement* Parser::ParseFunctionDeclaration(bool* ok) { | 1711 Statement* Parser::ParseFunctionDeclaration(bool* ok) { |
| 1711 // FunctionDeclaration :: | 1712 // FunctionDeclaration :: |
| 1712 // 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}' | 1713 // 'function' Identifier '(' FormalParameterListopt ')' '{' FunctionBody '}' |
| 1713 Expect(Token::FUNCTION, CHECK_OK); | 1714 Expect(Token::FUNCTION, CHECK_OK); |
| 1714 int function_token_position = scanner().location().beg_pos; | 1715 int function_token_position = scanner().location().beg_pos; |
| 1715 bool is_strict_reserved = false; | 1716 bool is_strict_reserved = false; |
| 1716 Handle<String> name = ParseIdentifierOrStrictReservedWord( | 1717 Handle<String> name = ParseIdentifierOrStrictReservedWord( |
| 1717 &is_strict_reserved, CHECK_OK); | 1718 &is_strict_reserved, CHECK_OK); |
| 1718 FunctionLiteral* fun = ParseFunctionLiteral(name, | 1719 FunctionLiteral* fun = ParseFunctionLiteral(name, |
| 1719 is_strict_reserved, | 1720 is_strict_reserved, |
| 1720 function_token_position, | 1721 function_token_position, |
| 1721 FunctionLiteral::DECLARATION, | 1722 FunctionLiteral::DECLARATION, |
| 1722 CHECK_OK); | 1723 CHECK_OK); |
| 1723 // Even if we're not at the top-level of the global or a function | 1724 // Even if we're not at the top-level of the global or a function |
| 1724 // scope, we treat is as such and introduce the function with it's | 1725 // scope, we treat is as such and introduce the function with it's |
| 1725 // initial value upon entering the corresponding scope. | 1726 // initial value upon entering the corresponding scope. |
| 1726 VariableMode mode = is_extended_mode() ? LET : VAR; | 1727 VariableMode mode = is_extended_mode() ? LET : VAR; |
| 1727 Declare(name, mode, fun, true, CHECK_OK); | 1728 VariableProxy* proxy = NewUnresolved(name, mode); |
| 1729 Declaration* declaration = |
| 1730 factory()->NewFunctionDeclaration(proxy, mode, fun, top_scope_); |
| 1731 Declare(declaration, true, CHECK_OK); |
| 1728 return factory()->NewEmptyStatement(); | 1732 return factory()->NewEmptyStatement(); |
| 1729 } | 1733 } |
| 1730 | 1734 |
| 1731 | 1735 |
| 1732 Block* Parser::ParseBlock(ZoneStringList* labels, bool* ok) { | 1736 Block* Parser::ParseBlock(ZoneStringList* labels, bool* ok) { |
| 1733 if (top_scope_->is_extended_mode()) return ParseScopedBlock(labels, ok); | 1737 if (top_scope_->is_extended_mode()) return ParseScopedBlock(labels, ok); |
| 1734 | 1738 |
| 1735 // Block :: | 1739 // Block :: |
| 1736 // '{' Statement* '}' | 1740 // '{' Statement* '}' |
| 1737 | 1741 |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1895 *ok = false; | 1899 *ok = false; |
| 1896 return NULL; | 1900 return NULL; |
| 1897 } | 1901 } |
| 1898 mode = LET; | 1902 mode = LET; |
| 1899 needs_init = true; | 1903 needs_init = true; |
| 1900 init_op = Token::INIT_LET; | 1904 init_op = Token::INIT_LET; |
| 1901 } else { | 1905 } else { |
| 1902 UNREACHABLE(); // by current callers | 1906 UNREACHABLE(); // by current callers |
| 1903 } | 1907 } |
| 1904 | 1908 |
| 1905 Scope* declaration_scope = (mode == LET || mode == CONST_HARMONY) | 1909 Scope* declaration_scope = DeclarationScope(mode); |
| 1906 ? top_scope_ : top_scope_->DeclarationScope(); | 1910 |
| 1907 // The scope of a var/const declared variable anywhere inside a function | 1911 // The scope of a var/const declared variable anywhere inside a function |
| 1908 // is the entire function (ECMA-262, 3rd, 10.1.3, and 12.2). Thus we can | 1912 // is the entire function (ECMA-262, 3rd, 10.1.3, and 12.2). Thus we can |
| 1909 // transform a source-level var/const declaration into a (Function) | 1913 // transform a source-level var/const declaration into a (Function) |
| 1910 // Scope declaration, and rewrite the source-level initialization into an | 1914 // Scope declaration, and rewrite the source-level initialization into an |
| 1911 // assignment statement. We use a block to collect multiple assignments. | 1915 // assignment statement. We use a block to collect multiple assignments. |
| 1912 // | 1916 // |
| 1913 // We mark the block as initializer block because we don't want the | 1917 // We mark the block as initializer block because we don't want the |
| 1914 // rewriter to add a '.result' assignment to such a block (to get compliant | 1918 // rewriter to add a '.result' assignment to such a block (to get compliant |
| 1915 // behavior for code such as print(eval('var x = 7')), and for cosmetic | 1919 // behavior for code such as print(eval('var x = 7')), and for cosmetic |
| 1916 // reasons when pretty-printing. Also, unless an assignment (initialization) | 1920 // reasons when pretty-printing. Also, unless an assignment (initialization) |
| (...skipping 26 matching lines...) Expand all Loading... |
| 1943 // which the variable or constant is declared. Only function variables have | 1947 // which the variable or constant is declared. Only function variables have |
| 1944 // an initial value in the declaration (because they are initialized upon | 1948 // an initial value in the declaration (because they are initialized upon |
| 1945 // entering the function). | 1949 // entering the function). |
| 1946 // | 1950 // |
| 1947 // If we have a const declaration, in an inner scope, the proxy is always | 1951 // If we have a const declaration, in an inner scope, the proxy is always |
| 1948 // bound to the declared variable (independent of possibly surrounding with | 1952 // bound to the declared variable (independent of possibly surrounding with |
| 1949 // statements). | 1953 // statements). |
| 1950 // For let/const declarations in harmony mode, we can also immediately | 1954 // For let/const declarations in harmony mode, we can also immediately |
| 1951 // pre-resolve the proxy because it resides in the same scope as the | 1955 // pre-resolve the proxy because it resides in the same scope as the |
| 1952 // declaration. | 1956 // declaration. |
| 1953 VariableProxy* proxy = Declare(name, mode, NULL, mode != VAR, CHECK_OK); | 1957 VariableProxy* proxy = NewUnresolved(name, mode); |
| 1958 Declaration* declaration = |
| 1959 factory()->NewVariableDeclaration(proxy, mode, top_scope_); |
| 1960 Declare(declaration, mode != VAR, CHECK_OK); |
| 1954 nvars++; | 1961 nvars++; |
| 1955 if (declaration_scope->num_var_or_const() > kMaxNumFunctionLocals) { | 1962 if (declaration_scope->num_var_or_const() > kMaxNumFunctionLocals) { |
| 1956 ReportMessageAt(scanner().location(), "too_many_variables", | 1963 ReportMessageAt(scanner().location(), "too_many_variables", |
| 1957 Vector<const char*>::empty()); | 1964 Vector<const char*>::empty()); |
| 1958 *ok = false; | 1965 *ok = false; |
| 1959 return NULL; | 1966 return NULL; |
| 1960 } | 1967 } |
| 1961 | 1968 |
| 1962 // Parse initialization expression if present and/or needed. A | 1969 // Parse initialization expression if present and/or needed. A |
| 1963 // declaration of the form: | 1970 // declaration of the form: |
| (...skipping 3838 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5802 ASSERT(info->isolate()->has_pending_exception()); | 5809 ASSERT(info->isolate()->has_pending_exception()); |
| 5803 } else { | 5810 } else { |
| 5804 result = parser.ParseProgram(info); | 5811 result = parser.ParseProgram(info); |
| 5805 } | 5812 } |
| 5806 } | 5813 } |
| 5807 info->SetFunction(result); | 5814 info->SetFunction(result); |
| 5808 return (result != NULL); | 5815 return (result != NULL); |
| 5809 } | 5816 } |
| 5810 | 5817 |
| 5811 } } // namespace v8::internal | 5818 } } // namespace v8::internal |
| OLD | NEW |