OLD | NEW |
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/parsing/parser.h" | 5 #include "src/parsing/parser.h" |
6 | 6 |
7 #include "src/api.h" | 7 #include "src/api.h" |
8 #include "src/ast/ast.h" | 8 #include "src/ast/ast.h" |
9 #include "src/ast/ast-expression-rewriter.h" | 9 #include "src/ast/ast-expression-rewriter.h" |
10 #include "src/ast/ast-expression-visitor.h" | 10 #include "src/ast/ast-expression-visitor.h" |
(...skipping 1951 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1962 kind = Variable::FUNCTION; | 1962 kind = Variable::FUNCTION; |
1963 } else if (declaration->IsVariableDeclaration() && | 1963 } else if (declaration->IsVariableDeclaration() && |
1964 declaration->AsVariableDeclaration()->is_class_declaration()) { | 1964 declaration->AsVariableDeclaration()->is_class_declaration()) { |
1965 kind = Variable::CLASS; | 1965 kind = Variable::CLASS; |
1966 declaration_group_start = | 1966 declaration_group_start = |
1967 declaration->AsVariableDeclaration()->declaration_group_start(); | 1967 declaration->AsVariableDeclaration()->declaration_group_start(); |
1968 } | 1968 } |
1969 var = declaration_scope->DeclareLocal( | 1969 var = declaration_scope->DeclareLocal( |
1970 name, mode, declaration->initialization(), kind, kNotAssigned, | 1970 name, mode, declaration->initialization(), kind, kNotAssigned, |
1971 declaration_group_start); | 1971 declaration_group_start); |
1972 } else if (((IsLexicalVariableMode(mode) || | 1972 } else if ((mode == CONST_LEGACY || var->mode() == CONST_LEGACY) && |
1973 IsLexicalVariableMode(var->mode())) && | 1973 !declaration_scope->is_script_scope()) { |
1974 // Allow duplicate function decls for web compat, see bug 4693. | 1974 // Duplicate legacy const definitions throw at runtime. |
1975 (is_strict(language_mode()) || !is_function_declaration || | 1975 DCHECK(is_sloppy(language_mode())); |
1976 !var->is_function())) || | 1976 Expression* expression = NewThrowSyntaxError( |
1977 ((mode == CONST_LEGACY || var->mode() == CONST_LEGACY) && | 1977 MessageTemplate::kVarRedeclaration, name, declaration->position()); |
1978 !declaration_scope->is_script_scope())) { | 1978 declaration_scope->SetIllegalRedeclaration(expression); |
1979 // The name was declared in this scope before; check for conflicting | 1979 } else if ((IsLexicalVariableMode(mode) || |
1980 // re-declarations. We have a conflict if either of the declarations is | 1980 IsLexicalVariableMode(var->mode())) && |
1981 // not a var (in script scope, we also have to ignore legacy const for | 1981 // Lexical bindings may appear for some parameters in sloppy |
1982 // compatibility). There is similar code in runtime.cc in the Declare | 1982 // mode even with --harmony-sloppy off. |
1983 // functions. The function CheckConflictingVarDeclarations checks for | 1983 (is_strict(language_mode()) || allow_harmony_sloppy())) { |
1984 // var and let bindings from different scopes whereas this is a check for | 1984 // Allow duplicate function decls for web compat, see bug 4693. |
1985 // conflicting declarations within the same scope. This check also covers | 1985 if (is_sloppy(language_mode()) && is_function_declaration && |
1986 // the special case | 1986 var->is_function()) { |
1987 // | 1987 DCHECK(IsLexicalVariableMode(mode) && |
1988 // function () { let x; { var x; } } | 1988 IsLexicalVariableMode(var->mode())); |
1989 // | 1989 ++use_counts_[v8::Isolate::kSloppyModeBlockScopedFunctionRedefinition]; |
1990 // because the var declaration is hoisted to the function scope where 'x' | 1990 } else { |
1991 // is already bound. | 1991 // The name was declared in this scope before; check for conflicting |
1992 DCHECK(IsDeclaredVariableMode(var->mode())); | 1992 // re-declarations. We have a conflict if either of the declarations |
1993 if (is_strict(language_mode()) || | 1993 // is not a var (in script scope, we also have to ignore legacy const |
1994 (allow_harmony_sloppy() && mode != CONST_LEGACY && | 1994 // for compatibility). There is similar code in runtime.cc in the |
1995 var->mode() != CONST_LEGACY)) { | 1995 // Declare functions. The function CheckConflictingVarDeclarations |
| 1996 // checks for var and let bindings from different scopes whereas this |
| 1997 // is a check for conflicting declarations within the same scope. This |
| 1998 // check also covers the special case |
| 1999 // |
| 2000 // function () { let x; { var x; } } |
| 2001 // |
| 2002 // because the var declaration is hoisted to the function scope where |
| 2003 // 'x' is already bound. |
| 2004 DCHECK(IsDeclaredVariableMode(var->mode())); |
1996 // In harmony we treat re-declarations as early errors. See | 2005 // In harmony we treat re-declarations as early errors. See |
1997 // ES5 16 for a definition of early errors. | 2006 // ES5 16 for a definition of early errors. |
1998 if (declaration_kind == DeclarationDescriptor::NORMAL) { | 2007 if (declaration_kind == DeclarationDescriptor::NORMAL) { |
1999 ParserTraits::ReportMessage(MessageTemplate::kVarRedeclaration, name); | 2008 ParserTraits::ReportMessage(MessageTemplate::kVarRedeclaration, name); |
2000 } else { | 2009 } else { |
2001 ParserTraits::ReportMessage(MessageTemplate::kParamDupe); | 2010 ParserTraits::ReportMessage(MessageTemplate::kParamDupe); |
2002 } | 2011 } |
2003 *ok = false; | 2012 *ok = false; |
2004 return nullptr; | 2013 return nullptr; |
2005 } | 2014 } |
2006 Expression* expression = NewThrowSyntaxError( | |
2007 MessageTemplate::kVarRedeclaration, name, declaration->position()); | |
2008 declaration_scope->SetIllegalRedeclaration(expression); | |
2009 } else if (mode == VAR) { | 2015 } else if (mode == VAR) { |
2010 var->set_maybe_assigned(); | 2016 var->set_maybe_assigned(); |
2011 } | 2017 } |
2012 } else if (declaration_scope->is_eval_scope() && | 2018 } else if (declaration_scope->is_eval_scope() && |
2013 is_sloppy(declaration_scope->language_mode()) && | 2019 is_sloppy(declaration_scope->language_mode()) && |
2014 !IsLexicalVariableMode(mode)) { | 2020 !IsLexicalVariableMode(mode)) { |
2015 // In a var binding in a sloppy direct eval, pollute the enclosing scope | 2021 // In a var binding in a sloppy direct eval, pollute the enclosing scope |
2016 // with this new binding by doing the following: | 2022 // with this new binding by doing the following: |
2017 // The proxy is bound to a lookup variable to force a dynamic declaration | 2023 // The proxy is bound to a lookup variable to force a dynamic declaration |
2018 // using the DeclareLookupSlot runtime function. | 2024 // using the DeclareLookupSlot runtime function. |
(...skipping 1615 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3634 } | 3640 } |
3635 *ok = false; | 3641 *ok = false; |
3636 return nullptr; | 3642 return nullptr; |
3637 } | 3643 } |
3638 | 3644 |
3639 Block* init_block = nullptr; | 3645 Block* init_block = nullptr; |
3640 | 3646 |
3641 // special case for legacy for (var/const x =.... in) | 3647 // special case for legacy for (var/const x =.... in) |
3642 if (!IsLexicalVariableMode(parsing_result.descriptor.mode) && | 3648 if (!IsLexicalVariableMode(parsing_result.descriptor.mode) && |
3643 decl.pattern->IsVariableProxy() && decl.initializer != nullptr) { | 3649 decl.pattern->IsVariableProxy() && decl.initializer != nullptr) { |
| 3650 ++use_counts_[v8::Isolate::kForInInitializer]; |
3644 const AstRawString* name = | 3651 const AstRawString* name = |
3645 decl.pattern->AsVariableProxy()->raw_name(); | 3652 decl.pattern->AsVariableProxy()->raw_name(); |
3646 VariableProxy* single_var = scope_->NewUnresolved( | 3653 VariableProxy* single_var = scope_->NewUnresolved( |
3647 factory(), name, Variable::NORMAL, each_beg_pos, each_end_pos); | 3654 factory(), name, Variable::NORMAL, each_beg_pos, each_end_pos); |
3648 init_block = factory()->NewBlock( | 3655 init_block = factory()->NewBlock( |
3649 nullptr, 2, true, parsing_result.descriptor.declaration_pos); | 3656 nullptr, 2, true, parsing_result.descriptor.declaration_pos); |
3650 init_block->statements()->Add( | 3657 init_block->statements()->Add( |
3651 factory()->NewExpressionStatement( | 3658 factory()->NewExpressionStatement( |
3652 factory()->NewAssignment(Token::ASSIGN, single_var, | 3659 factory()->NewAssignment(Token::ASSIGN, single_var, |
3653 decl.initializer, | 3660 decl.initializer, |
(...skipping 2118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5772 function->set_raw_name(name); | 5779 function->set_raw_name(name); |
5773 } else { | 5780 } else { |
5774 DCHECK(value->IsClassLiteral()); | 5781 DCHECK(value->IsClassLiteral()); |
5775 value->AsClassLiteral()->constructor()->set_raw_name(name); | 5782 value->AsClassLiteral()->constructor()->set_raw_name(name); |
5776 } | 5783 } |
5777 } | 5784 } |
5778 | 5785 |
5779 | 5786 |
5780 } // namespace internal | 5787 } // namespace internal |
5781 } // namespace v8 | 5788 } // namespace v8 |
OLD | NEW |