Reviewers: rossberg,

Description:
Object.observe: notify when element addition causes array growth


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

SVN Base: http://v8.googlecode.com/svn/branches/bleeding_edge

Affected files:
  M src/objects.cc
  M test/mjsunit/harmony/object-observe.js


Index: src/objects.cc
diff --git a/src/objects.cc b/src/objects.cc
index 570f6fbae4f7c0269db737c7e03ac6a33c0865df..244b732ed85a46aed029026d6a47b1268280450b 100644
--- a/src/objects.cc
+++ b/src/objects.cc
@@ -10307,6 +10307,7 @@ MaybeObject* JSObject::SetElement(uint32_t index,
   // From here on, everything has to be handlified.
   Handle<String> name;
   Handle<Object> old_value(isolate->heap()->the_hole_value());
+  Handle<Object> old_array_length;
   PropertyAttributes old_attributes = ABSENT;
   bool preexists = false;
   if (FLAG_harmony_observation && map()->is_observed()) {
@@ -10317,6 +10318,9 @@ MaybeObject* JSObject::SetElement(uint32_t index,
// TODO(observe): only read & set old_value if we have a data property
       old_value = Object::GetElement(self, index);
     }
+ // Store the old array length in case adding an element will grow the array.
+    if (!preexists && self->IsJSArray())
+      old_array_length = handle(Handle<JSArray>::cast(self)->length());
   }

   // Check for lookup interceptor
@@ -10332,6 +10336,12 @@ MaybeObject* JSObject::SetElement(uint32_t index,
   if (FLAG_harmony_observation && map()->is_observed()) {
PropertyAttributes new_attributes = self->GetLocalPropertyAttribute(*name);
     if (!preexists) {
+      if (self->IsJSArray() &&
+ !old_array_length->SameValue(Handle<JSArray>::cast(self)->length())) {
+        EnqueueChangeRecord(self, "updated",
+                            isolate->factory()->length_symbol(),
+                            old_array_length);
+      }
       EnqueueChangeRecord(self, "new", name, old_value);
} else if (new_attributes != old_attributes || old_value->IsTheHole()) {
       EnqueueChangeRecord(self, "reconfigured", name, old_value);
Index: test/mjsunit/harmony/object-observe.js
diff --git a/test/mjsunit/harmony/object-observe.js b/test/mjsunit/harmony/object-observe.js index 231fe91e18594797c5519eec6fbc9f2b839998ef..d4fb48bfe7904618846105e39d85d6c4d35c4b3c 100644
--- a/test/mjsunit/harmony/object-observe.js
+++ b/test/mjsunit/harmony/object-observe.js
@@ -399,3 +399,19 @@ observer.assertCallbackRecords([
   { object: obj, name: "3", type: "new" },
   { object: obj, name: "4", type: "new" },
 ]);
+
+// Adding elements past the end of an array should notify on length
+reset();
+var arr = [1, 2, 3];
+Object.observe(arr, observer.callback);
+arr[3] = 10;
+arr[100] = 20;
+arr[50] = 30; // no length change expected
+Object.deliverChangeRecords(observer.callback);
+observer.assertCallbackRecords([
+  { object: arr, name: 'length', type: 'updated', oldValue: 3 },
+  { object: arr, name: '3', type: 'new' },
+  { object: arr, name: 'length', type: 'updated', oldValue: 4 },
+  { object: arr, name: '100', type: 'new' },
+  { object: arr, name: '50', type: 'new' },
+]);


--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to