Reviewers: rossberg,

Description:
Fix ScopeInfo::StackSlotIndex to skip over non-locals

Block scoping can cause lexical variables in inner scopes to be allocated
stack slots in their outer declaration scope. Neither of the callers of this
method expected such variables to be returned, so it has been changed to
only search the portion of the stack slots that correspond
to locals. Also renamed the method to match the new behavior.

This method only has two callers, one a DCHECK in scopes.cc and the other
the accessor for Function.arguments. I have been unable to reproduce the
former case (though the old code definitely wasn't correct, from inspection). For the latter, it's not currently exercisable, given that Function.arguments
does not exist in strict mode and V8 does not yet implement block scoping in
sloppy mode.

BUG=4259
LOG=n

Please review this at https://codereview.chromium.org/1215423002/

Base URL: https://chromium.googlesource.com/v8/v8.git@master

Affected files (+8, -8 lines):
  M src/accessors.cc
  M src/objects.h
  M src/scopeinfo.cc
  M src/scopes.cc


Index: src/accessors.cc
diff --git a/src/accessors.cc b/src/accessors.cc
index 9850cd388e416549ddd43e81672f9688c58728b3..4186bf4edf7630cfd443e4a70321b9959a0e959e 100644
--- a/src/accessors.cc
+++ b/src/accessors.cc
@@ -1188,8 +1188,8 @@ Handle<Object> GetFunctionArguments(Isolate* isolate,
     if (!frame->is_optimized()) {
       // If there is an arguments variable in the stack, we return that.
       Handle<ScopeInfo> scope_info(function->shared()->scope_info());
-      int index = scope_info->StackSlotIndex(
-          isolate->heap()->arguments_string());
+      int index =
+ scope_info->StackLocalSlotIndex(isolate->heap()->arguments_string());
       if (index >= 0) {
         Handle<Object> arguments(frame->GetExpression(index), isolate);
         if (!arguments->IsArgumentsMarker()) return arguments;
Index: src/objects.h
diff --git a/src/objects.h b/src/objects.h
index 715076640c0968ccc1f8821e675d5e695d5188a7..1dcb35167d60ef0f6316777279b1828ecca1170a 100644
--- a/src/objects.h
+++ b/src/objects.h
@@ -4000,9 +4000,9 @@ class ScopeInfo : public FixedArray {

   // Lookup support for serialized scope info. Returns the
   // the stack slot index for a given slot name if the slot is
- // present; otherwise returns a value < 0. The name must be an internalized
-  // string.
-  int StackSlotIndex(String* name);
+  // allocated to a local; otherwise returns a value < 0.
+  // The name must be an internalized string.
+  int StackLocalSlotIndex(String* name);

   // Lookup support for serialized scope info. Returns the
// context slot index for a given slot name if the slot is present; otherwise
Index: src/scopeinfo.cc
diff --git a/src/scopeinfo.cc b/src/scopeinfo.cc
index f77ef96ebac4cd8da6bd33f1bd58936a021f354f..c82a6792fc665a755284d0c16753be755aecb74f 100644
--- a/src/scopeinfo.cc
+++ b/src/scopeinfo.cc
@@ -467,11 +467,11 @@ int ScopeInfo::StrongModeFreeVariableEndPosition(int var) {
 }


-int ScopeInfo::StackSlotIndex(String* name) {
+int ScopeInfo::StackLocalSlotIndex(String* name) {
   DCHECK(name->IsInternalizedString());
   if (length() > 0) {
int first_slot_index = Smi::cast(get(StackLocalFirstSlotIndex()))->value();
-    int start = StackLocalEntriesIndex();
+    int start = StackLocalEntriesIndex() + first_slot_index;
     int end = StackLocalEntriesIndex() + StackLocalCount();
     for (int i = start; i < end; ++i) {
       if (name == get(i)) {
Index: src/scopes.cc
diff --git a/src/scopes.cc b/src/scopes.cc
index 64f9584d59dcb5974952981386f3ff9a0ba50cd3..868b714400cb3337e5bd20ba40d6d7c3c6feed09 100644
--- a/src/scopes.cc
+++ b/src/scopes.cc
@@ -384,7 +384,7 @@ Variable* Scope::LookupLocal(const AstRawString* name) {
   // it's ok to get the Handle<String> here.
   // If we have a serialized scope info, we might find the variable there.
   // There should be no local slot with the given name.
- DCHECK(scope_info_->StackSlotIndex(*name_handle) < 0 || is_block_scope());
+  DCHECK(scope_info_->StackLocalSlotIndex(*name_handle) < 0);

   // Check context slot lookup.
   VariableMode mode;


--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to