Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(255)

Side by Side Diff: src/accessors.cc

Issue 11338048: Handle Object.observe notifications for setting Array.length (Closed) Base URL: git@github.com:rafaelw/v8@master
Patch Set: Respond to review, merged to trunk Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | src/objects.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 JSValue* wrapper = JSValue::cast(value); 88 JSValue* wrapper = JSValue::cast(value);
89 ASSERT(Isolate::Current()->context()->native_context()->number_function()-> 89 ASSERT(Isolate::Current()->context()->native_context()->number_function()->
90 has_initial_map()); 90 has_initial_map());
91 Map* number_map = Isolate::Current()->context()->native_context()-> 91 Map* number_map = Isolate::Current()->context()->native_context()->
92 number_function()->initial_map(); 92 number_function()->initial_map();
93 if (wrapper->map() == number_map) return wrapper->value(); 93 if (wrapper->map() == number_map) return wrapper->value();
94 return value; 94 return value;
95 } 95 }
96 96
97 97
98 static MaybeObject* ArraySetLengthObserved(Isolate* isolate,
99 Handle<JSArray> array,
100 Handle<Object> new_length_handle) {
101 List<Handle<String> > indices;
102 List<Handle<Object> > old_values;
103 Handle<Object> old_length_handle(array->length(), isolate);
104 uint32_t old_length;
105 CHECK(old_length_handle->ToArrayIndex(&old_length));
106 uint32_t new_length;
107 CHECK(new_length_handle->ToArrayIndex(&new_length));
108 for (uint32_t len = old_length; len > new_length; --len) {
rossberg 2012/11/08 14:56:28 If you want to avoid the off-by-one nuisance below
adamk 2012/11/08 15:11:23 Nope: first of all, need to add an if statement no
rossberg 2012/11/08 15:16:12 Note the +1. ;)
109 PropertyAttributes attributes = array->GetLocalElementAttribute(len - 1);
110 if (attributes == ABSENT) continue;
111 // A non-configurable property will cause the truncation operation to
112 // stop at this index.
113 if (attributes == DONT_DELETE) break;
114 // TODO(adamk): Don't fetch the old value if it's an accessor.
115 old_values.Add(Object::GetElement(array, len - 1));
116 indices.Add(isolate->factory()->Uint32ToString(len - 1));
117 }
118
119 MaybeObject* result = array->SetElementsLength(*new_length_handle);
120 Handle<Object> hresult;
121 if (!result->ToHandle(&hresult)) return result;
122
123 CHECK(array->length()->ToArrayIndex(&new_length));
124 if (old_length != new_length) {
125 for (int i = 0; i < indices.length(); ++i) {
126 JSObject::EnqueueChangeRecord(
127 array, "deleted", indices[i], old_values[i]);
128 }
129 JSObject::EnqueueChangeRecord(
130 array, "updated", isolate->factory()->length_symbol(),
131 old_length_handle);
132 }
133 return *hresult;
134 }
135
136
98 MaybeObject* Accessors::ArraySetLength(JSObject* object, Object* value, void*) { 137 MaybeObject* Accessors::ArraySetLength(JSObject* object, Object* value, void*) {
99 Isolate* isolate = object->GetIsolate(); 138 Isolate* isolate = object->GetIsolate();
100 139
101 // This means one of the object's prototypes is a JSArray and the 140 // This means one of the object's prototypes is a JSArray and the
102 // object does not have a 'length' property. Calling SetProperty 141 // object does not have a 'length' property. Calling SetProperty
103 // causes an infinite loop. 142 // causes an infinite loop.
104 if (!object->IsJSArray()) { 143 if (!object->IsJSArray()) {
105 return object->SetLocalPropertyIgnoreAttributes( 144 return object->SetLocalPropertyIgnoreAttributes(
106 isolate->heap()->length_symbol(), value, NONE); 145 isolate->heap()->length_symbol(), value, NONE);
107 } 146 }
108 147
109 value = FlattenNumber(value); 148 value = FlattenNumber(value);
110 149
111 // Need to call methods that may trigger GC. 150 // Need to call methods that may trigger GC.
112 HandleScope scope(isolate); 151 HandleScope scope(isolate);
113 152
114 // Protect raw pointers. 153 // Protect raw pointers.
115 Handle<JSObject> object_handle(object, isolate); 154 Handle<JSArray> array_handle(JSArray::cast(object), isolate);
116 Handle<Object> value_handle(value, isolate); 155 Handle<Object> value_handle(value, isolate);
117 156
118 bool has_exception; 157 bool has_exception;
119 Handle<Object> uint32_v = Execution::ToUint32(value_handle, &has_exception); 158 Handle<Object> uint32_v = Execution::ToUint32(value_handle, &has_exception);
120 if (has_exception) return Failure::Exception(); 159 if (has_exception) return Failure::Exception();
121 Handle<Object> number_v = Execution::ToNumber(value_handle, &has_exception); 160 Handle<Object> number_v = Execution::ToNumber(value_handle, &has_exception);
122 if (has_exception) return Failure::Exception(); 161 if (has_exception) return Failure::Exception();
123 162
124 if (uint32_v->Number() == number_v->Number()) { 163 if (uint32_v->Number() == number_v->Number()) {
125 return Handle<JSArray>::cast(object_handle)->SetElementsLength(*uint32_v); 164 if (FLAG_harmony_observation && array_handle->map()->is_observed()) {
165 return ArraySetLengthObserved(isolate, array_handle, uint32_v);
166 } else {
167 return array_handle->SetElementsLength(*uint32_v);
168 }
126 } 169 }
127 return isolate->Throw( 170 return isolate->Throw(
128 *isolate->factory()->NewRangeError("invalid_array_length", 171 *isolate->factory()->NewRangeError("invalid_array_length",
129 HandleVector<Object>(NULL, 0))); 172 HandleVector<Object>(NULL, 0)));
130 } 173 }
131 174
132 175
133 const AccessorDescriptor Accessors::ArrayLength = { 176 const AccessorDescriptor Accessors::ArrayLength = {
134 ArrayGetLength, 177 ArrayGetLength,
135 ArraySetLength, 178 ArraySetLength,
(...skipping 712 matching lines...) Expand 10 before | Expand all | Expand 10 after
848 info->set_data(Smi::FromInt(index)); 891 info->set_data(Smi::FromInt(index));
849 Handle<Object> getter = v8::FromCData(&ModuleGetExport); 892 Handle<Object> getter = v8::FromCData(&ModuleGetExport);
850 Handle<Object> setter = v8::FromCData(&ModuleSetExport); 893 Handle<Object> setter = v8::FromCData(&ModuleSetExport);
851 info->set_getter(*getter); 894 info->set_getter(*getter);
852 if (!(attributes & ReadOnly)) info->set_setter(*setter); 895 if (!(attributes & ReadOnly)) info->set_setter(*setter);
853 return info; 896 return info;
854 } 897 }
855 898
856 899
857 } } // namespace v8::internal 900 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « no previous file | src/objects.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698