Introduce with scope and rework variable resolution.

Review URL: http://codereview.chromium.org/7904008

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@9650 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
keuchel@chromium.org 2011-10-17 09:29:37 +00:00
parent 50ef25e0f3
commit 0706a98b2a
13 changed files with 262 additions and 277 deletions

View File

@ -1141,7 +1141,7 @@ void FullCodeGenerator::EmitLoadGlobalCheckExtensions(Variable* var,
} }
// If no outer scope calls eval, we do not need to check more // If no outer scope calls eval, we do not need to check more
// context extensions. // context extensions.
if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break; if (!s->outer_scope_calls_non_strict_eval() || s->is_eval_scope()) break;
s = s->outer_scope(); s = s->outer_scope();
} }

View File

@ -66,7 +66,6 @@ VariableProxy::VariableProxy(Isolate* isolate, Variable* var)
name_(var->name()), name_(var->name()),
var_(NULL), // Will be set by the call to BindTo. var_(NULL), // Will be set by the call to BindTo.
is_this_(var->is_this()), is_this_(var->is_this()),
inside_with_(false),
is_trivial_(false), is_trivial_(false),
position_(RelocInfo::kNoPosition) { position_(RelocInfo::kNoPosition) {
BindTo(var); BindTo(var);
@ -76,13 +75,11 @@ VariableProxy::VariableProxy(Isolate* isolate, Variable* var)
VariableProxy::VariableProxy(Isolate* isolate, VariableProxy::VariableProxy(Isolate* isolate,
Handle<String> name, Handle<String> name,
bool is_this, bool is_this,
bool inside_with,
int position) int position)
: Expression(isolate), : Expression(isolate),
name_(name), name_(name),
var_(NULL), var_(NULL),
is_this_(is_this), is_this_(is_this),
inside_with_(inside_with),
is_trivial_(false), is_trivial_(false),
position_(position) { position_(position) {
// Names must be canonicalized for fast equality checks. // Names must be canonicalized for fast equality checks.

View File

@ -1128,7 +1128,6 @@ class VariableProxy: public Expression {
Handle<String> name() const { return name_; } Handle<String> name() const { return name_; }
Variable* var() const { return var_; } Variable* var() const { return var_; }
bool is_this() const { return is_this_; } bool is_this() const { return is_this_; }
bool inside_with() const { return inside_with_; }
int position() const { return position_; } int position() const { return position_; }
void MarkAsTrivial() { is_trivial_ = true; } void MarkAsTrivial() { is_trivial_ = true; }
@ -1140,14 +1139,12 @@ class VariableProxy: public Expression {
Handle<String> name_; Handle<String> name_;
Variable* var_; // resolved variable, or NULL Variable* var_; // resolved variable, or NULL
bool is_this_; bool is_this_;
bool inside_with_;
bool is_trivial_; bool is_trivial_;
int position_; int position_;
VariableProxy(Isolate* isolate, VariableProxy(Isolate* isolate,
Handle<String> name, Handle<String> name,
bool is_this, bool is_this,
bool inside_with,
int position = RelocInfo::kNoPosition); int position = RelocInfo::kNoPosition);
friend class Scope; friend class Scope;

View File

@ -266,8 +266,7 @@ bool Context::GlobalIfNotShadowedByEval(Handle<String> name) {
} }
void Context::ComputeEvalScopeInfo(bool* outer_scope_calls_eval, void Context::ComputeEvalScopeInfo(bool* outer_scope_calls_non_strict_eval) {
bool* outer_scope_calls_non_strict_eval) {
// Skip up the context chain checking all the function contexts to see // Skip up the context chain checking all the function contexts to see
// whether they call eval. // whether they call eval.
Context* context = this; Context* context = this;
@ -275,16 +274,13 @@ void Context::ComputeEvalScopeInfo(bool* outer_scope_calls_eval,
if (context->IsFunctionContext()) { if (context->IsFunctionContext()) {
Handle<SerializedScopeInfo> scope_info( Handle<SerializedScopeInfo> scope_info(
context->closure()->shared()->scope_info()); context->closure()->shared()->scope_info());
if (scope_info->CallsEval()) { if (scope_info->CallsEval() && !scope_info->IsStrictMode()) {
*outer_scope_calls_eval = true;
if (!scope_info->IsStrictMode()) {
// No need to go further since the answers will not change from // No need to go further since the answers will not change from
// here. // here.
*outer_scope_calls_non_strict_eval = true; *outer_scope_calls_non_strict_eval = true;
return; return;
} }
} }
}
context = context->previous(); context = context->previous();
} }
} }

View File

@ -385,8 +385,7 @@ class Context: public FixedArray {
// Determine if any function scope in the context call eval and if // Determine if any function scope in the context call eval and if
// any of those calls are in non-strict mode. // any of those calls are in non-strict mode.
void ComputeEvalScopeInfo(bool* outer_scope_calls_eval, void ComputeEvalScopeInfo(bool* outer_scope_calls_non_strict_eval);
bool* outer_scope_calls_non_strict_eval);
// Code generation support. // Code generation support.
static int SlotOffset(int index) { static int SlotOffset(int index) {

View File

@ -1123,7 +1123,7 @@ void FullCodeGenerator::EmitLoadGlobalCheckExtensions(Variable* var,
// If no outer scope calls eval, we do not need to check more // If no outer scope calls eval, we do not need to check more
// context extensions. If we have reached an eval scope, we check // context extensions. If we have reached an eval scope, we check
// all extensions from this point. // all extensions from this point.
if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break; if (!s->outer_scope_calls_non_strict_eval() || s->is_eval_scope()) break;
s = s->outer_scope(); s = s->outer_scope();
} }

View File

@ -1149,7 +1149,7 @@ void FullCodeGenerator::EmitLoadGlobalCheckExtensions(Variable* var,
} }
// If no outer scope calls eval, we do not need to check more // If no outer scope calls eval, we do not need to check more
// context extensions. // context extensions.
if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break; if (!s->outer_scope_calls_non_strict_eval() || s->is_eval_scope()) break;
s = s->outer_scope(); s = s->outer_scope();
} }

View File

@ -407,9 +407,9 @@ unsigned* ScriptDataImpl::ReadAddress(int position) {
} }
Scope* Parser::NewScope(Scope* parent, Scope::Type type, bool inside_with) { Scope* Parser::NewScope(Scope* parent, Scope::Type type) {
Scope* result = new(zone()) Scope(parent, type); Scope* result = new(zone()) Scope(parent, type);
result->Initialize(inside_with); result->Initialize();
return result; return result;
} }
@ -459,13 +459,31 @@ class TargetScope BASE_EMBEDDED {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// LexicalScope is a support class to facilitate manipulation of the // LexicalScope and SaveScope are stack allocated support classes to facilitate
// Parser's scope stack. The constructor sets the parser's top scope // anipulation of the Parser's scope stack. The constructor sets the parser's
// to the incoming scope, and the destructor resets it. // top scope to the incoming scope, and the destructor resets it. Additionally,
// // LexicalScope stores transient information used during parsing.
// Additionally, it stores transient information used during parsing.
// These scopes are not kept around after parsing or referenced by syntax
// trees so they can be stack-allocated and hence used by the pre-parser. class SaveScope BASE_EMBEDDED {
public:
SaveScope(Parser* parser, Scope* scope)
: parser_(parser),
previous_top_scope_(parser->top_scope_) {
parser->top_scope_ = scope;
}
~SaveScope() {
parser_->top_scope_ = previous_top_scope_;
}
private:
// Bookkeeping
Parser* parser_;
// Previous values
Scope* previous_top_scope_;
};
class LexicalScope BASE_EMBEDDED { class LexicalScope BASE_EMBEDDED {
public: public:
@ -516,7 +534,6 @@ class LexicalScope BASE_EMBEDDED {
// Previous values // Previous values
LexicalScope* lexical_scope_parent_; LexicalScope* lexical_scope_parent_;
Scope* previous_scope_; Scope* previous_scope_;
int previous_with_nesting_level_;
unsigned previous_ast_node_id_; unsigned previous_ast_node_id_;
}; };
@ -529,11 +546,9 @@ LexicalScope::LexicalScope(Parser* parser, Scope* scope, Isolate* isolate)
parser_(parser), parser_(parser),
lexical_scope_parent_(parser->lexical_scope_), lexical_scope_parent_(parser->lexical_scope_),
previous_scope_(parser->top_scope_), previous_scope_(parser->top_scope_),
previous_with_nesting_level_(parser->with_nesting_level_),
previous_ast_node_id_(isolate->ast_node_id()) { previous_ast_node_id_(isolate->ast_node_id()) {
parser->top_scope_ = scope; parser->top_scope_ = scope;
parser->lexical_scope_ = this; parser->lexical_scope_ = this;
parser->with_nesting_level_ = 0;
isolate->set_ast_node_id(AstNode::kDeclarationsId + 1); isolate->set_ast_node_id(AstNode::kDeclarationsId + 1);
} }
@ -541,7 +556,6 @@ LexicalScope::LexicalScope(Parser* parser, Scope* scope, Isolate* isolate)
LexicalScope::~LexicalScope() { LexicalScope::~LexicalScope() {
parser_->top_scope_ = previous_scope_; parser_->top_scope_ = previous_scope_;
parser_->lexical_scope_ = lexical_scope_parent_; parser_->lexical_scope_ = lexical_scope_parent_;
parser_->with_nesting_level_ = previous_with_nesting_level_;
parser_->isolate()->set_ast_node_id(previous_ast_node_id_); parser_->isolate()->set_ast_node_id(previous_ast_node_id_);
} }
@ -578,7 +592,6 @@ Parser::Parser(Handle<Script> script,
script_(script), script_(script),
scanner_(isolate_->unicode_cache()), scanner_(isolate_->unicode_cache()),
top_scope_(NULL), top_scope_(NULL),
with_nesting_level_(0),
lexical_scope_(NULL), lexical_scope_(NULL),
target_stack_(NULL), target_stack_(NULL),
allow_natives_syntax_(allow_natives_syntax), allow_natives_syntax_(allow_natives_syntax),
@ -637,7 +650,7 @@ FunctionLiteral* Parser::DoParseProgram(Handle<String> source,
Handle<String> no_name = isolate()->factory()->empty_symbol(); Handle<String> no_name = isolate()->factory()->empty_symbol();
FunctionLiteral* result = NULL; FunctionLiteral* result = NULL;
{ Scope* scope = NewScope(top_scope_, type, inside_with()); { Scope* scope = NewScope(top_scope_, type);
LexicalScope lexical_scope(this, scope, isolate()); LexicalScope lexical_scope(this, scope, isolate());
if (strict_mode == kStrictMode) { if (strict_mode == kStrictMode) {
top_scope_->EnableStrictMode(); top_scope_->EnableStrictMode();
@ -727,7 +740,7 @@ FunctionLiteral* Parser::ParseLazy(CompilationInfo* info,
{ {
// Parse the function literal. // Parse the function literal.
Scope* scope = NewScope(top_scope_, Scope::GLOBAL_SCOPE, inside_with()); Scope* scope = NewScope(top_scope_, Scope::GLOBAL_SCOPE);
if (!info->closure().is_null()) { if (!info->closure().is_null()) {
scope = Scope::DeserializeScopeChain(info, scope); scope = Scope::DeserializeScopeChain(info, scope);
} }
@ -1429,7 +1442,7 @@ VariableProxy* Parser::Declare(Handle<String> name,
// a performance issue since it may lead to repeated // a performance issue since it may lead to repeated
// Runtime::DeclareContextSlot() calls. // Runtime::DeclareContextSlot() calls.
VariableProxy* proxy = declaration_scope->NewUnresolved( VariableProxy* proxy = declaration_scope->NewUnresolved(
name, false, scanner().location().beg_pos); name, scanner().location().beg_pos);
declaration_scope->AddDeclaration( declaration_scope->AddDeclaration(
new(zone()) Declaration(proxy, mode, fun, top_scope_)); new(zone()) Declaration(proxy, mode, fun, top_scope_));
@ -1582,20 +1595,16 @@ Block* Parser::ParseScopedBlock(ZoneStringList* labels, bool* ok) {
// Construct block expecting 16 statements. // Construct block expecting 16 statements.
Block* body = new(zone()) Block(isolate(), labels, 16, false); Block* body = new(zone()) Block(isolate(), labels, 16, false);
Scope* saved_scope = top_scope_; Scope* block_scope = NewScope(top_scope_, Scope::BLOCK_SCOPE);
Scope* block_scope = NewScope(top_scope_,
Scope::BLOCK_SCOPE,
inside_with());
if (top_scope_->is_strict_mode()) { if (top_scope_->is_strict_mode()) {
block_scope->EnableStrictMode(); block_scope->EnableStrictMode();
} }
top_scope_ = block_scope;
// Parse the statements and collect escaping labels. // Parse the statements and collect escaping labels.
Expect(Token::LBRACE, CHECK_OK);
{ SaveScope save_scope(this, block_scope);
TargetCollector collector; TargetCollector collector;
Target target(&this->target_stack_, &collector); Target target(&this->target_stack_, &collector);
Expect(Token::LBRACE, CHECK_OK);
{
Target target_body(&this->target_stack_, body); Target target_body(&this->target_stack_, body);
InitializationBlockFinder block_finder(top_scope_, target_stack_); InitializationBlockFinder block_finder(top_scope_, target_stack_);
@ -1608,7 +1617,6 @@ Block* Parser::ParseScopedBlock(ZoneStringList* labels, bool* ok) {
} }
} }
Expect(Token::RBRACE, CHECK_OK); Expect(Token::RBRACE, CHECK_OK);
top_scope_ = saved_scope;
block_scope = block_scope->FinalizeBlockScope(); block_scope = block_scope->FinalizeBlockScope();
body->set_block_scope(block_scope); body->set_block_scope(block_scope);
@ -1875,15 +1883,11 @@ Block* Parser::ParseVariableDeclarations(VariableDeclarationContext var_context,
// as the declaration. Thus dynamic lookups are unnecessary even if the // as the declaration. Thus dynamic lookups are unnecessary even if the
// block scope is inside a with. // block scope is inside a with.
if (value != NULL) { if (value != NULL) {
bool in_with = (mode == VAR) ? inside_with() : false; VariableProxy* proxy = initialization_scope->NewUnresolved(name);
VariableProxy* proxy =
initialization_scope->NewUnresolved(name, in_with);
Assignment* assignment = Assignment* assignment =
new(zone()) Assignment(isolate(), init_op, proxy, value, position); new(zone()) Assignment(isolate(), init_op, proxy, value, position);
if (block) {
block->AddStatement(new(zone()) ExpressionStatement(assignment)); block->AddStatement(new(zone()) ExpressionStatement(assignment));
} }
}
if (fni_ != NULL) fni_->Leave(); if (fni_ != NULL) fni_->Leave();
} while (peek() == Token::COMMA); } while (peek() == Token::COMMA);
@ -2105,10 +2109,12 @@ Statement* Parser::ParseWithStatement(ZoneStringList* labels, bool* ok) {
Expression* expr = ParseExpression(true, CHECK_OK); Expression* expr = ParseExpression(true, CHECK_OK);
Expect(Token::RPAREN, CHECK_OK); Expect(Token::RPAREN, CHECK_OK);
++with_nesting_level_;
top_scope_->DeclarationScope()->RecordWithStatement(); top_scope_->DeclarationScope()->RecordWithStatement();
Statement* stmt = ParseStatement(labels, CHECK_OK); Scope* with_scope = NewScope(top_scope_, Scope::WITH_SCOPE);
--with_nesting_level_; Statement* stmt;
{ SaveScope save_scope(this, with_scope);
stmt = ParseStatement(labels, CHECK_OK);
}
return new(zone()) WithStatement(expr, stmt); return new(zone()) WithStatement(expr, stmt);
} }
@ -2245,17 +2251,15 @@ TryStatement* Parser::ParseTryStatement(bool* ok) {
if (peek() == Token::LBRACE) { if (peek() == Token::LBRACE) {
Target target(&this->target_stack_, &catch_collector); Target target(&this->target_stack_, &catch_collector);
catch_scope = NewScope(top_scope_, Scope::CATCH_SCOPE, inside_with()); catch_scope = NewScope(top_scope_, Scope::CATCH_SCOPE);
if (top_scope_->is_strict_mode()) { if (top_scope_->is_strict_mode()) {
catch_scope->EnableStrictMode(); catch_scope->EnableStrictMode();
} }
VariableMode mode = harmony_scoping_ ? LET : VAR; VariableMode mode = harmony_scoping_ ? LET : VAR;
catch_variable = catch_scope->DeclareLocal(name, mode); catch_variable = catch_scope->DeclareLocal(name, mode);
Scope* saved_scope = top_scope_; SaveScope save_scope(this, catch_scope);
top_scope_ = catch_scope;
catch_block = ParseBlock(NULL, CHECK_OK); catch_block = ParseBlock(NULL, CHECK_OK);
top_scope_ = saved_scope;
} else { } else {
Expect(Token::LBRACE, CHECK_OK); Expect(Token::LBRACE, CHECK_OK);
} }
@ -2374,7 +2378,7 @@ Statement* Parser::ParseForStatement(ZoneStringList* labels, bool* ok) {
ParseVariableDeclarations(kForStatement, &name, CHECK_OK); ParseVariableDeclarations(kForStatement, &name, CHECK_OK);
if (peek() == Token::IN && !name.is_null()) { if (peek() == Token::IN && !name.is_null()) {
VariableProxy* each = top_scope_->NewUnresolved(name, inside_with()); VariableProxy* each = top_scope_->NewUnresolved(name);
ForInStatement* loop = new(zone()) ForInStatement(isolate(), labels); ForInStatement* loop = new(zone()) ForInStatement(isolate(), labels);
Target target(&this->target_stack_, loop); Target target(&this->target_stack_, loop);
@ -3065,9 +3069,7 @@ Expression* Parser::ParsePrimaryExpression(bool* ok) {
case Token::FUTURE_STRICT_RESERVED_WORD: { case Token::FUTURE_STRICT_RESERVED_WORD: {
Handle<String> name = ParseIdentifier(CHECK_OK); Handle<String> name = ParseIdentifier(CHECK_OK);
if (fni_ != NULL) fni_->PushVariableName(name); if (fni_ != NULL) fni_->PushVariableName(name);
result = top_scope_->NewUnresolved(name, result = top_scope_->NewUnresolved(name, scanner().location().beg_pos);
inside_with(),
scanner().location().beg_pos);
break; break;
} }
@ -3714,9 +3716,10 @@ FunctionLiteral* Parser::ParseFunctionLiteral(Handle<String> function_name,
// Function declarations are function scoped in normal mode, so they are // Function declarations are function scoped in normal mode, so they are
// hoisted. In harmony block scoping mode they are block scoped, so they // hoisted. In harmony block scoping mode they are block scoped, so they
// are not hoisted. // are not hoisted.
Scope* scope = (type == FunctionLiteral::DECLARATION && !harmony_scoping_) Scope* scope = (type == FunctionLiteral::DECLARATION &&
? NewScope(top_scope_->DeclarationScope(), Scope::FUNCTION_SCOPE, false) !harmony_scoping_)
: NewScope(top_scope_, Scope::FUNCTION_SCOPE, inside_with()); ? NewScope(top_scope_->DeclarationScope(), Scope::FUNCTION_SCOPE)
: NewScope(top_scope_, Scope::FUNCTION_SCOPE);
ZoneList<Statement*>* body = new(zone()) ZoneList<Statement*>(8); ZoneList<Statement*>* body = new(zone()) ZoneList<Statement*>(8);
int materialized_literal_count; int materialized_literal_count;
int expected_property_count; int expected_property_count;
@ -3779,8 +3782,7 @@ FunctionLiteral* Parser::ParseFunctionLiteral(Handle<String> function_name,
// instead of Variables and Proxis as is the case now. // instead of Variables and Proxis as is the case now.
if (type == FunctionLiteral::NAMED_EXPRESSION) { if (type == FunctionLiteral::NAMED_EXPRESSION) {
Variable* fvar = top_scope_->DeclareFunctionVar(function_name); Variable* fvar = top_scope_->DeclareFunctionVar(function_name);
VariableProxy* fproxy = VariableProxy* fproxy = top_scope_->NewUnresolved(function_name);
top_scope_->NewUnresolved(function_name, inside_with());
fproxy->BindTo(fvar); fproxy->BindTo(fvar);
body->Add(new(zone()) ExpressionStatement( body->Add(new(zone()) ExpressionStatement(
new(zone()) Assignment(isolate(), new(zone()) Assignment(isolate(),

View File

@ -43,6 +43,7 @@ class ParserLog;
class PositionStack; class PositionStack;
class Target; class Target;
class LexicalScope; class LexicalScope;
class SaveScope;
template <typename T> class ZoneListWrapper; template <typename T> class ZoneListWrapper;
@ -473,7 +474,7 @@ class Parser {
void ReportInvalidPreparseData(Handle<String> name, bool* ok); void ReportInvalidPreparseData(Handle<String> name, bool* ok);
void ReportMessage(const char* message, Vector<const char*> args); void ReportMessage(const char* message, Vector<const char*> args);
bool inside_with() const { return with_nesting_level_ > 0; } bool inside_with() const { return top_scope_->inside_with(); }
JavaScriptScanner& scanner() { return scanner_; } JavaScriptScanner& scanner() { return scanner_; }
Mode mode() const { return mode_; } Mode mode() const { return mode_; }
ScriptDataImpl* pre_data() const { return pre_data_; } ScriptDataImpl* pre_data() const { return pre_data_; }
@ -669,7 +670,7 @@ class Parser {
return &empty; return &empty;
} }
Scope* NewScope(Scope* parent, Scope::Type type, bool inside_with); Scope* NewScope(Scope* parent, Scope::Type type);
Handle<String> LookupSymbol(int symbol_id); Handle<String> LookupSymbol(int symbol_id);
@ -714,7 +715,6 @@ class Parser {
JavaScriptScanner scanner_; JavaScriptScanner scanner_;
Scope* top_scope_; Scope* top_scope_;
int with_nesting_level_;
LexicalScope* lexical_scope_; LexicalScope* lexical_scope_;
Mode mode_; Mode mode_;
@ -734,6 +734,7 @@ class Parser {
bool harmony_scoping_; bool harmony_scoping_;
friend class LexicalScope; friend class LexicalScope;
friend class SaveScope;
}; };

View File

@ -156,9 +156,8 @@ Scope::Scope(Scope* inner_scope,
unresolved_(16), unresolved_(16),
decls_(4), decls_(4),
already_resolved_(true) { already_resolved_(true) {
ASSERT(!scope_info.is_null());
SetDefaults(type, NULL, scope_info); SetDefaults(type, NULL, scope_info);
if (scope_info->HasHeapAllocatedLocals()) { if (!scope_info.is_null() && scope_info->HasHeapAllocatedLocals()) {
num_heap_slots_ = scope_info_->NumberOfContextSlots(); num_heap_slots_ = scope_info_->NumberOfContextSlots();
} }
AddInnerScope(inner_scope); AddInnerScope(inner_scope);
@ -202,10 +201,8 @@ void Scope::SetDefaults(Type type,
scope_calls_eval_ = false; scope_calls_eval_ = false;
// Inherit the strict mode from the parent scope. // Inherit the strict mode from the parent scope.
strict_mode_ = (outer_scope != NULL) && outer_scope->strict_mode_; strict_mode_ = (outer_scope != NULL) && outer_scope->strict_mode_;
outer_scope_calls_eval_ = false;
outer_scope_calls_non_strict_eval_ = false; outer_scope_calls_non_strict_eval_ = false;
inner_scope_calls_eval_ = false; inner_scope_calls_eval_ = false;
outer_scope_is_eval_scope_ = false;
force_eager_compilation_ = false; force_eager_compilation_ = false;
num_var_or_const_ = 0; num_var_or_const_ = 0;
num_stack_slots_ = 0; num_stack_slots_ = 0;
@ -224,13 +221,15 @@ Scope* Scope::DeserializeScopeChain(CompilationInfo* info,
bool contains_with = false; bool contains_with = false;
while (!context->IsGlobalContext()) { while (!context->IsGlobalContext()) {
if (context->IsWithContext()) { if (context->IsWithContext()) {
Scope* with_scope = new Scope(current_scope, WITH_SCOPE,
Handle<SerializedScopeInfo>::null());
current_scope = with_scope;
// All the inner scopes are inside a with. // All the inner scopes are inside a with.
contains_with = true; contains_with = true;
for (Scope* s = innermost_scope; s != NULL; s = s->outer_scope()) { for (Scope* s = innermost_scope; s != NULL; s = s->outer_scope()) {
s->scope_inside_with_ = true; s->scope_inside_with_ = true;
} }
} else { } else if (context->IsFunctionContext()) {
if (context->IsFunctionContext()) {
SerializedScopeInfo* scope_info = SerializedScopeInfo* scope_info =
context->closure()->shared()->scope_info(); context->closure()->shared()->scope_info();
current_scope = new Scope(current_scope, FUNCTION_SCOPE, current_scope = new Scope(current_scope, FUNCTION_SCOPE,
@ -247,7 +246,6 @@ Scope* Scope::DeserializeScopeChain(CompilationInfo* info,
} }
if (contains_with) current_scope->RecordWithStatement(); if (contains_with) current_scope->RecordWithStatement();
if (innermost_scope == NULL) innermost_scope = current_scope; if (innermost_scope == NULL) innermost_scope = current_scope;
}
// Forget about a with when we move to a context for a different function. // Forget about a with when we move to a context for a different function.
if (context->previous()->closure() != context->closure()) { if (context->previous()->closure() != context->closure()) {
@ -281,15 +279,15 @@ bool Scope::Analyze(CompilationInfo* info) {
} }
void Scope::Initialize(bool inside_with) { void Scope::Initialize() {
ASSERT(!already_resolved()); ASSERT(!already_resolved());
// Add this scope as a new inner scope of the outer scope. // Add this scope as a new inner scope of the outer scope.
if (outer_scope_ != NULL) { if (outer_scope_ != NULL) {
outer_scope_->inner_scopes_.Add(this); outer_scope_->inner_scopes_.Add(this);
scope_inside_with_ = outer_scope_->scope_inside_with_ || inside_with; scope_inside_with_ = outer_scope_->scope_inside_with_ || is_with_scope();
} else { } else {
scope_inside_with_ = inside_with; scope_inside_with_ = is_with_scope();
} }
// Declare convenience variables. // Declare convenience variables.
@ -300,13 +298,7 @@ void Scope::Initialize(bool inside_with) {
// instead load them directly from the stack. Currently, the only // instead load them directly from the stack. Currently, the only
// such parameter is 'this' which is passed on the stack when // such parameter is 'this' which is passed on the stack when
// invoking scripts // invoking scripts
if (is_catch_scope() || is_block_scope()) { if (is_declaration_scope()) {
ASSERT(outer_scope() != NULL);
receiver_ = outer_scope()->receiver();
} else {
ASSERT(is_function_scope() ||
is_global_scope() ||
is_eval_scope());
Variable* var = Variable* var =
variables_.Declare(this, variables_.Declare(this,
isolate_->factory()->this_symbol(), isolate_->factory()->this_symbol(),
@ -315,6 +307,9 @@ void Scope::Initialize(bool inside_with) {
Variable::THIS); Variable::THIS);
var->AllocateTo(Variable::PARAMETER, -1); var->AllocateTo(Variable::PARAMETER, -1);
receiver_ = var; receiver_ = var;
} else {
ASSERT(outer_scope() != NULL);
receiver_ = outer_scope()->receiver();
} }
if (is_function_scope()) { if (is_function_scope()) {
@ -441,15 +436,13 @@ Variable* Scope::DeclareGlobal(Handle<String> name) {
} }
VariableProxy* Scope::NewUnresolved(Handle<String> name, VariableProxy* Scope::NewUnresolved(Handle<String> name, int position) {
bool inside_with,
int position) {
// Note that we must not share the unresolved variables with // Note that we must not share the unresolved variables with
// the same name because they may be removed selectively via // the same name because they may be removed selectively via
// RemoveUnresolved(). // RemoveUnresolved().
ASSERT(!already_resolved()); ASSERT(!already_resolved());
VariableProxy* proxy = new(isolate_->zone()) VariableProxy( VariableProxy* proxy = new(isolate_->zone()) VariableProxy(
isolate_, name, false, inside_with, position); isolate_, name, false, position);
unresolved_.Add(proxy); unresolved_.Add(proxy);
return proxy; return proxy;
} }
@ -505,17 +498,19 @@ Declaration* Scope::CheckConflictingVarDeclarations() {
Declaration* decl = decls_[i]; Declaration* decl = decls_[i];
if (decl->mode() != VAR) continue; if (decl->mode() != VAR) continue;
Handle<String> name = decl->proxy()->name(); Handle<String> name = decl->proxy()->name();
bool cond = true;
for (Scope* scope = decl->scope(); cond ; scope = scope->outer_scope_) { // Iterate through all scopes until and including the declaration scope.
Scope* previous = NULL;
Scope* current = decl->scope();
do {
// There is a conflict if there exists a non-VAR binding. // There is a conflict if there exists a non-VAR binding.
Variable* other_var = scope->variables_.Lookup(name); Variable* other_var = current->variables_.Lookup(name);
if (other_var != NULL && other_var->mode() != VAR) { if (other_var != NULL && other_var->mode() != VAR) {
return decl; return decl;
} }
previous = current;
// Include declaration scope in the iteration but stop after. current = current->outer_scope_;
if (!scope->is_block_scope() && !scope->is_catch_scope()) cond = false; } while (!previous->is_declaration_scope());
}
} }
return NULL; return NULL;
} }
@ -563,16 +558,11 @@ void Scope::AllocateVariables(Handle<Context> context) {
// this information in the ScopeInfo and then use it here (by traversing // this information in the ScopeInfo and then use it here (by traversing
// the call chain stack, at compile time). // the call chain stack, at compile time).
bool eval_scope = is_eval_scope();
bool outer_scope_calls_eval = false;
bool outer_scope_calls_non_strict_eval = false; bool outer_scope_calls_non_strict_eval = false;
if (!is_global_scope()) { if (!is_global_scope()) {
context->ComputeEvalScopeInfo(&outer_scope_calls_eval, context->ComputeEvalScopeInfo(&outer_scope_calls_non_strict_eval);
&outer_scope_calls_non_strict_eval);
} }
PropagateScopeInfo(outer_scope_calls_eval, PropagateScopeInfo(outer_scope_calls_non_strict_eval);
outer_scope_calls_non_strict_eval,
eval_scope);
// 2) Resolve variables. // 2) Resolve variables.
Scope* global_scope = NULL; Scope* global_scope = NULL;
@ -625,8 +615,7 @@ int Scope::ContextChainLength(Scope* scope) {
Scope* Scope::DeclarationScope() { Scope* Scope::DeclarationScope() {
Scope* scope = this; Scope* scope = this;
while (scope->is_catch_scope() || while (!scope->is_declaration_scope()) {
scope->is_block_scope()) {
scope = scope->outer_scope(); scope = scope->outer_scope();
} }
return scope; return scope;
@ -649,6 +638,7 @@ static const char* Header(Scope::Type type) {
case Scope::GLOBAL_SCOPE: return "global"; case Scope::GLOBAL_SCOPE: return "global";
case Scope::CATCH_SCOPE: return "catch"; case Scope::CATCH_SCOPE: return "catch";
case Scope::BLOCK_SCOPE: return "block"; case Scope::BLOCK_SCOPE: return "block";
case Scope::WITH_SCOPE: return "with";
} }
UNREACHABLE(); UNREACHABLE();
return NULL; return NULL;
@ -748,14 +738,10 @@ void Scope::Print(int n) {
if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n"); if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n");
if (scope_contains_with_) Indent(n1, "// scope contains 'with'\n"); if (scope_contains_with_) Indent(n1, "// scope contains 'with'\n");
if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n"); if (scope_calls_eval_) Indent(n1, "// scope calls 'eval'\n");
if (outer_scope_calls_eval_) Indent(n1, "// outer scope calls 'eval'\n");
if (outer_scope_calls_non_strict_eval_) { if (outer_scope_calls_non_strict_eval_) {
Indent(n1, "// outer scope calls 'eval' in non-strict context\n"); Indent(n1, "// outer scope calls 'eval' in non-strict context\n");
} }
if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n"); if (inner_scope_calls_eval_) Indent(n1, "// inner scope calls 'eval'\n");
if (outer_scope_is_eval_scope_) {
Indent(n1, "// outer scope is 'eval' scope\n");
}
if (num_stack_slots_ > 0) { Indent(n1, "// "); if (num_stack_slots_ > 0) { Indent(n1, "// ");
PrintF("%d stack slots\n", num_stack_slots_); } PrintF("%d stack slots\n", num_stack_slots_); }
if (num_heap_slots_ > 0) { Indent(n1, "// "); if (num_heap_slots_ > 0) { Indent(n1, "// ");
@ -809,33 +795,21 @@ Variable* Scope::NonLocal(Handle<String> name, VariableMode mode) {
} }
// Lookup a variable starting with this scope. The result is either
// the statically resolved variable belonging to an outer scope, or
// NULL. It may be NULL because a) we couldn't find a variable, or b)
// because the variable is just a guess (and may be shadowed by
// another variable that is introduced dynamically via an 'eval' call
// or a 'with' statement).
Variable* Scope::LookupRecursive(Handle<String> name, Variable* Scope::LookupRecursive(Handle<String> name,
bool from_inner_scope, Handle<Context> context,
Variable** invalidated_local) { BindingKind* binding_kind) {
// If we find a variable, but the current scope calls 'eval', the found ASSERT(binding_kind != NULL);
// variable may not be the correct one (the 'eval' may introduce a
// property with the same name). In that case, remember that the variable
// found is just a guess.
bool guess = scope_calls_eval_;
// Try to find the variable in this scope. // Try to find the variable in this scope.
Variable* var = LocalLookup(name); Variable* var = LocalLookup(name);
// We found a variable and we are done. (Even if there is an 'eval' in
// this scope which introduces the same variable again, the resulting
// variable remains the same.)
if (var != NULL) { if (var != NULL) {
// We found a variable. If this is not an inner lookup, we are done. *binding_kind = BOUND;
// (Even if there is an 'eval' in this scope which introduces the
// same variable again, the resulting variable remains the same.
// Note that enclosing 'with' statements are handled at the call site.)
if (!from_inner_scope)
return var; return var;
}
} else {
// We did not find a variable locally. Check against the function variable, // We did not find a variable locally. Check against the function variable,
// if any. We can do this for all scopes, since the function variable is // if any. We can do this for all scopes, since the function variable is
// only present - if at all - for function scopes. // only present - if at all - for function scopes.
@ -844,39 +818,45 @@ Variable* Scope::LookupRecursive(Handle<String> name,
// between this scope and the outer scope. (ECMA-262, 3rd., requires that // between this scope and the outer scope. (ECMA-262, 3rd., requires that
// the name of named function literal is kept in an intermediate scope // the name of named function literal is kept in an intermediate scope
// in between this scope and the next outer scope.) // in between this scope and the next outer scope.)
*binding_kind = UNBOUND;
if (function_ != NULL && function_->name().is_identical_to(name)) { if (function_ != NULL && function_->name().is_identical_to(name)) {
var = function_->var(); var = function_->var();
*binding_kind = BOUND;
} else if (outer_scope_ != NULL) { } else if (outer_scope_ != NULL) {
var = outer_scope_->LookupRecursive(name, true, invalidated_local); var = outer_scope_->LookupRecursive(name, context, binding_kind);
// We may have found a variable in an outer scope. However, if if (*binding_kind == BOUND) var->MarkAsAccessedFromInnerScope();
// the current scope is inside a 'with', the actual variable may
// be a property introduced via the 'with' statement. Then, the
// variable we may have found is just a guess.
if (scope_inside_with_)
guess = true;
} }
// If we did not find a variable, we are done. if (is_with_scope()) {
if (var == NULL) // The current scope is a with scope, so the variable binding can not be
// statically resolved. However, note that it was necessary to do a lookup
// in the outer scope anyway, because if a binding exists in an outer scope,
// the associated variable has to be marked as potentially being accessed
// from inside of an inner with scope (the property may not be in the 'with'
// object).
*binding_kind = DYNAMIC_LOOKUP;
return NULL; return NULL;
} else if (is_eval_scope()) {
// No local binding was found, no 'with' statements have been encountered
// and the code is executed as part of a call to 'eval'. The calling context
// contains scope information that we can use to determine if the variable
// is global, i.e. the calling context chain does not contain a binding and
// no 'with' contexts.
ASSERT(*binding_kind == UNBOUND);
*binding_kind = context->GlobalIfNotShadowedByEval(name)
? UNBOUND_EVAL_SHADOWED : DYNAMIC_LOOKUP;
return NULL;
} else if (calls_non_strict_eval()) {
// A variable binding may have been found in an outer scope, but the current
// scope makes a non-strict 'eval' call, so the found variable may not be
// the correct one (the 'eval' may introduce a binding with the same name).
// In that case, change the lookup result to reflect this situation.
if (*binding_kind == BOUND) {
*binding_kind = BOUND_EVAL_SHADOWED;
} else if (*binding_kind == UNBOUND) {
*binding_kind = UNBOUND_EVAL_SHADOWED;
} }
ASSERT(var != NULL);
// If this is a lookup from an inner scope, mark the variable.
if (from_inner_scope) {
var->MarkAsAccessedFromInnerScope();
} }
// If the variable we have found is just a guess, invalidate the
// result. If the found variable is local, record that fact so we
// can generate fast code to get it if it is not shadowed by eval.
if (guess) {
if (!var->is_global()) *invalidated_local = var;
var = NULL;
}
return var; return var;
} }
@ -891,71 +871,44 @@ void Scope::ResolveVariable(Scope* global_scope,
if (proxy->var() != NULL) return; if (proxy->var() != NULL) return;
// Otherwise, try to resolve the variable. // Otherwise, try to resolve the variable.
Variable* invalidated_local = NULL; BindingKind binding_kind;
Variable* var = LookupRecursive(proxy->name(), false, &invalidated_local); Variable* var = LookupRecursive(proxy->name(), context, &binding_kind);
switch (binding_kind) {
if (proxy->inside_with()) { case BOUND:
// If we are inside a local 'with' statement, all bets are off // We found a variable binding.
// and we cannot resolve the proxy to a local variable even if break;
// we found an outer matching variable.
// Note that we must do a lookup anyway, because if we find one,
// we must mark that variable as potentially accessed from this
// inner scope (the property may not be in the 'with' object).
var = NonLocal(proxy->name(), DYNAMIC);
case BOUND_EVAL_SHADOWED:
// We found a variable variable binding that might be shadowed
// by 'eval' introduced variable bindings.
if (var->is_global()) {
var = NonLocal(proxy->name(), DYNAMIC_GLOBAL);
} else { } else {
// We are not inside a local 'with' statement. Variable* invalidated = var;
var = NonLocal(proxy->name(), DYNAMIC_LOCAL);
var->set_local_if_not_shadowed(invalidated);
}
break;
if (var == NULL) { case UNBOUND:
// We did not find the variable. We have a global variable // No binding has been found. Declare a variable in global scope.
// if we are in the global scope (we know already that we
// are outside a 'with' statement) or if there is no way
// that the variable might be introduced dynamically (through
// a local or outer eval() call, or an outer 'with' statement),
// or we don't know about the outer scope (because we are
// in an eval scope).
if (is_global_scope() ||
!(scope_inside_with_ || outer_scope_is_eval_scope_ ||
scope_calls_eval_ || outer_scope_calls_eval_)) {
// We must have a global variable.
ASSERT(global_scope != NULL); ASSERT(global_scope != NULL);
var = global_scope->DeclareGlobal(proxy->name()); var = global_scope->DeclareGlobal(proxy->name());
break;
} else if (scope_inside_with_) { case UNBOUND_EVAL_SHADOWED:
// If we are inside a with statement we give up and look up // No binding has been found. But some scope makes a
// the variable at runtime. // non-strict 'eval' call.
var = NonLocal(proxy->name(), DYNAMIC);
} else if (invalidated_local != NULL) {
// No with statements are involved and we found a local
// variable that might be shadowed by eval introduced
// variables.
var = NonLocal(proxy->name(), DYNAMIC_LOCAL);
var->set_local_if_not_shadowed(invalidated_local);
} else if (outer_scope_is_eval_scope_) {
// No with statements and we did not find a local and the code
// is executed with a call to eval. The context contains
// scope information that we can use to determine if the
// variable is global if it is not shadowed by eval-introduced
// variables.
if (context->GlobalIfNotShadowedByEval(proxy->name())) {
var = NonLocal(proxy->name(), DYNAMIC_GLOBAL); var = NonLocal(proxy->name(), DYNAMIC_GLOBAL);
break;
} else { case DYNAMIC_LOOKUP:
// The variable could not be resolved statically.
var = NonLocal(proxy->name(), DYNAMIC); var = NonLocal(proxy->name(), DYNAMIC);
break;
} }
} else { ASSERT(var != NULL);
// No with statements and we did not find a local and the code
// is not executed with a call to eval. We know that this
// variable is global unless it is shadowed by eval-introduced
// variables.
var = NonLocal(proxy->name(), DYNAMIC_GLOBAL);
}
}
}
proxy->BindTo(var); proxy->BindTo(var);
} }
@ -976,31 +929,17 @@ void Scope::ResolveVariablesRecursively(Scope* global_scope,
} }
bool Scope::PropagateScopeInfo(bool outer_scope_calls_eval, bool Scope::PropagateScopeInfo(bool outer_scope_calls_non_strict_eval ) {
bool outer_scope_calls_non_strict_eval,
bool outer_scope_is_eval_scope) {
if (outer_scope_calls_eval) {
outer_scope_calls_eval_ = true;
}
if (outer_scope_calls_non_strict_eval) { if (outer_scope_calls_non_strict_eval) {
outer_scope_calls_non_strict_eval_ = true; outer_scope_calls_non_strict_eval_ = true;
} }
if (outer_scope_is_eval_scope) {
outer_scope_is_eval_scope_ = true;
}
bool calls_eval = scope_calls_eval_ || outer_scope_calls_eval_;
bool is_eval = is_eval_scope() || outer_scope_is_eval_scope_;
bool calls_non_strict_eval = bool calls_non_strict_eval =
(scope_calls_eval_ && !is_strict_mode()) || (scope_calls_eval_ && !is_strict_mode()) ||
outer_scope_calls_non_strict_eval_; outer_scope_calls_non_strict_eval_;
for (int i = 0; i < inner_scopes_.length(); i++) { for (int i = 0; i < inner_scopes_.length(); i++) {
Scope* inner_scope = inner_scopes_[i]; Scope* inner_scope = inner_scopes_[i];
if (inner_scope->PropagateScopeInfo(calls_eval, if (inner_scope->PropagateScopeInfo(calls_non_strict_eval)) {
calls_non_strict_eval,
is_eval)) {
inner_scope_calls_eval_ = true; inner_scope_calls_eval_ = true;
} }
if (inner_scope->force_eager_compilation_) { if (inner_scope->force_eager_compilation_) {

View File

@ -94,7 +94,8 @@ class Scope: public ZoneObject {
FUNCTION_SCOPE, // The top-level scope for a function. FUNCTION_SCOPE, // The top-level scope for a function.
GLOBAL_SCOPE, // The top-level scope for a program or a top-level eval. GLOBAL_SCOPE, // The top-level scope for a program or a top-level eval.
CATCH_SCOPE, // The scope introduced by catch. CATCH_SCOPE, // The scope introduced by catch.
BLOCK_SCOPE // The scope introduced by a new block. BLOCK_SCOPE, // The scope introduced by a new block.
WITH_SCOPE // The scope introduced by with.
}; };
Scope(Scope* outer_scope, Type type); Scope(Scope* outer_scope, Type type);
@ -110,7 +111,7 @@ class Scope: public ZoneObject {
// The scope name is only used for printing/debugging. // The scope name is only used for printing/debugging.
void SetScopeName(Handle<String> scope_name) { scope_name_ = scope_name; } void SetScopeName(Handle<String> scope_name) { scope_name_ = scope_name; }
void Initialize(bool inside_with); void Initialize();
// Checks if the block scope is redundant, i.e. it does not contain any // Checks if the block scope is redundant, i.e. it does not contain any
// block scoped declarations. In that case it is removed from the scope // block scoped declarations. In that case it is removed from the scope
@ -149,7 +150,6 @@ class Scope: public ZoneObject {
// Create a new unresolved variable. // Create a new unresolved variable.
VariableProxy* NewUnresolved(Handle<String> name, VariableProxy* NewUnresolved(Handle<String> name,
bool inside_with,
int position = RelocInfo::kNoPosition); int position = RelocInfo::kNoPosition);
// Remove a unresolved variable. During parsing, an unresolved variable // Remove a unresolved variable. During parsing, an unresolved variable
@ -199,7 +199,7 @@ class Scope: public ZoneObject {
void RecordWithStatement() { scope_contains_with_ = true; } void RecordWithStatement() { scope_contains_with_ = true; }
// Inform the scope that the corresponding code contains an eval call. // Inform the scope that the corresponding code contains an eval call.
void RecordEvalCall() { scope_calls_eval_ = true; } void RecordEvalCall() { if (!is_global_scope()) scope_calls_eval_ = true; }
// Enable strict mode for the scope (unless disabled by a global flag). // Enable strict mode for the scope (unless disabled by a global flag).
void EnableStrictMode() { void EnableStrictMode() {
@ -215,6 +215,10 @@ class Scope: public ZoneObject {
bool is_global_scope() const { return type_ == GLOBAL_SCOPE; } bool is_global_scope() const { return type_ == GLOBAL_SCOPE; }
bool is_catch_scope() const { return type_ == CATCH_SCOPE; } bool is_catch_scope() const { return type_ == CATCH_SCOPE; }
bool is_block_scope() const { return type_ == BLOCK_SCOPE; } bool is_block_scope() const { return type_ == BLOCK_SCOPE; }
bool is_with_scope() const { return type_ == WITH_SCOPE; }
bool is_declaration_scope() const {
return is_eval_scope() || is_function_scope() || is_global_scope();
}
bool is_strict_mode() const { return strict_mode_; } bool is_strict_mode() const { return strict_mode_; }
bool is_strict_mode_eval_scope() const { bool is_strict_mode_eval_scope() const {
return is_eval_scope() && is_strict_mode(); return is_eval_scope() && is_strict_mode();
@ -222,7 +226,9 @@ class Scope: public ZoneObject {
// Information about which scopes calls eval. // Information about which scopes calls eval.
bool calls_eval() const { return scope_calls_eval_; } bool calls_eval() const { return scope_calls_eval_; }
bool outer_scope_calls_eval() const { return outer_scope_calls_eval_; } bool calls_non_strict_eval() {
return scope_calls_eval_ && !is_strict_mode();
}
bool outer_scope_calls_non_strict_eval() const { bool outer_scope_calls_non_strict_eval() const {
return outer_scope_calls_non_strict_eval_; return outer_scope_calls_non_strict_eval_;
} }
@ -383,10 +389,8 @@ class Scope: public ZoneObject {
bool strict_mode_; bool strict_mode_;
// Computed via PropagateScopeInfo. // Computed via PropagateScopeInfo.
bool outer_scope_calls_eval_;
bool outer_scope_calls_non_strict_eval_; bool outer_scope_calls_non_strict_eval_;
bool inner_scope_calls_eval_; bool inner_scope_calls_eval_;
bool outer_scope_is_eval_scope_;
bool force_eager_compilation_; bool force_eager_compilation_;
// True if it doesn't need scope resolution (e.g., if the scope was // True if it doesn't need scope resolution (e.g., if the scope was
@ -396,7 +400,7 @@ class Scope: public ZoneObject {
// Computed as variables are declared. // Computed as variables are declared.
int num_var_or_const_; int num_var_or_const_;
// Computed via AllocateVariables; function scopes only. // Computed via AllocateVariables; function, block and catch scopes only.
int num_stack_slots_; int num_stack_slots_;
int num_heap_slots_; int num_heap_slots_;
@ -409,9 +413,57 @@ class Scope: public ZoneObject {
Variable* NonLocal(Handle<String> name, VariableMode mode); Variable* NonLocal(Handle<String> name, VariableMode mode);
// Variable resolution. // Variable resolution.
// Possible results of a recursive variable lookup telling if and how a
// variable is bound. These are returned in the output parameter *binding_kind
// of the LookupRecursive function.
enum BindingKind {
// The variable reference could be statically resolved to a variable binding
// which is returned. There is no 'with' statement between the reference and
// the binding and no scope between the reference scope (inclusive) and
// binding scope (exclusive) makes a non-strict 'eval' call.
BOUND,
// The variable reference could be statically resolved to a variable binding
// which is returned. There is no 'with' statement between the reference and
// the binding, but some scope between the reference scope (inclusive) and
// binding scope (exclusive) makes a non-strict 'eval' call, that might
// possibly introduce variable bindings shadowing the found one. Thus the
// found variable binding is just a guess.
BOUND_EVAL_SHADOWED,
// The variable reference could not be statically resolved to any binding
// and thus should be considered referencing a global variable. NULL is
// returned. The variable reference is not inside any 'with' statement and
// no scope between the reference scope (inclusive) and global scope
// (exclusive) makes a non-strict 'eval' call.
UNBOUND,
// The variable reference could not be statically resolved to any binding
// NULL is returned. The variable reference is not inside any 'with'
// statement, but some scope between the reference scope (inclusive) and
// global scope (exclusive) makes a non-strict 'eval' call, that might
// possibly introduce a variable binding. Thus the reference should be
// considered referencing a global variable unless it is shadowed by an
// 'eval' introduced binding.
UNBOUND_EVAL_SHADOWED,
// The variable could not be statically resolved and needs to be looked up
// dynamically. NULL is returned. There are two possible reasons:
// * A 'with' statement has been encountered and there is no variable
// binding for the name between the variable reference and the 'with'.
// The variable potentially references a property of the 'with' object.
// * The code is being executed as part of a call to 'eval' and the calling
// context chain contains either a variable binding for the name or it
// contains a 'with' context.
DYNAMIC_LOOKUP
};
// Lookup a variable reference given by name recursively starting with this
// scope. If the code is executed because of a call to 'eval', the context
// parameter should be set to the calling context of 'eval'.
Variable* LookupRecursive(Handle<String> name, Variable* LookupRecursive(Handle<String> name,
bool from_inner_function, Handle<Context> context,
Variable** invalidated_local); BindingKind* binding_kind);
void ResolveVariable(Scope* global_scope, void ResolveVariable(Scope* global_scope,
Handle<Context> context, Handle<Context> context,
VariableProxy* proxy); VariableProxy* proxy);
@ -419,9 +471,7 @@ class Scope: public ZoneObject {
Handle<Context> context); Handle<Context> context);
// Scope analysis. // Scope analysis.
bool PropagateScopeInfo(bool outer_scope_calls_eval, bool PropagateScopeInfo(bool outer_scope_calls_non_strict_eval);
bool outer_scope_calls_non_strict_eval,
bool outer_scope_is_eval_scope);
bool HasTrivialContext() const; bool HasTrivialContext() const;
// Predicates. // Predicates.
@ -438,7 +488,7 @@ class Scope: public ZoneObject {
void AllocateVariablesRecursively(); void AllocateVariablesRecursively();
private: private:
// Construct a function or block scope based on the scope info. // Construct a scope based on the scope info.
Scope(Scope* inner_scope, Type type, Handle<SerializedScopeInfo> scope_info); Scope(Scope* inner_scope, Type type, Handle<SerializedScopeInfo> scope_info);
// Construct a catch scope with a binding for the name. // Construct a catch scope with a binding for the name.

View File

@ -154,6 +154,10 @@ class Variable: public ZoneObject {
Location location_; Location location_;
int index_; int index_;
// If this field is set, this variable references the stored locally bound
// variable, but it might be shadowed by variable bindings introduced by
// non-strict 'eval' calls between the reference scope (inclusive) and the
// binding scope (exclusive).
Variable* local_if_not_shadowed_; Variable* local_if_not_shadowed_;
// Valid as a LHS? (const and this are not valid LHS, for example) // Valid as a LHS? (const and this are not valid LHS, for example)

View File

@ -1099,7 +1099,7 @@ void FullCodeGenerator::EmitLoadGlobalCheckExtensions(Variable* var,
// If no outer scope calls eval, we do not need to check more // If no outer scope calls eval, we do not need to check more
// context extensions. If we have reached an eval scope, we check // context extensions. If we have reached an eval scope, we check
// all extensions from this point. // all extensions from this point.
if (!s->outer_scope_calls_eval() || s->is_eval_scope()) break; if (!s->outer_scope_calls_non_strict_eval() || s->is_eval_scope()) break;
s = s->outer_scope(); s = s->outer_scope();
} }