Commit c86b47f4 authored by ager@chromium.org's avatar ager@chromium.org

Rearrange the code in Scope::ResolveVariable.

I find this clearer and it also enables the DYNAMIC_LOCAL optimization
for code executed by eval that itself uses eval:

 eval("(function() { var x = 2; eval('1'); do stuff with x...; })()")

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

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@1322 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent bdaf8320
......@@ -608,25 +608,9 @@ void Scope::ResolveVariable(Scope* global_scope, VariableProxy* proxy) {
// 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_)) {
// If we are inside a with statement or the code is executed
// using eval, we give up and look up the variable at runtime.
var = NonLocal(proxy->name(), Variable::DYNAMIC);
} else if (!is_global_scope() &&
(scope_calls_eval_ || outer_scope_calls_eval_)) {
// If the code is not executed using eval and there are no
// with scopes, either we have a local or a global variable
// that might be shadowed by an eval-introduced variable.
if (invalidated_local != NULL) {
var = NonLocal(proxy->name(), Variable::DYNAMIC_LOCAL);
var->set_local_if_not_shadowed(invalidated_local);
} else {
var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
}
} else {
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 = new Variable(global_scope, proxy->name(),
......@@ -639,6 +623,32 @@ void Scope::ResolveVariable(Scope* global_scope, VariableProxy* proxy) {
// latter returns undefined. Sigh...
// var->rewrite_ = new Property(new Literal(env_->global()),
// new Literal(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(), Variable::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(), Variable::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. We don't know anything
// because we do not have information about the scopes
// surrounding the eval call.
var = NonLocal(proxy->name(), Variable::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.
var = NonLocal(proxy->name(), Variable::DYNAMIC_GLOBAL);
}
}
}
......
......@@ -34,4 +34,6 @@ function test(source) {
test("assertEquals(27, x);");
test("(function() { assertEquals(27, x) })();");
test("(function() { var y = 42; eval('1'); assertEquals(42, y); })();");
test("(function() { var y = 42; eval('var y = 2; var z = 2;'); assertEquals(2, y); })();");
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment