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

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: Merged with 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') | test/mjsunit/harmony/object-observe.js » ('J')
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 uint32_t end_index = old_length;
109 while (end_index && end_index-- > new_length) {
rossberg 2012/11/08 14:26:22 Can we make this a for loop?
adamk 2012/11/08 14:48:01 Done, but I'm not sure it looks better (have to de
110 PropertyAttributes attributes = array->GetLocalElementAttribute(end_index);
111 if (attributes == ABSENT) continue;
112 // A non-configurable property will cause the truncation operation to
113 // stop at this index.
114 if (attributes == DONT_DELETE) break;
115 // TODO(adamk): Don't fetch the old value if it's an accessor.
116 old_values.Add(Object::GetElement(array, end_index));
117 indices.Add(isolate->factory()->Uint32ToString(end_index));
118 }
119
120 MaybeObject* result = array->SetElementsLength(*new_length_handle);
121
rossberg 2012/11/08 14:26:22 Nit: move the empty line down by 2.
adamk 2012/11/08 14:48:01 Done.
122 Handle<Object> hresult;
123 if (!result->ToHandle(&hresult)) return result;
124 CHECK(array->length()->ToArrayIndex(&new_length));
125 if (old_length != new_length) {
126 for (int i = 0; i < indices.length(); ++i) {
127 JSObject::EnqueueChangeRecord(
128 array, "deleted", indices[i], old_values[i]);
129 }
130 JSObject::EnqueueChangeRecord(
131 array, "updated", isolate->factory()->length_symbol(),
132 old_length_handle);
133 }
134 return *hresult;
135 }
136
137
98 MaybeObject* Accessors::ArraySetLength(JSObject* object, Object* value, void*) { 138 MaybeObject* Accessors::ArraySetLength(JSObject* object, Object* value, void*) {
99 Isolate* isolate = object->GetIsolate(); 139 Isolate* isolate = object->GetIsolate();
100 140
101 // This means one of the object's prototypes is a JSArray and the 141 // This means one of the object's prototypes is a JSArray and the
102 // object does not have a 'length' property. Calling SetProperty 142 // object does not have a 'length' property. Calling SetProperty
103 // causes an infinite loop. 143 // causes an infinite loop.
104 if (!object->IsJSArray()) { 144 if (!object->IsJSArray()) {
105 return object->SetLocalPropertyIgnoreAttributes( 145 return object->SetLocalPropertyIgnoreAttributes(
106 isolate->heap()->length_symbol(), value, NONE); 146 isolate->heap()->length_symbol(), value, NONE);
107 } 147 }
108 148
109 value = FlattenNumber(value); 149 value = FlattenNumber(value);
110 150
111 // Need to call methods that may trigger GC. 151 // Need to call methods that may trigger GC.
112 HandleScope scope(isolate); 152 HandleScope scope(isolate);
113 153
114 // Protect raw pointers. 154 // Protect raw pointers.
115 Handle<JSObject> object_handle(object, isolate); 155 Handle<JSArray> array_handle(JSArray::cast(object), isolate);
116 Handle<Object> value_handle(value, isolate); 156 Handle<Object> value_handle(value, isolate);
117 157
118 bool has_exception; 158 bool has_exception;
119 Handle<Object> uint32_v = Execution::ToUint32(value_handle, &has_exception); 159 Handle<Object> uint32_v = Execution::ToUint32(value_handle, &has_exception);
120 if (has_exception) return Failure::Exception(); 160 if (has_exception) return Failure::Exception();
121 Handle<Object> number_v = Execution::ToNumber(value_handle, &has_exception); 161 Handle<Object> number_v = Execution::ToNumber(value_handle, &has_exception);
122 if (has_exception) return Failure::Exception(); 162 if (has_exception) return Failure::Exception();
123 163
124 if (uint32_v->Number() == number_v->Number()) { 164 if (uint32_v->Number() == number_v->Number()) {
125 return Handle<JSArray>::cast(object_handle)->SetElementsLength(*uint32_v); 165 if (FLAG_harmony_observation && array_handle->map()->is_observed()) {
166 return ArraySetLengthObserved(isolate, array_handle, uint32_v);
167 } else {
168 return array_handle->SetElementsLength(*uint32_v);
169 }
126 } 170 }
127 return isolate->Throw( 171 return isolate->Throw(
128 *isolate->factory()->NewRangeError("invalid_array_length", 172 *isolate->factory()->NewRangeError("invalid_array_length",
129 HandleVector<Object>(NULL, 0))); 173 HandleVector<Object>(NULL, 0)));
130 } 174 }
131 175
132 176
133 const AccessorDescriptor Accessors::ArrayLength = { 177 const AccessorDescriptor Accessors::ArrayLength = {
134 ArrayGetLength, 178 ArrayGetLength,
135 ArraySetLength, 179 ArraySetLength,
(...skipping 712 matching lines...) Expand 10 before | Expand all | Expand 10 after
848 info->set_data(Smi::FromInt(index)); 892 info->set_data(Smi::FromInt(index));
849 Handle<Object> getter = v8::FromCData(&ModuleGetExport); 893 Handle<Object> getter = v8::FromCData(&ModuleGetExport);
850 Handle<Object> setter = v8::FromCData(&ModuleSetExport); 894 Handle<Object> setter = v8::FromCData(&ModuleSetExport);
851 info->set_getter(*getter); 895 info->set_getter(*getter);
852 if (!(attributes & ReadOnly)) info->set_setter(*setter); 896 if (!(attributes & ReadOnly)) info->set_setter(*setter);
853 return info; 897 return info;
854 } 898 }
855 899
856 900
857 } } // namespace v8::internal 901 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « no previous file | src/objects.h » ('j') | test/mjsunit/harmony/object-observe.js » ('J')

Powered by Google App Engine
This is Rietveld 408576698