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/v8.h" | 5 #include "src/v8.h" |
6 | 6 |
7 #include "src/api.h" | 7 #include "src/api.h" |
8 #include "src/ast.h" | 8 #include "src/ast.h" |
9 #include "src/ast-literal-reindexer.h" | 9 #include "src/ast-literal-reindexer.h" |
10 #include "src/bailout-reason.h" | 10 #include "src/bailout-reason.h" |
(...skipping 1993 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2004 DCHECK(proxy->raw_name() != NULL); | 2004 DCHECK(proxy->raw_name() != NULL); |
2005 const AstRawString* name = proxy->raw_name(); | 2005 const AstRawString* name = proxy->raw_name(); |
2006 VariableMode mode = declaration->mode(); | 2006 VariableMode mode = declaration->mode(); |
2007 Scope* declaration_scope = DeclarationScope(mode); | 2007 Scope* declaration_scope = DeclarationScope(mode); |
2008 Variable* var = NULL; | 2008 Variable* var = NULL; |
2009 | 2009 |
2010 // If a suitable scope exists, then we can statically declare this | 2010 // If a suitable scope exists, then we can statically declare this |
2011 // variable and also set its mode. In any case, a Declaration node | 2011 // variable and also set its mode. In any case, a Declaration node |
2012 // will be added to the scope so that the declaration can be added | 2012 // will be added to the scope so that the declaration can be added |
2013 // to the corresponding activation frame at runtime if necessary. | 2013 // to the corresponding activation frame at runtime if necessary. |
2014 // For instance declarations inside an eval scope need to be added | 2014 // For instance, var declarations inside a sloppy eval scope need |
2015 // to the calling function context. | 2015 // to be added to the calling function context. Similarly, strict |
2016 // Similarly, strict mode eval scope does not leak variable declarations to | 2016 // mode eval scope and lexical eval bindings do not leak variable |
2017 // the caller's scope so we declare all locals, too. | 2017 // declarations to the caller's scope so we declare all locals, too. |
2018 if (declaration_scope->is_function_scope() || | 2018 if (declaration_scope->is_function_scope() || |
2019 declaration_scope->is_strict_eval_scope() || | |
2020 declaration_scope->is_block_scope() || | 2019 declaration_scope->is_block_scope() || |
2021 declaration_scope->is_module_scope() || | 2020 declaration_scope->is_module_scope() || |
2022 declaration_scope->is_script_scope()) { | 2021 declaration_scope->is_script_scope() || |
| 2022 (declaration_scope->is_eval_scope() && |
| 2023 (is_strict(declaration_scope->language_mode()) || |
| 2024 IsLexicalVariableMode(mode)))) { |
2023 // Declare the variable in the declaration scope. | 2025 // Declare the variable in the declaration scope. |
2024 var = declaration_scope->LookupLocal(name); | 2026 var = declaration_scope->LookupLocal(name); |
2025 if (var == NULL) { | 2027 if (var == NULL) { |
2026 // Declare the name. | 2028 // Declare the name. |
2027 Variable::Kind kind = Variable::NORMAL; | 2029 Variable::Kind kind = Variable::NORMAL; |
2028 int declaration_group_start = -1; | 2030 int declaration_group_start = -1; |
2029 if (declaration->IsFunctionDeclaration()) { | 2031 if (declaration->IsFunctionDeclaration()) { |
2030 kind = Variable::FUNCTION; | 2032 kind = Variable::FUNCTION; |
2031 } else if (declaration->IsVariableDeclaration() && | 2033 } else if (declaration->IsVariableDeclaration() && |
2032 declaration->AsVariableDeclaration()->is_class_declaration()) { | 2034 declaration->AsVariableDeclaration()->is_class_declaration()) { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2065 } | 2067 } |
2066 *ok = false; | 2068 *ok = false; |
2067 return nullptr; | 2069 return nullptr; |
2068 } | 2070 } |
2069 Expression* expression = NewThrowSyntaxError( | 2071 Expression* expression = NewThrowSyntaxError( |
2070 MessageTemplate::kVarRedeclaration, name, declaration->position()); | 2072 MessageTemplate::kVarRedeclaration, name, declaration->position()); |
2071 declaration_scope->SetIllegalRedeclaration(expression); | 2073 declaration_scope->SetIllegalRedeclaration(expression); |
2072 } else if (mode == VAR) { | 2074 } else if (mode == VAR) { |
2073 var->set_maybe_assigned(); | 2075 var->set_maybe_assigned(); |
2074 } | 2076 } |
| 2077 } else if (declaration_scope->is_eval_scope() && |
| 2078 is_sloppy(declaration_scope->language_mode()) && |
| 2079 !IsLexicalVariableMode(mode)) { |
| 2080 // In a var binding in a sloppy direct eval, pollute the enclosing scope |
| 2081 // with this new binding by doing the following: |
| 2082 // The proxy is bound to a lookup variable to force a dynamic declaration |
| 2083 // using the DeclareLookupSlot runtime function. |
| 2084 Variable::Kind kind = Variable::NORMAL; |
| 2085 // TODO(sigurds) figure out if kNotAssigned is OK here |
| 2086 var = new (zone()) Variable(declaration_scope, name, mode, kind, |
| 2087 declaration->initialization(), kNotAssigned); |
| 2088 var->AllocateTo(VariableLocation::LOOKUP, -1); |
| 2089 resolve = true; |
2075 } | 2090 } |
2076 | 2091 |
| 2092 |
2077 // We add a declaration node for every declaration. The compiler | 2093 // We add a declaration node for every declaration. The compiler |
2078 // will only generate code if necessary. In particular, declarations | 2094 // will only generate code if necessary. In particular, declarations |
2079 // for inner local variables that do not represent functions won't | 2095 // for inner local variables that do not represent functions won't |
2080 // result in any generated code. | 2096 // result in any generated code. |
2081 // | 2097 // |
2082 // Note that we always add an unresolved proxy even if it's not | 2098 // Note that we always add an unresolved proxy even if it's not |
2083 // used, simply because we don't know in this method (w/o extra | 2099 // used, simply because we don't know in this method (w/o extra |
2084 // parameters) if the proxy is needed or not. The proxy will be | 2100 // parameters) if the proxy is needed or not. The proxy will be |
2085 // bound during variable resolution time unless it was pre-bound | 2101 // bound during variable resolution time unless it was pre-bound |
2086 // below. | 2102 // below. |
2087 // | 2103 // |
2088 // WARNING: This will lead to multiple declaration nodes for the | 2104 // WARNING: This will lead to multiple declaration nodes for the |
2089 // same variable if it is declared several times. This is not a | 2105 // same variable if it is declared several times. This is not a |
2090 // semantic issue as long as we keep the source order, but it may be | 2106 // semantic issue as long as we keep the source order, but it may be |
2091 // a performance issue since it may lead to repeated | 2107 // a performance issue since it may lead to repeated |
2092 // RuntimeHidden_DeclareLookupSlot calls. | 2108 // RuntimeHidden_DeclareLookupSlot calls. |
2093 declaration_scope->AddDeclaration(declaration); | 2109 declaration_scope->AddDeclaration(declaration); |
2094 | 2110 |
2095 if (mode == CONST_LEGACY && declaration_scope->is_script_scope()) { | 2111 if (mode == CONST_LEGACY && declaration_scope->is_script_scope()) { |
2096 // For global const variables we bind the proxy to a variable. | 2112 // For global const variables we bind the proxy to a variable. |
2097 DCHECK(resolve); // should be set by all callers | 2113 DCHECK(resolve); // should be set by all callers |
2098 Variable::Kind kind = Variable::NORMAL; | 2114 Variable::Kind kind = Variable::NORMAL; |
2099 var = new (zone()) Variable(declaration_scope, name, mode, kind, | 2115 var = new (zone()) Variable(declaration_scope, name, mode, kind, |
2100 kNeedsInitialization, kNotAssigned); | 2116 kNeedsInitialization, kNotAssigned); |
2101 } else if (declaration_scope->is_eval_scope() && | |
2102 is_sloppy(declaration_scope->language_mode())) { | |
2103 // For variable declarations in a sloppy eval scope the proxy is bound | |
2104 // to a lookup variable to force a dynamic declaration using the | |
2105 // DeclareLookupSlot runtime function. | |
2106 Variable::Kind kind = Variable::NORMAL; | |
2107 // TODO(sigurds) figure out if kNotAssigned is OK here | |
2108 var = new (zone()) Variable(declaration_scope, name, mode, kind, | |
2109 declaration->initialization(), kNotAssigned); | |
2110 var->AllocateTo(VariableLocation::LOOKUP, -1); | |
2111 resolve = true; | |
2112 } | 2117 } |
2113 | 2118 |
2114 // If requested and we have a local variable, bind the proxy to the variable | 2119 // If requested and we have a local variable, bind the proxy to the variable |
2115 // at parse-time. This is used for functions (and consts) declared inside | 2120 // at parse-time. This is used for functions (and consts) declared inside |
2116 // statements: the corresponding function (or const) variable must be in the | 2121 // statements: the corresponding function (or const) variable must be in the |
2117 // function scope and not a statement-local scope, e.g. as provided with a | 2122 // function scope and not a statement-local scope, e.g. as provided with a |
2118 // 'with' statement: | 2123 // 'with' statement: |
2119 // | 2124 // |
2120 // with (obj) { | 2125 // with (obj) { |
2121 // function f() {} | 2126 // function f() {} |
(...skipping 3888 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6010 Expression* Parser::SpreadCallNew(Expression* function, | 6015 Expression* Parser::SpreadCallNew(Expression* function, |
6011 ZoneList<v8::internal::Expression*>* args, | 6016 ZoneList<v8::internal::Expression*>* args, |
6012 int pos) { | 6017 int pos) { |
6013 args->InsertAt(0, function, zone()); | 6018 args->InsertAt(0, function, zone()); |
6014 | 6019 |
6015 return factory()->NewCallRuntime( | 6020 return factory()->NewCallRuntime( |
6016 ast_value_factory()->reflect_construct_string(), NULL, args, pos); | 6021 ast_value_factory()->reflect_construct_string(), NULL, args, pos); |
6017 } | 6022 } |
6018 } // namespace internal | 6023 } // namespace internal |
6019 } // namespace v8 | 6024 } // namespace v8 |
OLD | NEW |