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