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

Side by Side Diff: src/parser.cc

Issue 1292753007: [es6] Parameter scopes for sloppy eval (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Comments Created 5 years, 4 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') | src/pattern-rewriter.cc » ('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/parser.h" 5 #include "src/parser.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 1980 matching lines...) Expand 10 before | Expand all | Expand 10 after
1991 // Let/const variables in harmony mode are always added to the immediately 1991 // Let/const variables in harmony mode are always added to the immediately
1992 // enclosing scope. 1992 // enclosing scope.
1993 return DeclarationScope(mode)->NewUnresolved( 1993 return DeclarationScope(mode)->NewUnresolved(
1994 factory(), name, Variable::NORMAL, scanner()->location().beg_pos, 1994 factory(), name, Variable::NORMAL, scanner()->location().beg_pos,
1995 scanner()->location().end_pos); 1995 scanner()->location().end_pos);
1996 } 1996 }
1997 1997
1998 1998
1999 Variable* Parser::Declare(Declaration* declaration, 1999 Variable* Parser::Declare(Declaration* declaration,
2000 DeclarationDescriptor::Kind declaration_kind, 2000 DeclarationDescriptor::Kind declaration_kind,
2001 bool resolve, bool* ok) { 2001 bool resolve, bool* ok, Scope* scope) {
2002 VariableProxy* proxy = declaration->proxy(); 2002 VariableProxy* proxy = declaration->proxy();
2003 DCHECK(proxy->raw_name() != NULL); 2003 DCHECK(proxy->raw_name() != NULL);
2004 const AstRawString* name = proxy->raw_name(); 2004 const AstRawString* name = proxy->raw_name();
2005 VariableMode mode = declaration->mode(); 2005 VariableMode mode = declaration->mode();
2006 Scope* declaration_scope = DeclarationScope(mode); 2006 if (scope == nullptr) scope = scope_;
2007 Scope* declaration_scope =
2008 IsLexicalVariableMode(mode) ? scope : scope->DeclarationScope();
2007 Variable* var = NULL; 2009 Variable* var = NULL;
2008 2010
2009 // If a suitable scope exists, then we can statically declare this 2011 // If a suitable scope exists, then we can statically declare this
2010 // variable and also set its mode. In any case, a Declaration node 2012 // variable and also set its mode. In any case, a Declaration node
2011 // will be added to the scope so that the declaration can be added 2013 // will be added to the scope so that the declaration can be added
2012 // to the corresponding activation frame at runtime if necessary. 2014 // to the corresponding activation frame at runtime if necessary.
2013 // For instance, var declarations inside a sloppy eval scope need 2015 // For instance, var declarations inside a sloppy eval scope need
2014 // to be added to the calling function context. Similarly, strict 2016 // to be added to the calling function context. Similarly, strict
2015 // mode eval scope and lexical eval bindings do not leak variable 2017 // mode eval scope and lexical eval bindings do not leak variable
2016 // declarations to the caller's scope so we declare all locals, too. 2018 // declarations to the caller's scope so we declare all locals, too.
(...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after
2470 parsing_result->descriptor.mode = LET; 2472 parsing_result->descriptor.mode = LET;
2471 parsing_result->descriptor.needs_init = true; 2473 parsing_result->descriptor.needs_init = true;
2472 parsing_result->descriptor.init_op = Token::INIT_LET; 2474 parsing_result->descriptor.init_op = Token::INIT_LET;
2473 } else { 2475 } else {
2474 UNREACHABLE(); // by current callers 2476 UNREACHABLE(); // by current callers
2475 } 2477 }
2476 2478
2477 parsing_result->descriptor.declaration_scope = 2479 parsing_result->descriptor.declaration_scope =
2478 DeclarationScope(parsing_result->descriptor.mode); 2480 DeclarationScope(parsing_result->descriptor.mode);
2479 parsing_result->descriptor.scope = scope_; 2481 parsing_result->descriptor.scope = scope_;
2482 parsing_result->descriptor.hoist_scope = nullptr;
2480 2483
2481 2484
2482 bool first_declaration = true; 2485 bool first_declaration = true;
2483 int bindings_start = peek_position(); 2486 int bindings_start = peek_position();
2484 bool is_for_iteration_variable; 2487 bool is_for_iteration_variable;
2485 do { 2488 do {
2486 if (fni_ != NULL) fni_->Enter(); 2489 if (fni_ != NULL) fni_->Enter();
2487 2490
2488 // Parse name. 2491 // Parse name.
2489 if (!first_declaration) Consume(Token::COMMA); 2492 if (!first_declaration) Consume(Token::COMMA);
(...skipping 1839 matching lines...) Expand 10 before | Expand all | Expand 10 after
4329 factory()->NewBlock(NULL, 1, true, RelocInfo::kNoPosition); 4332 factory()->NewBlock(NULL, 1, true, RelocInfo::kNoPosition);
4330 for (int i = 0; i < parameters.params.length(); ++i) { 4333 for (int i = 0; i < parameters.params.length(); ++i) {
4331 auto parameter = parameters.params[i]; 4334 auto parameter = parameters.params[i];
4332 // TODO(caitp,rossberg): Remove special handling for rest once desugared. 4335 // TODO(caitp,rossberg): Remove special handling for rest once desugared.
4333 if (parameter.is_rest) break; 4336 if (parameter.is_rest) break;
4334 DeclarationDescriptor descriptor; 4337 DeclarationDescriptor descriptor;
4335 descriptor.declaration_kind = DeclarationDescriptor::PARAMETER; 4338 descriptor.declaration_kind = DeclarationDescriptor::PARAMETER;
4336 descriptor.parser = this; 4339 descriptor.parser = this;
4337 descriptor.declaration_scope = scope_; 4340 descriptor.declaration_scope = scope_;
4338 descriptor.scope = scope_; 4341 descriptor.scope = scope_;
4342 descriptor.hoist_scope = nullptr;
4339 descriptor.mode = LET; 4343 descriptor.mode = LET;
4340 descriptor.is_const = false; 4344 descriptor.is_const = false;
4341 descriptor.needs_init = true; 4345 descriptor.needs_init = true;
4342 descriptor.declaration_pos = parameter.pattern->position(); 4346 descriptor.declaration_pos = parameter.pattern->position();
4343 descriptor.initialization_pos = parameter.pattern->position(); 4347 descriptor.initialization_pos = parameter.pattern->position();
4344 descriptor.init_op = Token::INIT_LET; 4348 descriptor.init_op = Token::INIT_LET;
4345 Expression* initial_value = 4349 Expression* initial_value =
4346 factory()->NewVariableProxy(parameters.scope->parameter(i)); 4350 factory()->NewVariableProxy(parameters.scope->parameter(i));
4347 if (parameter.initializer != nullptr) { 4351 if (parameter.initializer != nullptr) {
4348 // IS_UNDEFINED($param) ? initializer : $param 4352 // IS_UNDEFINED($param) ? initializer : $param
4349 auto condition = factory()->NewCompareOperation( 4353 auto condition = factory()->NewCompareOperation(
4350 Token::EQ_STRICT, 4354 Token::EQ_STRICT,
4351 factory()->NewVariableProxy(parameters.scope->parameter(i)), 4355 factory()->NewVariableProxy(parameters.scope->parameter(i)),
4352 factory()->NewUndefinedLiteral(RelocInfo::kNoPosition), 4356 factory()->NewUndefinedLiteral(RelocInfo::kNoPosition),
4353 RelocInfo::kNoPosition); 4357 RelocInfo::kNoPosition);
4354 initial_value = factory()->NewConditional( 4358 initial_value = factory()->NewConditional(
4355 condition, parameter.initializer, initial_value, 4359 condition, parameter.initializer, initial_value,
4356 RelocInfo::kNoPosition); 4360 RelocInfo::kNoPosition);
4357 descriptor.initialization_pos = parameter.initializer->position(); 4361 descriptor.initialization_pos = parameter.initializer->position();
4358 } 4362 }
4359 DeclarationParsingResult::Declaration decl( 4363
4360 parameter.pattern, parameter.pattern->position(), initial_value); 4364 Scope* param_scope = scope_;
4361 PatternRewriter::DeclareAndInitializeVariables(init_block, &descriptor, 4365 Block* param_block = init_block;
4362 &decl, nullptr, CHECK_OK); 4366 if (parameter.initializer != nullptr && scope_->calls_sloppy_eval()) {
4367 param_scope = NewScope(scope_, BLOCK_SCOPE);
4368 param_scope->set_is_declaration_scope();
4369 param_scope->set_start_position(parameter.pattern->position());
4370 param_scope->set_end_position(RelocInfo::kNoPosition);
4371 param_scope->RecordEvalCall();
4372 param_block = factory()->NewBlock(NULL, 8, true, RelocInfo::kNoPosition);
4373 param_block->set_scope(param_scope);
4374 descriptor.hoist_scope = scope_;
4375 }
4376
4377 {
4378 BlockState block_state(&scope_, param_scope);
4379 DeclarationParsingResult::Declaration decl(
4380 parameter.pattern, parameter.pattern->position(), initial_value);
4381 PatternRewriter::DeclareAndInitializeVariables(param_block, &descriptor,
4382 &decl, nullptr, CHECK_OK);
4383 }
4384
4385 if (parameter.initializer != nullptr && scope_->calls_sloppy_eval()) {
4386 param_scope = param_scope->FinalizeBlockScope();
4387 if (param_scope != nullptr) {
4388 CheckConflictingVarDeclarations(param_scope, CHECK_OK);
4389 }
4390 init_block->AddStatement(param_block, zone());
4391 }
4363 } 4392 }
4364 return init_block; 4393 return init_block;
4365 } 4394 }
4366 4395
4367 4396
4368 ZoneList<Statement*>* Parser::ParseEagerFunctionBody( 4397 ZoneList<Statement*>* Parser::ParseEagerFunctionBody(
4369 const AstRawString* function_name, int pos, 4398 const AstRawString* function_name, int pos,
4370 const ParserFormalParameters& parameters, FunctionKind kind, 4399 const ParserFormalParameters& parameters, FunctionKind kind,
4371 FunctionLiteral::FunctionType function_type, bool* ok) { 4400 FunctionLiteral::FunctionType function_type, bool* ok) {
4372 // Everything inside an eagerly parsed function will be parsed eagerly 4401 // Everything inside an eagerly parsed function will be parsed eagerly
(...skipping 13 matching lines...) Expand all
4386 result->Add(NULL, zone()); 4415 result->Add(NULL, zone());
4387 } 4416 }
4388 4417
4389 // For concise constructors, check that they are constructed, 4418 // For concise constructors, check that they are constructed,
4390 // not called. 4419 // not called.
4391 if (i::IsConstructor(kind)) { 4420 if (i::IsConstructor(kind)) {
4392 AddAssertIsConstruct(result, pos); 4421 AddAssertIsConstruct(result, pos);
4393 } 4422 }
4394 4423
4395 ZoneList<Statement*>* body = result; 4424 ZoneList<Statement*>* body = result;
4396 Scope* inner_scope = nullptr; 4425 Scope* inner_scope = scope_;
4397 Block* inner_block = nullptr; 4426 Block* inner_block = nullptr;
4398 if (!parameters.is_simple) { 4427 if (!parameters.is_simple) {
4399 inner_scope = NewScope(scope_, BLOCK_SCOPE); 4428 inner_scope = NewScope(scope_, BLOCK_SCOPE);
4400 inner_scope->set_is_declaration_scope(); 4429 inner_scope->set_is_declaration_scope();
4401 inner_scope->set_start_position(scanner()->location().beg_pos); 4430 inner_scope->set_start_position(scanner()->location().beg_pos);
4402 inner_block = factory()->NewBlock(NULL, 8, true, RelocInfo::kNoPosition); 4431 inner_block = factory()->NewBlock(NULL, 8, true, RelocInfo::kNoPosition);
4403 inner_block->set_scope(inner_scope); 4432 inner_block->set_scope(inner_scope);
4404 body = inner_block->statements(); 4433 body = inner_block->statements();
4405 } 4434 }
4406 4435
4407 { 4436 {
4408 BlockState block_state(&scope_, inner_scope ? inner_scope : scope_); 4437 BlockState block_state(&scope_, inner_scope);
4409 4438
4410 // For generators, allocate and yield an iterator on function entry. 4439 // For generators, allocate and yield an iterator on function entry.
4411 if (IsGeneratorFunction(kind)) { 4440 if (IsGeneratorFunction(kind)) {
4412 ZoneList<Expression*>* arguments = 4441 ZoneList<Expression*>* arguments =
4413 new(zone()) ZoneList<Expression*>(0, zone()); 4442 new(zone()) ZoneList<Expression*>(0, zone());
4414 CallRuntime* allocation = factory()->NewCallRuntime( 4443 CallRuntime* allocation = factory()->NewCallRuntime(
4415 ast_value_factory()->empty_string(), 4444 ast_value_factory()->empty_string(),
4416 Runtime::FunctionForId(Runtime::kCreateJSGeneratorObject), arguments, 4445 Runtime::FunctionForId(Runtime::kCreateJSGeneratorObject), arguments,
4417 pos); 4446 pos);
4418 VariableProxy* init_proxy = factory()->NewVariableProxy( 4447 VariableProxy* init_proxy = factory()->NewVariableProxy(
(...skipping 1623 matching lines...) Expand 10 before | Expand all | Expand 10 after
6042 Expression* Parser::SpreadCallNew(Expression* function, 6071 Expression* Parser::SpreadCallNew(Expression* function,
6043 ZoneList<v8::internal::Expression*>* args, 6072 ZoneList<v8::internal::Expression*>* args,
6044 int pos) { 6073 int pos) {
6045 args->InsertAt(0, function, zone()); 6074 args->InsertAt(0, function, zone());
6046 6075
6047 return factory()->NewCallRuntime( 6076 return factory()->NewCallRuntime(
6048 ast_value_factory()->reflect_construct_string(), NULL, args, pos); 6077 ast_value_factory()->reflect_construct_string(), NULL, args, pos);
6049 } 6078 }
6050 } // namespace internal 6079 } // namespace internal
6051 } // namespace v8 6080 } // namespace v8
OLDNEW
« no previous file with comments | « src/parser.h ('k') | src/pattern-rewriter.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698