| 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 = 0; | |
| 105 CHECK(old_length_handle->ToArrayIndex(&old_length)); | |
| 106 uint32_t new_length = 0; | |
| 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 | |
| 139 MaybeObject* Accessors::ArraySetLength(JSObject* object, Object* value, void*) { | 98 MaybeObject* Accessors::ArraySetLength(JSObject* object, Object* value, void*) { |
| 140 Isolate* isolate = object->GetIsolate(); | 99 Isolate* isolate = object->GetIsolate(); |
| 141 | 100 |
| 142 // This means one of the object's prototypes is a JSArray and the | 101 // This means one of the object's prototypes is a JSArray and the |
| 143 // object does not have a 'length' property. Calling SetProperty | 102 // object does not have a 'length' property. Calling SetProperty |
| 144 // causes an infinite loop. | 103 // causes an infinite loop. |
| 145 if (!object->IsJSArray()) { | 104 if (!object->IsJSArray()) { |
| 146 return object->SetLocalPropertyIgnoreAttributes( | 105 return object->SetLocalPropertyIgnoreAttributes( |
| 147 isolate->heap()->length_symbol(), value, NONE); | 106 isolate->heap()->length_symbol(), value, NONE); |
| 148 } | 107 } |
| 149 | 108 |
| 150 value = FlattenNumber(value); | 109 value = FlattenNumber(value); |
| 151 | 110 |
| 152 // Need to call methods that may trigger GC. | 111 // Need to call methods that may trigger GC. |
| 153 HandleScope scope(isolate); | 112 HandleScope scope(isolate); |
| 154 | 113 |
| 155 // Protect raw pointers. | 114 // Protect raw pointers. |
| 156 Handle<JSArray> array_handle(JSArray::cast(object), isolate); | 115 Handle<JSArray> array_handle(JSArray::cast(object), isolate); |
| 157 Handle<Object> value_handle(value, isolate); | 116 Handle<Object> value_handle(value, isolate); |
| 158 | 117 |
| 159 bool has_exception; | 118 bool has_exception; |
| 160 Handle<Object> uint32_v = Execution::ToUint32(value_handle, &has_exception); | 119 Handle<Object> uint32_v = Execution::ToUint32(value_handle, &has_exception); |
| 161 if (has_exception) return Failure::Exception(); | 120 if (has_exception) return Failure::Exception(); |
| 162 Handle<Object> number_v = Execution::ToNumber(value_handle, &has_exception); | 121 Handle<Object> number_v = Execution::ToNumber(value_handle, &has_exception); |
| 163 if (has_exception) return Failure::Exception(); | 122 if (has_exception) return Failure::Exception(); |
| 164 | 123 |
| 165 if (uint32_v->Number() == number_v->Number()) { | 124 if (uint32_v->Number() == number_v->Number()) { |
| 166 if (FLAG_harmony_observation && array_handle->map()->is_observed()) { | 125 return array_handle->SetElementsLength(*uint32_v); |
| 167 return ArraySetLengthObserved(isolate, array_handle, uint32_v); | |
| 168 } else { | |
| 169 return array_handle->SetElementsLength(*uint32_v); | |
| 170 } | |
| 171 } | 126 } |
| 172 return isolate->Throw( | 127 return isolate->Throw( |
| 173 *isolate->factory()->NewRangeError("invalid_array_length", | 128 *isolate->factory()->NewRangeError("invalid_array_length", |
| 174 HandleVector<Object>(NULL, 0))); | 129 HandleVector<Object>(NULL, 0))); |
| 175 } | 130 } |
| 176 | 131 |
| 177 | 132 |
| 178 const AccessorDescriptor Accessors::ArrayLength = { | 133 const AccessorDescriptor Accessors::ArrayLength = { |
| 179 ArrayGetLength, | 134 ArrayGetLength, |
| 180 ArraySetLength, | 135 ArraySetLength, |
| (...skipping 712 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 893 info->set_data(Smi::FromInt(index)); | 848 info->set_data(Smi::FromInt(index)); |
| 894 Handle<Object> getter = v8::FromCData(&ModuleGetExport); | 849 Handle<Object> getter = v8::FromCData(&ModuleGetExport); |
| 895 Handle<Object> setter = v8::FromCData(&ModuleSetExport); | 850 Handle<Object> setter = v8::FromCData(&ModuleSetExport); |
| 896 info->set_getter(*getter); | 851 info->set_getter(*getter); |
| 897 if (!(attributes & ReadOnly)) info->set_setter(*setter); | 852 if (!(attributes & ReadOnly)) info->set_setter(*setter); |
| 898 return info; | 853 return info; |
| 899 } | 854 } |
| 900 | 855 |
| 901 | 856 |
| 902 } } // namespace v8::internal | 857 } } // namespace v8::internal |
| OLD | NEW |