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
// 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();
}

View File

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

View File

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

View File

@ -266,8 +266,7 @@ bool Context::GlobalIfNotShadowedByEval(Handle<String> name) {
}
void Context::ComputeEvalScopeInfo(bool* outer_scope_calls_eval,
bool* outer_scope_calls_non_strict_eval) {
void Context::ComputeEvalScopeInfo(bool* outer_scope_calls_non_strict_eval) {
// Skip up the context chain checking all the function contexts to see
// whether they call eval.
Context* context = this;
@ -275,14 +274,11 @@ void Context::ComputeEvalScopeInfo(bool* outer_scope_calls_eval,
if (context->IsFunctionContext()) {
Handle<SerializedScopeInfo> scope_info(
context->closure()->shared()->scope_info());
if (scope_info->CallsEval()) {
*outer_scope_calls_eval = true;
if (!scope_info->IsStrictMode()) {
// No need to go further since the answers will not change from
// here.
*outer_scope_calls_non_strict_eval = true;
return;
}
if (scope_info->CallsEval() && !scope_info->IsStrictMode()) {
// No need to go further since the answers will not change from
// here.
*outer_scope_calls_non_strict_eval = true;
return;
}
}
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
// any of those calls are in non-strict mode.
void ComputeEvalScopeInfo(bool* outer_scope_calls_eval,
bool* outer_scope_calls_non_strict_eval);
void ComputeEvalScopeInfo(bool* outer_scope_calls_non_strict_eval);
// Code generation support.
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
// context extensions. If we have reached an eval scope, we check
// 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();
}

View File

@ -1149,7 +1149,7 @@ void FullCodeGenerator::EmitLoadGlobalCheckExtensions(Variable* var,
}
// If no outer scope calls eval, we do not need to check more
// 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();
}

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

View File

@ -43,6 +43,7 @@ class ParserLog;
class PositionStack;
class Target;
class LexicalScope;
class SaveScope;
template <typename T> class ZoneListWrapper;
@ -473,7 +474,7 @@ class Parser {
void ReportInvalidPreparseData(Handle<String> name, bool* ok);
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_; }
Mode mode() const { return mode_; }
ScriptDataImpl* pre_data() const { return pre_data_; }
@ -669,7 +670,7 @@ class Parser {
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);
@ -714,7 +715,6 @@ class Parser {
JavaScriptScanner scanner_;
Scope* top_scope_;
int with_nesting_level_;
LexicalScope* lexical_scope_;
Mode mode_;
@ -734,6 +734,7 @@ class Parser {
bool harmony_scoping_;
friend class LexicalScope;
friend class SaveScope;
};

View File

@ -156,9 +156,8 @@ Scope::Scope(Scope* inner_scope,
unresolved_(16),
decls_(4),
already_resolved_(true) {
ASSERT(!scope_info.is_null());
SetDefaults(type, NULL, scope_info);
if (scope_info->HasHeapAllocatedLocals()) {
if (!scope_info.is_null() && scope_info->HasHeapAllocatedLocals()) {
num_heap_slots_ = scope_info_->NumberOfContextSlots();
}
AddInnerScope(inner_scope);
@ -202,10 +201,8 @@ void Scope::SetDefaults(Type type,
scope_calls_eval_ = false;
// Inherit the strict mode from the parent scope.
strict_mode_ = (outer_scope != NULL) && outer_scope->strict_mode_;
outer_scope_calls_eval_ = false;
outer_scope_calls_non_strict_eval_ = false;
inner_scope_calls_eval_ = false;
outer_scope_is_eval_scope_ = false;
force_eager_compilation_ = false;
num_var_or_const_ = 0;
num_stack_slots_ = 0;
@ -224,30 +221,31 @@ Scope* Scope::DeserializeScopeChain(CompilationInfo* info,
bool contains_with = false;
while (!context->IsGlobalContext()) {
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.
contains_with = true;
for (Scope* s = innermost_scope; s != NULL; s = s->outer_scope()) {
s->scope_inside_with_ = true;
}
} else if (context->IsFunctionContext()) {
SerializedScopeInfo* scope_info =
context->closure()->shared()->scope_info();
current_scope = new Scope(current_scope, FUNCTION_SCOPE,
Handle<SerializedScopeInfo>(scope_info));
} else if (context->IsBlockContext()) {
SerializedScopeInfo* scope_info =
SerializedScopeInfo::cast(context->extension());
current_scope = new Scope(current_scope, BLOCK_SCOPE,
Handle<SerializedScopeInfo>(scope_info));
} else {
if (context->IsFunctionContext()) {
SerializedScopeInfo* scope_info =
context->closure()->shared()->scope_info();
current_scope = new Scope(current_scope, FUNCTION_SCOPE,
Handle<SerializedScopeInfo>(scope_info));
} else if (context->IsBlockContext()) {
SerializedScopeInfo* scope_info =
SerializedScopeInfo::cast(context->extension());
current_scope = new Scope(current_scope, BLOCK_SCOPE,
Handle<SerializedScopeInfo>(scope_info));
} else {
ASSERT(context->IsCatchContext());
String* name = String::cast(context->extension());
current_scope = new Scope(current_scope, Handle<String>(name));
}
if (contains_with) current_scope->RecordWithStatement();
if (innermost_scope == NULL) innermost_scope = current_scope;
ASSERT(context->IsCatchContext());
String* name = String::cast(context->extension());
current_scope = new Scope(current_scope, Handle<String>(name));
}
if (contains_with) current_scope->RecordWithStatement();
if (innermost_scope == NULL) innermost_scope = current_scope;
// Forget about a with when we move to a context for a different function.
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());
// Add this scope as a new inner scope of the outer scope.
if (outer_scope_ != NULL) {
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 {
scope_inside_with_ = inside_with;
scope_inside_with_ = is_with_scope();
}
// Declare convenience variables.
@ -300,13 +298,7 @@ void Scope::Initialize(bool inside_with) {
// instead load them directly from the stack. Currently, the only
// such parameter is 'this' which is passed on the stack when
// invoking scripts
if (is_catch_scope() || is_block_scope()) {
ASSERT(outer_scope() != NULL);
receiver_ = outer_scope()->receiver();
} else {
ASSERT(is_function_scope() ||
is_global_scope() ||
is_eval_scope());
if (is_declaration_scope()) {
Variable* var =
variables_.Declare(this,
isolate_->factory()->this_symbol(),
@ -315,6 +307,9 @@ void Scope::Initialize(bool inside_with) {
Variable::THIS);
var->AllocateTo(Variable::PARAMETER, -1);
receiver_ = var;
} else {
ASSERT(outer_scope() != NULL);
receiver_ = outer_scope()->receiver();
}
if (is_function_scope()) {
@ -441,15 +436,13 @@ Variable* Scope::DeclareGlobal(Handle<String> name) {
}
VariableProxy* Scope::NewUnresolved(Handle<String> name,
bool inside_with,
int position) {
VariableProxy* Scope::NewUnresolved(Handle<String> name, int position) {
// Note that we must not share the unresolved variables with
// the same name because they may be removed selectively via
// RemoveUnresolved().
ASSERT(!already_resolved());
VariableProxy* proxy = new(isolate_->zone()) VariableProxy(
isolate_, name, false, inside_with, position);
isolate_, name, false, position);
unresolved_.Add(proxy);
return proxy;
}
@ -505,17 +498,19 @@ Declaration* Scope::CheckConflictingVarDeclarations() {
Declaration* decl = decls_[i];
if (decl->mode() != VAR) continue;
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.
Variable* other_var = scope->variables_.Lookup(name);
Variable* other_var = current->variables_.Lookup(name);
if (other_var != NULL && other_var->mode() != VAR) {
return decl;
}
// Include declaration scope in the iteration but stop after.
if (!scope->is_block_scope() && !scope->is_catch_scope()) cond = false;
}
previous = current;
current = current->outer_scope_;
} while (!previous->is_declaration_scope());
}
return NULL;
}
@ -563,16 +558,11 @@ void Scope::AllocateVariables(Handle<Context> context) {
// this information in the ScopeInfo and then use it here (by traversing
// 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;
if (!is_global_scope()) {
context->ComputeEvalScopeInfo(&outer_scope_calls_eval,
&outer_scope_calls_non_strict_eval);
context->ComputeEvalScopeInfo(&outer_scope_calls_non_strict_eval);
}
PropagateScopeInfo(outer_scope_calls_eval,
outer_scope_calls_non_strict_eval,
eval_scope);
PropagateScopeInfo(outer_scope_calls_non_strict_eval);
// 2) Resolve variables.
Scope* global_scope = NULL;
@ -625,8 +615,7 @@ int Scope::ContextChainLength(Scope* scope) {
Scope* Scope::DeclarationScope() {
Scope* scope = this;
while (scope->is_catch_scope() ||
scope->is_block_scope()) {
while (!scope->is_declaration_scope()) {
scope = scope->outer_scope();
}
return scope;
@ -649,6 +638,7 @@ static const char* Header(Scope::Type type) {
case Scope::GLOBAL_SCOPE: return "global";
case Scope::CATCH_SCOPE: return "catch";
case Scope::BLOCK_SCOPE: return "block";
case Scope::WITH_SCOPE: return "with";
}
UNREACHABLE();
return NULL;
@ -748,14 +738,10 @@ void Scope::Print(int n) {
if (scope_inside_with_) Indent(n1, "// scope inside 'with'\n");
if (scope_contains_with_) Indent(n1, "// scope contains 'with'\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_) {
Indent(n1, "// outer scope calls 'eval' in non-strict context\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, "// ");
PrintF("%d stack slots\n", num_stack_slots_); }
if (num_heap_slots_ > 0) { Indent(n1, "// ");
@ -809,74 +795,68 @@ 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,
bool from_inner_scope,
Variable** invalidated_local) {
// If we find a variable, but the current scope calls 'eval', the found
// 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_;
Handle<Context> context,
BindingKind* binding_kind) {
ASSERT(binding_kind != NULL);
// Try to find the variable in this scope.
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) {
// We found a variable. If this is not an inner lookup, we are done.
// (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;
*binding_kind = BOUND;
return var;
}
} else {
// 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
// only present - if at all - for function scopes.
//
// This lookup corresponds to a lookup in the "intermediate" scope sitting
// between this scope and the outer scope. (ECMA-262, 3rd., requires that
// the name of named function literal is kept in an intermediate scope
// in between this scope and the next outer scope.)
if (function_ != NULL && function_->name().is_identical_to(name)) {
var = function_->var();
// 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
// only present - if at all - for function scopes.
//
// This lookup corresponds to a lookup in the "intermediate" scope sitting
// between this scope and the outer scope. (ECMA-262, 3rd., requires that
// the name of named function literal is kept in an intermediate scope
// in between this scope and the next outer scope.)
*binding_kind = UNBOUND;
if (function_ != NULL && function_->name().is_identical_to(name)) {
var = function_->var();
*binding_kind = BOUND;
} else if (outer_scope_ != NULL) {
var = outer_scope_->LookupRecursive(name, context, binding_kind);
if (*binding_kind == BOUND) var->MarkAsAccessedFromInnerScope();
}
} else if (outer_scope_ != NULL) {
var = outer_scope_->LookupRecursive(name, true, invalidated_local);
// We may have found a variable in an outer scope. However, if
// 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 (is_with_scope()) {
// 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;
} 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;
}
// If we did not find a variable, we are done.
if (var == NULL)
return NULL;
}
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;
}
@ -891,71 +871,44 @@ void Scope::ResolveVariable(Scope* global_scope,
if (proxy->var() != NULL) return;
// Otherwise, try to resolve the variable.
Variable* invalidated_local = NULL;
Variable* var = LookupRecursive(proxy->name(), false, &invalidated_local);
BindingKind binding_kind;
Variable* var = LookupRecursive(proxy->name(), context, &binding_kind);
switch (binding_kind) {
case BOUND:
// We found a variable binding.
break;
if (proxy->inside_with()) {
// If we are inside a local 'with' statement, all bets are off
// and we cannot resolve the proxy to a local variable even if
// 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);
} else {
// We are not inside a local 'with' statement.
if (var == NULL) {
// We did not find the variable. We have a global variable
// 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);
var = global_scope->DeclareGlobal(proxy->name());
} else if (scope_inside_with_) {
// If we are inside a with statement we give up and look up
// the variable at runtime.
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);
} else {
var = NonLocal(proxy->name(), DYNAMIC);
}
} else {
// 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.
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 {
Variable* invalidated = var;
var = NonLocal(proxy->name(), DYNAMIC_LOCAL);
var->set_local_if_not_shadowed(invalidated);
}
}
break;
case UNBOUND:
// No binding has been found. Declare a variable in global scope.
ASSERT(global_scope != NULL);
var = global_scope->DeclareGlobal(proxy->name());
break;
case UNBOUND_EVAL_SHADOWED:
// No binding has been found. But some scope makes a
// non-strict 'eval' call.
var = NonLocal(proxy->name(), DYNAMIC_GLOBAL);
break;
case DYNAMIC_LOOKUP:
// The variable could not be resolved statically.
var = NonLocal(proxy->name(), DYNAMIC);
break;
}
ASSERT(var != NULL);
proxy->BindTo(var);
}
@ -976,31 +929,17 @@ void Scope::ResolveVariablesRecursively(Scope* global_scope,
}
bool Scope::PropagateScopeInfo(bool outer_scope_calls_eval,
bool outer_scope_calls_non_strict_eval,
bool outer_scope_is_eval_scope) {
if (outer_scope_calls_eval) {
outer_scope_calls_eval_ = true;
}
bool Scope::PropagateScopeInfo(bool outer_scope_calls_non_strict_eval ) {
if (outer_scope_calls_non_strict_eval) {
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 =
(scope_calls_eval_ && !is_strict_mode()) ||
outer_scope_calls_non_strict_eval_;
for (int i = 0; i < inner_scopes_.length(); i++) {
Scope* inner_scope = inner_scopes_[i];
if (inner_scope->PropagateScopeInfo(calls_eval,
calls_non_strict_eval,
is_eval)) {
if (inner_scope->PropagateScopeInfo(calls_non_strict_eval)) {
inner_scope_calls_eval_ = true;
}
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.
GLOBAL_SCOPE, // The top-level scope for a program or a top-level eval.
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);
@ -110,7 +111,7 @@ class Scope: public ZoneObject {
// The scope name is only used for printing/debugging.
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
// 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.
VariableProxy* NewUnresolved(Handle<String> name,
bool inside_with,
int position = RelocInfo::kNoPosition);
// Remove a unresolved variable. During parsing, an unresolved variable
@ -199,7 +199,7 @@ class Scope: public ZoneObject {
void RecordWithStatement() { scope_contains_with_ = true; }
// 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).
void EnableStrictMode() {
@ -215,6 +215,10 @@ class Scope: public ZoneObject {
bool is_global_scope() const { return type_ == GLOBAL_SCOPE; }
bool is_catch_scope() const { return type_ == CATCH_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_eval_scope() const {
return is_eval_scope() && is_strict_mode();
@ -222,7 +226,9 @@ class Scope: public ZoneObject {
// Information about which scopes 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 {
return outer_scope_calls_non_strict_eval_;
}
@ -383,10 +389,8 @@ class Scope: public ZoneObject {
bool strict_mode_;
// Computed via PropagateScopeInfo.
bool outer_scope_calls_eval_;
bool outer_scope_calls_non_strict_eval_;
bool inner_scope_calls_eval_;
bool outer_scope_is_eval_scope_;
bool force_eager_compilation_;
// 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.
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_heap_slots_;
@ -409,9 +413,57 @@ class Scope: public ZoneObject {
Variable* NonLocal(Handle<String> name, VariableMode mode);
// 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,
bool from_inner_function,
Variable** invalidated_local);
Handle<Context> context,
BindingKind* binding_kind);
void ResolveVariable(Scope* global_scope,
Handle<Context> context,
VariableProxy* proxy);
@ -419,9 +471,7 @@ class Scope: public ZoneObject {
Handle<Context> context);
// Scope analysis.
bool PropagateScopeInfo(bool outer_scope_calls_eval,
bool outer_scope_calls_non_strict_eval,
bool outer_scope_is_eval_scope);
bool PropagateScopeInfo(bool outer_scope_calls_non_strict_eval);
bool HasTrivialContext() const;
// Predicates.
@ -438,7 +488,7 @@ class Scope: public ZoneObject {
void AllocateVariablesRecursively();
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);
// Construct a catch scope with a binding for the name.

View File

@ -154,6 +154,10 @@ class Variable: public ZoneObject {
Location location_;
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_;
// 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
// context extensions. If we have reached an eval scope, we check
// 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();
}