Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(654)

Side by Side Diff: src/parser.cc

Issue 1234433002: [es6] Sloppy functions in block (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Don't report errors Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/parser.h ('k') | test/mjsunit/harmony/block-function-sloppy.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 1975 matching lines...) Expand 10 before | Expand all | Expand 10 after
1986 // Let/const variables in harmony mode are always added to the immediately 1986 // Let/const variables in harmony mode are always added to the immediately
1987 // enclosing scope. 1987 // enclosing scope.
1988 return DeclarationScope(mode)->NewUnresolved( 1988 return DeclarationScope(mode)->NewUnresolved(
1989 factory(), name, Variable::NORMAL, scanner()->location().beg_pos, 1989 factory(), name, Variable::NORMAL, scanner()->location().beg_pos,
1990 scanner()->location().end_pos); 1990 scanner()->location().end_pos);
1991 } 1991 }
1992 1992
1993 1993
1994 Variable* Parser::Declare(Declaration* declaration, 1994 Variable* Parser::Declare(Declaration* declaration,
1995 DeclarationDescriptor::Kind declaration_kind, 1995 DeclarationDescriptor::Kind declaration_kind,
1996 bool resolve, bool* ok) { 1996 bool report_error, bool* ok) {
arv (Not doing code reviews) 2015/07/10 16:56:33 Turned out that all callers always passed resolve
1997 VariableProxy* proxy = declaration->proxy(); 1997 VariableProxy* proxy = declaration->proxy();
1998 DCHECK(proxy->raw_name() != NULL); 1998 DCHECK(proxy->raw_name() != NULL);
1999 const AstRawString* name = proxy->raw_name(); 1999 const AstRawString* name = proxy->raw_name();
2000 VariableMode mode = declaration->mode(); 2000 VariableMode mode = declaration->mode();
2001 Scope* declaration_scope = DeclarationScope(mode); 2001 Scope* declaration_scope = DeclarationScope(mode);
2002 Variable* var = NULL; 2002 Variable* var = NULL;
2003 2003
2004 // If a suitable scope exists, then we can statically declare this 2004 // If a suitable scope exists, then we can statically declare this
2005 // variable and also set its mode. In any case, a Declaration node 2005 // variable and also set its mode. In any case, a Declaration node
2006 // will be added to the scope so that the declaration can be added 2006 // will be added to the scope so that the declaration can be added
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
2045 // the special case 2045 // the special case
2046 // 2046 //
2047 // function () { let x; { var x; } } 2047 // function () { let x; { var x; } }
2048 // 2048 //
2049 // because the var declaration is hoisted to the function scope where 'x' 2049 // because the var declaration is hoisted to the function scope where 'x'
2050 // is already bound. 2050 // is already bound.
2051 DCHECK(IsDeclaredVariableMode(var->mode())); 2051 DCHECK(IsDeclaredVariableMode(var->mode()));
2052 if (is_strict(language_mode()) || allow_harmony_sloppy()) { 2052 if (is_strict(language_mode()) || allow_harmony_sloppy()) {
2053 // In harmony we treat re-declarations as early errors. See 2053 // In harmony we treat re-declarations as early errors. See
2054 // ES5 16 for a definition of early errors. 2054 // ES5 16 for a definition of early errors.
2055 if (declaration_kind == DeclarationDescriptor::NORMAL) { 2055 if (report_error) {
2056 ParserTraits::ReportMessage(MessageTemplate::kVarRedeclaration, name); 2056 if (declaration_kind == DeclarationDescriptor::NORMAL) {
2057 } else { 2057 ParserTraits::ReportMessage(MessageTemplate::kVarRedeclaration,
2058 ParserTraits::ReportMessage(MessageTemplate::kStrictParamDupe); 2058 name);
2059 } else {
2060 ParserTraits::ReportMessage(MessageTemplate::kStrictParamDupe);
2061 }
2059 } 2062 }
2060 *ok = false; 2063 *ok = false;
2061 return nullptr; 2064 return nullptr;
2062 } 2065 }
2063 Expression* expression = NewThrowSyntaxError( 2066 Expression* expression = NewThrowSyntaxError(
2064 MessageTemplate::kVarRedeclaration, name, declaration->position()); 2067 MessageTemplate::kVarRedeclaration, name, declaration->position());
2065 declaration_scope->SetIllegalRedeclaration(expression); 2068 declaration_scope->SetIllegalRedeclaration(expression);
2066 } else if (mode == VAR) { 2069 } else if (mode == VAR) {
2067 var->set_maybe_assigned(); 2070 var->set_maybe_assigned();
2068 } 2071 }
(...skipping 12 matching lines...) Expand all
2081 // 2084 //
2082 // WARNING: This will lead to multiple declaration nodes for the 2085 // WARNING: This will lead to multiple declaration nodes for the
2083 // same variable if it is declared several times. This is not a 2086 // same variable if it is declared several times. This is not a
2084 // semantic issue as long as we keep the source order, but it may be 2087 // semantic issue as long as we keep the source order, but it may be
2085 // a performance issue since it may lead to repeated 2088 // a performance issue since it may lead to repeated
2086 // RuntimeHidden_DeclareLookupSlot calls. 2089 // RuntimeHidden_DeclareLookupSlot calls.
2087 declaration_scope->AddDeclaration(declaration); 2090 declaration_scope->AddDeclaration(declaration);
2088 2091
2089 if (mode == CONST_LEGACY && declaration_scope->is_script_scope()) { 2092 if (mode == CONST_LEGACY && declaration_scope->is_script_scope()) {
2090 // For global const variables we bind the proxy to a variable. 2093 // For global const variables we bind the proxy to a variable.
2091 DCHECK(resolve); // should be set by all callers
2092 Variable::Kind kind = Variable::NORMAL; 2094 Variable::Kind kind = Variable::NORMAL;
2093 var = new (zone()) Variable(declaration_scope, name, mode, kind, 2095 var = new (zone()) Variable(declaration_scope, name, mode, kind,
2094 kNeedsInitialization, kNotAssigned); 2096 kNeedsInitialization, kNotAssigned);
2095 } else if (declaration_scope->is_eval_scope() && 2097 } else if (declaration_scope->is_eval_scope() &&
2096 is_sloppy(declaration_scope->language_mode())) { 2098 is_sloppy(declaration_scope->language_mode())) {
2097 // For variable declarations in a sloppy eval scope the proxy is bound 2099 // For variable declarations in a sloppy eval scope the proxy is bound
2098 // to a lookup variable to force a dynamic declaration using the 2100 // to a lookup variable to force a dynamic declaration using the
2099 // DeclareLookupSlot runtime function. 2101 // DeclareLookupSlot runtime function.
2100 Variable::Kind kind = Variable::NORMAL; 2102 Variable::Kind kind = Variable::NORMAL;
2101 // TODO(sigurds) figure out if kNotAssigned is OK here 2103 // TODO(sigurds) figure out if kNotAssigned is OK here
2102 var = new (zone()) Variable(declaration_scope, name, mode, kind, 2104 var = new (zone()) Variable(declaration_scope, name, mode, kind,
2103 declaration->initialization(), kNotAssigned); 2105 declaration->initialization(), kNotAssigned);
2104 var->AllocateTo(VariableLocation::LOOKUP, -1); 2106 var->AllocateTo(VariableLocation::LOOKUP, -1);
2105 resolve = true;
2106 } 2107 }
2107 2108
2108 // If requested and we have a local variable, bind the proxy to the variable 2109 // If requested and we have a local variable, bind the proxy to the variable
2109 // at parse-time. This is used for functions (and consts) declared inside 2110 // at parse-time. This is used for functions (and consts) declared inside
2110 // statements: the corresponding function (or const) variable must be in the 2111 // statements: the corresponding function (or const) variable must be in the
2111 // function scope and not a statement-local scope, e.g. as provided with a 2112 // function scope and not a statement-local scope, e.g. as provided with a
2112 // 'with' statement: 2113 // 'with' statement:
2113 // 2114 //
2114 // with (obj) { 2115 // with (obj) {
2115 // function f() {} 2116 // function f() {}
2116 // } 2117 // }
2117 // 2118 //
2118 // which is translated into: 2119 // which is translated into:
2119 // 2120 //
2120 // with (obj) { 2121 // with (obj) {
2121 // // in this case this is not: 'var f; f = function () {};' 2122 // // in this case this is not: 'var f; f = function () {};'
2122 // var f = function () {}; 2123 // var f = function () {};
2123 // } 2124 // }
2124 // 2125 //
2125 // Note that if 'f' is accessed from inside the 'with' statement, it 2126 // Note that if 'f' is accessed from inside the 'with' statement, it
2126 // will be allocated in the context (because we must be able to look 2127 // will be allocated in the context (because we must be able to look
2127 // it up dynamically) but it will also be accessed statically, i.e., 2128 // it up dynamically) but it will also be accessed statically, i.e.,
2128 // with a context slot index and a context chain length for this 2129 // with a context slot index and a context chain length for this
2129 // initialization code. Thus, inside the 'with' statement, we need 2130 // initialization code. Thus, inside the 'with' statement, we need
2130 // both access to the static and the dynamic context chain; the 2131 // both access to the static and the dynamic context chain; the
2131 // runtime needs to provide both. 2132 // runtime needs to provide both.
2132 if (resolve && var != NULL) { 2133 if (var != NULL) {
2133 proxy->BindTo(var); 2134 proxy->BindTo(var);
2134 } 2135 }
2135 return var; 2136 return var;
2136 } 2137 }
2137 2138
2138 2139
2139 // Language extension which is only enabled for source files loaded 2140 // Language extension which is only enabled for source files loaded
2140 // through the API's extension mechanism. A native function 2141 // through the API's extension mechanism. A native function
2141 // declaration is resolved by looking up the function through a 2142 // declaration is resolved by looking up the function through a
2142 // callback provided by the extension. 2143 // callback provided by the extension.
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
2210 : (is_strict(language_mode()) || allow_harmony_sloppy()) && 2211 : (is_strict(language_mode()) || allow_harmony_sloppy()) &&
2211 !(scope_->is_script_scope() || scope_->is_eval_scope() || 2212 !(scope_->is_script_scope() || scope_->is_eval_scope() ||
2212 scope_->is_function_scope()) 2213 scope_->is_function_scope())
2213 ? LET 2214 ? LET
2214 : VAR; 2215 : VAR;
2215 VariableProxy* proxy = NewUnresolved(name, mode); 2216 VariableProxy* proxy = NewUnresolved(name, mode);
2216 Declaration* declaration = 2217 Declaration* declaration =
2217 factory()->NewFunctionDeclaration(proxy, mode, fun, scope_, pos); 2218 factory()->NewFunctionDeclaration(proxy, mode, fun, scope_, pos);
2218 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK); 2219 Declare(declaration, DeclarationDescriptor::NORMAL, true, CHECK_OK);
2219 if (names) names->Add(name, zone()); 2220 if (names) names->Add(name, zone());
2221
2222 // For sloppy function in block we also add a var binding that gets assigned
2223 // to at the location of the FunctionDeclaration -- but only if introducing
2224 // this var binding does not lead to an early error.
2225 if (is_sloppy(language_mode()) && scope_->is_block_scope() &&
2226 allow_harmony_sloppy()) {
2227 VariableProxy* var_proxy = NewUnresolved(name, VAR);
2228 Declaration* declaration =
2229 factory()->NewVariableDeclaration(var_proxy, VAR, scope_, pos);
2230 bool var_ok = true;
2231 const bool report_error = false;
2232 Declare(declaration, DeclarationDescriptor::NORMAL, report_error, &var_ok);
2233 if (!var_ok) {
2234 scope_->RemoveUnresolved(var_proxy);
2235 } else {
2236 // At the location of the FunctionDeclaration we assign to the var
2237 // binding.
2238 Assignment* assignment = factory()->NewAssignment(
2239 Token::ASSIGN, var_proxy, NewUnresolved(name, mode), pos);
2240 return factory()->NewExpressionStatement(assignment,
2241 RelocInfo::kNoPosition);
2242 }
2243 }
2220 return factory()->NewEmptyStatement(RelocInfo::kNoPosition); 2244 return factory()->NewEmptyStatement(RelocInfo::kNoPosition);
2221 } 2245 }
2222 2246
2223 2247
2224 Statement* Parser::ParseClassDeclaration(ZoneList<const AstRawString*>* names, 2248 Statement* Parser::ParseClassDeclaration(ZoneList<const AstRawString*>* names,
2225 bool* ok) { 2249 bool* ok) {
2226 // ClassDeclaration :: 2250 // ClassDeclaration ::
2227 // 'class' Identifier ('extends' LeftHandExpression)? '{' ClassBody '}' 2251 // 'class' Identifier ('extends' LeftHandExpression)? '{' ClassBody '}'
2228 // 2252 //
2229 // A ClassDeclaration 2253 // A ClassDeclaration
(...skipping 3714 matching lines...) Expand 10 before | Expand all | Expand 10 after
5944 Expression* Parser::SpreadCallNew(Expression* function, 5968 Expression* Parser::SpreadCallNew(Expression* function,
5945 ZoneList<v8::internal::Expression*>* args, 5969 ZoneList<v8::internal::Expression*>* args,
5946 int pos) { 5970 int pos) {
5947 args->InsertAt(0, function, zone()); 5971 args->InsertAt(0, function, zone());
5948 5972
5949 return factory()->NewCallRuntime( 5973 return factory()->NewCallRuntime(
5950 ast_value_factory()->reflect_construct_string(), NULL, args, pos); 5974 ast_value_factory()->reflect_construct_string(), NULL, args, pos);
5951 } 5975 }
5952 } // namespace internal 5976 } // namespace internal
5953 } // namespace v8 5977 } // namespace v8
OLDNEW
« no previous file with comments | « src/parser.h ('k') | test/mjsunit/harmony/block-function-sloppy.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698